home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / util2 / scrub.lzh / SCRUB.C next >
C/C++ Source or Header  |  1986-04-14  |  7KB  |  243 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. 28/Apr/84
  7.    Revised to work on IBM-PC using Lattice. (P.H. Mack)
  8. 26/Sep/82
  9.     Forces MSB of all characters to 0, then scans for control
  10.     codes. TAB, CR and LF are passed unchanged to the output
  11.     file. US (soft hyphen) is replaced by a hard hyphen.
  12.     Checking for legal CP/M filename on destination file
  13.     added. Expanded "usage" message. Added "working" messages.
  14.     Bill Bolton. 
  15.  
  16.     This program was developed from a program called SCRUB
  17.     on BDS "C" User Group disk "Utilities 2" (Volume 2 in
  18.     the Software Tools RCPM BDSCAT.ALL).
  19. */
  20.  
  21. /*
  22.     Macros for constant definitions
  23. */
  24.  
  25. #include "STDIO.H"
  26.  
  27. #define VERSION 2    /* main version number */
  28. #define REVISION 1    /* 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 BUFSIZ     1024
  33. #define CPMEOF     0x1A
  34.  
  35. int _fmode = 0x8000;
  36. FILE *fdin, *fdout, *fopen();
  37. char *fname[3];
  38. /*
  39.     Argument vector indices
  40. */
  41.  
  42. /***************************************************
  43.     main to open the files for scrub()
  44.     and handle invocation errors.
  45. ****************************************************/
  46.  
  47. main(argc,argv)
  48. int argc;
  49. char *argv[];
  50.  
  51. {
  52.     char inbuf[BUFSIZ],outbuf[BUFSIZ];
  53.     char buf[12];
  54.  
  55.     cprintf("\nWordStar file Scrubber Version %d.%d\n",VERSION,REVISION);
  56.     cprintf("Bill Bolton, Software Tools\n");
  57.     if( argc != 3 ){
  58.             usage();
  59.             exit();
  60.     }
  61.     else {
  62.         fname[1]=argv[1];fname[2]=argv[2];
  63.         if((fdin = fopen(fname[1],"r")) == NULL){
  64.             cprintf("\nCannot find file %s\n",fname[1]);
  65.             usage();
  66.             exit();
  67.         }
  68.         else {
  69.             if(bad_name(fname[2])) {
  70.                 cprintf("\nBad file name %s\n",fname[2]);
  71.                 usage();
  72.                 exit();
  73.             }
  74.             else {
  75.                 if((fdout  = fopen(fname[2],"w")) == NULL ){
  76.                     cprintf("\nCan't open %s\n",fname[2]);
  77.                     exit();
  78.                 }
  79.                 else {
  80.                     cprintf("\nWorking\n\r ");
  81.                     scrub(fdin,fdout);
  82.                 }
  83.             }
  84.         }
  85.     }
  86.     fclose(fdin);
  87.     fclose(fdout);
  88.     exit();
  89. }
  90.  
  91. /***************************************************
  92.  procedure scrub -- copy file to file deleting unwanted control chars
  93. ****************************************************/
  94.  
  95. scrub(filein,fileout)
  96. FILE   *filein;    /* the input file buffer */
  97. FILE   *fileout;    /* the output file buffer */
  98.  
  99. {
  100.     int c;            /* 1 char buffer */
  101.     unsigned count;        /* count of characters processed */
  102.     unsigned count1;    /* high word of count */
  103.     unsigned killed;    /* numbers of bytes deleted */
  104.     unsigned hyphen;    /* number of soft hyphens replaced */
  105.  
  106.     count  = 0;
  107.     killed = 0;
  108.     hyphen = 0;
  109.  
  110.     while( (c = getc(filein) & 0x7F) != EOF  && c != CPMEOF ){
  111.         count++;
  112.         if (count % WORKING == 0)
  113.             cprintf("*");        /* still alive */
  114.         if (count % NEXTLINE == 0)
  115.             cprintf("\n\t");        /* new line every so often */
  116.         if( c >= ' ' && c < '\177' )    /* visable character ? */
  117.             putc(c,fileout);
  118.         else
  119.             switch(c) {
  120.                 case '\r':
  121.                 case '\n':
  122.                 case '\t':
  123.                     putc(c,fileout); /* ok control chars */
  124.                     break;
  125.  
  126.                 case '\037':    /* replace WS soft hyphen */
  127.                     putc('-',fileout);
  128.                     hyphen++;
  129.                     break;
  130.  
  131.                 default:
  132.                     killed++;
  133.                     break;         /* ignore it */
  134.               }
  135.     }
  136.     putc(CPMEOF,fileout);             /* sent textual end of file */
  137.     cprintf("\n");
  138.     cprintf("\n%u characters processed\n",count);
  139.     cprintf("%u characters were deleted\n",killed);
  140.     cprintf("%u soft hyphens replaced\n",hyphen);
  141. }
  142.  
  143. /***************************************************
  144.  check for bad name
  145. ****************************************************/
  146. int bad_name(buf)
  147.  
  148. char    *buf;
  149.  
  150. {
  151.     char    fcb[36];
  152.  
  153.     setfcb(fcb,buf);
  154.     if (fcb[0] < 0 || fcb[0] > 15)    /* check for valid drive */
  155.         return(ERROR);
  156.     return(valid_check(fcb));
  157. }
  158.  
  159. /***************************************************
  160.  
  161. ****************************************************/
  162. int valid_check(buf)
  163.  
  164. char    *buf;
  165.  
  166. {
  167.     int    index;
  168.  
  169.     for (index = 1; index < 12; ++index){
  170.         if (bad_char(index,buf))
  171.             return(-1);
  172.     }
  173.     return(0);
  174. }
  175.  
  176. /***************************************************
  177.  Checks if a character is legal in a CP/M directory entry or file control
  178.  block 
  179. ****************************************************/
  180.  
  181. int bad_char(index,buf)
  182.  
  183. int    index;
  184. char    *buf;
  185.  
  186. {
  187.     char    temp[1];        /* Transient storage for > del test */
  188.     int    space;
  189.  
  190.     if (index == 1 || index == 9)    /* Reset at start of filename & typ */
  191.         space = 0;         
  192.     switch (buf[index]){
  193.     case  '*':
  194.     case  ',':
  195.     case  '.':
  196.     case  ':':
  197.     case  ';':
  198.     case  '<':
  199.     case  '=':
  200.     case  '>':
  201.     case  '?':
  202.     case  '[':
  203.     case  ']':
  204.     case  DEL:
  205.         return(-1);
  206.     case  ' ':            /* Space is conditionally illegal */
  207.         if (space == 7)        /* Filename is all spaces is a */
  208.             return(-1);    /* definite error */
  209.         space++;        /* Else just keep track of how many */
  210.         return(0);
  211.     }
  212.     if (buf[index] < ' ' || (buf[index] >= 'a' && buf[index] <= 'z'))
  213.         return(-1);
  214.     if (buf[index] > DEL){        /* Bit 7 set may be legal attribute */
  215.         temp[0] = buf[index] & DEL; /* so force it to 0 and try again */
  216.         return (bad_char(0,temp));
  217.     }
  218.     else
  219.         return(space);         /* Space preceeding char IS illegal */
  220. }
  221.  
  222. /***************************************************
  223.  usage description
  224. ****************************************************/
  225. usage()
  226.  
  227. {
  228.         cprintf("\nUsage:\n\n\r");
  229.         cprintf("\tSCRUB d:file1 d:file2\n\n\r");
  230.         cprintf("Where:\n\r");
  231.         cprintf("\tfile1 = source file, (* and ? not allowed)\n\r");
  232.         cprintf("\tfile2 = destination file, (* and ? not allowed)\n\r");
  233.         cprintf("\td:    = optional drive identifier\n\n\r");
  234.         cprintf("i.e.\tSCRUB A:FOOBAR.WST B:FUBAR.DOC\n\r");
  235. }
  236.  
  237. /* end of scrub */
  238.  
  239.  
  240.  
  241.  
  242.  
  243.