home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / REMTAB.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  66 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /* remtab.c 12-4-91 Robert Mashlan, Public Domain
  4.    modified 28 mar 93 by Bob Stout
  5.  
  6.    Filter for removing tabs.  All tabs in the input will be replaced
  7.    with spaces.  This filter takes one optional command line
  8.    parameter, which specifies the number spaces to replace for a tab.
  9.    If no size is specifies, it defaults to 8.
  10.  
  11.    example usage:
  12.  
  13.    remtab 6 < tabbed.c > untabbed.c
  14.  
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19.  
  20. #define BUFSIZE 4096
  21.  
  22.  
  23. int main(int argc, char **argv )
  24. {
  25.       int tabsize = 8;
  26.  
  27.       if (argc > 1)                 /* look for command line parameter */
  28.       {
  29.             if (0 == (tabsize = atoi(argv[1])))
  30.                   tabsize = 8;
  31.       }
  32.  
  33.       while (1)
  34.       {
  35.             char buf[BUFSIZE];
  36.             int nr, i, j, pos = 0;
  37.  
  38.             nr = fread(buf, 1, sizeof(buf), stdin);
  39.             for (i = 0; i < nr; i++)
  40.             {
  41.                   switch (buf[i])
  42.                   {
  43.                   case '\t':              /* replace tabs with spaces   */
  44.                         for(j = pos % tabsize; j < tabsize; ++j)
  45.                         {
  46.                               putchar(' ');
  47.                               ++pos;
  48.                         }
  49.                         break;
  50.  
  51.                   case '\n':              /* start a new line           */
  52.                         pos = -1;         /* this will become 0 when... */
  53.  
  54.                         /* ...we fall through to...   */
  55.  
  56.                   default:
  57.                         putchar(buf[i]);/* send character through unchanged */
  58.                         ++pos;
  59.                   }
  60.             }
  61.             if (nr < sizeof(buf))
  62.                   break;
  63.       }
  64.       return 0;
  65. }
  66.