home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 186_01 / tabify5.c < prev    next >
Text File  |  1985-08-21  |  3KB  |  132 lines

  1.  
  2. /*
  3.     Tabify.c    written by Leor Zolman
  4.  
  5.     This filter takes sequences of spaces in a file and turns
  6.     them, whenever possible, into tabs. Usage:
  7.  
  8.         A>tabify oldfile newfile <cr>
  9.  
  10.     Quoted strings are not processed, but there should NOT be
  11.     any `lone' double quotes within the file being tabified.
  12.  
  13. Ver 5    (named to distinguish between TABIFY and TABIFY4 and to
  14.     encourage distribution of the file.)
  15.     Changes:
  16.         1. Asterisks no longer displayed
  17.         2. #DEFINE used for TEXT/ASM versions
  18.     By S. Kluger  11/13/83
  19.  
  20.     Comment out one of the following #defines:
  21. */
  22.  
  23. #define QUOT '''    /*  for ASM file processing */
  24. /*        
  25. #define QUOT '"'    /*  for TEXT file processing */
  26. */
  27.  
  28. #include "bdscio.h"
  29.  
  30. int scount, column, ifd, ofd, i;
  31. int c;
  32. char ibuf[BUFSIZ], obuf[BUFSIZ];
  33.  
  34. main(argc,argv)
  35. char **argv;
  36. {
  37.     if (argc < 2 || argc > 3) {
  38.         printf("usage: tabify oldfile [newfile]\n");
  39.         exit();
  40.     }
  41.  
  42.     ifd = fopen(argv[1],ibuf);
  43.  
  44.     if (argc == 2) argv[2] = "tabify.tmp";
  45.     ofd = fcreat(argv[2],obuf);
  46.  
  47.     if (ifd == ERROR || ofd == ERROR) {
  48.         printf("Can't open file(s)\n");
  49.         exit();
  50.     }
  51.  
  52.     scount = column = 0;
  53.  
  54.     do {
  55.         c = getc(ibuf);
  56.         if (c == ERROR) {
  57.             putc(CPMEOF,obuf);
  58.             break;
  59.          }
  60.         switch(c) {
  61.            case '\r':    putc1(c,obuf);
  62.                 scount = column = 0;
  63.                 break;
  64.            case '\n':    putc1(c,obuf);
  65.                 scount = 0;
  66.                 break;
  67.            case ' ':    column++;
  68.                 scount++;
  69.                 if (!(column%8)) {
  70.                    if (scount > 1)
  71.                     putc1('\t',obuf);
  72.                    else
  73.                     putc1(' ',obuf);
  74.                     scount = 0;
  75.                  }
  76.                 break;
  77.            case '\t':    scount = 0;
  78.                 column += (8-column%8);
  79.                 putc1('\t',obuf);
  80.                 break;
  81.            case QUOT:    for (i = 0; i < scount; i++)
  82.                     putc1(' ',obuf);
  83.                 putc1(QUOT,obuf);
  84.                 do {
  85.                    c = getc(ibuf);
  86.                    if (c == ERROR) {
  87.                     printf("Quote error.\n");
  88.                     exit();
  89.                    }
  90.                    putc1(c,obuf);
  91.                 } while (c != QUOT);
  92.                 do {
  93.                     c = getc(ibuf);
  94.                     putc1(c,obuf);
  95.                 } while (c != '\n');
  96.                 column = scount = 0;
  97.                 break;
  98.            case CPMEOF:    putc(CPMEOF,obuf);
  99.                 break;
  100.            default:    for (i=0; i<scount; i++)
  101.                     putc1(' ',obuf);
  102.                 scount = 0;
  103.                 column++;
  104.                 putc1 (c,obuf);
  105.          }
  106.      } while (c != CPMEOF);
  107.  
  108.     fclose(ibuf);
  109.     fclose(obuf);
  110.  
  111.     if (argc == 2) {
  112.         unlink(argv[1]);
  113.         rename(argv[2],argv[1]);
  114.     }
  115. }
  116.  
  117. putc1(c,buf)
  118. char c;
  119. {
  120.     if (putc(c,buf) < 0) {
  121.         printf("Write error (out of disk space?)\n");
  122.         exit();
  123.     }
  124. }
  125. }
  126. }
  127.  
  128. putc1(c,buf)
  129. char c;
  130. {
  131.     if (putc(c,buf) < 0) {
  132.         printf("Write error (out of disk space?)\n"