summaryrefslogtreecommitdiff
path: root/src/intersim/config_utils.cpp
blob: a62ea051756e6cb29c895bc8899d886e78c637d9 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "booksim.hpp"
#include <iostream>
#include <stdlib.h>
#include <cstring>

#include "config_utils.hpp"

Configuration *Configuration::theConfig = 0;

Configuration::Configuration( )
{
   theConfig = this;
   _config_file = 0;
}

void Configuration::AddStrField( const string &field, const string &value )
{
   _str_map[field] = strdup( value.c_str( ) );
}

void Configuration::Assign( const string &field, const string &value )
{
   map<string,char *>::const_iterator match;

   match = _str_map.find( field );
   if ( match != _str_map.end( ) ) {
      free( _str_map[field] );
      _str_map[field] = strdup( value.c_str( ) );
   } else {
      string errmsg = "Unknown field ";
      errmsg += field;

      ParseError( errmsg, 0 );
   }
}

void Configuration::Assign( const string &field, unsigned int value )
{
   map<string,unsigned int>::const_iterator match;

   match = _int_map.find( field );
   if ( match != _int_map.end( ) ) {
      _int_map[field] = value;
   } else {
      string errmsg = "Unknown field ";
      errmsg += field;

      ParseError( errmsg, 0 );
   }
}

void Configuration::Assign( const string &field, double value )
{
   map<string,double>::const_iterator match;

   match = _float_map.find( field );
   if ( match != _float_map.end( ) ) {
      _float_map[field] = value;
   } else {
      string errmsg = "Unknown field ";
      errmsg += field;

      ParseError( errmsg, 0 );
   }
}

void Configuration::GetStr( const string &field, string &value, const string &def ) const
{
   map<string,char *>::const_iterator match;

   match = _str_map.find( field );
   if ( match != _str_map.end( ) ) {
      value = match->second;
   } else {
      value = def;
   }
}

unsigned int Configuration::GetInt( const string &field, unsigned int def ) const
{
   map<string,unsigned int>::const_iterator match;
   unsigned int r = def;

   match = _int_map.find( field );
   if ( match != _int_map.end( ) ) {
      r = match->second;
   }

   return r;
}

double Configuration::GetFloat( const string &field, double def ) const
{  
   map<string,double>::const_iterator match;
   double r = def;

   match = _float_map.find( field );
   if ( match != _float_map.end( ) ) {
      r = match->second;
   }

   return r;
}

void Configuration::Parse( const string& filename )
{
   if ( ( _config_file = fopen( filename.c_str( ), "r" ) ) == 0 ) {
      cerr << "Could not open configuration file " << filename << endl;
      exit( -1 );
   }

   configparse( );

   fclose( _config_file );
   _config_file = 0;
}

void Configuration::Parse( const char* filename )
{
   if ( ( _config_file = fopen( filename , "r" ) ) == 0 ) {
      cerr << "Could not open configuration file " << filename << endl;
      exit( -1 );
   }

   configparse( );

   fclose( _config_file );
   _config_file = 0;
}


int Configuration::Input( char *line, int max_size )
{
   int length = 0;

   if ( _config_file ) {
      length = fread( line, 1, max_size, _config_file );
   }

   return length;
}

void Configuration::ParseError( const string &msg, unsigned int lineno ) const
{
   if ( lineno ) {
      cerr << "Parse error on line " << lineno << " : " << msg << endl;
   } else {
      cerr << "Parse error : " << msg << endl;
   }

   exit( -1 );
}

Configuration *Configuration::GetTheConfig( )
{
   return theConfig;
}

//============================================================

int config_input( char *line, int max_size )
{
   return Configuration::GetTheConfig( )->Input( line, max_size );
}

bool ParseArgs( Configuration *cf, int argc, char **argv )
{
   bool rc = false;

   if ( argc > 1 ) {
      cf->Parse( argv[1] );
      rc = true;
   }

   return rc;
}