OpenTREP Logo  0.07.4
C++ Open Travel Request Parsing Library
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 
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  // //////////////////////////////////////////////////////////////////////
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  assert (isSuccess == true);
142  continue;
143 
144  } else if (lastKey == keyDBUser) {
145  const bool isSuccess =
146  oStrMap.insert (std::make_pair ("user", keyStr)).second;
147  assert (isSuccess == true);
148  continue;
149 
150  } else if (lastKey == keyDBPasswd) {
151  const bool isSuccess =
152  oStrMap.insert (std::make_pair ("password", keyStr)).second;
153  assert (isSuccess == true);
154  continue;
155  }
156  }
157  }
158 
162  // SQL database name
163  const StringMap_T::const_iterator itDBName = oStrMap.find ("db");
164  if (itDBName == oStrMap.end()) {
165  std::ostringstream errStr;
166  errStr << "Error when parsing the SQL database connection string ('"
167  << iSQLDBConnStr << "'), the 'db' value cannot be found";
168  OPENTREP_LOG_ERROR (errStr.str());
169  throw SQLDatabaseConnectionStringParsingException (errStr.str());
170  }
171 
172  // SQL database user
173  const StringMap_T::const_iterator itDBUser = oStrMap.find ("user");
174  if (itDBUser == oStrMap.end()) {
175  std::ostringstream errStr;
176  errStr << "Error when parsing the SQL database connection string ('"
177  << iSQLDBConnStr << "'), the 'user' value cannot be found";
178  OPENTREP_LOG_ERROR (errStr.str());
179  throw SQLDatabaseConnectionStringParsingException (errStr.str());
180  }
181 
182  // SQL database password
183  const StringMap_T::const_iterator itDBPasswd = oStrMap.find ("password");
184  if (itDBPasswd == oStrMap.end()) {
185  std::ostringstream errStr;
186  errStr << "Error when parsing the SQL database connection string ('"
187  << iSQLDBConnStr << "'), the 'password' value cannot be found";
188  OPENTREP_LOG_ERROR (errStr.str());
189  throw SQLDatabaseConnectionStringParsingException (errStr.str());
190  }
191 
192  return oStrMap;
193  }
194 
195  // //////////////////////////////////////////////////////////////////////
196  SQLDBConnectionString_T
198  const DeploymentNumber_T& iDeploymentNumber) {
199  std::ostringstream oStr;
200 
201  // SQL database name
202  const StringMap_T::const_iterator itDBName = iStringMap.find ("db");
203  assert (itDBName != iStringMap.end());
204  const std::string& lDBName = itDBName->second;
205 
206  // SQL database user
207  const StringMap_T::const_iterator itDBUser = iStringMap.find ("user");
208  assert (itDBUser != iStringMap.end());
209  const std::string& lDBUser = itDBUser->second;
210 
211  // SQL database password
212  const StringMap_T::const_iterator itDBPasswd = iStringMap.find ("password");
213  assert (itDBPasswd != iStringMap.end());
214  const std::string& lDBPasswd = itDBPasswd->second;
215 
216  //
217  oStr << "db=" << lDBName;
218  if (lDBName != "mysql") {
219  oStr << iDeploymentNumber;
220  }
221 
222  //
223  oStr << " user=" << lDBUser;
224 
225  //
226  oStr << " password=" << lDBPasswd;
227 
228  return SQLDBConnectionString_T (oStr.str());
229  }
230 
231  // //////////////////////////////////////////////////////////////////////
232  std::string
234  const DeploymentNumber_T& iDeploymentNumber) {
235  std::ostringstream oStr;
236 
237  for (StringMap_T::const_iterator itDBKV = iStringMap.begin();
238  itDBKV != iStringMap.end(); ++itDBKV) {
239  const std::string& lDBKey = itDBKV->first;
240  const std::string& lDBValue = itDBKV->second;
241  oStr << lDBKey << "=";
242  oStr << lDBValue;
243  if (lDBKey == "db" && lDBValue != "mysql"
244  && iDeploymentNumber != DEFAULT_OPENTREP_DEPLOYMENT_NUMBER_SIZE) {
245  oStr << iDeploymentNumber;
246  }
247  oStr << " ";
248  }
249 
250  return oStr.str();
251  }
252 
253  // //////////////////////////////////////////////////////////////////////
254  std::string
256  const std::string& iSQLDBConnStr,
257  const DeploymentNumber_T& iDeploymentNumber) {
258  std::ostringstream oStr;
259 
260  if (iDBType == DBType::NODB) {
261  // Do nothing at this stage
262  oStr << "";
263 
264  } else if (iDBType == DBType::SQLITE3) {
265  oStr << iSQLDBConnStr << iDeploymentNumber;
266 
267  } else if (iDBType == DBType::MYSQL) {
268  // Parse the connection string
269  const SQLDBConnectionString_T lSQLDBConnStr (iSQLDBConnStr);
270  const StringMap_T& lStrMap = parseMySQLConnectionString (lSQLDBConnStr);
271 
272  // Re-build the new connection string, taking into account the
273  // deployment number/version
274  const std::string& lNewSQLDBConnStr =
275  displayMySQLConnectionString (lStrMap, iDeploymentNumber);
276  oStr << lNewSQLDBConnStr;
277  }
278 
279  //
280  return oStr.str();
281  }
282 }
OPENTREP::DBType::MYSQL
Definition: DBType.hpp:22
OPENTREP::DBType::SQLITE3
Definition: DBType.hpp:21
OPENTREP::buildMySQLConnectionString
SQLDBConnectionString_T buildMySQLConnectionString(const StringMap_T &iStringMap, const DeploymentNumber_T &iDeploymentNumber)
Definition: Utilities.cpp:197
OPENTREP::WordList_T
std::list< Word_T > WordList_T
Definition: OPENTREP_Types.hpp:690
OPENTREP::displayMySQLConnectionString
std::string displayMySQLConnectionString(const StringMap_T &iStringMap, const DeploymentNumber_T &iDeploymentNumber)
Definition: Utilities.cpp:233
OPENTREP::StringMap_T
std::map< const std::string, std::string > StringMap_T
Definition: Utilities.hpp:43
OPENTREP::SQLDBConnectionString_T
Definition: OPENTREP_Types.hpp:56
OPENTREP::DBType::NODB
Definition: DBType.hpp:20
OPENTREP::parseAndDisplayConnectionString
std::string parseAndDisplayConnectionString(const DBType &iDBType, const std::string &iSQLDBConnStr, const DeploymentNumber_T &iDeploymentNumber)
Definition: Utilities.cpp:255
OPENTREP::SQLDatabaseConnectionStringParsingException
Definition: OPENTREP_exceptions.hpp:321
OPENTREP
Definition: BasChronometer.cpp:10
OPENTREP::NbOfWords_T
unsigned short NbOfWords_T
Definition: OPENTREP_Types.hpp:710
OPENTREP::createStringFromWordList
std::string createStringFromWordList(const WordList_T &iWordList, const NbOfWords_T iSplitIdx, const bool iFromBeginningFlag)
Definition: Utilities.cpp:43
DBType.hpp
Logger.hpp
OPENTREP::parseMySQLConnectionString
StringMap_T parseMySQLConnectionString(const SQLDBConnectionString_T &iSQLDBConnStr)
Definition: Utilities.cpp:107
OPENTREP_exceptions.hpp
OPENTREP::DeploymentNumber_T
unsigned short DeploymentNumber_T
Definition: OPENTREP_Types.hpp:108
OPENTREP::DEFAULT_OPENTREP_DEPLOYMENT_NUMBER_SIZE
const unsigned short DEFAULT_OPENTREP_DEPLOYMENT_NUMBER_SIZE
OPENTREP::DBType
Enumeration of database types.
Definition: DBType.hpp:17
OPENTREP_LOG_ERROR
#define OPENTREP_LOG_ERROR(iToBeLogged)
Definition: Logger.hpp:24
Utilities.hpp
OPENTREP::tokeniseStringIntoWordList
void tokeniseStringIntoWordList(const std::string &iPhrase, WordList_T &ioWordList)
Definition: Utilities.cpp:19