home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / cpp112.zoo / src / generic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-07  |  3.5 KB  |  175 lines

  1. /*
  2. generic -- a pseudo-template code processor
  3.  
  4.              WARNING   WARNING   WARNING
  5. This code is basically a hack.  Only a bare minimum of error checking is
  6. done.  Use caution.
  7.  
  8. Usage:
  9.              parm 1 ---\    /--- parm 2
  10.                  vvvvv vvv
  11.     generic fred.zg joebob:1 alice:257 harvey:17
  12.             ^^^^^^^^ ^^^^^^^^^ ^^^^^^^^^
  13.             \--------|---------/
  14.               parameter groups
  15.  
  16. Output file name is input file name without the trailing 'g'.
  17.  
  18. If there is a line in the input file beginning with "@0", it is removed
  19. and all lines above it appear only once in the output.  All other lines
  20. appear once per parameter group.  A '@' followed by a digit from 1 to 9
  21. is replaced by the corresponding parameter from the current parameter
  22. group.  The text "@@" is replaced by a single '@'.  All other text
  23. passes through unchanged.
  24.  
  25. Example:
  26.  
  27. Input file:
  28. --------------------
  29. This is a test.
  30. @0
  31. This is @1 a @2.
  32. --------------------
  33.  
  34. Command:
  35. generic fred.zg only:test not:drill
  36.  
  37. Output:
  38. --------------------
  39. This is a test.
  40. This is only a test.
  41. This is not a drill.
  42. --------------------
  43. */
  44.  
  45. #include <stdio.h>
  46. #include <stddef.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <ctype.h>
  50.  
  51. #define LINEBUF 1024
  52.  
  53. char *line = 0;
  54. size_t size;
  55. FILE *inf, *outf;
  56. char *parms[9];
  57. fpos_t seekpoint;
  58.  
  59.  
  60. int getline()
  61. {
  62.   register char *s, *t;
  63.   ptrdiff_t dp;
  64.  
  65.   if (!line)
  66.     line = malloc(size = LINEBUF);
  67.   s = line;
  68.   if (!fgets(s, size, inf)) {
  69.     return 0;
  70.   }
  71.   t = s + strlen(s);
  72.   for (;;) {
  73.     if (t[-1] != '\n') {
  74.       if (size - (t - s) < LINEBUF) {
  75.     dp = t - s;
  76.     size += LINEBUF;
  77.     s = (char *)realloc(s, size);
  78.     t = s + dp;
  79.       }
  80.       if (!fgets(t, (int)(size - (t - s)), inf)) {
  81.     *t = '\0';
  82.     break;
  83.       }
  84.       t += strlen(t);
  85.     } else
  86.       break;
  87.   }
  88.   *t = '\0';
  89.   line = s;
  90.   return 1;
  91. }
  92.  
  93. void set_parms(s) register char *s;
  94. {
  95.   register char *t;
  96.   register int i;
  97.  
  98.   for (i = 0; i <9; i++)
  99.     parms[i] = 0;
  100.   for (i = 0; i < 9 && *s; i++) {
  101.     t = strchr(s, ':');
  102.     if (!t)
  103.       t = s + strlen(s);
  104.     *t = '\0';
  105.     parms[i] = s;
  106.     s = t + 1;
  107.   }
  108. }
  109.  
  110. void process_line()
  111. {
  112.   register char *s, *t;
  113.  
  114.   s = line;
  115.   if (*s == '@' && s[1] == '0') {
  116.     fgetpos(inf, &seekpoint);
  117.     return;
  118.   }
  119.   for (;;) {
  120.     t = strchr(s, '@');
  121.     if (!t) {
  122.       fputs(s, outf);
  123.       return;
  124.     }
  125.     *t++ = '\0';
  126.     fputs(s, outf);
  127.     if (*t == '@') {
  128.       fputc('@', outf);
  129.       s = t + 1;
  130.       continue;
  131.     }
  132.     if (!isdigit(*t) || *t == '0') {
  133.       fputc('@', outf);
  134.       s = t;
  135.       continue;
  136.     }
  137.     if (parms[*t - '1'])
  138.       fputs(parms[*t - '1'], outf);
  139.     s = t + 1;
  140.   }
  141. }
  142.  
  143. main(argc, argv) int argc; char *argv[];
  144. {
  145.   char buf[512];
  146.   register char *s = buf;
  147.   register int i;
  148.  
  149.   if (argc < 3) {
  150.     fprintf(stderr, "usage: %s infile parmlist [parmlist ...]\n", argv[0]);
  151.     exit(1);
  152.   }
  153.   if (!(inf = fopen(argv[1], "r"))) {
  154.     fprintf(stderr, "%s: cannot open input file %s\n", argv[0], argv[1]);
  155.     exit(1);
  156.   }
  157.   fgetpos(inf, &seekpoint);
  158.   strcpy(buf, argv[1]);
  159.   s += strlen(s) - 1;
  160.   if (*s == 'g') *s = '\0'; else {*++s = 'g'; *++s = '\0';}
  161.   if (!(outf = fopen(buf, "w"))) {
  162.     fprintf(stderr, "%s: cannot open output file %s\n", argv[0], buf);
  163.     exit(1);
  164.   }
  165.   for (i = 2; i < argc; i++) {
  166.     set_parms(argv[i]);
  167.     while (getline())
  168.       process_line();
  169.     fsetpos(inf, &seekpoint);
  170.   }
  171.   fclose(inf);
  172.   fclose(outf);
  173.   return 0;
  174. }
  175.