campo-sirio/al/examples/ex10con.cpp
alex 714dd74636 Archive Library versione 2.00
git-svn-id: svn://10.65.10.50/trunk@5350 c028cbd2-c16b-5b4b-a496-9718f37d4682
1997-10-09 16:09:54 +00:00

116 lines
3.2 KiB
C++
Executable File

//
// EX10CON.CPP
//
// C++/DOS Example program for ArchiveLib 2.0
//
// Copyright (c) Greenleaf Software, Inc. 1994 - 1996
// All Rights Reserved
//
// MEMBERS/FUNCTIONS DEMONSTRATED
//
//
// DESCRIPTION
//
// This example program is used to show off some of the pattern
// matching functions. It is sort of a baby grep, able to look
// through any list of files for a particular string. The format
// you pass on the command line is:
//
// EX10DOS pattern wild-card
//
// You can omit wild-card, or wild-card and pattern, in which case
// the defaults of "new" and "*.CPP" will be used.
//
// REVISION HISTORY
//
// February 1, 1996 2.0A : Second release
//
//
#include <fstream.h>
#include "al.h"
#include "_match.h"
#include <conio.h>
void scan( char *name, char *pattern );
int main( int argc, char *argv[] )
{
cout << "Archive Library 2.0\nEX10CON.CPP\n\n";
cout << "This example program is used to show off some of the pattern\n";
cout << "matching functions. It is sort of a baby grep, able to look\n";
cout << "through any list of files for a particular string. The format\n";
cout << "you pass on the command line is:\n";
cout << "\n";
cout << " EX10CON pattern wild-card\n";
cout << "\n";
cout << "You can omit wild-card, or wild-card and pattern, in which case\n";
cout << "the defaults of 'new' and '*.CPP' will be used.\n\n";
getch();
//
// The first part of the program just sets up the arguments, then
// calls a couple of the match diagnostic functions.
//
char *names = "*.CPP";
char *pattern = "new";
if ( argc > 1 )
pattern = argv[ 1 ];
if ( argc > 2 )
names = argv[ 2 ];
cout << "Pattern : " << pattern << "\n";
int error_type;
cout << "contains wild characters: "
<< is_pattern( pattern )
<< "\n";
cout << "Is valid: "
<< is_valid_pattern( pattern, &error_type )
<< "\n";
if ( matche( "[A-", "test" ) != MATCH_PATTERN )
cout << "Logic error in match engine\n";
cout << "Hit any key to continue..." << flush;
getch();
cout << endl;
//
// The second part of the program creates an extended pattern, which
// should look like this: "*pattern*". We are going to search for
// that search pattern in each line of each file. The file names are
// expanded one by one, and the scanner subroutine is called.
//
char *extended_pattern = new char[ strlen( pattern ) + 2 ];
strcpy( extended_pattern, "*" );
strcat( extended_pattern, pattern );
strcat( extended_pattern, "*" );
ALWildCardExpander *machine = new ALWildCardExpander( names );
char *name;
while ( ( name = machine->GetNextFile() ) != 0 )
scan( name, extended_pattern );
delete machine;
return 1;
}
//
// This routine reads in a line at a time from the specified file. It
// tests each line for a match to the pattern. Any matches are printed out,
// and that is that.
//
void scan( char *name, char *pattern )
{
char buffer[ 133 ];
int line_number = 0;
ifstream input;
input.open( name );
do {
input.getline( buffer, 133 );
line_number++;
if ( match( pattern, buffer ) ) {
cout << name << " " << line_number << ":" << buffer << "\n";
}
} while ( !input.eof() );
}