home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d09xx / d0975.lha / PCal / pcalinit.c < prev    next >
C/C++ Source or Header  |  1993-11-28  |  4KB  |  171 lines

  1. /*
  2.  * Create a .h file from a .ps file.  Strips out leading and trailing 
  3.  * whitespace, blank lines, and lines consisting solely of comments,
  4.  * except for the very first block of comments/blanklines, which are
  5.  * turned into C comments at the top of the file.
  6.  * 
  7.  *   14-sep-90  Jamie Zawinski  created.
  8.  *
  9.  * Revision history:
  10.  *
  11.  *    4.3    AWR    10/15/91    minor tweak to avoid redefinition
  12.  *                    of PROTOS
  13.  *
  14.  *    4.0    AWR    02/25/91    added optional third argument for
  15.  *                    name of array
  16.  *
  17.  *        AWR    02/19/91    added function prototypes; documented
  18.  *
  19.  *        AWR    01/16/91    Escape " and \ in quoted strings;
  20.  *                    strip trailing comments; skip FF
  21.  *
  22.  *    2.6    JAB    10/18/90    Add exit(0).
  23.  *
  24.  *    2.3    JWZ    09/14/90    Author
  25.  */
  26.  
  27. #include <stdio.h>
  28. #include <ctype.h>
  29. #include <string.h>
  30.  
  31. #if !defined(PROTOS) && (defined(__STDC__) || defined(AMIGA))
  32. #define PROTOS
  33. #endif
  34.  
  35. #define FALSE    0
  36. #define TRUE    1
  37.  
  38. #define ARRAY_NAME    "header"    /* default name of array in .h file */
  39.  
  40. #define IS_WHITESPACE(c) \
  41.     ((c) == ' ' || (c) == '\t' || (c) == '\n' || c == '\f')
  42.  
  43. #define IS_POSTSCRIPT(s)    ((s)[0] != '%' && (s)[0] != '\0')
  44.  
  45.  
  46. /*
  47.  * strip_white: strip leading and trailing whitespace from 'string'; return
  48.  * pointer to first non-whitespace character
  49.  */
  50. #ifdef PROTOS
  51. char *strip_white (char *string)
  52. #else
  53. char *strip_white (string)
  54.      char *string;
  55. #endif
  56. {
  57.     int n;
  58.     for (; IS_WHITESPACE(*string); string++)
  59.     ;
  60.     n = strlen(string)-1;
  61.     for (; IS_WHITESPACE(string[n]); n--)
  62.     string[n] = '\0';
  63.     return string;
  64. }
  65.  
  66.  
  67. /*
  68.  * strip_comment: strip comment and any preceding whitespace from 'string';
  69.  * return pointer to 'string'
  70.  */
  71. #ifdef PROTOS
  72. char *strip_comment (char *string)
  73. #else
  74. char *strip_comment (string)
  75.      char *string;
  76. #endif
  77. {
  78.     char *p;
  79.     if ((p = strchr(string, '%')) != NULL) {
  80.     *p = '\0';
  81.     string = strip_white(string);
  82.     }
  83.     return string;
  84. }
  85.  
  86.  
  87. /*
  88.  * escape: copy string 'in' to string 'out', escaping the characters \ and ";
  89.  * return pointer to 'out'
  90.  */
  91. #ifdef PROTOS
  92. char *escape(char *out, char *in)
  93. #else
  94. char *escape(out, in)
  95.      char *out, *in;
  96. #endif
  97. {
  98.    char c, *sv_out = out;
  99.  
  100.    for (; c = *in; *out++ = *in++)
  101.        if (c == '\\' || c == '"')
  102.       *out++ = '\\';
  103.  
  104.    *out = '\0';
  105.    return sv_out;
  106. }
  107.  
  108.  
  109. #ifdef PROTOS
  110. int main(int argc, char *argv[])
  111. #else
  112. int main (argc, argv)
  113.      int argc; char *argv[];
  114. #endif
  115. {
  116.     FILE *in, *out;
  117.     char line[256], line2[512], *L, *array;
  118.     int in_initial_comments = TRUE;
  119.  
  120.     /* retrieve arguments and attempt to open input and output files */
  121.  
  122.     if (argc < 3 || argc > 4) {
  123.        fprintf(stderr, "usage: %s <infile>.ps <outfile>.h [<arrayname>]\n",
  124.         argv[0]);
  125.        exit(-1); }
  126.     
  127.     in = fopen(argv[1], "r");
  128.     if (NULL == in) {
  129.        fprintf(stderr, "%s: couldn't open %s\n", argv[0], argv[1]);
  130.        exit(-1); }
  131.     
  132.     out = fopen(argv[2], "w");
  133.     if (NULL == out) {
  134.        fprintf(stderr, "%s: couldn't open %s\n", argv[0], argv[2]);
  135.        exit(-1); }
  136.     
  137.     array = argc == 4 ? argv[3] : ARRAY_NAME;
  138.  
  139.     /* print topline comment on output file */
  140.  
  141.     fprintf (out, "/*\n * %s: automatically generated by %s from %s\n",
  142.        argv[2], argv[0], argv[1]);
  143.     fprintf (out, " *\n *\tDO NOT EDIT THIS FILE!\n *\n");
  144.  
  145.     /* main loop - copy lines from input file, to output file, preserving
  146.      * only initial block of comments and blank lines
  147.      */
  148.  
  149.     while ( fgets(line, 255, in) != NULL ) {
  150.        L = strip_white(line);            /* strip whitespace */
  151.  
  152.        if ( IS_POSTSCRIPT(L) ) {        /* PostScript source? */
  153.       if ( in_initial_comments ) {        /* first PS line? */
  154.          in_initial_comments = FALSE;
  155.          fprintf(out, " */\n\nchar *%s[] = {\n", array);
  156.       }
  157.       L = strip_comment(L);            /* copy string to output */
  158.       L = escape(line2, L);
  159.       fprintf(out, "  \"%s\",\n", L);
  160.        } else                    /* blank or comment line */
  161.       if ( in_initial_comments )        /* copy only initial block */
  162.          fprintf(out, " * %s\n", L);
  163.     }
  164.  
  165.     fprintf(out, "  (char *)0,\n};\n");        /* terminate array decl */
  166.  
  167.     fclose(out);            /* close files and exit */
  168.     fclose(in);
  169.     exit (0);
  170. }
  171.