home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / SIMTEL / CPMUG / CPMUG048.ARK / TABIFY.C < prev    next >
C/C++ Source or Header  |  1984-04-29  |  2KB  |  106 lines

  1. /*
  2.     Tabify
  3.     written by Leor Zolman
  4.  
  5.     command format:
  6.         tabify filename new_filename
  7.  
  8.     This program takes a text file and converts sequences
  9.     of spaces into tabs wherever possible.
  10.  
  11.     Revised 2/1/81 to process "scount" when encountering
  12.     an opening '"'; Ward C.
  13. */
  14.  
  15. #include "bdscio.h"
  16.     int scount, column, ifd, ofd, i, c;
  17.     char ibuf[BUFSIZ], obuf[BUFSIZ];
  18. main(argc,argv)
  19. char **argv;
  20. {
  21.     if (argc != 3) {
  22.         printf("usage: tabify oldfile newfile\n");
  23.         exit();
  24.     }
  25.     ifd = fopen(argv[1],ibuf);
  26.     ofd = fcreat(argv[2],obuf);
  27.     if (ifd == ERROR || ofd == ERROR) {
  28.         printf("Can't open file(s)\n");
  29.         exit();
  30.     }
  31.  
  32.     scount = column = 0;
  33.  
  34.     do {
  35.         c = getc(ibuf);
  36.         if (c == ERROR) {
  37.             putc(CPMEOF,obuf);
  38.             break;
  39.          }
  40.         switch(c) {
  41.            case '\r':    putc1(c,obuf);
  42.                 scount = column = 0;
  43.                 break;
  44.            case '\n':    putc1(c,obuf);
  45.                 scount = 0;
  46.                 break;
  47.            case ' ':    column++;
  48.                 scount++;
  49.                 if (!(column%8)) {
  50.                    if (scount > 1)
  51.                     putc1('\t',obuf);
  52.                    else
  53.                     putc1(' ',obuf);
  54.                     scount = 0;
  55.                  }
  56.                 break;
  57.            case '\t':    scount = 0;
  58.                 column += (8-column%8);
  59.                 putc1('\t',obuf);
  60.                 break;
  61.            case '"':    outspc('"');
  62.                 do {
  63.                    c = getc(ibuf);
  64.                    if (c == ERROR) {
  65.                     printf("Quote error.\n");
  66.                     exit();
  67.                    }
  68.                    putc1(c,obuf);
  69.                 } while (c != '"');
  70.                 do {
  71.                     c = getc(ibuf);
  72.                     putc1(c,obuf);
  73.                 } while (c != '\n');
  74.                 column = scount = 0;
  75.                 break;
  76.            case 0x1a:    putc(CPMEOF,obuf);
  77.                 break;
  78.            default:    outspc(c);
  79.                 column++;
  80.          }
  81.      } while (c != CPMEOF);
  82.  
  83.     fflush(obuf);
  84.     fclose(ibuf);
  85.     fclose(obuf);
  86. }
  87.  
  88. outspc(c)    /* output any stored spaces */
  89. char c;
  90. {    for (i=0; i<scount; i++)
  91.         putc1(' ',obuf);
  92.     scount = 0;
  93.     putc1(c,obuf);
  94. }
  95.  
  96. putc1(c,buf)
  97. char c;
  98. {
  99.     putchar(c);
  100.     if (putc(c,buf) < 0) {
  101.         printf("putc just retured an error!\n");
  102.         exit();
  103.     }
  104. }
  105.  
  106.