#include using namespace std; #include "parseconfig.hfa" static bool comments( ifstream & in, string & name ) { while () { in | name; if ( fail( in ) ) return true; if ( name.substr(0,1) != "#" ) break; in | nl; // ignore remainder of line } // for return false; } // comments // Process the configuration file to set the simulation parameters. void parseConfig( const char * configFile, ConfigParms & cparms ) { enum { Parmnum = 11 }; struct { const char * name; // configuration name bool used; // already supplied ? unsigned int & value; // location to put configuration value } static parms[Parmnum] = { { "StopCost", false, cparms.stopCost }, { "NumStudents", false, cparms.numStudents }, { "NumStops", false, cparms.numStops }, { "MaxNumStudents", false, cparms.maxNumStudents }, { "TimerDelay", false, cparms.timerDelay }, { "MaxStudentDelay", false, cparms.maxStudentDelay }, { "MaxStudentTrips", false, cparms.maxStudentTrips }, { "GroupoffDelay", false, cparms.groupoffDelay }, { "ConductorDelay", false, cparms.conductorDelay }, { "ParentalDelay", false, cparms.parentalDelay }, { "NumCouriers", false, cparms.numCouriers }, }; string name; int value; unsigned int cnt, posn, numOfParm = 0; try { ifstream in; open( in, configFile ); // open the configuration file for input for ( cnt; Parmnum ) { // parameter names can appear in any order if ( comments( in, name ) ) break; // eof ? for ( posn; posn < Parmnum && name != parms[posn].name; ++posn ); // linear search if ( posn == Parmnum ) break; // configuration not found ? if ( parms[posn].used ) break; // duplicate configuration ? in | value; if ( value < 0 ) { exit | "Error: file \"" | configFile | "\" parameter " | name | " value " | value | " must be non-negative."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** // } // if if ( fail( in ) ) break; in | nl; // ignore remainder of line ++numOfParm; parms[posn].used = true; parms[posn].value = value; } // for if ( numOfParm != Parmnum ) { exit | "Error: file \"" | configFile | "\" is corrupt."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** // } // if if ( ! comments( in, name ) ) { // ! eof ? exit | "Error: file \"" | configFile | "\" has extraneous data."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** // } // if } catch( Open_Failure * ex; ex->istream == &in ) { exit | "Error: could not open input file \"" | configFile | "\""; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** // } finally { close( in ); } // try if ( cparms.numStops < 2 ) { exit | "Error: file \"" | configFile | "\" parameter NumStops value " | cparms.numStops | " must be at least 2."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** // } if ( cparms.numStudents < 1 ) { exit | "Error: file \"" | configFile | "\" parameter NumStudents value " | cparms.numStudents | " must be at least 1."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** // } if ( cparms.numCouriers < 1 ) { exit | "Error: file \"" | configFile | "\" parameter NumCouriers value " | cparms.numCouriers | " must be at least 1."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** // } } // processConfigFile // Local Variables: // // tab-width: 4 // // compile-command: "cfa parseconfig.cfa" // // End: //