-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cc
executable file
·73 lines (68 loc) · 2.64 KB
/
config.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <fstream>
#include <limits> // numeric_limits
using namespace std;
#include "config.h"
static bool comments( ifstream & in, string & name ) {
for ( ;; ) {
in >> name;
if ( in.fail() ) return true;
if ( name.substr(0,1) != "#" ) break;
in.ignore( numeric_limits<int>::max(), '\n' ); // ignore remainder of line
} // for
return false;
} // comments
// Process the configuration file to set the simulation parameters.
void processConfigFile( const char * configFile, ConfigParms & cparms ) {
enum { Parmnum = 10 };
struct {
const char * name; // configuration name
bool used; // already supplied ?
unsigned int & value; // location to put configuration value
} parms[Parmnum] = {
{ "SodaCost", false, cparms.sodaCost },
{ "NumStudents", false, cparms.numStudents },
{ "MaxPurchases", false, cparms.maxPurchases },
{ "NumVendingMachines", false, cparms.numVendingMachines },
{ "MaxStockPerFlavour", false, cparms.maxStockPerFlavour },
{ "MaxShippedPerFlavour", false, cparms.maxShippedPerFlavour },
{ "TimeBetweenShipments", false, cparms.timeBetweenShipments },
{ "GroupoffDelay", false, cparms.groupoffDelay },
{ "ParentalDelay", false, cparms.parentalDelay },
{ "NumCouriers", false, cparms.numCouriers },
};
string name;
int value;
unsigned int cnt, posn, numOfParm = 0;
try {
ifstream in( configFile ); // open the configuration file for input
for ( cnt = 0 ; cnt < Parmnum; cnt += 1 ) { // parameter names can appear in any order
if ( comments( in, name ) ) break; // eof ?
for ( posn = 0; posn < Parmnum && name != parms[posn].name; posn += 1 ); // linear search
if ( posn == Parmnum ) break; // configuration not found ?
if ( parms[posn].used ) break; // duplicate configuration ?
in >> value;
if ( value <= 0 ) {
cerr << "Error: file \"" << configFile << "\" parameter " << name
<< " value " << value << " must be positive." << endl;
exit( EXIT_FAILURE );
} // if
if ( in.fail() ) break;
in.ignore( numeric_limits<int>::max(), '\n' ); // ignore remainder of line
numOfParm += 1;
parms[posn].used = true;
parms[posn].value = value;
} // for
if ( numOfParm != Parmnum ) {
cerr << "Error: file \"" << configFile << "\" is corrupt." << endl;
exit( EXIT_FAILURE );
} // if
if ( ! comments( in, name ) ) { // ! eof ?
cerr << "Error: file \"" << configFile << "\" has extraneous data." << endl;
exit( EXIT_FAILURE );
} // if
} catch( uFile::Failure & ) {
cerr << "Error: could not open input file \"" << configFile << "\"" << endl;
exit( EXIT_FAILURE );
} // try
} // processConfigFile