-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConfig.cpp
166 lines (126 loc) · 4.59 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "Config.h"
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
bool ConfigData::iskey(const std::string& s) const
{
return count(s) != 0;
}
Config::Config():ConfigData(){
VERBOSE=0L;
}
//---------------------------------------------------------------------------
// The extraction operator reads configuration::data until EOF.
// Invalid data is ignored.
//
std::istream & operator >> ( std::istream& ins, ConfigData& d )
{
std::string s, key, value;
// For each (key, value) pair in the file
while(std::getline(ins, s))
{
std::string::size_type begin = s.find_first_not_of( " \f\t\v" );
// Skip blank lines
if (begin == std::string::npos) continue;
// Skip commentary
if (std::string( "#;" ).find( s[ begin ] ) != std::string::npos) continue;
// Extract the key value
std::string::size_type end = s.find( '=', begin );
key = s.substr( begin, end - begin );
// (No leading or trailing whitespace allowed)
key.erase( key.find_last_not_of( " \f\t\v" ) + 1 );
// No blank keys allowed
if (key.empty()) continue;
// Extract the value (no leading or trailing whitespace allowed)
begin = s.find_first_not_of( " \f\n\r\t\v", end + 1 );
end = s.find_last_not_of( " \f\n\r\t\v" ) + 1;
value = s.substr(begin, end - begin);
// Insert the properly extracted (key, value) pair into the map
d[key] = value;
}
return ins;
}
//---------------------------------------------------------------------------
// The insertion operator writes all configuration::data to stream.
//
std::ostream& operator << ( std::ostream& outs, const ConfigData& d )
{
ConfigData::const_iterator iter;
for (iter = d.begin(); iter != d.end(); iter++)
outs << iter->first << " = " << iter->second << std::endl;
return outs;
}
Config::Config(std::string path_config): ConfigData(){
LoadFromFile(path_config, true);
}
bool Config::LoadFromFile(std::string path_config, bool is_need_exit){
std::ifstream f(path_config.c_str());
f >> (*this);
f.close();
this->GSOAP_SERVER_PORT = 8080; // default port
std::vector<std::string> required_fields;
std::vector<std::string> missing_fields;
required_fields.push_back(std::string("GSOAP_SERVER_PORT"));
required_fields.push_back(std::string("GSOAP_SERVER_ROOT"));
required_fields.push_back(std::string("GSOAP_COOKIE_DOMAIN"));
required_fields.push_back(std::string("GSOAP_COOKIE_PATH"));
required_fields.push_back(std::string("CATIWEB_WEBSERVICE_API"));
required_fields.push_back(std::string("CATIWEB_COOKIE_DOMAIN"));
required_fields.push_back(std::string("CATIWEB_COOKIE_PATH"));
required_fields.push_back(std::string("CATIWEB_PIPELINES"));
required_fields.push_back(std::string("COOKIE_NAME_AUTH"));
required_fields.push_back(std::string("WSDL_FILE_PATH"));
required_fields.push_back(std::string("VERBOSE"));
for(int i=0; i<required_fields.size(); i++)
{
std::string required_field = required_fields[i];
if(!this->iskey(required_field))
{
missing_fields.push_back(required_field);
}
}
if(missing_fields.size() > 0)
{
std::cout << "Config file misses a parameter....." << std::endl;
for(int i=0;i<missing_fields.size();i++)
{
std::cout << "missing :" << missing_fields[i] << std::endl;
}
if(is_need_exit)
{
exit(EXIT_FAILURE);
}
return false;
}
this->GSOAP_SERVER_PORT = atoi((*this)["GSOAP_SERVER_PORT"].c_str());
this->GSOAP_SERVER_ROOT = (*this)["GSOAP_SERVER_ROOT"];
this->GSOAP_COOKIE_DOMAIN = (*this)["GSOAP_COOKIE_DOMAIN"];
this->GSOAP_COOKIE_PATH = (*this)["GSOAP_COOKIE_PATH"];
this->CATIWEB_WEBSERVICE_API = (*this)["CATIWEB_WEBSERVICE_API"];
this->CATIWEB_COOKIE_DOMAIN = (*this)["CATIWEB_COOKIE_DOMAIN"];
this->CATIWEB_COOKIE_PATH = (*this)["CATIWEB_COOKIE_PATH"];
this->CATIWEB_PIPELINES = (*this)["CATIWEB_PIPELINES"];
this->COOKIE_NAME_AUTH = (*this)["COOKIE_NAME_AUTH"];
this->WSDL_FILE_PATH = (*this)["WSDL_FILE_PATH"];
this->VERBOSE = atoi((*this)["VERBOSE"].c_str());
if(this->VERBOSE)
{
std::cout << "Configuration loaded:"<< std::endl;
Config::const_iterator iter;
for(iter = this->begin(); iter != this->end(); iter++)
{
std::cout << iter->first << " = " << iter->second << std::endl;
}
}
return true;
}
std::string Config::get_route_catiweb_pipelines(){
std::string url;
char url_buf[URL_MAX_LEN];
snprintf(url_buf, URL_MAX_LEN, "%s%s", CATIWEB_WEBSERVICE_API.c_str(), CATIWEB_PIPELINES.c_str());
url = url_buf;
return url;
}