home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / ht2sp_15.zip / ht2sp / ht2sp.cpp next >
C/C++ Source or Header  |  1998-10-11  |  5KB  |  155 lines

  1. // $Id: ht2sp.cpp,v 1.5 1998-10-11 00:09:10-04 rl Exp $
  2.  
  3. ///////////////////////////////////////////////////////////////////////////
  4. //                                                                        /
  5. // ht2sp.cpp                                                              /
  6. // Substitute spaces for horizontal tabs in a text file                   /
  7. // 1994-10-04, Rolf Lochbuehler                                           /
  8. //                                                                        /
  9. ///////////////////////////////////////////////////////////////////////////
  10.  
  11.  
  12. #include <stdlib.h>
  13. #include <iostream.h>
  14. #include <fstream.h>
  15.  
  16. // Characters:
  17. #define ASCII_HT ('\t')
  18. #define ASCII_SP (' ')
  19. #define ASCII_0  ('0')
  20. #define ASCII_LINEBREAK ('\n')
  21.  
  22.  
  23. void help( void );
  24.  
  25.  
  26. void main ( int argc, char *argv[] )
  27.   {
  28.   
  29.   int i;
  30.   int colsPerTab;
  31.   int col;
  32.   int skip;
  33.   char c;
  34.  
  35.   if( (argc < 3) || (argc > 4) )
  36.     {
  37.     help();
  38.     return;
  39.     }
  40.   
  41.   if( (0 == stricmp(argv[1],"/h")) || (0 == stricmp(argv[1],"-h")) )
  42.     {
  43.     help();
  44.     return;
  45.     }
  46.  
  47.   // Create input file stream
  48.   ifstream ifs( argv[1], ios::in|ios::nocreate );
  49.   if( !ifs )
  50.     {
  51.     cout << "*Ht2Sp Error* Cannot read " << argv[1] << ". File may not exist." << endl;
  52.     return;
  53.     }
  54.   
  55.   // Create output file stream
  56.   ofstream ofs( argv[2], ios::out|ios::noreplace );
  57.   if( !ofs )
  58.     {
  59.     cout << "*Ht2Sp Error* Cannot write " << argv[2] << ". File may exist already." << endl;
  60.     return;
  61.     }
  62.   
  63.   // Default number of columns per tab
  64.   if( 3 == argc )
  65.     colsPerTab = 8;
  66.   else
  67.     colsPerTab = atoi( argv[3] );
  68.   if( colsPerTab < 1 )
  69.     {
  70.     cout << "*Ht2Sp Error* " << colsPerTab << " is not a valid columns-per-tab value. This value must be 1 or greater!" << endl;
  71.     return;
  72.     }
  73.   
  74.   // Read input stream and substitute spaces for tabs
  75.   col = 1;
  76.   skip = colsPerTab;
  77.   while( ifs.get(c) )
  78.     switch( c )
  79.       {
  80.  
  81.       case ASCII_LINEBREAK :
  82.         {
  83.         col = 1;
  84.         skip = colsPerTab;
  85.         ofs.put( ASCII_LINEBREAK );
  86.         break;
  87.         }
  88.  
  89.       case ASCII_HT :
  90.         {
  91.         for( i = 1; i <= skip; ++i ) 
  92.           ofs.put( ASCII_SP );
  93.         col += skip;
  94.         skip = colsPerTab - ((col % colsPerTab) - 1);
  95.         if( 0 == (col % colsPerTab) ) 
  96.           skip = 1;
  97.         break;
  98.         }
  99.  
  100.       default :
  101.         {
  102.         ofs.put( c );
  103.         col += 1;
  104.         skip = colsPerTab - ((col % colsPerTab) - 1);
  105.         if( 0 == (col % colsPerTab) )
  106.           skip = 1;
  107.         // break;
  108.         }
  109.  
  110.       }   // end switch, end while
  111.  
  112.   if( !ifs.eof() )
  113.     {
  114.     cout << "*Ht2Sp Error* Error while reading " << argv[1] << endl;
  115.     return;
  116.     }
  117.   
  118.   return;
  119.   
  120.   }   // end main()
  121.  
  122.                   
  123.  
  124. ///////////////////////////////////////////////////////////////////////////
  125. //                                                                        /
  126. // help()                                                                 /
  127. // Print help for user                                                    /
  128. //                                                                        /
  129. ///////////////////////////////////////////////////////////////////////////
  130. void help ( void )
  131.   {
  132.   cout << endl;
  133.   cout << "Ht2Sp $Revision: 1.5 $, Rolf Lochbuehler <rolf@together.net>" << endl;
  134.   cout << "Purpose:" << endl;
  135.   cout << "  Substitute space characters for horizontal tab characters in a text file," << endl;
  136.   cout << "  preserving column alignment" << endl;
  137.   cout << "Usage:" << endl;
  138.   cout << "  ht2sp [/h] [IN OUT [NUM]]" << endl;
  139.   cout << "Arguments:" << endl;
  140.   cout << "  (none)       Print this help info, then exit" << endl;
  141.   cout << "  /h (or -h)   Print this help info, then exit" << endl;
  142.   cout << "  IN           Name of input text file" << endl;
  143.   cout << "  OUT          Name of output file" << endl;
  144.   cout << "  NUM          Number of columns per tab (default: 8)" << endl;
  145.   cout << "Examples:" << endl;
  146.   cout << "  ht2sp a.txt b.txt" << endl;
  147.   cout << "  ht2sp x.txt y.txt 6" << endl;
  148.   cout << "Note:" << endl;
  149.   cout << "  The number of columns per tab specified as NUM (or the default of 8 columns)" << endl;
  150.   cout << "  should be equal to the number of columns per tab used when the file was" << endl;
  151.   cout << "  formatted originally. Otherwise ht2sp may disturb the column alignment." << endl;
  152.   return;
  153.   }
  154.  
  155.