home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 142_01 / unscrub.c < prev    next >
Text File  |  1985-03-10  |  4KB  |  164 lines

  1. /*
  2.     Program to 'unscrub' an ASCII text file for processing with
  3.     Wordstar.    Ref Your Computer May 1984, p 120
  4.  
  5.     Modification History - most recent first.
  6.  
  7.     14 Oct 1984, Modified to compile using Aztec CII
  8.     ver 1.05g under CP/M 2.2.  Functions agetc and aputc 
  9.     used are ASCII versions of getc and putc which translate
  10.     \n from and to CR/LF.  Nested switch in linefeed count
  11.     detection rewritten to appease Aztec compiler.  The SECOND
  12.     successive linefeed is taken to mean a new paragraph, and
  13.     put out as 2 CR/LF pairs, to make up for the one lost. 
  14.     Alan Coates.
  15.  
  16.     18 Feb 1984, Bill Bolton, from idea by Harvey Lord.
  17.     Digital Research C.
  18.  
  19.  */
  20.  
  21. #include "libc.h"
  22.  
  23. /*    Macros for constant definitions        */
  24.  
  25. #define VERS 1        /* main version number    */
  26. #define REVISION 1    /* sub version number    */
  27. #define DEL 0x7F    /* ASCII delete        */
  28. #define WORKING 1024
  29. #define NEXTLINE (WORKING*32)
  30. #define CPMEOF 0x1A
  31. #define ERROR 0
  32. #define FERROR -1
  33. #define NO 0
  34. #define void int    /* Aztec CII v 1.05g doesn't like void */
  35. #define YES_CR1 1
  36. #define YES_CR2    2
  37. #define YES_SP    3
  38. /*
  39.     Argument vector indices
  40.  */
  41.  
  42. #define FROM_FILE    1
  43. #define TO_FILE        2
  44. /*
  45.     main to open files for unscrub()
  46.     and handle invocation errors
  47.  */
  48.  
  49. main(argc,argv)
  50. int argc;
  51. char *argv[];
  52. {
  53.     FILE *fdin,*fdout;
  54.     char buf[12];
  55.     printf("\n\tWordstar file UNSCRUBber, CP/M-80 Version %d.%d\n",
  56.         VERS,REVISION);
  57.     printf("Bill Bolton, Software Tools, modified by Alan Coates\n");
  58.     if ( argc != 3 )
  59.         usage();
  60.     else {
  61.         if( (fdin = fopen(argv[FROM_FILE],"r")) == ERROR){
  62.           printf("\nCant find file%s\n",argv[FROM_FILE]);
  63.           usage();
  64.         }
  65.         else {
  66.             if( (fdout = fopen(argv[TO_FILE],"w")) == ERROR)
  67.               printf("\nCant open %s\n",argv[TO_FILE]);
  68.             else{
  69.                 printf("\nWorking ");
  70.                 unscrub(fdin,fdout);
  71.             }
  72.         }
  73.     }
  74.     exit(0);    /* dummy argument in Aztec CII */
  75. }
  76. /*
  77.     procedure unscrub -- copy file to file deleting unwanted control chars
  78.  */
  79.  
  80. void unscrub(fpin,fpout) /* Aztec doesn't like same name for arguments */
  81. FILE *fpin;    /* the input file buffer    */
  82. FILE *fpout;    /* and the output    */
  83.  
  84. {
  85.     int c;        /* 1 char buffer    */
  86.     int loop;    /* counter        */
  87.     long count;    /* chars processed    */
  88.     long killed;    /* bytes deleted    */
  89.     int cr_flag;
  90.     int space_flag;
  91.  
  92.     loop     = 0;
  93.     count     = 0;
  94.     killed    = 0;
  95.     cr_flag = NO;    
  96.     space_flag = NO;
  97.  
  98.     while((c = agetc(fpin)) != EOF && c != CPMEOF)
  99.     {
  100.         c &= 0x7F;
  101.         loop++;
  102.         count++;
  103.         if (loop % WORKING == 0)
  104.         {
  105.             printf("*");    /* still alive!     */
  106.             if (loop % NEXTLINE == 0)
  107.                 printf("\n\t");    /* newline at intervals    */
  108.             
  109.         }
  110.  
  111.         switch (c)
  112.         {
  113.         case '\n':
  114.             if (cr_flag == NO){
  115.                 putc(' ',fpout); /* replace with a space */
  116.                 cr_flag = YES_CR1;    /* reset flag */
  117.                 killed++;
  118.             }
  119.             else if (cr_flag == YES_CR1){
  120.                 aputc('\n',fpout); 
  121.                 aputc('\n',fpout); /* 2xCR/LF, new para */
  122.                 cr_flag = YES_CR2;
  123.             }
  124.             else
  125.                 aputc('\n',fpout); /* save other linefeeds */
  126.             break;
  127.  
  128.         case ' ':
  129.             if (space_flag == NO)    /* first space?    */
  130.                 putc(c,fpout);    /* then keep it    */
  131.             else
  132.                 killed++;
  133.             space_flag = YES_SP;    /* flag to discard later sp */
  134.             break;
  135.  
  136.         default:
  137.             cr_flag = NO;
  138.             space_flag = NO;
  139.             putc(c,fpout);        /* write all else to output */
  140.             break;
  141.         }
  142.     }
  143.  
  144.     putc(CPMEOF,fpout);
  145.     printf("\n");
  146.     if (fclose(fpout) == FERROR)
  147.         exit(puts("\nOutput file close error\n"));
  148.     
  149.     printf("\n%ld characters processed\n",count);
  150.     printf("%ld characters were deleted.\n",killed);
  151. }
  152.  
  153. void usage()
  154.  
  155. {
  156.     printf("\nUsage:\n");
  157.     printf("\tUNSCRUB d:file1 d:file2\n\n");
  158.     printf("Prepares a single spaced ASCII file for reformatting\n");
  159.     printf("with Wordstar, by removing CR/LFs from the end of lines\n");
  160.     printf("within single spaced paragraphs, removing excess spaces\n");
  161.     printf("between words, leaving the file ready for ^B formatting\n");
  162.     exit(0);
  163. }
  164.