OpenTREP Logo  0.07.18
C++ Open Travel Request Parsing Library
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
12#include <opentrep/DBType.hpp>
15
16namespace 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 // //////////////////////////////////////////////////////////////////////
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());
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());
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());
208 }
209
210 return oStrMap;
211 }
212
213 // //////////////////////////////////////////////////////////////////////
214 SQLDBConnectionString_T
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
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 std::string
274 const std::string& iSQLDBConnStr,
275 const DeploymentNumber_T& iDeploymentNumber) {
276 std::ostringstream oStr;
277
278 if (iDBType == DBType::NODB) {
279 // Do nothing at this stage
280 oStr << "";
281
282 } else if (iDBType == DBType::SQLITE3) {
283 oStr << iSQLDBConnStr << iDeploymentNumber;
284
285 } else if (iDBType == DBType::MYSQL) {
286 // Parse the connection string
287 const SQLDBConnectionString_T lSQLDBConnStr (iSQLDBConnStr);
288 const StringMap_T& lStrMap = parseMySQLConnectionString (lSQLDBConnStr);
289
290 // Re-build the new connection string, taking into account the
291 // deployment number/version
292 const std::string& lNewSQLDBConnStr =
293 displayMySQLConnectionString (lStrMap, iDeploymentNumber);
294 oStr << lNewSQLDBConnStr;
295 }
296
297 //
298 return oStr.str();
299 }
300}
#define OPENTREP_LOG_ERROR(iToBeLogged)
Definition Logger.hpp:24
std::list< Word_T > WordList_T
void tokeniseStringIntoWordList(const std::string &iPhrase, WordList_T &ioWordList)
Definition Utilities.cpp:19
SQLDBConnectionString_T buildMySQLConnectionString(const StringMap_T &iStringMap, const DeploymentNumber_T &iDeploymentNumber)
std::string createStringFromWordList(const WordList_T &iWordList, const NbOfWords_T iSplitIdx, const bool iFromBeginningFlag)
Definition Utilities.cpp:43
std::string displayMySQLConnectionString(const StringMap_T &iStringMap, const DeploymentNumber_T &iDeploymentNumber)
const unsigned short DEFAULT_OPENTREP_DEPLOYMENT_NUMBER_SIZE
std::string parseAndDisplayConnectionString(const DBType &iDBType, const std::string &iSQLDBConnStr, const DeploymentNumber_T &iDeploymentNumber)
StringMap_T parseMySQLConnectionString(const SQLDBConnectionString_T &iSQLDBConnStr)
unsigned short DeploymentNumber_T
std::map< const std::string, std::string > StringMap_T
Definition Utilities.hpp:43
unsigned short NbOfWords_T
Enumeration of database types.
Definition DBType.hpp:17