home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / cpgms.zip / PCFUPD.C < prev    next >
C/C++ Source or Header  |  1985-11-17  |  7KB  |  272 lines

  1. #include "stdio.h"
  2. #define EOF 0x1a
  3. #define ERROR 0
  4. #define CPMEOF 0X1A
  5. #define MAXINDX 50
  6. #define MAXDATA 1024
  7. #define NULL 0
  8. #define EOS  0
  9. #define DeSmet 1
  10.  
  11.  
  12. char    *documentation[] = {
  13. "pcfupd updates a PC-FILE III data file.",
  14. "   pcfupd [flags] [file] [<file] ",
  15. "",
  16. "Flags are single characters preceeded by '-':",
  17. "   -       none are yet defined",
  18. "",
  19. "Input is from the given file.",
  20. "Output is to stdout.  To print add >PRN: to your command.",
  21. 0 };
  22.  
  23. int dbug = 0;
  24.  
  25. struct    {
  26.     char fname[12];
  27.     int  flen;
  28.     int  fpos;
  29.     } fields[MAXINDX];
  30.  
  31. int totlen = 0;
  32. int nflds  = 0;
  33. int indxlen, datalen;
  34.  
  35. main(argc, argv)
  36. char *argv[];
  37. {
  38.    register char   *p, fstrng[12];
  39.    register int    c, i;
  40.     int nfile;
  41.     char tolower();
  42.    FILE        *f, fdta, fidx;
  43.  
  44.    if (argc == 2 && argv[1][0] == '?' && argv[1][1] == 0) {
  45.       help(documentation);
  46.       return;
  47.       }
  48.    nfile = argc-1;
  49.    for (i=1; i < argc; ++i) {
  50.       p = argv[i];
  51.       if (*p == '-') {
  52.      ++p;
  53.      while (c = *p++) {
  54.         switch(tolower(c)) {
  55.  
  56.         case '?':
  57.            help(documentation);
  58.            break;
  59.  
  60.         case 'D':
  61.         case 'd':
  62.            dbug++;
  63.            break;
  64.  
  65.         default:
  66.            usage("Unknown flag");
  67.         }
  68.      }
  69.      argv[i] = 0;
  70.      --nfile;
  71.       }
  72.    }
  73.     if (nfile == 0)
  74.         puts("PC-FILE data filename missing\n");
  75.     else if (nfile > 1)
  76.         usage("Too many filenames");
  77.     else {
  78.         strncpy(fstrng, p, sizeof(fstrng)-4);
  79.         strncat(fstrng, ".HDR", sizeof(fstrng));
  80.         if ((f=fopen(fstrng, "r")) == NULL)
  81.            cant(fstrng);
  82.         else {
  83.            loadpc(f, fstrng);
  84.            fclose(f);
  85.         }
  86.     }
  87.         strncpy(fstrng, p, sizeof(fstrng)-4);
  88.         strncat(fstrng, ".DTA", sizeof(fstrng));
  89.         if ((fdta=fopen(fstrng, "r")) == NULL) {
  90.            cant(fstrng);
  91.            exit(1);
  92.         }
  93.  
  94.         strncpy(fstrng, p, sizeof(fstrng)-4);
  95.         strncat(fstrng, ".INX", sizeof(fstrng));
  96.         if ((fidx=fopen(fstrng, "r")) == NULL) {
  97.            cant(fstrng);
  98.            exit(1);
  99.         }
  100.  
  101.         else {
  102.            pcfupd(fdta, fidx, p);
  103.            fclose(fdta);
  104.            fclose(fidx);
  105.         }
  106. }
  107.  
  108.  
  109. /*******************************************************/
  110.  
  111. usage(s)
  112. char    *s;
  113. {
  114.    puts("?LIST-E-");
  115.    puts(s);
  116.    puts("\n");
  117.    puts("Usage: pcfupd [-wul] [file] [<file] [>file].  \n");
  118.    puts(" pcfupd ? for help\n");
  119.    exit(1);
  120. }
  121.  
  122.  
  123. /*******************************************************/
  124.  
  125. loadpc(fp, fn)
  126. FILE       *fp;       /* File to process        */
  127. char       *fn;       /* File name (for -f option)  */
  128. /*
  129.  * load PC-FILE III field definitions fron <file>.HDR.
  130.  */
  131. {
  132.     char c, *fgets(), lstrng[10];
  133.     int iflg, i, fgetc();
  134.  
  135.     totlen = 0;
  136.     while ((c=fgetc(fp)) != EOF && nflds < MAXINDX) {
  137.  
  138.          /*  get rest of name  */
  139.         fields[nflds].fname[0] = c;
  140.         for(i = 1; i < sizeof(fields[0].fname); ++i) {
  141.             if ((c = fgetc(fp)) == EOF)
  142.             error("I/O Error on <file>.HDR","");
  143.             fields[nflds].fname[i] = c;
  144.             if ((c == '\r') || (c == '\n') || (c == EOS))
  145.             break;
  146.         }
  147.  
  148.       /*  remove trailing blanks   */
  149.          if (fields[nflds].fname[i-1] == ' ')
  150.          while (fields[nflds].fname[i-1] == ' ')
  151.              i--;
  152.  
  153.         fields[nflds].fname[i] = EOS;
  154.  
  155.  
  156.          /*  remove trailing trash    */
  157.         while (c != '\n')
  158.             if ((c = fgetc(fp)) == EOF)
  159.             error("I/O Error on <file>.HDR","");
  160.  
  161.          /*  get length of field  */
  162.         if(fgets(lstrng,sizeof(lstrng),fp) == NULL)
  163.             error("Read error on <file>.HDR","");
  164.         fields[nflds].flen = atoi(lstrng);
  165.  
  166.          /*  get position of field    */
  167.         fields[nflds].fpos = totlen;
  168.         totlen += fields[nflds].flen;
  169.  
  170.         if (dbug > 1)
  171.             printf(" n=%s l=%d p=%d\n", fields[nflds].fname,
  172.               fields[nflds].flen, fields[nflds].fpos);
  173.  
  174.          /*  bump the pointer and continue    */
  175.         nflds++;
  176.     }
  177. }
  178.  
  179.  
  180. /*******************************************************/
  181.  
  182. pcfupd(fdta, fidx, fn)
  183. FILE       *fdta, *fidx;       /* Files to process          */
  184. char       *fn;            /* File name   */
  185. /*
  186.  * update a PC-FILE III data file.
  187.  */
  188. {
  189.     char c, indxrec[MAXINDX*2+4], datarec[MAXDATA], lstrng[5];
  190.     int imod, irecs, drecs, *idxptr;
  191.     long dtask1, dtask2, idxsk1, idxsk2, lseek();
  192.  
  193.     drecs = irecs = 0;
  194.     indxlen = (nflds * 2) + 4;      /* length of *.INX recs  */
  195.     datalen = totlen + 1;          /* length of *.DTA recs  */
  196.     idxptr = &indxrec[nflds*2-1];      /* location of pointer   */
  197.     if (dbug)
  198.         printf(" indxlen=%d,  datalen=%d\n", indxlen, datalen);
  199.  
  200.     if (indxlen > sizeof(indxrec))
  201.         error("Index rec size too large\n","");
  202.     if (datalen > sizeof(datarec))
  203.         error("Data rec size too large\n","");
  204.  
  205.      /*  read an index record  */
  206.     idxsk1 = lseek(fidx,0l,0);   /* get starting position  */
  207.     while (fread(indxrec,indxlen,1,fidx) != NULL) {
  208.  
  209.      /*  ignore deleted records             */
  210.            if (indxrec[1] == indxrec[2] && indxrec[1] == '/')
  211.            continue;
  212.            else
  213.            irecs++;
  214.  
  215.            imod = 0;
  216.      /*  update the index entry, if necessary.        */
  217.      /*    (insert update code here)            */
  218.      /*     set imod != zero if re-write necessary  */
  219.  
  220.      /*  re-write the index entry, if necessary.  */
  221.            if (imod) {
  222.            idxsk2 = lseek(fidx,0l,1);    /* save pos of next record  */
  223.            if (lseek(fidx, idxsk1, 0))    /* locate addr to re-write */
  224.                error("Seek error on index file","");
  225.            if (fwrite(indxrec, indxlen,1,fidx) == NULL)
  226.                error("Re-write failed on index file","");
  227.            if (lseek(fidx, idxsk2, 0))    /* reposition for next read */
  228.                error("Seek error2 on index file","");
  229.            }
  230.  
  231.            imod = 1;
  232.      /*  select based on index entry, if desired    */
  233.      /*     (insert selection code here)        */
  234.      /*     set imod == zero if read not necessary  */
  235.  
  236.      /*  read the data file record, if necessary.   */
  237.            if (imod) {
  238.            strncpy(lstrng,*idxptr,4);    /* move to work area  */
  239.            lstrng[4] = EOS;        /* end of string      */
  240.            dtask1 = (atoi(lstrng) - 1) * datalen;
  241.            if (dbug)
  242.                printf("dtask1=%d, idxptr=%d\n", dtask1, *idxptr);
  243.  
  244.            if (dtask1 >= 0) {
  245.                if (lseek(fidx, dtask1, 0))  /* locate for re-write */
  246.                error("Seek error on data file","");
  247.                if (fgets(datarec,datalen,fdta) == NULL)
  248.                error("Read error on data file\n","");
  249.                drecs++;
  250.            } else
  251.                error("Seek addr < zero\n","");
  252.  
  253.            imod = 0;
  254.      /*  update the data record, if necessary.        */
  255.      /*    (insert update code here)            */
  256.      /*     set imod != zero if re-write necessary  */
  257.            imod = 1;   /*debug*/
  258.  
  259.      /*  re-write the data entry, if necessary.  */
  260.            if (imod) {
  261.                if (dbug)
  262.                printf("dtask1 on rewrite=%d\n", dtask1);
  263.                if (lseek(fdta, dtask1, 0))  /* loc addr to re-write */
  264.                error("Seek error on data file","");
  265.                if (fwrite(datarec, datalen,1,fdta) == NULL)
  266.                error("Re-write failed on data file","");
  267.            }
  268.  
  269.         }
  270.     }
  271. }
  272.