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
RevLine 
[1896c1f]1#include <fstream.hfa>
[16f9aca]2#include "parseconfig.hfa"
3
[2c2d32b]4bool comments( ifstream & in, char * name ) {
[181ef73]5 while () {
[1896c1f]6 in | name;
7 if ( fail( in ) ) return true;
[2c2d32b]8 if ( name[0] != '#' ) break;
[1896c1f]9 in | nl; // ignore remainder of line
[16f9aca]10 } // for
11 return false;
12} // comments
13
14// Process the configuration file to set the simulation parameters.
[1896c1f]15void parseConfig( const char * configFile, ConfigParms & cparms ) {
[16f9aca]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
[2c2d32b]21 } parms[Parmnum] = {
[16f9aca]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 };
[2c2d32b]34 char * name;
[16f9aca]35 int value;
36 unsigned int cnt, posn, numOfParm = 0;
37
[1896c1f]38 ifstream in;
[c2016b6]39 try {
[1896c1f]40 open( in, configFile ); // open the configuration file for input
[16f9aca]41
[181ef73]42 for ( cnt; Parmnum ) { // parameter names can appear in any order
[16f9aca]43 if ( comments( in, name ) ) break; // eof ?
[181ef73]44 for ( posn; posn < Parmnum && name != parms[posn].name; ++posn ); // linear search
[16f9aca]45 if ( posn == Parmnum ) break; // configuration not found ?
46 if ( parms[posn].used ) break; // duplicate configuration ?
[1896c1f]47 in | value;
[16f9aca]48 if ( value < 0 ) {
[3c124da]49 close( in );
[1896c1f]50 exit | "Error: file \"" | configFile | "\" parameter " | name
51 | " value " | value | " must be non-negative."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
[16f9aca]52 } // if
[1896c1f]53 if ( fail( in ) ) break;
54 in | nl; // ignore remainder of line
[dac3455]55 ++numOfParm;
[16f9aca]56 parms[posn].used = true;
57 parms[posn].value = value;
58 } // for
[1896c1f]59 } catch( Open_Failure * ex; ex->istream == &in ) {
60 exit | "Error: could not open input file \"" | configFile | "\""; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
[16f9aca]61 } // try
62
[50dcfad]63 close( in );
64
[1896c1f]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!!! *** //
[16f9aca]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.