home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advos2 / ch16 / clean.c next >
Encoding:
C/C++ Source or Header  |  1988-12-12  |  2.5 KB  |  81 lines

  1. /*
  2.     CLEAN.C     Filter to turn document files into 
  3.                 normal text files.
  4.  
  5.     Copyright 1988 Ray Duncan
  6.  
  7.     Compile:    C>CL CLEAN.C
  8.  
  9.     Usage:      C>CLEAN  <infile >outfile
  10.  
  11.     All text characters are passed through with high bit stripped
  12.     off.  Form feeds, carriage returns, and line feeds are passed
  13.     through.  Tabs are expanded to spaces.  All other control codes
  14.     are discarded.
  15. */
  16.  
  17. #include <stdio.h>
  18.  
  19. #define TAB_WIDTH   8               /* width of a tab stop */
  20.  
  21. #define TAB     '\x09'              /* ASCII tab character */
  22. #define LF      '\x0A'              /* ASCII line feed */
  23. #define FF      '\x0C'              /* ASCII form feed */
  24. #define CR      '\x0D'              /* ASCII carriage return */
  25. #define BLANK   '\x20'              /* ASCII space code */
  26. #define EOFMK   '\x1A'              /* Ctrl-Z end of file */
  27.  
  28.  
  29. main(int argc, char *argv[])
  30. {   
  31.     char c;                         /* char. from stdin */
  32.     int col = 0;                    /* column counter */
  33.  
  34.     while((c = getchar()) != EOF)   /* read input character */
  35.     {   
  36.         c &= 0x07F;                 /* strip high bit */    
  37.  
  38.         switch(c)                   /* decode character */
  39.         {   
  40.             case LF:                /* if line feed or */
  41.             case CR:                /* carriage return, */
  42.                 col=0;              /* reset column count */
  43.  
  44.             case FF:                /* if form feed, carriage */
  45.                 wchar(c);           /* return, or line feed */
  46.                 break;              /* pass character through */
  47.  
  48.             case TAB:               /* if tab expand to spaces */
  49.                 do wchar(BLANK);
  50.                 while((++col % TAB_WIDTH) != 0);
  51.                 break;
  52.  
  53.             default:                /* discard other control */
  54.                 if(c >= BLANK)      /* characters, pass text */
  55.                 {                   /* characters through */
  56.                     wchar(c);
  57.                     col++;          /* bump column counter */
  58.                 }
  59.                 break;
  60.         }
  61.     }
  62.     wchar(EOFMK);                   /* write end-of-file mark */
  63.     exit(0);                    
  64. }
  65.  
  66.  
  67. /*  
  68.     Write a character to the standard output.  If
  69.     write fails, display error message and terminate.
  70. */
  71.  
  72. wchar(char c)
  73. {   
  74.     if((putchar(c) == EOF) && (c != EOFMK))
  75.     {   
  76.         fputs("clean: disk full",stderr);
  77.         exit(1);
  78.     }
  79. }
  80.  
  81.