home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 112.lha / Tabs / entab.c < prev    next >
C/C++ Source or Header  |  1986-11-20  |  2KB  |  124 lines

  1. /* entab.c - a program to change runs of spaces in a file to a TAB character.
  2.  
  3.     Usage:  entab from_file to_file
  4.  
  5. By  Ron Charlton   20-FEB-88  (taken from a program ENTAB.BAS by uSOFT)
  6.     9002 Balcor Circle
  7.     Knoxville, TN 37923
  8.  
  9.    CIS 71450,3216
  10. */
  11.  
  12. #include "stdio.h"
  13.  
  14. #define TABSIZE 8
  15. #define MAXLINE 255
  16. #define TRUE 1
  17. int tabstops[MAXLINE+1];
  18.  
  19. void _wb_parse(){} /* null routine to reduce size of executable */
  20.  
  21. main(argc,argv)
  22. int argc;
  23. char *argv[];
  24. {
  25. register int c, in_column, out_column;
  26. FILE *in, *out, *fopen();
  27.  
  28. if (argc != 3)
  29.   quit();
  30.  
  31. if ( (in = fopen(argv[1], "r")) == NULL )
  32.   {
  33.   printf("Can't open input file.\n");
  34.   quit();
  35.   }
  36.  
  37. if ( (out = fopen(argv[2], "w")) == NULL )
  38.   {
  39.   printf("Can't open output file.\n");
  40.   quit();
  41.   }
  42.  
  43. SetTabPos();            /* set tab columns */
  44.  
  45. out_column = 1;
  46.  
  47. for (;;)            /* exit on end-of-file */
  48.   {
  49.   in_column = out_column;
  50.   /* replace a run of spaces with a TAB when you reach a tab column */
  51.   for (;;)
  52.     {
  53.     if ( ( c = getc(in) ) == EOF ) exit(0);
  54.     if ( c != ' ' && c != '\t' ) break;
  55.     ++in_column;
  56.     if ( c == '\t' || ThisIsATab(in_column))
  57.       {
  58.       if ( (in_column - out_column > 1) || c == '\t' )    /* no tab if 1 space */
  59.         {
  60.         if ( putc('\t', out)  == EOF )    /* output a TAB */
  61.           cantwrite();
  62.         }
  63.       else
  64.         if ( putc(' ', out) == EOF )    /* output a SPACE */
  65.           cantwrite();
  66.       /* go to a tab column if we have a tab and this is not a tab column */
  67.       while ( !ThisIsATab(in_column) )
  68.         ++in_column;
  69.       out_column = in_column;
  70.       }
  71.     }
  72.   /* output spaces left over */
  73.   while ( out_column < in_column )
  74.     {
  75.     if ( putc(' ', out) == EOF )
  76.           cantwrite();
  77.     ++out_column;
  78.     }
  79.   /* output the non-space character */
  80.   if ( putc(c, out) == EOF )
  81.     cantwrite();
  82.   /* reset column position if this is the end of the line */
  83.   if ( c == '\n' )
  84.     out_column = 1;
  85.   else
  86.     ++out_column;
  87.   }
  88. }
  89.  
  90.  
  91. SetTabPos()
  92. {
  93. /* set tab positions in array tabstops */
  94. int i;
  95. for (i=1; i <= 255; i++)
  96.   tabstops[i] = ( ( i % TABSIZE ) == 1 );
  97. }
  98.  
  99.  
  100. ThisIsATab(column)
  101. int column;
  102. {
  103. /* is this a tab column? */
  104. if ( column > MAXLINE )
  105.   return(TRUE);
  106. else
  107.   return(tabstops[column]);
  108. }
  109.  
  110.  
  111. cantwrite()
  112. {
  113. /* some error has occurred on output file */
  114. printf("Can't complete output file.\n");
  115. exit(20);
  116. }
  117.  
  118.  
  119. quit()
  120. {
  121. printf("Usage:  entab in_file out_file\n");
  122. exit(20);
  123. }
  124.