home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / data / igrep / igrep.cpp next >
Encoding:
C/C++ Source or Header  |  1996-10-29  |  2.0 KB  |  69 lines

  1. //************************************************************
  2. // Data Types - Using IString::isLike    
  3. //
  4. // Copyright (C) 1994, Law, Leong, Love, Olson, Tsuji.
  5. // Copyright (c) 1997 John Wiley & Sons, Inc. 
  6. // All Rights Reserved.
  7. //
  8. // DESCRIPTION:                                                                  
  9. //   This program searches the argument file for lines that contain an argument  
  10. //   IString::isLike "pattern".                                                  
  11. //                                                                               
  12. //   Usage:   igrep <file> <pattern>                                             
  13. //                                                                               
  14. //
  15. //************************************************************
  16. #include <istring.hpp>
  17. #include <fstream.h>
  18.  
  19. int main ( int argc, char *argv[] )
  20.   {
  21.   int
  22.     result = 0;
  23.  
  24.   // Validate the input arguments.
  25.   if ( argc != 3 )
  26.     {
  27.     cerr << "Usage:  igrep  <file>  <pattern>\a" << endl;
  28.     return 0;
  29.     }
  30.  
  31.   IString
  32.     file( argv[1] ),
  33.     pattern( argv[2] );
  34.  
  35.   // Pad the pattern at both ends with wildcards (so the pattern
  36.   // is found anywhere in an input line).                                                            |
  37.   pattern = IString( 0, 1, 
  38.                      (char*)pattern, pattern.length(), 
  39.                      0, 1, 
  40.                      '*' );
  41.  
  42.   // Open the input file.                                            
  43.   ifstream
  44.     input( file );
  45.  
  46.   if ( !input )
  47.     {
  48.     cerr << "Error opening file.\a" << endl;
  49.     return 0;
  50.     }
  51.  
  52.   // Read lines, looking for a pattern.                                           
  53.   unsigned
  54.     lineNo = 0;
  55.   while ( input )
  56.     {
  57.     IString
  58.       line = IString::lineFrom( input );
  59.     lineNo++;
  60.     if ( line.isLike( pattern ) )
  61.       {
  62.       result++;
  63.       cout << IString( lineNo ).rightJustify( 4 ) << " "
  64.            << line << endl;
  65.       }
  66.     }
  67.   return result;
  68.   }
  69.