home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / TABS.ZIP / DETAB.C < prev    next >
C/C++ Source or Header  |  1990-09-20  |  3KB  |  99 lines

  1. /* detab [1.0.2] - 07-Dec-87 16:35                               John Layman */
  2.  
  3. /*****************************************************************************\
  4. * This filter removes tabs from a file by expanding them to blanks.           *
  5. *                                                                             *
  6. * Synopsis:                     detab [t1..tn]                                *
  7. *                                                                             *
  8. * where the optional parameter(s) indicate tab positions.                     *
  9. *                                                                             *
  10. * When no parameter is present, the filter presumes the default system tab    *
  11. * settings (i.e. where column MOD 8 = 1).                                     *
  12. *                                                                             *
  13. * When a single parameter is present, it is considered to be the tab gap      *
  14. * (i.e. tabs are where column MOD gap = 1).                                   *
  15. *                                                                             *
  16. * When multiple parameters are present, they explicitly define each tab       *
  17. * position.                                                                   *
  18. \*****************************************************************************/
  19.  
  20. #include <stdio.h>
  21.  
  22. #define DEFAULT 8
  23. #define MAXLEN  255
  24.  
  25. void chktab(), settab(), settabs();
  26.  
  27. char pos[MAXLEN];       /* tab bar, where tab stops == '\t'     */
  28. int hi;             /* column of highest (rightmost) tab stop */
  29.  
  30.  
  31. main(argc,argv)
  32.   int argc;
  33.   char *argv[];
  34. {
  35.   int ch, col, i;
  36.  
  37.   if (argc == 1)
  38.     settabs(DEFAULT);
  39.   else
  40.   if (argc == 2)
  41.     settabs(atoi(argv[1]));
  42.   else
  43.   for (i = 1; i < argc; i++)
  44.     settab(atoi(argv[i]));
  45.  
  46.   col = 1;
  47.   while ((ch = getchar()) != EOF)
  48.     if (ch == '\t')
  49.       do
  50.       {
  51.         putchar(' ');
  52.         col++;
  53.       }
  54.       while (col < hi && pos[col] != '\t');
  55.     else 
  56.     {
  57.       putchar(ch);
  58.       if (ch == '\n')
  59.         col = 1;
  60.       else
  61.         col++;
  62.     }
  63. }
  64.  
  65.  
  66. void settab(col)
  67.   int col;
  68. {
  69.   chktab(col);
  70.   pos[col] = '\t';
  71.   if (col > hi)
  72.     hi = col;
  73. }
  74.  
  75.  
  76. void settabs(gap)
  77.   int gap;
  78. {
  79.   int col;
  80.  
  81.   chktab(gap);
  82.   for (col = gap+1; col <= MAXLEN; col += gap)
  83.   {
  84.     pos[col] = '\t';
  85.     hi = col;
  86.   }
  87. }
  88.  
  89.  
  90. void chktab(col)
  91.   int col;
  92. {
  93.   if (col < 1 || col > MAXLEN)
  94.   {
  95.     fputs("Invalid parameter\n",stderr);
  96.     exit(1);
  97.   }
  98. }
  99.