home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource3 / 142_01 / scrub.c < prev    next >
Text File  |  1985-03-11  |  4KB  |  144 lines

  1. /*
  2.     Program to "scrub" a WordStar text file back to a
  3.     standard ASCII file.
  4.  
  5. VERSION LIST, most recent version first
  6.  
  7. 14/Oct/84
  8.     Speedup (as given in Your Computer May 1984 by Bill Bolton),
  9.     and adapted to Aztec CII v1.05g for CP/M 2.2 by Alan Coates.
  10.  
  11. 26/Sep/82
  12.     Forces MSB of all characters to 0, then scans for control
  13.     codes. TAB, CR and LF are passed unchanged to the output
  14.     file. US (soft hyphen) is replaced by a hard hyphen.
  15.     Checking for legal CP/M filename on destination file
  16.     added. Expanded "usage" message. Added "working" messages.
  17.     Bill Bolton. 
  18.  
  19.     This program was developed from a program called SCRUB
  20.     on BDS "C" User Group disk "Utilities 2" (Volume 2 in
  21.     the Software Tools RCPM BDSCAT.ALL).
  22. */
  23.  
  24.  
  25. #include "libc.h"    /* Aztec header file*/
  26. #define void int    /* void not supported */
  27. #define VERSION 1    /* main version number */
  28. #define REVISION 3    /* sub version number */
  29. #define DEL 0x7F    /* ASCII delete character */
  30. #define WORKING 1024    /* number of chars between progress markers */
  31. #define NEXTLINE (WORKING * 32) /* number of progess chars on a screen line */
  32. #define CPMEOF 0x1a    /* changed from 0x19    */
  33.  
  34. #define FROM_FILE 1
  35. #define TO_FILE   2
  36.  
  37. /*
  38.     main to open the files for scrub()
  39.     and handle invocation errors.
  40. */
  41.  
  42. main(argc,argv)
  43. int argc;
  44. char *argv[];
  45.  
  46. {
  47.     FILE *fdin;
  48.     FILE *fdout;
  49.     char buf[12];
  50.     void scrub();
  51.     printf("\n\tWordStar file Scrubber Version %d.%d\n",VERSION,REVISION);
  52.     printf("Bill Bolton, Software Tools, modified by Alan Coates\n");
  53.     if( argc != 3 )
  54.             usage();
  55.     fdin = fopen(argv[FROM_FILE],"r");
  56.     if (fdin == NULL){
  57.         printf("\nCannot find file %s\n",argv[FROM_FILE]);
  58.         usage();
  59.     }
  60.     fdout  = fopen(argv[TO_FILE],"w");
  61.     if (fdout == NULL){
  62.         printf("\nCan't open %s\n",argv[TO_FILE]);
  63.         usage();
  64.     }
  65.     printf("\nWorking ");
  66.     scrub(fdin,fdout);
  67.     exit(0);
  68. }
  69.  
  70. /*
  71.     procedure scrub -- copy file to file deleting unwanted control chars
  72. */
  73.  
  74. void scrub(fpin,fpout)
  75. FILE *fpin;    /* the input file buffer */
  76. FILE *fpout;    /* the output file buffer */
  77.  
  78. {
  79.     int c;            /* 1 char buffer */
  80.     int loop;        /* a loop counter*/
  81.     long count;        /* count of characters processed */
  82.     long killed;        /* numbers of bytes deleted */
  83.     long hyphen;        /* number of soft hyphens replaced */
  84.  
  85.     loop = 0;
  86.     count  = 0;
  87.     killed = 0;
  88.     hyphen = 0;
  89.  
  90.     while( (c = agetc(fpin)) != EOF  && c != CPMEOF ){
  91.         c &= 0x7F;
  92.         loop++;
  93.         count++;
  94.         if (loop % WORKING == 0)
  95.             printf("*");        /* still alive */
  96.         if (loop % NEXTLINE == 0)
  97.             printf("\n\t");        /* new line every so often */
  98.         if( c >= ' ' && c < '\177' )    /* visible character ? */
  99.             aputc(c,fpout);
  100.         else    {
  101.             switch(c) {
  102.                 case '\r':
  103.                 case '\n':
  104.                 case '\t':
  105.                     aputc(c,fpout); /* ok control chars */
  106.                     break;
  107.  
  108.                 case '\037':    /* replace WS soft hyphen */
  109.                     aputc('-',fpout);
  110.                     hyphen++;
  111.                     break;
  112.  
  113.                 default:
  114.                     killed++;
  115.                     break;         /* ignore it */
  116.               }
  117.         }
  118.     }
  119.     aputc(CPMEOF,fpout);             /* sent textual end of file */
  120.     printf("\n");
  121.     if( fclose(fpout) == EOF ){        /* fclose returns -1 on error*/
  122.         printf("\nOutput file close error\n");
  123.         exit(0);
  124.     }
  125.     printf("\n%d characters processed\n",count);
  126.     printf("%d characters were deleted\n",killed);
  127.     printf("%d soft hyphens replaced\n",hyphen);
  128. }
  129.  
  130. void usage()
  131.  
  132. {
  133.         printf("\nUsage:\n\n");
  134.         printf("\tSCRUB d:file1 d:file2\n\n");
  135.         printf("Where:\n");
  136.         printf("\tfile1 = source file, (* and ? not allowed)\n");
  137.         printf("\tfile2 = destination file, (* and ? not allowed)\n");
  138.         printf("\td:    = optional drive identifier\n\n");
  139.         printf("e.g.\tSCRUB A:FOOBAR.WST B:FOOBAR.SCR\n");
  140.         exit(0);    /* dummy argument in Aztec v 1.05g */
  141. }
  142.  
  143. /* end of scrub */
  144.