source: libcfa/src/parseconfig.cfa@ 50dcfad

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

Removed useless finally block

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