C   124

trim c

Guest on 26th June 2022 04:00:03 AM

  1. /************************************************************************/
  2. /*                                                                      */
  3. /*      This compacts a text file, by discarding unnecessary            */
  4. /*                                                                      */
  5. /*  History:                                                            */
  6. /*    rjs  18sep89  Original version.                                   */
  7. /*    rjs  17feb04  Gosh is it really
  8.  old! Return a status.    */
  9. /*                                                                      */
  10. /* Operations performed include:                                        */
  11. /*      Trim off trailing  blanks.                                      */
  12. /*      Replace multiple spaces with tabs where possible.               */
  13. /*      Remove FORTRAN sequence numbers (-f flag).                      */
  14. /*      Handle backspace and delete characters.                         */
  15. /*      Delete blank lines (-b flag).                                   */
  16. /*                                                                      */
  17. /************************************************************************/
  18.  
  19. #define private static
  20. #include <stdio.h>
  21. #define MAXLINE 512
  22.  
  23. #define TRUE 1
  24. #define FALSE 0
  25.  
  26. private void process();
  27.  
  28. /************************************************************************/
  29. int main(argc,argv)
  30. int argc;
  31. char *argv[];
  32. {
  33.   char *outfile,*s;
  34.   int fort,blank,i,nfiles;
  35.   FILE *outfd,*fin;
  36.  
  37.   nfiles = 0;
  38.   outfile = NULL;
  39.   fort = FALSE;
  40.   blank = FALSE;
  41.   for(i=1; i < argc; i++){
  42.     s = argv[i];
  43.     if(*s == '-'){
  44.       while(*++s)switch(*s){
  45.         case 'f': fort = TRUE; break;
  46.         case 'b': blank   = TRUE; break;
  47.         default: fprintf(stderr,"Unrecognised flag %c\n",*s);
  48.       }
  49.       argv[i] = 0;
  50.     } else if(*s == '>') {
  51.       argv[i++] = NULL;
  52.       if(i < argc){
  53.         outfile = argv[i];
  54.         argv[i] = NULL;
  55.       }
  56.     } else nfiles++;
  57.   }
  58.  
  59. /* Open the output file, if required. */
  60.  
  61.   if(outfile == NULL) outfd = stdout;
  62.   else outfd = fopen(outfile,"w");
  63.   if( outfd == NULL) { perror("open out"); return(0); }
  64.  
  65. /* Process each of the input files. */
  66.  
  67.   if(nfiles == 0) process(outfd,stdin,fort,blank);
  68.   else for(i=1; i<argc; i++){
  69.     if(argv[i] != NULL){
  70.       fin = fopen(argv[i],"r");
  71.       if(fin == NULL) perror(argv[i]);
  72.       else{
  73.         process(outfd,fin,fort,blank);
  74.         fclose(fin);
  75.       }
  76.     }
  77.   }
  78.  return(0);
  79. }
  80. /************************************************************************/
  81. private void process(fout,fin,fort,blank)
  82. FILE *fout,*fin;
  83. int fort,blank;
  84. /*
  85.   Process an input file, and write it to the output.
  86.   Inputs:
  87.     fout        Stdio output file.
  88.     fin         FILE* of the input.
  89. ------------------------------------------------------------------------*/
  90. {
  91.   char line[MAXLINE];
  92.   char *s,*t;
  93.   int dofort,dowrite,linelen0,linelen;
  94.  
  95. /* Process the input file. */
  96.  
  97.   while(fgets(line,MAXLINE,fin) != NULL){
  98.  
  99. /* Eliminate backspace and delete characters from the line, as well as
  100.    any other rubbish. */
  101.  
  102.     t = line;
  103.     for(s=line; *s; s++){
  104.       if((char)*s == '\b' || (char)*s == '\177' ){ if(t > line) t--; }
  105.       else if(((char)*s < ' ' && (char)*s != '\t') || ((char)*s < '\0'));
  106.       else *t++ = *s;
  107.     }
  108.     *t = 0;
  109.  
  110. /*  Determine if we want to perform a FORTRAN trimming of this line. */
  111.  
  112.     dofort = fort && line[0] <= ' ';
  113.  
  114. /* Go through, shortening the line. */
  115.  
  116.     linelen = linelen0 = 0;
  117.     t = line;
  118.     for(s=line; *s; s++){
  119.       if(*s == '\t') linelen = 8*(linelen/8 + 1);
  120.       else if(*s == ' ') linelen++;
  121.       else if(dofort && linelen >= 72) break;
  122.       else {
  123.         if(linelen > linelen0){
  124.           if(linelen0 % 8 == 7){*t++ = ' '; linelen0++;}
  125.           while(8*(linelen0/8 + 1) <= linelen){
  126.             *t++ = '\t';
  127.             linelen0 = 8*(linelen0/8 + 1);
  128.           }
  129.           while(linelen0+1 <= linelen){
  130.             *t++ = ' ';
  131.             linelen0++;
  132.           }
  133.         }
  134.         *t++ = *s;
  135.         linelen0 = ++linelen;
  136.       }
  137.     }
  138.  
  139. /* Trim back the line to the first non-blank character. */
  140.  
  141.     t--;
  142.     while(t >= line && *t == ' ')t--;
  143.  
  144. /* Add a line feed and a zero terminating byte. */
  145.  
  146.     *(++t) = '\n'; *(++t) = 0;
  147.  
  148. /* Write out the line, if desired. */
  149.  
  150.     dowrite = line[0] != '\n' || !blank;
  151.     if(dowrite) fputs(line,fout);
  152.   }
  153. }

Raw Paste


Login or Register to edit or fork this paste. It's free.