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
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
38        try {
[1896c1f]39                ifstream in;
40
41                open( in, configFile );                                                 // open the configuration file for input
[16f9aca]42
[181ef73]43                for ( cnt; Parmnum ) {                                                  // parameter names can appear in any order
[16f9aca]44                  if ( comments( in, name ) ) break;                    // eof ?
[181ef73]45                        for ( posn; posn < Parmnum && name != parms[posn].name; ++posn ); // linear search
[16f9aca]46                  if ( posn == Parmnum ) break;                                 // configuration not found ?
47                  if ( parms[posn].used ) break;                                // duplicate configuration ?
[1896c1f]48                        in | value;
[16f9aca]49                        if ( value < 0 ) {
[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
59
60                if ( numOfParm != Parmnum ) {
[1896c1f]61                        exit | "Error: file \"" | configFile | "\" is corrupt."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
[16f9aca]62                } // if
63                if ( ! comments( in, name ) ) {                                 // ! eof ?
[1896c1f]64                        exit | "Error: file \"" | configFile | "\" has extraneous data."; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
[16f9aca]65                } // if
[1896c1f]66        } catch( Open_Failure * ex; ex->istream == &in ) {
67                exit | "Error: could not open input file \"" | configFile | "\""; // *** DOES THIS PRINT TO STDERR??? IT MUST!!! *** //
[c3c76cd]68        } finally {
69                close( in );
[16f9aca]70        } // try
71
[1896c1f]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!!! *** //
[16f9aca]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.