[1896c1f] | 1 | #include <fstream.hfa> |
---|
[16f9aca] | 2 | using namespace std; |
---|
| 3 | #include "parseconfig.hfa" |
---|
| 4 | |
---|
| 5 | static bool comments( ifstream & in, string & name ) { |
---|
| 6 | for ( ;; ) { |
---|
[1896c1f] | 7 | in | name; |
---|
| 8 | if ( fail( in ) ) return true; |
---|
[16f9aca] | 9 | if ( name.substr(0,1) != "#" ) break; |
---|
[1896c1f] | 10 | in | nl; // ignore remainder of line |
---|
[16f9aca] | 11 | } // for |
---|
| 12 | return false; |
---|
| 13 | } // comments |
---|
| 14 | |
---|
| 15 | // Process the configuration file to set the simulation parameters. |
---|
[1896c1f] | 16 | void parseConfig( const char * configFile, ConfigParms & cparms ) { |
---|
[16f9aca] | 17 | enum { Parmnum = 11 }; |
---|
| 18 | struct { |
---|
| 19 | const char * name; // configuration name |
---|
| 20 | bool used; // already supplied ? |
---|
| 21 | unsigned int & value; // location to put configuration value |
---|
| 22 | } static parms[Parmnum] = { |
---|
| 23 | { "StopCost", false, cparms.stopCost }, |
---|
| 24 | { "NumStudents", false, cparms.numStudents }, |
---|
| 25 | { "NumStops", false, cparms.numStops }, |
---|
| 26 | { "MaxNumStudents", false, cparms.maxNumStudents }, |
---|
| 27 | { "TimerDelay", false, cparms.timerDelay }, |
---|
| 28 | { "MaxStudentDelay", false, cparms.maxStudentDelay }, |
---|
| 29 | { "MaxStudentTrips", false, cparms.maxStudentTrips }, |
---|
| 30 | { "GroupoffDelay", false, cparms.groupoffDelay }, |
---|
| 31 | { "ConductorDelay", false, cparms.conductorDelay }, |
---|
| 32 | { "ParentalDelay", false, cparms.parentalDelay }, |
---|
| 33 | { "NumCouriers", false, cparms.numCouriers }, |
---|
| 34 | }; |
---|
| 35 | string name; |
---|
| 36 | int value; |
---|
| 37 | unsigned int cnt, posn, numOfParm = 0; |
---|
| 38 | |
---|
| 39 | try { |
---|
[1896c1f] | 40 | ifstream in; |
---|
| 41 | |
---|
| 42 | open( in, configFile ); // open the configuration file for input |
---|
[16f9aca] | 43 | |
---|
[dac3455] | 44 | for ( cnt = 0 ; cnt < Parmnum; ++cnt ) { // parameter names can appear in any order |
---|
[16f9aca] | 45 | if ( comments( in, name ) ) break; // eof ? |
---|
[dac3455] | 46 | for ( posn = 0; posn < Parmnum && name != parms[posn].name; ++posn ); // linear search |
---|
[16f9aca] | 47 | if ( posn == Parmnum ) break; // configuration not found ? |
---|
| 48 | if ( parms[posn].used ) break; // duplicate configuration ? |
---|
[1896c1f] | 49 | in | value; |
---|
[16f9aca] | 50 | if ( value < 0 ) { |
---|
[1896c1f] | 51 | exit | "Error: file \"" | configFile | "\" parameter " | name |
---|
| 52 | | " value " | value | " must be non-negative."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** // |
---|
[16f9aca] | 53 | } // if |
---|
[1896c1f] | 54 | if ( fail( in ) ) break; |
---|
| 55 | in | nl; // ignore remainder of line |
---|
[dac3455] | 56 | ++numOfParm; |
---|
[16f9aca] | 57 | parms[posn].used = true; |
---|
| 58 | parms[posn].value = value; |
---|
| 59 | } // for |
---|
| 60 | |
---|
| 61 | if ( numOfParm != Parmnum ) { |
---|
[1896c1f] | 62 | exit | "Error: file \"" | configFile | "\" is corrupt."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** // |
---|
[16f9aca] | 63 | } // if |
---|
| 64 | if ( ! comments( in, name ) ) { // ! eof ? |
---|
[1896c1f] | 65 | exit | "Error: file \"" | configFile | "\" has extraneous data."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** // |
---|
[16f9aca] | 66 | } // if |
---|
[1896c1f] | 67 | } catch( Open_Failure * ex; ex->istream == &in ) { |
---|
| 68 | exit | "Error: could not open input file \"" | configFile | "\""; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** // |
---|
[c3c76cd] | 69 | } finally { |
---|
| 70 | close( in ); |
---|
[16f9aca] | 71 | } // try |
---|
| 72 | |
---|
[1896c1f] | 73 | if ( cparms.numStops < 2 ) { |
---|
| 74 | exit | "Error: file \"" | configFile | "\" parameter NumStops value " |
---|
| 75 | | cparms.numStops | " must be at least 2."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** // |
---|
| 76 | } |
---|
| 77 | if ( cparms.numStudents < 1 ) { |
---|
| 78 | exit | "Error: file \"" | configFile | "\" parameter NumStudents value " |
---|
| 79 | | cparms.numStudents | " must be at least 1."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** // |
---|
| 80 | } |
---|
| 81 | if ( cparms.numCouriers < 1 ) { |
---|
| 82 | exit | "Error: file \"" | configFile | "\" parameter NumCouriers value " |
---|
| 83 | | cparms.numCouriers | " must be at least 1."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** // |
---|
[16f9aca] | 84 | } |
---|
| 85 | } // processConfigFile |
---|
| 86 | |
---|
| 87 | // Local Variables: // |
---|
| 88 | // tab-width: 4 // |
---|
| 89 | // compile-command: "cfa parseconfig.cfa" // |
---|
| 90 | // End: // |
---|