home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_07 / 8n07137a < prev    next >
Text File  |  1990-06-19  |  4KB  |  159 lines

  1. /*
  2. &cstruse
  3. cstr - Produce compilable C STRing from a text file.
  4.  
  5. Syntax:
  6.         cstr file.str
  7.  
  8. Where:
  9.    file.str
  10.         is the name of a file containing text.  The filename need not end
  11.         in ".str", but it cannot end in ".c" (since the output would
  12.         destroy the input).
  13.  
  14. Descriptionn:
  15.         From a text file, cstr produces a C file containing a character
  16.         array named after the input file and initialized with its text.
  17.         If the first line of the text file is of the form:
  18.  
  19.         &name size
  20.  
  21.         it will be taken to indicate the name and the dimension (in bytes)
  22.         of the character array.  The "size" specification is optional.
  23. */
  24. /* ------------------------------------------------------------------ */
  25. /*  cstr - produce compilable C STRing from a text file.              */
  26. /*                                                                    */
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31.  
  32. #define MAX_LINE   512                /*  largest input line expected */
  33. #define MAX_NAME   128                /*  largest file name expected  */
  34.  
  35. main(argc,argv)
  36. char    **argv;
  37. {
  38.     int   i;
  39.     FILE  *fin, *fout;
  40.     char  foutname[MAX_NAME+1];
  41.     char  sname[MAX_NAME+1];
  42.  
  43.     fprintf(stderr, "cstr 1.0\n");
  44.     if(argc < 2)                      /*  if not command-line args... */
  45.         usage();
  46.     for(i = 1; i < argc; ++i)  {      /*  for each input file...      */
  47.         if( !makeout(argv[i], foutname, sname))  {
  48.             printf("%s: Input filename ('%s') must not end in '.c'\n",
  49.                     argv[0],argv[i]);
  50.             exit(EXIT_FAILURE);         /*  defined in <stdlib.h>     */
  51.         }
  52.         fin  = fopen(argv[i], "r");
  53.         if(!fin) {
  54.             printf("%s: Can't open '%s" for input\n", argv[0],argv[i]);
  55.             exit(EXIT_FAILURE);
  56.         }
  57.         fout = fopen(foutname, "w");
  58.         if(!fout) {
  59.             printf("%s: Can't open '%s' for output\n", argv[0],argv[i]);
  60.             exit(EXIT_FAILURE);
  61.         }
  62.         cstr(fin, fout, sname);
  63.         fclose(fin);
  64.         fclose(fout);
  65.     }
  66. }
  67. /*  usage - display correct usage and exit.  */
  68.  
  69. usage()
  70. {
  71. /*  extern char cstruse[]; */
  72.  
  73. /*  fputs(cstruse,stderr); */
  74.     exit(EXIT_FAILURE);               /*  defined in <stdlib.h>  */
  75. }
  76.  
  77. /*  makeout - make output name from input name.  */
  78.  
  79. static
  80. int     makeout(in, out, sname)
  81. char    *in, *out, *sname;
  82. {
  83.     char  c;
  84.     char  *t = "c";
  85.     while((*sname++=*out++=c=*in++) != '\0')
  86.         if(c == '.')
  87.             if (!strcmp(*in, *t))
  88.                 return(0);
  89.             else
  90.                 break;
  91.     if(c != '.')  {         /*  output has '.', even if input didn't. */
  92.         *out++ = '.';
  93.         *sname = '\0';
  94.     }
  95.     else
  96.         *--sname = '\0';
  97.     *out++ = 'c';
  98.     *out++ = '\0';
  99.     return(1);
  100. }
  101.  
  102. /*  cstr - do the actual work...  */
  103.  
  104. static
  105. int     cstr(fin, fout, sname)
  106. FILE    *fin, *fout;
  107. char    *sname;
  108. {
  109.     char    line[MAX_LINE+1], *maxdimp;
  110.     int     maxdim = 0, c, i;
  111.  
  112.     if((c = fgetc(fin)) == '&')  {    /*  if input specifies name and */
  113.                                       /*    dimension...              */
  114.         fgets(line, MAX_LINE, fin);
  115.         maxdimp = strpbrk(line, "\t\n");
  116.         if(*maxdimp)
  117.             *maxdimp++ = '\0';
  118.         maxdim  =  atoi(maxdimp);
  119.     }
  120.     else  {
  121.         ungetc(c, fin);
  122.         strcpy(line, sname);
  123.     }
  124.     fprintf(fout, "char %s[", line);
  125.     if(maxdim)
  126.         fprintf(fout, "%d", maxdim);
  127.     fprintf(fout, "]  =\n      {");
  128.  
  129.     for(i = 0; (c=fetc(fin)) !=EOF; ++i)   {
  130.         if(!(i%12))
  131.             fprintf(fout,"\n  ");
  132.         if(c == '\n')
  133.             fprintf(fout," '\\n',");
  134.         else if(c == '\t')
  135.             fprintf(fout," '\\t',");
  136.         else if(c == '\\')
  137.             fprintf(fout, " '\\\\',");
  138.         else
  139.             fprintf(fout,"  '%c',",c);
  140.     }
  141.     if (!(i%12)  {
  142.         fprintf(fout, "\n  ");
  143.         fprintf(fout,"   '\\0'\n   };\n");
  144.     }
  145.     else
  146.         fprintf(fout, "  '\\0'\n  };\n");
  147.     fprintf(stderr, "Did %d chars\n", i);
  148. }
  149. /* ------------------------------------------------------------------ */
  150. /* ------------------------------------------------------------------ */
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.