source: libcfa/src/parseconfig.cfa@ 1896c1f

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation pthread-emulation qualifiedEnum stuck-waitfor-destruct
Last change on this file since 1896c1f was 1896c1f, checked in by Jacob Prud'homme <jafprudhomme@…>, 5 years ago

Convert to using CFA file stream I/O

  • Property mode set to 100644
File size: 3.5 KB
Line 
1#include <fstream.hfa>
2#include <limits> // numeric_limits
3using namespace std;
4#include "parseconfig.hfa"
5
6static bool comments( ifstream & in, string & name ) {
7 for ( ;; ) {
8 in | name;
9 if ( fail( in ) ) return true;
10 if ( name.substr(0,1) != "#" ) break;
11 in | nl; // ignore remainder of line
12 } // for
13 return false;
14} // comments
15
16// Process the configuration file to set the simulation parameters.
17void parseConfig( const char * configFile, ConfigParms & cparms ) {
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 {
41 ifstream in;
42
43 open( in, configFile ); // open the configuration file for input
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 ?
50 in | value;
51 if ( value < 0 ) {
52 close( in );
53 exit | "Error: file \"" | configFile | "\" parameter " | name
54 | " value " | value | " must be non-negative."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
55 } // if
56 if ( fail( in ) ) break;
57 in | nl; // ignore remainder of line
58 numOfParm += 1;
59 parms[posn].used = true;
60 parms[posn].value = value;
61 } // for
62
63 if ( numOfParm != Parmnum ) {
64 close( in );
65 exit | "Error: file \"" | configFile | "\" is corrupt."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
66 } // if
67 if ( ! comments( in, name ) ) { // ! eof ?
68 close( in );
69 exit | "Error: file \"" | configFile | "\" has extraneous data."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
70 } // if
71 } catch( Open_Failure * ex; ex->istream == &in ) {
72 close( in );
73 exit | "Error: could not open input file \"" | configFile | "\""; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
74 } // try
75
76 close( in );
77
78 if ( cparms.numStops < 2 ) {
79 exit | "Error: file \"" | configFile | "\" parameter NumStops value "
80 | cparms.numStops | " must be at least 2."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
81 }
82 if ( cparms.numStudents < 1 ) {
83 exit | "Error: file \"" | configFile | "\" parameter NumStudents value "
84 | cparms.numStudents | " must be at least 1."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
85 }
86 if ( cparms.numCouriers < 1 ) {
87 exit | "Error: file \"" | configFile | "\" parameter NumCouriers value "
88 | cparms.numCouriers | " must be at least 1."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
89 }
90} // processConfigFile
91
92// Local Variables: //
93// tab-width: 4 //
94// compile-command: "cfa parseconfig.cfa" //
95// End: //
Note: See TracBrowser for help on using the repository browser.