source: libcfa/src/parseconfig.cfa @ 16f9aca

enumforall-pointer-decayjacob/cs343-translationpthread-emulationqualifiedEnum
Last change on this file since 16f9aca was 16f9aca, checked in by Jacob Prud'homme <jafprudhomme@…>, 19 months ago

Created new library with code directly from CS 343 A6

  • Property mode set to 100644
File size: 3.4 KB
Line 
1#include <iostream>
2#include <fstream>
3#include <limits>                                                                               // numeric_limits
4using namespace std;
5#include "parseconfig.hfa"
6
7static bool comments( ifstream & in, string & name ) {
8        for ( ;; ) {
9                in >> name;
10          if ( in.fail() ) return true;
11          if ( name.substr(0,1) != "#" ) break;
12                in.ignore( numeric_limits<int>::max(), '\n' );  // ignore remainder of line
13        } // for
14        return false;
15} // comments
16
17// Process the configuration file to set the simulation parameters.
18void processConfigFile( const char *configFile, ConfigParms & cparms ) {
19        enum { Parmnum = 11 };
20        struct {
21                const char * name;                                                              // configuration name
22                bool used;                                                                              // already supplied ?
23                unsigned int & value;                                                   // location to put configuration value
24        } static parms[Parmnum] = {
25                { "StopCost", false, cparms.stopCost },
26                { "NumStudents", false, cparms.numStudents },
27                { "NumStops", false, cparms.numStops },
28                { "MaxNumStudents", false, cparms.maxNumStudents },
29                { "TimerDelay", false, cparms.timerDelay },
30                { "MaxStudentDelay", false, cparms.maxStudentDelay },
31                { "MaxStudentTrips", false, cparms.maxStudentTrips },
32                { "GroupoffDelay", false, cparms.groupoffDelay },
33                { "ConductorDelay", false, cparms.conductorDelay },
34                { "ParentalDelay", false, cparms.parentalDelay },
35                { "NumCouriers", false, cparms.numCouriers },
36        };
37        string name;
38        int value;
39        unsigned int cnt, posn, numOfParm = 0;
40
41        try {
42                ifstream in( configFile );                                              // open the configuration file for input
43
44                for ( cnt = 0 ; cnt < Parmnum; cnt += 1 ) {             // parameter names can appear in any order
45                  if ( comments( in, name ) ) break;                    // eof ?
46                        for ( posn = 0; posn < Parmnum && name != parms[posn].name; posn += 1 ); // linear search
47                  if ( posn == Parmnum ) break;                                 // configuration not found ?
48                  if ( parms[posn].used ) break;                                // duplicate configuration ?
49                        in >> value;
50                        if ( value < 0 ) {
51                                cerr << "Error: file \"" << configFile << "\" parameter " << name
52                                         << " value " << value << " must be non-negative." << endl;
53                                exit( EXIT_FAILURE );
54                        } // if
55                  if ( in.fail() ) break;
56                        in.ignore( numeric_limits<int>::max(), '\n' ); // 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                        cerr << "Error: file \"" << configFile << "\" is corrupt." << endl;
64                        exit( EXIT_FAILURE );
65                } // if
66                if ( ! comments( in, name ) ) {                                 // ! eof ?
67                        cerr << "Error: file \"" << configFile << "\" has extraneous data." << endl;
68                        exit( EXIT_FAILURE );
69                } // if
70        } catch( uFile::Failure & ) {
71                cerr << "Error: could not open input file \"" << configFile << "\"" << endl;
72                exit( EXIT_FAILURE );
73        } // try
74
75        if (cparms.numStops < 2) {
76        cerr << "Error: file \"" << configFile << "\" parameter NumStops value "
77             << cparms.numStops << " must be at least 2." << endl;
78        exit( EXIT_FAILURE );
79        }
80    if (cparms.numStudents < 1) {
81        cerr << "Error: file \"" << configFile << "\" parameter NumStudents value "
82             << cparms.numStudents << " must be at least 1." << endl;
83        exit( EXIT_FAILURE );
84    }
85    if (cparms.numCouriers < 1) {
86        cerr << "Error: file \"" << configFile << "\" parameter NumCouriers value "
87             << cparms.numCouriers << " must be at least 1." << endl;
88        exit( EXIT_FAILURE );
89    }
90} // processConfigFile
91
92// Local Variables: //
93// tab-width: 4 //
94// compile-command: "cfa parseconfig.cfa" //
95// End: //
Note: See TracBrowser for help on using the repository browser.