source: libcfa/src/parseconfig.cfa @ c3c76cd

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationpthread-emulationqualifiedEnum
Last change on this file since c3c76cd was c3c76cd, checked in by Jacob Prud'homme <jafprudhomme@…>, 3 years ago

Coalesced all file closes to one location

  • Property mode set to 100644
File size: 3.4 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                                exit | "Error: file \"" | configFile | "\" parameter " | name
53                                         | " value " | value | " must be non-negative."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
54                        } // if
55                  if ( fail( in ) ) break;
56                        in | nl; // ignore remainder of line
57                        numOfParm += 1;
58                        parms[posn].used = true;
59                        parms[posn].value = value;
60                } // for
61
62                if ( numOfParm != Parmnum ) {
63                        exit | "Error: file \"" | configFile | "\" is corrupt."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
64                } // if
65                if ( ! comments( in, name ) ) {                                 // ! eof ?
66                        exit | "Error: file \"" | configFile | "\" has extraneous data."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
67                } // if
68        } catch( Open_Failure * ex; ex->istream == &in ) {
69                exit | "Error: could not open input file \"" | configFile | "\""; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
70        } finally {
71                close( in );
72        } // try
73
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!!! *** //
85        }
86} // processConfigFile
87
88// Local Variables: //
89// tab-width: 4 //
90// compile-command: "cfa parseconfig.cfa" //
91// End: //
Note: See TracBrowser for help on using the repository browser.