source: libcfa/src/parseconfig.cfa @ c2016b6

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

Declared ifstream before try block

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