home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / top2src.zip / TOPACT.C < prev    next >
C/C++ Source or Header  |  2000-07-13  |  14KB  |  465 lines

  1.  
  2. /*  Copyright 1993 - 2000 Paul J. Sidorsky
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License, version 2, as
  6.     published by the Free Software Foundation.
  7.  
  8.     This program is distributed in the hope that it will be useful,
  9.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.     GNU General Public License for more details.
  12.  
  13.     You should have received a copy of the GNU General Public License
  14.     along with this program; if not, write to the Free Software
  15.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  16. */
  17.  
  18. #include <stdio.h>
  19. #include <io.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <fcntl.h>
  23. #include <share.h>
  24. #include <sys\stat.h>
  25.  
  26. #ifdef __OS2__
  27. #undef random
  28. #define random(xxx) od_random(xxx)
  29. #endif
  30.  
  31. #if defined(__OS2__) || defined(__WIN32__)
  32. #define XFAR
  33. #define XINT short int
  34. #else
  35. #define XFAR far
  36. #define XINT int
  37. #endif
  38.  
  39. #define crtest(vvvv) if (vvvv[strlen(vvvv) - 1] != 10)\
  40.                          res = 1;
  41. #define stripcr(vvvv) if (vvvv[strlen(vvvv) - 1] == 10)\
  42.                           vvvv[strlen(vvvv) - 1] = 0
  43.  
  44. typedef struct
  45.     {
  46.     unsigned long datstart; /* Location of first action's data. */
  47.     unsigned long txtstart; /* Location of first action's text. */
  48.     unsigned char name[31];
  49.     unsigned XINT minsec;
  50.     unsigned XINT maxsec;
  51.     unsigned long minchannel;
  52.     unsigned long maxchannel;
  53.     unsigned long numactions;
  54.     } action_file_typ;
  55.  
  56. typedef struct
  57.     {
  58.     char          type;   /* -1 = deleted */
  59.     unsigned char verb[11];
  60.     unsigned long textofs; /* From txtstart */
  61.     unsigned XINT responselen; /* 0 = N/A */
  62.     unsigned XINT singularlen; /* 0 = N/A */
  63.     unsigned XINT plurallen;  /* 0 = N/A */
  64.     } action_data_typ;
  65.  
  66. typedef struct
  67.     {
  68.     unsigned char *responsetext;
  69.     unsigned char *singulartext;
  70.     unsigned char *pluraltext;
  71.     } action_ptr_typ;
  72.  
  73. void main(XINT argc, char *argv[]);
  74. void actfile_compile(unsigned char *ifname, unsigned char *ofname);
  75.  
  76. unsigned char loadbuf[513];
  77. unsigned char iname[256], oname[256];
  78.  
  79. void main(XINT argc, char *argv[])
  80.     {
  81.  
  82.     printf("\nTOPACT - Action Compiler for TOP 2.00.\n\n");
  83.  
  84.     if (argc < 2 || argc > 3)
  85.         {
  86.         printf("Command line error!  Syntax:\n\n");
  87.         printf("TOPACT <infile> [<outfile>]\n\n");
  88.         printf("<infile> is the file to compile.  If an extension is not "
  89.                "specified, .ACT will\n         be used.\n");
  90.         printf("<outfile> is the file to output the compiled information "
  91.                "to.  If an extension\n          is not specified, .TAC ");
  92.         printf("be used.  If <outfile> is not specified, the\n          "
  93.                "base name of <infile> will be used with the extension of "
  94.                ".TAC.\n");
  95.         exit(0);
  96.         }
  97.  
  98.     strcpy(iname, argv[1]);
  99.     if (!strchr(iname, '.'))
  100.         {
  101.         strcat(iname, ".ACT");
  102.         }
  103.     strupr(iname);
  104.  
  105.     if (argc == 2)
  106.         {
  107.         strcpy(oname, iname);
  108.         if (strchr(oname, '.'))
  109.             {
  110.             strcpy(strchr(oname, '.'), ".TAC");
  111.             }
  112.         }
  113.     else
  114.         {
  115.         strcpy(oname, argv[2]);
  116.         if (!strchr(oname, '.'))
  117.             {
  118.             strcat(oname, ".TAC");
  119.             }
  120.         }
  121.     strupr(oname);
  122.  
  123.     actfile_compile(iname, oname);
  124.  
  125.     exit(0);
  126.  
  127.     }
  128.  
  129. void actfile_compile(unsigned char *ifname, unsigned char *ofname)
  130.     {
  131.     FILE *ifil = NULL;
  132.     XINT ofil;
  133.     action_file_typ afile;
  134.     action_data_typ tact;
  135.     unsigned long count = 0, dloc = 0, tloc = 0, d;
  136.     XINT res;
  137.  
  138.     printf("--- BEGIN COMPILE ---\n");
  139.     printf("Opening %s...", ifname);
  140.  
  141.     ifil = fopen(ifname, "r+t");
  142.     if (!ifil)
  143.         {
  144.         printf("ERROR!\nCan't open %s!\n", ifname);
  145.         return;
  146.         }
  147.  
  148.     printf("Done!\nOpening %s...", ofname);
  149.  
  150.     if (!access(ofname, 0))
  151.         {
  152.         printf("WARNING!\nFile already exists!  Making backup...");
  153.         strcpy(loadbuf, ofname);
  154.         if (strchr(loadbuf, '.'))
  155.             {
  156.             strcpy(strchr(loadbuf, '.'), ".BAK");
  157.             }
  158.         else
  159.             {
  160.             strcat(loadbuf, ".BAK");
  161.             }
  162.         strupr(loadbuf);
  163.         unlink(loadbuf);
  164.         rename(ofname, loadbuf);
  165.         printf("Done!\nBacked up to %s.\nOpening %s...", loadbuf,
  166.                ofname);
  167.         }
  168.  
  169.     ofil = sopen(ofname, O_RDWR | O_CREAT | O_BINARY, SH_DENYNONE,
  170.                  S_IREAD | S_IWRITE);
  171.     if (ofil == -1)
  172.         {
  173.         printf("ERROR!\nCan't open %s!\n", ofname);
  174.         fclose(ifil);
  175.         return;
  176.         }
  177.  
  178.     printf("Done!\n--- PASS 1 ---\n");
  179.     printf("Getting configuration information...");
  180.  
  181.     res = 0;
  182.     res += (fgets(loadbuf, 512, ifil) == NULL);
  183.     crtest(loadbuf);
  184.     stripcr(loadbuf);
  185.     if (strlen(loadbuf) > 30)
  186.         {
  187.         printf("WARNING!\nList Name too long - truncated to 30 "
  188.                "characters.\n");
  189.         printf("Continuing to get configuration information...");
  190.         }
  191.     strncpy(afile.name, loadbuf, 30);
  192.     afile.name[31] = '\0';
  193.     res += (fgets(loadbuf, 512, ifil) == NULL);
  194.     crtest(loadbuf);
  195.     afile.minsec = strtoul(loadbuf, NULL, 10);
  196.     if (!strnicmp(loadbuf, "MINSEC", 6))
  197.         {
  198.         afile.minsec = 0;
  199.         }
  200.     res += (fgets(loadbuf, 512, ifil) == NULL);
  201.     crtest(loadbuf);
  202.     afile.maxsec = strtoul(loadbuf, NULL, 10);
  203.     if (!strnicmp(loadbuf, "MAXSEC", 6))
  204.         {
  205.         afile.maxsec = 65535;
  206.         }
  207.     res += (fgets(loadbuf, 512, ifil) == NULL);
  208.     crtest(loadbuf);
  209.     afile.minchannel = strtoul(loadbuf, NULL, 10);
  210.     if (!strnicmp(loadbuf, "MINCHA", 6))
  211.         {
  212.         afile.minchannel = 1UL;
  213.         }
  214.     if (!strnicmp(loadbuf, "MINPUB", 6))
  215.         {
  216.         afile.minchannel = 1UL;
  217.         }
  218.     if (!strnicmp(loadbuf, "MINPER", 6))
  219.         {
  220.         afile.minchannel = 4000000000UL;
  221.         }
  222.     if (!strnicmp(loadbuf, "MINCON", 6))
  223.         {
  224.         afile.minchannel = 4001000000UL;
  225.         }
  226.     res += (fgets(loadbuf, 512, ifil) == NULL);
  227.     crtest(loadbuf);
  228.     afile.maxchannel = strtoul(loadbuf, NULL, 10);
  229.     if (!strnicmp(loadbuf, "MAXPUB", 6))
  230.         {
  231.         afile.maxchannel = 3999999999UL;
  232.         }
  233.     if (!strnicmp(loadbuf, "MAXPER", 6))
  234.         {
  235.         afile.maxchannel = 4000999999UL;
  236.         }
  237.     if (!strnicmp(loadbuf, "MAXCON", 6))
  238.         {
  239.         afile.maxchannel = 0xFFFFFFFEUL;
  240.         }
  241.     if (!strnicmp(loadbuf, "MAXCHA", 6))
  242.         {
  243.         afile.maxchannel = 0xFFFFFFFEUL;
  244.         }
  245.     if (res)
  246.         {
  247.         printf("ERROR!\nCan't read configuration information!\n");
  248.         fclose(ifil);
  249.         close(ofil);
  250.         return;
  251.         }
  252.  
  253.     printf("Done!\nName: \"%s\"  MinSec: %u  MaxSec = %u\n", afile.name,
  254.            afile.minsec, afile.maxsec);
  255.     printf("MinChan: %lu  MaxChan: %lu\n", afile.minchannel,
  256.            afile.maxchannel);
  257.  
  258.     afile.datstart = (long) sizeof(action_file_typ);
  259.  
  260.     tloc = (long) sizeof(action_file_typ);
  261.  
  262.     printf("Counting actions...%5i", count);
  263.  
  264.     while(!feof(ifil))
  265.         {
  266.         res = 0;
  267.         res += (fgets(loadbuf, 512, ifil) == NULL);
  268.         crtest(loadbuf);
  269.         res += (fgets(loadbuf, 512, ifil) == NULL);
  270.         crtest(loadbuf);
  271.         res += (fgets(loadbuf, 512, ifil) == NULL);
  272.         crtest(loadbuf);
  273.         res += (fgets(loadbuf, 512, ifil) == NULL);
  274.         crtest(loadbuf);
  275.         res += (fgets(loadbuf, 512, ifil) == NULL);
  276.         crtest(loadbuf);
  277.         if (!res)
  278.             {
  279.             tloc += (long) sizeof(action_data_typ);
  280.             printf("\b\b\b\b\b%5i", ++count);
  281.             }
  282.         }
  283.  
  284.     afile.txtstart = tloc;
  285.     afile.numactions = count;
  286.  
  287.     printf("...Done!\nWriting configuration information...");
  288.  
  289.     lseek(ofil, 0, SEEK_SET);
  290.     write(ofil, &afile, sizeof(action_file_typ));
  291.  
  292.     printf("Done!\n--- PASS 2 ---\n");
  293.     printf("Skipping configuration information (not needed on second "
  294.            "pass)...");
  295.  
  296.     dloc = 0;
  297.     tloc = 0;
  298.     fseek(ifil, 0, SEEK_SET);
  299.  
  300.     res = 0;
  301.     res += (fgets(loadbuf, 512, ifil) == NULL);
  302.     crtest(loadbuf);
  303.     res += (fgets(loadbuf, 512, ifil) == NULL);
  304.     crtest(loadbuf);
  305.     res += (fgets(loadbuf, 512, ifil) == NULL);
  306.     crtest(loadbuf);
  307.     res += (fgets(loadbuf, 512, ifil) == NULL);
  308.     crtest(loadbuf);
  309.     res += (fgets(loadbuf, 512, ifil) == NULL);
  310.     crtest(loadbuf);
  311.     if (res)
  312.         {
  313.         printf("ERROR!\nCan't skip configuration information!\n");
  314.         fclose(ifil);
  315.         close(ofil);
  316.         return;
  317.         }
  318.  
  319.     d = 0;
  320.     printf("Done!\nReading actions...%5i", d);
  321.  
  322.     for (; d < afile.numactions; d++)
  323.         {
  324.         res = 0;
  325.         res = (fgets(loadbuf, 512, ifil) == NULL);
  326.         crtest(loadbuf);
  327.         if (res)
  328.             {
  329.             printf("...ERROR!\nCan't read type for action #%i!\n",
  330.                    d + 1);
  331.             printf("Continuing to compile actions...%5i", d + 1);
  332.             continue;
  333.             }
  334.         tact.type = -1;
  335.         if (!strnicmp(loadbuf, "NOR", 3))
  336.             {
  337.             tact.type = 0;
  338.             }
  339.         if (!strnicmp(loadbuf, "TAL", 3))
  340.             {
  341.             tact.type = 1;
  342.             }
  343.         res = (fgets(loadbuf, 512, ifil) == NULL);
  344.         crtest(loadbuf);
  345.         if (res)
  346.             {
  347.             printf("...ERROR!\nCan't read verb for action #%i!\n",
  348.                    d + 1);
  349.             printf("Continuing to compile actions...%5i", d + 1);
  350.             continue;
  351.             }
  352.         stripcr(loadbuf);
  353.         if (strlen(loadbuf) > 10)
  354.             {
  355.             printf("...WARNING!\nVerb \"%s\" is too long.\n", loadbuf);
  356.             printf("Truncated to 10 characters.\n");
  357.             printf("Continuing to compile actions...%5i", d);
  358.             }
  359.         strncpy(tact.verb, loadbuf, 10);
  360.         tact.verb[10] = '\0';
  361.         if (tact.type == -1)
  362.             {
  363.             printf("...WARNING!\nInvalid action type for \"%s\" action!\n",
  364.                    tact.verb);
  365.             printf("Continuing to compile actions...%5i", d);
  366.             }
  367.         tact.textofs = tloc;
  368.         res = (fgets(loadbuf, 512, ifil) == NULL);
  369.         crtest(loadbuf);
  370.         if (res)
  371.             {
  372.             printf("...ERROR!\nCan't read response text for action #%i!\n",
  373.                    d + 1);
  374.             printf("Continuing to compile actions...%5i", d + 1);
  375.             continue;
  376.             }
  377.         stripcr(loadbuf);
  378.         if (!stricmp(loadbuf, "N/A"))
  379.             {
  380.             loadbuf[0] = '\0';
  381.             }
  382.         tact.responselen = strlen(loadbuf);
  383.         lseek(ofil, afile.txtstart + tloc, SEEK_SET);
  384.         res = write(ofil, loadbuf, tact.responselen);
  385.         if (res == -1)
  386.             {
  387.             printf("ERROR!\nCan't write response text for action #%i!\n",
  388.                    d + 1);
  389.             printf("Continuing to compile actions...%5i", d + 1);
  390.             continue;
  391.             }
  392.         tloc += (long) tact.responselen;
  393.  
  394.         res = (fgets(loadbuf, 512, ifil) == NULL);
  395.         crtest(loadbuf);
  396.         if (res)
  397.             {
  398.             printf("...ERROR!\nCan't read singular text for action #%i!\n",
  399.                    d + 1);
  400.             printf("Continuing to compile actions...%5i", d + 1);
  401.             continue;
  402.             }
  403.         stripcr(loadbuf);
  404.         if (!stricmp(loadbuf, "N/A"))
  405.             {
  406.             loadbuf[0] = '\0';
  407.             }
  408.         tact.singularlen = strlen(loadbuf);
  409.         lseek(ofil, afile.txtstart + tloc, SEEK_SET);
  410.         res = write(ofil, loadbuf, tact.singularlen);
  411.         if (res == -1)
  412.             {
  413.             printf("ERROR!\nCan't write singular text for action #%i!\n",
  414.                    d + 1);
  415.             printf("Continuing to compile actions...%5i", d + 1);
  416.             continue;
  417.             }
  418.         tloc += (long) tact.singularlen;
  419.  
  420.         res = (fgets(loadbuf, 512, ifil) == NULL);
  421.         crtest(loadbuf);
  422.         if (res)
  423.             {
  424.             printf("...ERROR!\nCan't read plural text for action #%i!\n",
  425.                    d + 1);
  426.             printf("Continuing to compile actions...%5i", d + 1);
  427.             continue;
  428.             }
  429.         stripcr(loadbuf);
  430.         if (!stricmp(loadbuf, "N/A"))
  431.             {
  432.             loadbuf[0] = '\0';
  433.             }
  434.         tact.plurallen = strlen(loadbuf);
  435.         lseek(ofil, afile.txtstart + tloc, SEEK_SET);
  436.         res = write(ofil, loadbuf, tact.plurallen);
  437.         tloc += (long) tact.plurallen;
  438.         if (res == -1)
  439.             {
  440.             printf("ERROR!\nCan't write plural text for action #%i!\n",
  441.                    d + 1);
  442.             printf("Continuing to compile actions...%5i", d + 1);
  443.             continue;
  444.             }
  445.  
  446.         lseek(ofil, afile.datstart + dloc, SEEK_SET);
  447.         res = write(ofil, &tact, sizeof(action_data_typ));
  448.         if (res == -1)
  449.             {
  450.             printf("ERROR!\nCan't write action data for action #%i!\n",
  451.                    d + 1);
  452.             printf("Continuing to compile actions...%5i", d + 1);
  453.             continue;
  454.             }
  455.         dloc += sizeof(action_data_typ);
  456.         printf("\b\b\b\b\b%5i", d + 1);
  457.         }
  458.  
  459.     fclose(ifil);
  460.     close(ofil);
  461.  
  462.     printf("...Done!\n--- COMPILE COMPLETED ---\n");
  463.  
  464.     }
  465.