0.08.01
C++ Open Travel Request Parsing Library
Toggle main menu visibility
Loading...
Searching...
No Matches
Utilities.cpp
Go to the documentation of this file.
1
// //////////////////////////////////////////////////////////////////////
2
// Import section
3
// //////////////////////////////////////////////////////////////////////
4
// STL
5
#include <cassert>
6
#include <ostream>
7
#include <sstream>
8
// Boost (Extended STL)
9
#include <boost/tokenizer.hpp>
10
// OpenTrep
11
#include <
opentrep/OPENTREP_exceptions.hpp
>
12
#include <
opentrep/DBType.hpp
>
13
#include <
opentrep/basic/Utilities.hpp
>
14
#include <
opentrep/service/Logger.hpp
>
15
16
namespace
OPENTREP
{
17
18
// //////////////////////////////////////////////////////////////////////
19
void
tokeniseStringIntoWordList
(
const
std::string& iPhrase,
20
WordList_T
& ioWordList) {
21
// Empty the word list
22
ioWordList.clear();
23
24
// Boost Tokeniser
25
typedef
boost::tokenizer<boost::char_separator<char> > Tokeniser_T;
26
27
// Define the single-character separators.
28
// Note that multi-byte Unicode characters (e.g., “, ”)
29
// should not be inserted here
30
const
boost::char_separator<char>
31
lSepatorList(
" .,;:|+-*/_=!@#$%`~^&(){}[]?'<>\""
);
32
33
// Initialise the phrase to be tokenised
34
Tokeniser_T lTokens (iPhrase, lSepatorList);
35
for
(Tokeniser_T::const_iterator tok_iter = lTokens.begin();
36
tok_iter != lTokens.end(); ++tok_iter) {
37
const
std::string& lTerm = *tok_iter;
38
ioWordList.push_back (lTerm);
39
}
40
}
41
42
// //////////////////////////////////////////////////////////////////////
43
std::string
createStringFromWordList
(
const
WordList_T
& iWordList,
44
const
NbOfWords_T
iSplitIdx,
45
const
bool
iFromBeginningFlag) {
46
std::ostringstream oStr;
47
48
// Browse the left-hand side of the string
49
NbOfWords_T
idx = 0;
50
WordList_T::const_iterator itWord = iWordList.begin();
51
for
( ; itWord != iWordList.end(); ++itWord, ++idx) {
52
53
if
(iFromBeginningFlag ==
true
) {
54
// The beginning of the word list is needed
55
56
// Check whether the sub-list has reached the expected split point,
57
// if any
58
if
(iSplitIdx != 0 && idx >= iSplitIdx) {
59
break
;
60
}
61
62
//
63
if
(idx > 0) {
64
oStr <<
" "
;
65
}
66
//
67
const
std::string& lWord = *itWord;
68
oStr << lWord;
69
70
}
else
{
71
// The end of the word list is needed
72
73
// Check whether the sub-list has reached the expected split point,
74
// if any
75
if
(iSplitIdx == 0 || idx >= iSplitIdx) {
76
break
;
77
}
78
}
79
}
80
81
// The beginning of the word list is needed
82
if
(iFromBeginningFlag ==
true
) {
83
return
oStr.str();
84
}
85
86
// The end of the word list is needed
87
assert (iFromBeginningFlag ==
false
);
88
89
// Browse the right-hand side of the string
90
for
( ; itWord != iWordList.end(); ++itWord, ++idx) {
91
// The end of the word list is needed
92
93
//
94
if
(idx > iSplitIdx) {
95
oStr <<
" "
;
96
}
97
//
98
const
std::string& lWord = *itWord;
99
oStr << lWord;
100
}
101
102
return
oStr.str();
103
}
104
105
// //////////////////////////////////////////////////////////////////////
106
StringMap_T
107
parseMySQLConnectionString
(
const
SQLDBConnectionString_T
& iSQLDBConnStr) {
108
StringMap_T
oStrMap;
109
110
std::stringstream lConnStream (iSQLDBConnStr);
111
std::string kvStr;
112
std::vector<std::string> kvList;
113
unsigned
short
keyDBName = 0;
114
unsigned
short
keyDBUser = 0;
115
unsigned
short
keyDBPasswd = 0;
116
unsigned
short
lastKey = 0;
117
118
while
(std::getline (lConnStream, kvStr,
' '
)) {
119
std::stringstream kvStream (kvStr);
120
std::string keyStr;
121
122
while
(std::getline (kvStream, keyStr,
'='
)) {
123
if
(keyStr ==
"db"
) {
124
++lastKey;
125
keyDBName = lastKey;
126
continue
;
127
128
}
else
if
(keyStr ==
"user"
) {
129
++lastKey;
130
keyDBUser = lastKey;
131
continue
;
132
133
}
else
if
(keyStr ==
"password"
) {
134
++lastKey;
135
keyDBPasswd = lastKey;
136
continue
;
137
138
}
else
if
(lastKey == keyDBName) {
139
const
bool
isSuccess =
140
oStrMap.insert (std::make_pair (
"db"
, keyStr)).second;
141
if
(isSuccess ==
false
) {
142
std::ostringstream errStr;
143
errStr <<
"Internal error while inserting the SQL database name ('"
144
<< keyDBName <<
"') into the internal STL map"
;
145
OPENTREP_LOG_ERROR
(errStr.str());
146
}
147
assert (isSuccess ==
true
);
148
continue
;
149
150
}
else
if
(lastKey == keyDBUser) {
151
const
bool
isSuccess =
152
oStrMap.insert (std::make_pair (
"user"
, keyStr)).second;
153
if
(isSuccess ==
false
) {
154
std::ostringstream errStr;
155
errStr <<
"Internal error while inserting the SQL database user ('"
156
<< keyDBUser <<
"') into the internal STL map"
;
157
OPENTREP_LOG_ERROR
(errStr.str());
158
}
159
assert (isSuccess ==
true
);
160
continue
;
161
162
}
else
if
(lastKey == keyDBPasswd) {
163
const
bool
isSuccess =
164
oStrMap.insert (std::make_pair (
"password"
, keyStr)).second;
165
if
(isSuccess ==
false
) {
166
std::ostringstream errStr;
167
errStr <<
"Internal error while inserting the SQL database password "
168
<<
" into the internal STL map"
;
169
OPENTREP_LOG_ERROR
(errStr.str());
170
}
171
assert (isSuccess ==
true
);
172
continue
;
173
}
174
}
175
}
176
180
// SQL database name
181
const
StringMap_T::const_iterator itDBName = oStrMap.find (
"db"
);
182
if
(itDBName == oStrMap.end()) {
183
std::ostringstream errStr;
184
errStr <<
"Error when parsing the SQL database connection string ('"
185
<< iSQLDBConnStr <<
"'), the 'db' value cannot be found"
;
186
OPENTREP_LOG_ERROR
(errStr.str());
187
throw
SQLDatabaseConnectionStringParsingException
(errStr.str());
188
}
189
190
// SQL database user
191
const
StringMap_T::const_iterator itDBUser = oStrMap.find (
"user"
);
192
if
(itDBUser == oStrMap.end()) {
193
std::ostringstream errStr;
194
errStr <<
"Error when parsing the SQL database connection string ('"
195
<< iSQLDBConnStr <<
"'), the 'user' value cannot be found"
;
196
OPENTREP_LOG_ERROR
(errStr.str());
197
throw
SQLDatabaseConnectionStringParsingException
(errStr.str());
198
}
199
200
// SQL database password
201
const
StringMap_T::const_iterator itDBPasswd = oStrMap.find (
"password"
);
202
if
(itDBPasswd == oStrMap.end()) {
203
std::ostringstream errStr;
204
errStr <<
"Error when parsing the SQL database connection string ('"
205
<< iSQLDBConnStr <<
"'), the 'password' value cannot be found"
;
206
OPENTREP_LOG_ERROR
(errStr.str());
207
throw
SQLDatabaseConnectionStringParsingException
(errStr.str());
208
}
209
210
return
oStrMap;
211
}
212
213
// //////////////////////////////////////////////////////////////////////
214
SQLDBConnectionString_T
215
buildMySQLConnectionString
(
const
StringMap_T
& iStringMap,
216
const
DeploymentNumber_T
& iDeploymentNumber) {
217
std::ostringstream oStr;
218
219
// SQL database name
220
const
StringMap_T::const_iterator itDBName = iStringMap.find (
"db"
);
221
assert (itDBName != iStringMap.end());
222
const
std::string& lDBName = itDBName->second;
223
224
// SQL database user
225
const
StringMap_T::const_iterator itDBUser = iStringMap.find (
"user"
);
226
assert (itDBUser != iStringMap.end());
227
const
std::string& lDBUser = itDBUser->second;
228
229
// SQL database password
230
const
StringMap_T::const_iterator itDBPasswd = iStringMap.find (
"password"
);
231
assert (itDBPasswd != iStringMap.end());
232
const
std::string& lDBPasswd = itDBPasswd->second;
233
234
//
235
oStr <<
"db="
<< lDBName;
236
if
(lDBName !=
"mysql"
) {
237
oStr << iDeploymentNumber;
238
}
239
240
//
241
oStr <<
" user="
<< lDBUser;
242
243
//
244
oStr <<
" password="
<< lDBPasswd;
245
246
return
SQLDBConnectionString_T
(oStr.str());
247
}
248
249
// //////////////////////////////////////////////////////////////////////
250
std::string
251
displayMySQLConnectionString
(
const
StringMap_T
& iStringMap,
252
const
DeploymentNumber_T
& iDeploymentNumber) {
253
std::ostringstream oStr;
254
255
for
(StringMap_T::const_iterator itDBKV = iStringMap.begin();
256
itDBKV != iStringMap.end(); ++itDBKV) {
257
const
std::string& lDBKey = itDBKV->first;
258
const
std::string& lDBValue = itDBKV->second;
259
oStr << lDBKey <<
"="
;
260
oStr << lDBValue;
261
if
(lDBKey ==
"db"
&& lDBValue !=
"mysql"
262
&& iDeploymentNumber !=
DEFAULT_OPENTREP_DEPLOYMENT_NUMBER_SIZE
) {
263
oStr << iDeploymentNumber;
264
}
265
oStr <<
" "
;
266
}
267
268
return
oStr.str();
269
}
270
271
// //////////////////////////////////////////////////////////////////////
272
StringMap_T
273
parsePGConnectionString
(
const
SQLDBConnectionString_T
& iSQLDBConnStr) {
274
StringMap_T
oStrMap;
275
276
// Split on spaces; each token is "key=value". Split each token on the
277
// first '=' only, so that passwords containing '=' are handled correctly.
278
std::stringstream lConnStream (iSQLDBConnStr);
279
std::string kvStr;
280
281
while
(std::getline (lConnStream, kvStr,
' '
)) {
282
if
(kvStr.empty()) {
283
continue
;
284
}
285
286
const
size_t
eqPos = kvStr.find (
'='
);
287
if
(eqPos == std::string::npos) {
288
continue
;
289
}
290
291
const
std::string lKey = kvStr.substr (0, eqPos);
292
const
std::string lVal = kvStr.substr (eqPos + 1);
293
oStrMap[lKey] = lVal;
294
}
295
296
// Validate required fields
297
if
(oStrMap.find (
"dbname"
) == oStrMap.end()) {
298
std::ostringstream errStr;
299
errStr <<
"Error when parsing the PG connection string ('"
300
<< iSQLDBConnStr <<
"'), the 'dbname' value cannot be found"
;
301
OPENTREP_LOG_ERROR
(errStr.str());
302
throw
SQLDatabaseConnectionStringParsingException
(errStr.str());
303
}
304
305
if
(oStrMap.find (
"user"
) == oStrMap.end()) {
306
std::ostringstream errStr;
307
errStr <<
"Error when parsing the PG connection string ('"
308
<< iSQLDBConnStr <<
"'), the 'user' value cannot be found"
;
309
OPENTREP_LOG_ERROR
(errStr.str());
310
throw
SQLDatabaseConnectionStringParsingException
(errStr.str());
311
}
312
313
if
(oStrMap.find (
"password"
) == oStrMap.end()) {
314
std::ostringstream errStr;
315
errStr <<
"Error when parsing the PG connection string ('"
316
<< iSQLDBConnStr <<
"'), the 'password' value cannot be found"
;
317
OPENTREP_LOG_ERROR
(errStr.str());
318
throw
SQLDatabaseConnectionStringParsingException
(errStr.str());
319
}
320
321
return
oStrMap;
322
}
323
324
// //////////////////////////////////////////////////////////////////////
325
SQLDBConnectionString_T
326
buildPGConnectionString
(
const
StringMap_T
& iStringMap,
327
const
DeploymentNumber_T
& iDeploymentNumber) {
328
std::ostringstream oStr;
329
330
const
StringMap_T::const_iterator itDBName = iStringMap.find (
"dbname"
);
331
assert (itDBName != iStringMap.end());
332
oStr <<
"dbname="
<< itDBName->second << iDeploymentNumber;
333
334
const
StringMap_T::const_iterator itDBUser = iStringMap.find (
"user"
);
335
assert (itDBUser != iStringMap.end());
336
oStr <<
" user="
<< itDBUser->second;
337
338
const
StringMap_T::const_iterator itDBPasswd = iStringMap.find (
"password"
);
339
assert (itDBPasswd != iStringMap.end());
340
oStr <<
" password="
<< itDBPasswd->second;
341
342
// Preserve optional host and port
343
const
StringMap_T::const_iterator itHost = iStringMap.find (
"host"
);
344
if
(itHost != iStringMap.end()) {
345
oStr <<
" host="
<< itHost->second;
346
}
347
348
const
StringMap_T::const_iterator itPort = iStringMap.find (
"port"
);
349
if
(itPort != iStringMap.end()) {
350
oStr <<
" port="
<< itPort->second;
351
}
352
353
return
SQLDBConnectionString_T
(oStr.str());
354
}
355
356
// //////////////////////////////////////////////////////////////////////
357
std::string
358
displayPGConnectionString
(
const
StringMap_T
& iStringMap,
359
const
DeploymentNumber_T
& iDeploymentNumber) {
360
std::ostringstream oStr;
361
362
for
(StringMap_T::const_iterator itDBKV = iStringMap.begin();
363
itDBKV != iStringMap.end(); ++itDBKV) {
364
const
std::string& lDBKey = itDBKV->first;
365
const
std::string& lDBValue = itDBKV->second;
366
oStr << lDBKey <<
"="
;
367
oStr << lDBValue;
368
if
(lDBKey ==
"dbname"
369
&& iDeploymentNumber !=
DEFAULT_OPENTREP_DEPLOYMENT_NUMBER_SIZE
) {
370
oStr << iDeploymentNumber;
371
}
372
oStr <<
" "
;
373
}
374
375
return
oStr.str();
376
}
377
378
// //////////////////////////////////////////////////////////////////////
379
std::string
380
parseAndDisplayConnectionString
(
const
DBType
& iDBType,
381
const
std::string& iSQLDBConnStr,
382
const
DeploymentNumber_T
& iDeploymentNumber) {
383
std::ostringstream oStr;
384
385
if
(iDBType ==
DBType::NODB
) {
386
// Do nothing at this stage
387
oStr <<
""
;
388
389
}
else
if
(iDBType ==
DBType::SQLITE3
) {
390
oStr << iSQLDBConnStr << iDeploymentNumber;
391
392
}
else
if
(iDBType ==
DBType::MYSQL
) {
393
// Parse the connection string
394
const
SQLDBConnectionString_T
lSQLDBConnStr (iSQLDBConnStr);
395
const
StringMap_T
& lStrMap =
parseMySQLConnectionString
(lSQLDBConnStr);
396
397
// Re-build the new connection string, taking into account the
398
// deployment number/version
399
const
std::string& lNewSQLDBConnStr =
400
displayMySQLConnectionString
(lStrMap, iDeploymentNumber);
401
oStr << lNewSQLDBConnStr;
402
403
}
else
if
(iDBType ==
DBType::PG
) {
404
// Parse the connection string
405
const
SQLDBConnectionString_T
lSQLDBConnStr (iSQLDBConnStr);
406
const
StringMap_T
& lStrMap =
parsePGConnectionString
(lSQLDBConnStr);
407
408
// Re-build the new connection string, taking into account the
409
// deployment number/version
410
const
std::string& lNewSQLDBConnStr =
411
displayPGConnectionString
(lStrMap, iDeploymentNumber);
412
oStr << lNewSQLDBConnStr;
413
}
414
415
//
416
return
oStr.str();
417
}
418
}
DBType.hpp
Logger.hpp
OPENTREP_LOG_ERROR
#define OPENTREP_LOG_ERROR(iToBeLogged)
Definition
Logger.hpp:24
OPENTREP_exceptions.hpp
Utilities.hpp
OPENTREP::SQLDatabaseConnectionStringParsingException
Definition
OPENTREP_exceptions.hpp:333
OPENTREP
Definition
BasChronometer.cpp:10
OPENTREP::WordList_T
std::list< Word_T > WordList_T
Definition
OPENTREP_Types.hpp:690
OPENTREP::tokeniseStringIntoWordList
void tokeniseStringIntoWordList(const std::string &iPhrase, WordList_T &ioWordList)
Definition
Utilities.cpp:19
OPENTREP::buildMySQLConnectionString
SQLDBConnectionString_T buildMySQLConnectionString(const StringMap_T &iStringMap, const DeploymentNumber_T &iDeploymentNumber)
Definition
Utilities.cpp:215
OPENTREP::createStringFromWordList
std::string createStringFromWordList(const WordList_T &iWordList, const NbOfWords_T iSplitIdx, const bool iFromBeginningFlag)
Definition
Utilities.cpp:43
OPENTREP::displayMySQLConnectionString
std::string displayMySQLConnectionString(const StringMap_T &iStringMap, const DeploymentNumber_T &iDeploymentNumber)
Definition
Utilities.cpp:251
OPENTREP::DEFAULT_OPENTREP_DEPLOYMENT_NUMBER_SIZE
const unsigned short DEFAULT_OPENTREP_DEPLOYMENT_NUMBER_SIZE
OPENTREP::buildPGConnectionString
SQLDBConnectionString_T buildPGConnectionString(const StringMap_T &iStringMap, const DeploymentNumber_T &iDeploymentNumber)
Definition
Utilities.cpp:326
OPENTREP::displayPGConnectionString
std::string displayPGConnectionString(const StringMap_T &iStringMap, const DeploymentNumber_T &iDeploymentNumber)
Definition
Utilities.cpp:358
OPENTREP::parseAndDisplayConnectionString
std::string parseAndDisplayConnectionString(const DBType &iDBType, const std::string &iSQLDBConnStr, const DeploymentNumber_T &iDeploymentNumber)
Definition
Utilities.cpp:380
OPENTREP::parseMySQLConnectionString
StringMap_T parseMySQLConnectionString(const SQLDBConnectionString_T &iSQLDBConnStr)
Definition
Utilities.cpp:107
OPENTREP::DeploymentNumber_T
unsigned short DeploymentNumber_T
Definition
OPENTREP_Types.hpp:108
OPENTREP::parsePGConnectionString
StringMap_T parsePGConnectionString(const SQLDBConnectionString_T &iSQLDBConnStr)
Definition
Utilities.cpp:273
OPENTREP::StringMap_T
std::map< const std::string, std::string > StringMap_T
Definition
Utilities.hpp:43
OPENTREP::NbOfWords_T
unsigned short NbOfWords_T
Definition
OPENTREP_Types.hpp:710
OPENTREP::DBType
Enumeration of database types.
Definition
DBType.hpp:17
OPENTREP::DBType::MYSQL
@ MYSQL
Definition
DBType.hpp:23
OPENTREP::DBType::PG
@ PG
Definition
DBType.hpp:22
OPENTREP::DBType::SQLITE3
@ SQLITE3
Definition
DBType.hpp:21
OPENTREP::DBType::NODB
@ NODB
Definition
DBType.hpp:20
OPENTREP::SQLDBConnectionString_T
Definition
OPENTREP_Types.hpp:56
Generated on
for OpenTREP by
1.17.0