home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / prev.tar.gz / prev.tar / prepro.c < prev    next >
C/C++ Source or Header  |  1991-01-29  |  2KB  |  101 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #ifdef VMS
  4. #include <types.h>
  5. #else
  6. #include <sys/types.h>
  7. #endif
  8. #ifdef MSC
  9. #include <fcntl.h>
  10. #include <string.h>
  11. #define rindex strrchr
  12. #else
  13. #ifdef SYSV
  14. #include <fcntl.h>
  15. #else
  16. #ifdef VMS
  17. #include <file.h>
  18. #else
  19. #include <sys/file.h>
  20. #endif
  21. #endif
  22. #endif
  23. #ifdef VMS
  24. #include <string.h>
  25. #define rindex strrchr
  26. #endif
  27. #ifdef MIPS
  28. #include <string.h>
  29. #define rindex strrchr
  30. #endif
  31.  
  32. extern char    *frameid;
  33.  
  34. /*
  35.  * prepro
  36.  *
  37.  *    open and read file1 and write it to the file pointed to by f2
  38.  * doing what preprocessing is necessary along the way.
  39.  */
  40. prepro(file1, f2)
  41.     char    *file1;
  42.     FILE    *f2;
  43. {
  44.     FILE    *f1;
  45.     int    c, lastc, incomment, linecount, i;
  46.     char    buf[BUFSIZ];
  47.  
  48. #ifdef MSC
  49.     if ((f1 = fopen(file1, "rt")) == NULL) {
  50. #else
  51.     if ((f1 = fopen(file1, "r")) == NULL) {
  52. #endif
  53.         sprintf(buf, "art: unable to open file %s.\n", file1);
  54.         fatal(buf);
  55.     }
  56.  
  57.     fprintf(f2, "# 1 \"%s\"\n", file1);
  58.  
  59.     incomment = 0;
  60.     linecount = 1;
  61.     lastc = 0;
  62.  
  63.     c = fgetc(f1);
  64.     while (!feof(f1)) {
  65.         if (c == '*' && lastc == '/')
  66.             incomment++;
  67.         if (c == '/' && lastc == '*')
  68.             incomment--;
  69.         else if (c == '\n')
  70.             linecount++;
  71.  
  72.         if (incomment)
  73.             fputc(c, f2);
  74.         else {
  75.             if (c == 'i')
  76.                 if ((c = fgetc(f1)) == 'n')
  77.                     if (fscanf(f1, "clude %s", buf) == 1) {
  78.                         i = strlen(buf) - 8;
  79.                         if (i > 0 && strcmp(&buf[i], ".frameid") == 0)
  80.                             strcpy(&buf[i + 1], frameid);
  81.                         prepro(buf, f2);
  82.                         fprintf(f2, "# %d \"%s\"\n", linecount, file1);
  83.                     } else {
  84.                         fputc('i', f2);
  85.                         fputc(c, f2);
  86.                     }
  87.                 else {
  88.                     fputc('i', f2);
  89.                     fputc(c, f2);
  90.                 }
  91.             else
  92.                 fputc(c, f2);
  93.         }
  94.  
  95.         lastc = c;
  96.         c = fgetc(f1);
  97.     }
  98.  
  99.     fclose(f1);
  100. }
  101.