-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconfig.cpp
148 lines (123 loc) · 3.34 KB
/
config.cpp
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
#include "config.h"
Config::Config( std::string filename )
{
this->filename = filename;
std::ifstream cfile( filename );
if( !cfile.is_open() ) {
printf("Can't open config file!\n");
exit(-1);
}
std::string buf;
while( std::getline (cfile, buf ) )
{
if( buf.length() > 0 )
{
buf.erase(remove_if(buf.begin(), buf.end(), isspace), buf.end());
if( buf[0] == '#' || buf[0] == ';' || ( buf[0] == '/' && buf[1] == '/' ) || buf[0] == '=' )
continue;
std::size_t i = buf.find_last_of('=');
if( i == std::string::npos )
continue;
if( i+1 == buf.length() )
continue;
// var name
std::string key = buf.substr(0, i);
if( key == "" ) continue;
// var value
std::string value = buf.substr( i+1, buf.length() );
if( value == "" ) continue;
// std::cout << "Line parsed:\t'" << key << "'\t->\t'" << value << "'" << std::endl;
this->params[key] = value;
}
}
}
bool Config::getParam( std::string name, std::string ¶m )
{
if( name == "" )
return false;
std::unordered_map<std::string,std::string>::const_iterator found = this->params.find( name );
if( found != this->params.end() ) {
param = found->second;
return true;
} else {
return false;
}
}
bool Config::getParam( std::string name, short unsigned int ¶m )
{
if( name == "" )
return false;
std::unordered_map<std::string,std::string>::const_iterator found = this->params.find( name );
if( found != this->params.end() ) {
std::string param_str = found->second;
char *temp;
short unsigned int _val = strtol( param_str.c_str(), &temp, 0);
if( *temp != '\0' ) {
printf("Can't convert '%s' parameter to int: '%s'\n", name.c_str(), param_str.c_str() );
exit(-1);
}
param = _val;
} else {
return false;
}
}
bool Config::getParam( std::string name, int ¶m )
{
if( name == "" )
return false;
std::unordered_map<std::string,std::string>::const_iterator found = this->params.find( name );
if( found != this->params.end() ) {
std::string param_str = found->second;
char *temp;
int _val = strtol( param_str.c_str(), &temp, 0);
if( *temp != '\0' ) {
printf("Can't convert '%s' parameter to int: '%s'\n", name.c_str(), param_str.c_str() );
exit(-1);
}
param = _val;
return true;
} else {
return false;
}
}
bool Config::getQueues(int &from, int &to)
{
std::string mq;
if( this->getParam( "multiqueues", mq ) ) {
// multi-queues
mq.erase(remove_if(mq.begin(), mq.end(), isspace), mq.end()); // trim
std::size_t i = mq.find_last_of(':');
if( i == std::string::npos )
return false;
if( i+1 == mq.length() )
return false;
// start
std::string f = mq.substr(0, i);
// end
std::string t = mq.substr( i+1, mq.length() );
char *temp;
int _val = strtol( f.c_str(), &temp, 0 );
if( *temp != '\0' ) {
printf("Config: Multiqueue starting queue num is not numeric: %s\n", f.c_str() );
exit(-1);
}
from = _val;
_val = strtol( t.c_str(), &temp, 0 );
if( *temp != '\0' ) {
printf("Config: Multiqueue ending queue num is not numeric: %s\n", t.c_str() );
exit(-1);
}
to = _val;
if( from < 0 || to < 0 ) {
printf("Config: Queue num can't be < 0 (%s)\n", mq.c_str() );
exit(-1);
}
if( to <= from ) {
printf("Config: Ending queue must be > starting queue num (%s)\n", mq.c_str() );
exit(-1);
}
return true;
} else {
return false;
}
}