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