source: libcfa/src/parseconfig.cfa @ 2c2d32b

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

Removed remaining bits of C++ code

  • 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        try {
39                ifstream in;
40
41                open( in, configFile );                                                 // open the configuration file for input
42
43                for ( cnt; Parmnum ) {                                                  // parameter names can appear in any order
44                  if ( comments( in, name ) ) break;                    // eof ?
45                        for ( posn; posn < Parmnum && name != parms[posn].name; ++posn ); // linear search
46                  if ( posn == Parmnum ) break;                                 // configuration not found ?
47                  if ( parms[posn].used ) break;                                // duplicate configuration ?
48                        in | value;
49                        if ( value < 0 ) {
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
60                if ( numOfParm != Parmnum ) {
61                        exit | "Error: file \"" | configFile | "\" is corrupt."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
62                } // if
63                if ( ! comments( in, name ) ) {                                 // ! eof ?
64                        exit | "Error: file \"" | configFile | "\" has extraneous data."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
65                } // if
66        } catch( Open_Failure * ex; ex->istream == &in ) {
67                exit | "Error: could not open input file \"" | configFile | "\""; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
68        } finally {
69                close( in );
70        } // try
71
72        if ( cparms.numStops < 2 ) {
73                exit | "Error: file \"" | configFile | "\" parameter NumStops value "
74                         | cparms.numStops | " must be at least 2."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
75        }
76        if ( cparms.numStudents < 1 ) {
77                exit | "Error: file \"" | configFile | "\" parameter NumStudents value "
78                         | cparms.numStudents | " must be at least 1."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
79        }
80        if ( cparms.numCouriers < 1 ) {
81                exit | "Error: file \"" | configFile | "\" parameter NumCouriers value "
82                         | cparms.numCouriers | " must be at least 1."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
83        }
84} // processConfigFile
85
86// Local Variables: //
87// tab-width: 4 //
88// compile-command: "cfa parseconfig.cfa" //
89// End: //
Note: See TracBrowser for help on using the repository browser.