home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / MacGofer 0.22d / MacGofer Sources / gofsplit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-06  |  2.4 KB  |  92 lines  |  [TEXT/MPS ]

  1. /*-----------------------------------------------------------------------------
  2.  * gofsplit.c   This program can be used to split the C code output produced
  3.  *              by the Gofer compiler into a number of smaller C files, each
  4.  *              of which can be compiled separately before linking to obtain
  5.  *              the final executable program.
  6.  *
  7.  *              Why bother?  Some C compilers seem to die on really large
  8.  *              input files, even if the function definitions themselves are
  9.  *              reasonably sized.  Perhaps they try to do some fancy global
  10.  *              optimization/data flow analysis...?
  11.  *
  12.  * For example: gofc + project              -- produces project.c as output
  13.  *              gofsplit project.c part .c  -- produces part00.c, part01.c,...
  14.  *              cc -O -c part00.c ...       -- compile individual files
  15.  *              cc -o project part00.o ...  -- link to produce executable
  16.  *                                             program
  17.  *---------------------------------------------------------------------------*/
  18.  
  19. #include <stdio.h>
  20.  
  21. #define LONGLINE 1024
  22. char theLine[LONGLINE+1];
  23. int  foundEof=0;
  24.  
  25. FILE *newPart(prefix,partNo,suffix)
  26. char *prefix;
  27. int  partNo;
  28. char *suffix; {
  29.     static char outputFile[256];
  30.     FILE *tmp;
  31.     sprintf(outputFile,"%s%02d%s",prefix,partNo,suffix);
  32.     tmp = fopen(outputFile,"w");
  33.     if (!tmp) {
  34.     fprintf(stderr,"Can't write %s\n",outputFile);
  35.     exit(1);
  36.     }
  37.     printf("Writing %s\n",outputFile);
  38.     if (partNo>0)
  39.     fprintf(tmp,"#include \"gofc.h\"\n\n");
  40.     return tmp;
  41. }
  42.  
  43. int readLine(fp)
  44. FILE *fp; {
  45.     int c;
  46.     int i = 0;
  47.     if (!foundEof) {
  48.     while ((c=fgetc(fp))!=EOF && c!='\n')
  49.         theLine[i++] = c;
  50.     if (c==EOF)
  51.         foundEof = 1;
  52.     else
  53.         theLine[i++] = c;
  54.     theLine[i] = '\0';
  55.     }
  56.     return foundEof;
  57. }
  58.  
  59. main(argc,argv)
  60. int argc;
  61. char *argv[]; {
  62.     if (argc!=4)
  63.     fprintf(stderr,"gofsplit: spli inputfile prefix suffix\n");
  64.     else {
  65.     char *inputFile = argv[1];
  66.     char *prefix    = argv[2];
  67.     char *suffix    = argv[3];
  68.     int  partCount  = 0;
  69.     FILE *ifile;
  70.     FILE *ofile;
  71.  
  72.     ifile = fopen(inputFile,"r");
  73.     if (!ifile)
  74.         fprintf(stderr,"Can't read %s\n",inputFile);
  75.     else {
  76.         do {
  77.         FILE *ofile=newPart(prefix,partCount++,suffix);
  78.         int linecount=0;
  79.         do {
  80.             if (readLine(ifile))
  81.             break;
  82.             linecount++;
  83.             fprintf(ofile,"%s",theLine);
  84.         } while (linecount<3000 || strcmp(theLine,"End\n")!=0);
  85.         fclose(ofile);
  86.         } while (!foundEof);
  87.         fclose(ifile);
  88.     }
  89.     }
  90. }
  91.  
  92.