Repository navigation
Expand file tree
/
Copy pathConfigReader.cpp
More file actions
228 lines (198 loc) · 7.52 KB
/
Copy pathConfigReader.cpp
File metadata and controls
228 lines (198 loc) · 7.52 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <algorithm>
#include <fstream>
#include <vector>
#include <cstring>
#include <sys/time.h>
#include <sstream>
#define SI_SUPPORT_IOSTREAMS
using namespace std;
#include "SimpleIni.h"
#include "ConfigReader.h"
#include "config.h"
#include "Logger.h"
ConfigReader::ConfigReader(void)
{
}
ConfigReader::~ConfigReader(void)
{
}
ConfigReader::InitReturnValues ConfigReader::readConfig(const string &fileName){
//ICT_Bill_Accepptor_Driver.conf - Sections
const string sectionPort("Port");
const string sectionBills("Bills");
//ICT_Bill_Accepptor_Driver.conf, Port section
const string keyPort("Name");
//ICT_Bill_Accepptor_Driver.conf, Bill section
const string keyBill_1("1");
const string keyBill_2("2");
const string keyBill_3("3");
const string keyBill_4("4");
const string keyBill_5("5");
const string keyBill_6("6");
CSimpleIniA ini;
ini.SetUnicode(false);
ini.SetMultiKey(false);
ini.SetMultiLine(false);
string err_str;
// load the file
std::ifstream instream;
instream.open(fileName.c_str(), std::ifstream::in | std::ifstream::binary);
if(!instream.good()){
err_str="Cannot open configuration file \""+fileName+"\"";
Logger::instance().logFatal(err_str);
return INIT_RETURN_FILE_OPEN_FAILED;
}
if (ini.LoadData(instream) < 0){
err_str="Cannot load INI file data from config file";
Logger::instance().logFatal(err_str);
return INIT_RETURN_FILE_OPEN_FAILED;
}
instream.close();
const char *valuePort=ini.GetValue(sectionPort.c_str(), keyPort.c_str(), NULL);
const char *valueBill_1=ini.GetValue(sectionBills.c_str(), keyBill_1.c_str(), NULL);
const char *valueBill_2=ini.GetValue(sectionBills.c_str(), keyBill_2.c_str(), NULL);
const char *valueBill_3=ini.GetValue(sectionBills.c_str(), keyBill_3.c_str(), NULL);
const char *valueBill_4=ini.GetValue(sectionBills.c_str(), keyBill_4.c_str(), NULL);
const char *valueBill_5=ini.GetValue(sectionBills.c_str(), keyBill_5.c_str(), NULL);
const char *valueBill_6=ini.GetValue(sectionBills.c_str(), keyBill_6.c_str(), NULL);
if(valuePort==NULL){
err_str=string("Cannot load \"") + keyPort + "\" value from \"" + sectionPort + "\" section in config file";
Logger::instance().logError(err_str);
return INIT_RETURN_VALUE_LOAD_FAILED;
}
configValues.portName=string(valuePort);
stringstream converter;
string buf;
int billCounter=0;
if(valueBill_1==NULL){
err_str=string("Cannot load \"") + keyBill_1 + "\" value from \"" + sectionBills + "\" section in config file";
Logger::instance().logWarning(err_str);
}else{
buf=string(valueBill_1);
if(buf.length()>0){
Logger::instance().logInfo("Bill 1 value = "+string(valueBill_1));
billCounter++;
converter<<string(valueBill_1);
converter>>configValues.bill1_value;
if(converter.fail() || configValues.bill1_value<1){
Logger::instance().logInfo("Bill 1 value not valid!");
return INIT_RETURN_VALUE_LOAD_FAILED;
}
converter.clear();
converter.str(std::string());
}else{
Logger::instance().logInfo("Bill 1 value not defined");
configValues.bill1_value=-1;
}
}
if(valueBill_2==NULL){
err_str=string("Cannot load \"") + keyBill_2 + "\" value from \"" + sectionBills + "\" section in config file";
Logger::instance().logWarning(err_str);
}else{
buf=string(valueBill_2);
if(buf.length()>0){
Logger::instance().logInfo("Bill 2 value = "+string(valueBill_2));
billCounter++;
converter<<string(valueBill_2);
converter>>configValues.bill2_value;
if(converter.fail() || configValues.bill2_value<1){
Logger::instance().logInfo("Bill 2 value not valid!");
return INIT_RETURN_VALUE_LOAD_FAILED;
}
converter.clear();
converter.str(std::string());
}else{
Logger::instance().logInfo("Bill 2 value not defined");
configValues.bill2_value=-1;
}
}
if(valueBill_3==NULL){
err_str=string("Cannot load \"") + keyBill_3 + "\" value from \"" + sectionBills + "\" section in config file";
Logger::instance().logWarning(err_str);
}else{
buf=string(valueBill_3);
if(buf.length()>0){
Logger::instance().logInfo("Bill 3 value = "+string(valueBill_3));
billCounter++;
converter<<string(valueBill_3);
converter>>configValues.bill3_value;
if(converter.fail() || configValues.bill3_value<1){
Logger::instance().logInfo("Bill 3 value not valid!");
return INIT_RETURN_VALUE_LOAD_FAILED;
}
converter.clear();
converter.str(std::string());
}else{
Logger::instance().logInfo("Bill 3 value not defined");
configValues.bill3_value=-1;
}
}
if(valueBill_4==NULL){
err_str=string("Cannot load \"") + keyBill_4 + "\" value from \"" + sectionBills + "\" section in config file";
Logger::instance().logWarning(err_str);
}else{
buf=string(valueBill_4);
if(buf.length()>0){
Logger::instance().logInfo("Bill 4 value = "+string(valueBill_4));
billCounter++;
converter<<string(valueBill_4);
converter>>configValues.bill4_value;
if(converter.fail() || configValues.bill4_value<1){
Logger::instance().logInfo("Bill 4 value not valid!");
return INIT_RETURN_VALUE_LOAD_FAILED;
}
converter.clear();
converter.str(std::string());
}else{
Logger::instance().logInfo("Bill 4 value not defined");
configValues.bill4_value=-1;
}
}
if(valueBill_5==NULL){
err_str=string("Cannot load \"") + keyBill_5 + "\" value from \"" + sectionBills + "\" section in config file";
Logger::instance().logWarning(err_str);
}else{
buf=string(valueBill_5);
if(buf.length()>0){
Logger::instance().logInfo("Bill 5 value = "+string(valueBill_5));
billCounter++;
converter<<string(valueBill_5);
converter>>configValues.bill5_value;
if(converter.fail() || configValues.bill5_value<1){
Logger::instance().logInfo("Bill 5 value not valid!");
return INIT_RETURN_VALUE_LOAD_FAILED;
}
converter.clear();
converter.str(std::string());
}else{
Logger::instance().logInfo("Bill 5 value not defined");
configValues.bill5_value=-1;
}
}
if(valueBill_6==NULL){
err_str=string("Cannot load \"") + keyBill_6 + "\" value from \"" + sectionBills + "\" section in config file";
Logger::instance().logWarning(err_str);
}else{
buf=string(valueBill_6);
if(buf.length()>0){
Logger::instance().logInfo("Bill 6 value = "+string(valueBill_6));
billCounter++;
converter<<string(valueBill_6);
converter>>configValues.bill6_value;
if(converter.fail() || configValues.bill6_value<1){
Logger::instance().logInfo("Bill 6 value not valid!");
return INIT_RETURN_VALUE_LOAD_FAILED;
}
converter.clear();
converter.str(std::string());
}else{
Logger::instance().logInfo("Bill 6 value not defined");
configValues.bill6_value=-1;
}
}
if(billCounter==0){
Logger::instance().logError("No bill values defined in the configuration.");
return INIT_RETURN_VALUE_LOAD_FAILED;
}
return INIT_RETURN_OK;
}