home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / clarion / untab.zip / UNTAB.C < prev    next >
Text File  |  1989-04-24  |  1KB  |  59 lines

  1. /*--------------------- untab.c -----------------------------------
  2.  *
  3.  *  Chuck Eckenroed   (208) 385-0702   April 1989
  4.  *
  5.  *  This program reads a file and replaces all occurances of
  6.  *  the tab charter (0x09) with the proper number of spaces (1 to 8)
  7.  *  to remove 8 character tab spacing.
  8.  *
  9.  */
  10.  
  11. #include <stdio.h>
  12. #define TEMPFILE "$untab$.$$$"
  13. #define TABSIZE 8
  14.  
  15. main(argc,argv)
  16. int argc;
  17. char *argv[];
  18.  
  19. {
  20.  
  21.     FILE *in_file, *tmp_file;
  22.     char ch;
  23.     int  i,col = 0,tab_cnt;
  24.  
  25.     if (argc != 2)
  26.         {printf("Format: C>untab filename.ext"); exit();}
  27.     if ((in_file=fopen(argv[1],"rb")) == NULL)
  28.         {perror("Open error"); exit();}
  29.     if ((tmp_file=fopen(TEMPFILE,"wb")) == NULL)
  30.         {perror("Temp file open error"); exit();}
  31.  
  32.     while((ch=getc(in_file)) != EOF) {
  33.         switch(ch)    {
  34.             case '\t':
  35.                 ch = ' ';
  36.                 tab_cnt = (next_tab(col) - col);
  37.                 for (i= 0; i< tab_cnt - 1; i++)
  38.                     putc(ch, tmp_file);
  39.                 col += i;
  40.                 break;
  41.             case '\r' :
  42.             case '\n' :
  43.                 col = 0;
  44.                 col--;
  45.                 break;
  46.         }                                         /* end switch */
  47.         putc(ch, tmp_file);
  48.         col++;
  49.     }                                             /* end while */
  50.     fclose(in_file);
  51.     fclose(tmp_file);
  52.     remove(argv[1]);
  53.     rename(TEMPFILE,argv[1]);
  54. }
  55.  
  56. next_tab(int x)
  57. {
  58.     return (x / TABSIZE + 1) * TABSIZE;
  59. }