home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_03_01 / 3n01062a < prev    next >
Text File  |  1991-11-24  |  3KB  |  113 lines

  1. /*
  2.  *  WINEXT.C - DOS program to fix up bextstr output for Windows.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. /* Biggest line we can handle */
  9. #define MAX_LINE    (1024*1)
  10.  
  11. typedef unsigned long   ULONG;
  12.  
  13. FILE    *OpenFile(char *FileName, char *OpenMode)
  14.     {
  15.     FILE    *File = fopen(FileName, OpenMode);
  16.     if(File == NULL)
  17.         {
  18.         fprintf(stderr, "Can't open file '%s' for '%s'\n",
  19.             FileName, OpenMode);
  20.         exit(EXIT_FAILURE);
  21.         }
  22.     else
  23.         return File;
  24.     }
  25.  
  26. ULONG   GetLineCount(FILE *InputFile, char *InputLine)
  27.     {
  28.     if(fgets(InputLine, MAX_LINE, InputFile))
  29.         return atol(InputLine);
  30.     else
  31.         {
  32.         fprintf(stderr, "Input file is empty\n");
  33.         exit(EXIT_FAILURE);
  34.         }
  35.     }
  36.  
  37. size_t  ConvertLine(FILE *OutputFile, char *InputLine)
  38.     {
  39.     size_t  LineLength = 0;
  40.     char    C;
  41.  
  42.     for(; (C=*InputLine++); ++LineLength)
  43.         if(C == '\\')
  44.             switch(C = *InputLine++)
  45.                 {
  46.                 /* RC chokes on "\"" */
  47.                 case '"'    :
  48.                     fputs("\\042", OutputFile);
  49.                     break;
  50.                 default :
  51.                     fputc('\\', OutputFile);
  52.                     fputc(C, OutputFile);
  53.                 }
  54.         else
  55.             fputc(C, OutputFile);
  56.  
  57.     return LineLength + 1; /* add one for end-of-string */
  58.     }
  59.  
  60.  
  61. ULONG   Convert(FILE *InputFile, FILE *OutputFile,
  62.                 char *InputLine, ULONG TotalLines)
  63.     {
  64.     ULONG   CharCount = 0;
  65.     ULONG   LineCount = 0;
  66.  
  67.     while((TotalLines>0) && fgets(InputLine, MAX_LINE, InputFile))
  68.         {
  69.         --TotalLines;
  70.         if(strlen(InputLine))
  71.             InputLine[strlen(InputLine)-1]  = '\0';
  72.  
  73.         /* LineCount+1 because bexstr starts numbering
  74.          * at 2 for some reason.                        */
  75.         fprintf(OutputFile, "\t%lu\t\"", ++LineCount+1);
  76.         CharCount   += ConvertLine(OutputFile, InputLine);
  77.         fputs("\"\n", OutputFile);
  78.         }
  79.     if(TotalLines != 0)
  80.         {
  81.         fprintf(stderr, "Error: string file delivered %lu less lines than delivered\n", TotalLines);
  82.         exit(EXIT_FAILURE);
  83.         }
  84.     return CharCount;
  85.     }
  86.  
  87.  
  88. int     main(int argc, char **argv)
  89.     {
  90.     FILE    *InputFile, *OutputFile, *CountFile;
  91.     char    *InputLine = (char *)malloc(MAX_LINE);
  92.     ULONG   TotalLines, TotalChars;
  93.  
  94.     if(InputLine == NULL)
  95.         {
  96.         fprintf(stderr, "Can't allocate %ul bytes\n", MAX_LINE);
  97.         exit(EXIT_FAILURE);
  98.         }
  99.     InputFile   = OpenFile("ext.str", "r");
  100.     OutputFile  = OpenFile("ext.rc", "w");
  101.     CountFile   = OpenFile("extcnt.h", "w");
  102.     TotalLines  = GetLineCount(InputFile, InputLine);
  103.     fprintf(stdout, "Processing %lu lines\n", TotalLines);
  104.     TotalChars  = Convert(InputFile, OutputFile,
  105.                           InputLine, TotalLines);
  106.     fprintf(CountFile, "#define WINEXT_LINES (%lu)\n", TotalLines);
  107.     fprintf(CountFile, "#define WINEXT_CHARS (%lu)\n", TotalChars);
  108.     fclose(InputFile);
  109.     fclose(OutputFile);
  110.     fclose(CountFile);
  111.     exit(EXIT_SUCCESS);
  112.     }
  113.