home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume15 / yp-quote / unstr.c < prev   
Encoding:
C/C++ Source or Header  |  1988-06-07  |  3.6 KB  |  166 lines

  1. # include    <stdio.h>
  2. # include    <ctype.h>
  3. # include    "strfile.h"
  4.  
  5. # define    TRUE    1
  6. # define    FALSE    0
  7.  
  8. /*
  9.  *    This program un-does what "strfile" makes, thereby obtaining the
  10.  * original file again.  This can be invoked with the name of the output
  11.  * file, the input file, or both. If invoked with only a single argument
  12.  * ending in ".dat", it is pressumed to be the input file and the output
  13.  * file will be the same stripped of the ".dat".  If the single argument
  14.  * doesn't end in ".dat", then it is presumed to be the output file, and
  15.  * the input file is that name prepended by a ".dat".  If both are given
  16.  * they are treated literally as the input and output files.
  17.  *
  18.  *    Ken Arnold        Aug 13, 1978
  19.  */
  20.  
  21. # define    DELIM_CH    '-'
  22.  
  23. char    Infile[100],            /* name of input file */
  24.     Outfile[100],            /* name of output file */
  25.     Delimch = '%';            /* delimiter character */
  26.  
  27. short    Oflag = FALSE;            /* use order of initial table */
  28.  
  29. FILE    *Inf, *Outf;
  30.  
  31. char    *rindex(), *malloc(), *strcat(), *strcpy();
  32.  
  33. main(ac, av)
  34. int    ac;
  35. char    **av;
  36. {
  37.     register int    c;
  38.     register int    nstr, delim;
  39.     static STRFILE    tbl;        /* description table */
  40.  
  41.     getargs(ac, av);
  42.     if ((Inf = fopen(Infile, "r")) == NULL) {
  43.         perror(Infile);
  44.         exit(-1);
  45.         /* NOTREACHED */
  46.     }
  47.     if ((Outf = fopen(Outfile, "w")) == NULL) {
  48.         perror(Outfile);
  49.         exit(-1);
  50.         /* NOTREACHED */
  51.     }
  52.     (void) fread((char *) &tbl, sizeof tbl, 1, Inf);
  53.     if (Oflag) {
  54.         order_unstr(&tbl);
  55.         exit(0);
  56.         /* NOTREACHED */
  57.     }
  58.     nstr = tbl.str_numstr;
  59.     (void) fseek(Inf, (off_t) (sizeof (off_t) * (nstr + 1)), 1);
  60.     delim = 0;
  61.     nstr = 0;
  62.     while ((c = getc(Inf)) != EOF)
  63.         if (c != '\0')
  64.             putc(c, Outf);
  65.         else if (nstr != tbl.str_numstr - 1)
  66.             if (++nstr == tbl.str_delims[delim]) {
  67.                 fprintf(Outf, "%c-\n", Delimch);
  68.                 delim++;
  69.             }
  70.             else
  71.                 fprintf(Outf, "%c%c\n", Delimch, Delimch);
  72.     exit(0);
  73.     /* NOTREACHED */
  74. }
  75.  
  76. getargs(ac, av)
  77. register int    ac;
  78. register char    *av[];
  79. {
  80.     register int    i;
  81.     register char    *sp;
  82.     register int    j;
  83.     register short    bad;
  84.  
  85.     bad = 0;
  86.     for (i = 1; i < ac; i++)  {
  87.         if (av[i][0] != '-') {
  88.             (void) strcpy(Infile, av[i]);
  89.             if (i + 1 >= ac) {
  90.                 (void) strcpy(Outfile, Infile);
  91.                 if ((sp = rindex(av[i], '.')) &&
  92.                     strcmp(sp, ".dat") == 0)
  93.                     Outfile[strlen(Outfile) - 4] = '\0';
  94.                 else
  95.                     (void) strcat(Infile, ".dat");
  96.             }
  97.             else
  98.                 (void) strcpy(Outfile, av[i + 1]);
  99.             break;
  100.         }
  101.         else if (av[i][1] == '\0') {
  102.             printf("usage: unstr [-o] [-cC] datafile[.dat] [outfile]\n");
  103.             exit(0);
  104.             /* NOTREACHED */
  105.         }
  106.         else
  107.             for (sp = &av[i][1]; *sp != '\0'; sp++)
  108.                 switch (*sp) {
  109.                   case 'o':    /* print out in seekptr order */
  110.                     Oflag++;
  111.                     break;
  112.                   case 'c': /* new delimiting char */
  113.                     if ((Delimch = *++sp) == '\0') {
  114.                         --sp;
  115.                         Delimch = *av[++i];
  116.                     }
  117.                     if (!isascii(Delimch)) {
  118.                         fprintf(stderr,
  119.                             "bad delimiting character: 0x%x\n",
  120.                             Delimch);
  121.                         bad++;
  122.                     }
  123.                     break;
  124.                   default:
  125.                     fprintf(stderr, "unknown flag: '%c'\n",
  126.                         *sp);
  127.                     bad++;
  128.                     break;
  129.                 }
  130.     }
  131.     if (bad) {
  132.         printf("use \"%s -\" to get usage\n", av[0]);
  133.         exit(-1);
  134.     }
  135. }
  136.  
  137. order_unstr(tbl)
  138. STRFILE    *tbl;
  139. {
  140.     register int    i, c;
  141.     register int    delim;
  142.     register off_t    *seekpts;
  143.  
  144.     seekpts = (off_t *) malloc(sizeof *seekpts * tbl->str_numstr);    /* NOSTRICT */
  145.     if (seekpts == NULL) {
  146.         perror("malloc");
  147.         exit(-1);
  148.         /* NOTREACHED */
  149.     }
  150.     (void) fread((char *) seekpts, sizeof *seekpts, (int) tbl->str_numstr,
  151.              Inf);
  152.     delim = 0;
  153.     for (i = 0; i < tbl->str_numstr; i++, seekpts++) {
  154.         if (i != 0)
  155.             if (i == tbl->str_delims[delim]) {
  156.                 fputs("%-\n", Outf);
  157.                 delim++;
  158.             }
  159.             else
  160.                 fputs("%%\n", Outf);
  161.         (void) fseek(Inf, *seekpts, 0);
  162.         while ((c = getc(Inf)) != '\0')
  163.             putc(c, Outf);
  164.     }
  165. }
  166.