home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 344_01 / itab.c < prev    next >
Text File  |  1989-06-07  |  2KB  |  88 lines

  1. /* HEADER:       (cat #);
  2.    TITLE:        Tab Insertion Text Filter;
  3.    DATE:         06/07/1989;
  4.    DESCRIPTION:  "Reads an existing text file, formatted with only spaces, and
  5.                  produces a new file, formatted with the optimum combination
  6.                  of spaces and tabs of a specified width. The original file can
  7.                  be produced from a file containing tabs (presumably of a
  8.                  different width) using XTAB.COM.";
  9.    KEYWORDS:     filter, detab, text formatters, tabs, file.;
  10.    SYSTEM:       MS-DOS;
  11.    FILENAME:     ITAB.C;
  12.    SEE-ALSO:     xtab.c, xitab.txt;
  13.    AUTHOR:       Eric Horner;
  14.    COMPILERS:    Turbo C 2.0;
  15. */
  16.  
  17. #include <stdio.h>
  18.  
  19.     /***** error messages *****/
  20.  
  21. char *ers[] =
  22. {
  23.     "\7\nUnable to open input file!\n",
  24.     "\7\nUnable to open output file!\n",
  25.     "\7\nUsage is: itab infile outfile tabs\n\n(tabs = new tab width).\n",
  26.     "\ninfile must contain no tabs, and can be made using xtab.com!\n"
  27. };    
  28.  
  29. main(int argc, char *argv[])
  30. {
  31.     int ch, charcnt, spccnt, tabs;
  32.     FILE *infile, *outfile;
  33.  
  34.     if (argc == 4)
  35.     {
  36.     if ((infile = fopen(argv[1], "r")) == 0)
  37.     {
  38.         printf("%s", ers[0]);
  39.         fclose(infile);
  40.         exit(1);
  41.     }
  42.     if ((outfile = fopen(argv[2], "w")) == 0)
  43.     {
  44.         printf("%s", ers[1]);
  45.         fclose(infile);
  46.         fclose(outfile);
  47.         exit(1);
  48.     }
  49.     tabs = atoi(argv[3]);        /* get number of spaces per tab */
  50.     charcnt = 0;            /* char count within line       */
  51.     spccnt = 0;            /* space count within tab    */
  52.  
  53.     while ((ch = fgetc(infile)) != EOF)
  54.     {
  55.         switch (ch)
  56.         {
  57.         case '\x20': ++charcnt;
  58.                  ++spccnt;
  59.                  if (charcnt%tabs == 0)
  60.                  {
  61.                 spccnt = 0;
  62.                 fputc('\t', outfile);
  63.                  }
  64.                  break;
  65.             default: if (spccnt > 0)
  66.                  {
  67.                  for(;spccnt > 0; --spccnt)
  68.                  fputc('\x20', outfile);
  69.                  }
  70.                  if ((ch != '\0') && (ch != '\n'))
  71.                  ++charcnt;
  72.                  if (ch == '\n')
  73.                  charcnt = 0;
  74.                  fputc((char) ch, outfile);
  75.                  break;
  76.         };
  77.     }
  78.     }
  79.     else
  80.     {
  81.     printf("%s", ers[2]);
  82.     printf("%s", ers[3]);
  83.     exit(1);
  84.     }
  85.     fclose(infile);
  86.     fclose(outfile);
  87. }
  88.