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

  1. /* HEADER:       (cat #);
  2.    TITLE:        Tab Extraction Text Filter;
  3.    DATE:         06/07/1989;
  4.    DESCRIPTION:  "Reads an existing text file, and replaces <tab> characters
  5.                  with spaces, so as to retain the original formatting. Note
  6.                  that this is NOT the same as replacing <tab> characters
  7.                  with a fixed number of spaces, which destroys the original
  8.                  formatting. The original tab setting value is passed as a
  9.                  command line parameter.";
  10.    KEYWORDS:     filter, detab, text formatters, file.;
  11.    SYSTEM:       MS-DOS;
  12.    FILENAME:     XTAB.C;
  13.    SEE-ALSO:     itab.c, xitab.txt;
  14.    AUTHOR:       Eric Horner;
  15.    COMPILERS:    Turbo C 2.0;
  16. */
  17.  
  18. #include <stdio.h>
  19.  
  20.     /***** error messages *****/
  21.  
  22. char *ers[] =
  23. {
  24.     "\7\nUnable to open input file!\n",
  25.     "\7\nUnable to open output file!\n",
  26.     "\7\nUsage is: xtab infile outfile tabs\n\n(tabs = spaces per tab).\n"
  27. };    
  28.  
  29.  
  30. main(int argc, char *argv[])
  31. {
  32.     int ch, charcnt, tabs, tabcnt;
  33.     FILE *infile, *outfile;
  34.  
  35.     if (argc == 4)
  36.     {
  37.     if ((infile = fopen(argv[1], "r")) == 0)
  38.     {
  39.         printf("%s", ers[0]);
  40.         fclose(infile);
  41.         exit(1);
  42.     }
  43.     if ((outfile = fopen(argv[2], "w")) == 0)
  44.     {
  45.         printf("%s", ers[1]);
  46.         fclose(infile);
  47.         fclose(outfile);
  48.         exit(1);
  49.     }
  50.     tabs = atoi(argv[3]);        /* get number of spaces per tab */
  51.     charcnt = 0;            /* char count within line       */
  52.  
  53.     while ((ch = fgetc(infile)) != EOF)
  54.     {
  55.         switch (ch)
  56.         {
  57.         case '\t': tabcnt = (tabs - charcnt%tabs);
  58.                charcnt += tabcnt;
  59.                for(;tabcnt > 0; tabcnt--)
  60.                fputc('\x20', outfile);    /* spaces */
  61.                break;
  62.         default:   ++charcnt;
  63.                if (ch == '\n')
  64.                    charcnt = 0;
  65.                fputc((char) ch, outfile);
  66.                break;
  67.         };
  68.     }
  69.     }
  70.     else
  71.     {
  72.     printf("%s", ers[2]);
  73.     exit(1);
  74.     }
  75.  
  76.     fclose(infile);
  77.     fclose(outfile);
  78. }
  79.