home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_07_02 / v7n2028a.txt < prev    next >
Text File  |  1988-12-04  |  3KB  |  126 lines

  1. /*
  2.  *  cstr - produce compilable C STRing from a text file.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "blstr.h"
  8.  
  9. #define MAX_LINE    512         /* largest input line we expect */
  10. #define MAX_NAME    128         /* largest file name we expect  */
  11.  
  12.     main(argc, argv)
  13. char    **argv;
  14. {
  15. int     i;
  16. FILE    *fin, *fout;
  17. char    foutname[MAX_NAME+1];
  18. char    sname[MAX_NAME+1];
  19.  
  20. fprintf(stderr, "cstr 1.0\n");
  21.     if(argc < 2)                  /* if no command-line args  */
  22.         usage();
  23.     for(i = 1; i < argc; ++i) {   /* for each input file */
  24.         if( !makeout(argv[1], foutname, sname) )
  25.             error("%s: Input filename ('%s') must not end in '.c'\n",
  26.                     argv[0], argv[1]);
  27.         fin     = fopen(argv[1], "r");
  28.         if(!fin)
  29.             error("%s: Can't open '%s' for input\n", argv[0], argv[i]);
  30.     fout    = fopen(foutname, "w");
  31.         if(!fout)
  32.             error("%s: Can't open '%s' for input\n", argv[0], argv[i]);
  33.         cstr(fin, fout, sname);
  34.         fclose(fin);    fclose(fout);
  35.         }
  36. }
  37.  
  38. /*
  39.  *  usage - display correct usage and exit.
  40.  */
  41.  
  42.     usage()
  43. {
  44. extern char cstruse[];
  45.  
  46.     fputs(cstruse, stderr);
  47.     exit(EXIT_FAILURE);     /* defined in "stdlib.h" */
  48. }
  49.  
  50.  
  51. /*
  52.  *  makeout - make output name from input name.
  53.  */
  54.  
  55. static
  56. int     makeout(in, out, sname)
  57. char    *in, *out, *sname;
  58. {
  59. char    c;
  60.  
  61.     while((*sname++=*out++=c=*in++) != '\0')
  62.         if(c == '.')
  63.             if (!strcmp(*in, "c"))
  64.                 return (0);
  65.             else
  66.                 break;
  67.     if(c != '.') {       /* output has '.', even if input didn't */
  68.         *out++  = '.';
  69.         *sname = '\0';
  70.         }
  71.     else
  72.         *--sname = '\0';
  73.     *out++  = 'c';
  74.     *out++  = '\0';
  75.     return (1);
  76. }
  77.  
  78. /*
  79.  *  cstr - do the actual work.
  80.  */
  81. static
  82. int     cstr(fin, fout, sname)
  83. FILE    *fin, *fout;
  84. char    *sname;
  85. {
  86. char    line[MAX_LINE+1], *maxdimp;
  87. int     maxdim = 0, c, i;
  88.  
  89.     if((c = fgetc(fin)) == '&') {  /* if input specifies name and dimension  */
  90.         fgets(line, MAX_LINE, fin);
  91.         maxdimp = strbreak(line, " \t\n");
  92.         if(*maxdimp)
  93.             *maxdimp++  = '\0';
  94.         maxdim      = atoi(maxdimp);
  95.         }
  96.     else {
  97.         ungetc(c, fin);
  98.         strcpy(line, sname);
  99.         }
  100.     fprintf(fout, "char %s[", line);
  101.     if(maxdim)
  102.         fprintf(fout, "%d", maxdim);
  103.     fprintf(fout, "]    =\n    {");
  104.  
  105.     for(i = 0; (c = fgetc(fin)) != EOF; ++i) {
  106.         if(!(i%12))
  107.             fprintf(fout, "\n   ");
  108.         if(c == '\n')
  109.             fprintf(fout, " '\\n',");
  110.         else if(c == '\t')
  111.             fprintf(fout, " '\\t',");
  112.         else if(c == '\\')
  113.             fprintf(fout, " '\\\\',");
  114.         else
  115.             fprintf(fout, "  '%c',", c);
  116.         }
  117.     if (!(i%12)) {
  118.         fprintf(fout, "\n   ");
  119.         fprintf(fout, "  '\\0'\n    };\n");
  120.         }
  121.     else
  122.         fprintf(fout, " '\\0'\n    };\n");
  123.     
  124.     fprintf(stderr, "Did %d chars\n", i);
  125. }
  126.