//////////////////////////////////////////////////////////////// // // emailClassTester.cpp // // A main program for testing the EmailMessage class. // // REVISION HISTORY // 3/2/01 (Jeff Ondich) Written for CS117 class demo and // a midterm exam. // //////////////////////////////////////////////////////////////// #include #include #include "emailClass.h" ////////////////////////////////////////////////////// // Function prototypes. ////////////////////////////////////////////////////// void OpenEmailFile( ifstream& inputFile, string fileName ); ////////////////////////////////////////////////////// // main program ////////////////////////////////////////////////////// int main() { // Get the e-mail file's name from the user. string fileName; cout << "Please type the name of your e-mail file: "; cin >> fileName; ifstream inputFile; OpenEmailFile( inputFile, fileName ); EmailMessage messages[100]; int nMessages = 0; string line; string returnPath, date, subject, message; while( getline( inputFile, line ) ) { if( line.find( "From " ) == 0 ) { messages[nMessages].SetData( returnPath, subject, date, message ); nMessages++; returnPath = ""; date = ""; subject = ""; message = ""; } if( line.find( "Return-Path: " ) == 0 && returnPath == "" ) returnPath = line; if( line.find( "Date: " ) == 0 && date == "" ) date = line; if( line.find( "Subject: " ) == 0 && subject == "" ) subject = line; message += line + '\n'; } inputFile.close(); for( int i=0; i < nMessages; i++ ) { cout << messages[i].GetSender() << endl; cout << messages[i].GetSubject() << endl << endl; } return( 0 ); } ////////////////////////////////////////////////////// // Opens an input stream corresponding to // the file of the given name. If the file can't // be opened, we give the user a descriptive // error message and quit the program. ////////////////////////////////////////////////////// void OpenEmailFile( ifstream& inputFile, string fileName ) { inputFile.open( fileName.c_str() ); if( !(inputFile.is_open()) ) { cout << "An error occurred during the attempt to open " << fileName << "." << endl << "Perhaps the file does not exist." << endl; exit( 1 ); } }