1 | #include <fstream.hfa> |
---|
2 | using namespace std; |
---|
3 | #include "parseconfig.hfa" |
---|
4 | |
---|
5 | static bool comments( ifstream & in, string & name ) { |
---|
6 | for ( ;; ) { |
---|
7 | in | name; |
---|
8 | if ( fail( in ) ) return true; |
---|
9 | if ( name.substr(0,1) != "#" ) break; |
---|
10 | in | nl; // ignore remainder of line |
---|
11 | } // for |
---|
12 | return false; |
---|
13 | } // comments |
---|
14 | |
---|
15 | // Process the configuration file to set the simulation parameters. |
---|
16 | void parseConfig( const char * configFile, ConfigParms & cparms ) { |
---|
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 { |
---|
40 | ifstream in; |
---|
41 | |
---|
42 | open( in, configFile ); // open the configuration file for input |
---|
43 | |
---|
44 | for ( cnt = 0 ; cnt < Parmnum; ++cnt ) { // parameter names can appear in any order |
---|
45 | if ( comments( in, name ) ) break; // eof ? |
---|
46 | for ( posn = 0; posn < Parmnum && name != parms[posn].name; ++posn ); // linear search |
---|
47 | if ( posn == Parmnum ) break; // configuration not found ? |
---|
48 | if ( parms[posn].used ) break; // duplicate configuration ? |
---|
49 | in | value; |
---|
50 | if ( value < 0 ) { |
---|
51 | exit | "Error: file \"" | configFile | "\" parameter " | name |
---|
52 | | " value " | value | " must be non-negative."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** // |
---|
53 | } // if |
---|
54 | if ( fail( in ) ) break; |
---|
55 | in | nl; // ignore remainder of line |
---|
56 | ++numOfParm; |
---|
57 | parms[posn].used = true; |
---|
58 | parms[posn].value = value; |
---|
59 | } // for |
---|
60 | |
---|
61 | if ( numOfParm != Parmnum ) { |
---|
62 | exit | "Error: file \"" | configFile | "\" is corrupt."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** // |
---|
63 | } // if |
---|
64 | if ( ! comments( in, name ) ) { // ! eof ? |
---|
65 | exit | "Error: file \"" | configFile | "\" has extraneous data."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** // |
---|
66 | } // if |
---|
67 | } catch( Open_Failure * ex; ex->istream == &in ) { |
---|
68 | exit | "Error: could not open input file \"" | configFile | "\""; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** // |
---|
69 | } finally { |
---|
70 | close( in ); |
---|
71 | } // try |
---|
72 | |
---|
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!!! *** // |
---|
84 | } |
---|
85 | } // processConfigFile |
---|
86 | |
---|
87 | // Local Variables: // |
---|
88 | // tab-width: 4 // |
---|
89 | // compile-command: "cfa parseconfig.cfa" // |
---|
90 | // End: // |
---|