StringUtils.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/common/StringUtils.h
22 
23  \brief Utility functions for dealing with strings.
24 
25  \ingroup common
26 */
27 
28 #ifndef __TERRALIB_COMMON_INTERNAL_STRINGUTILS_H
29 #define __TERRALIB_COMMON_INTERNAL_STRINGUTILS_H
30 
31 // TerraLib
32 #include "Config.h"
33 
34 // STL
35 #include <cinttypes>
36 #include <cstdio>
37 #include <cstring>
38 #include <limits>
39 #include <map>
40 #include <string>
41 #include <vector>
42 
43 // Boost
44 #include <boost/cstdint.hpp>
45 
46 
47 namespace te
48 {
49  namespace common
50  {
51  /*!
52  \brief It converts a short integer value to a string.
53 
54  \return The integer value converted to a string.
55  */
56  inline std::string Convert2String(boost::int16_t value)
57  {
58  char name[std::numeric_limits<boost::int16_t>::digits10 + 2];
59  sprintf(name, "%hd", value);
60  return name;
61  }
62 
63  /*!
64  \brief It converts a unsigned short integer value to a string.
65 
66  \return The integer value converted to a string.
67  */
68  inline std::string Convert2String(boost::uint16_t value)
69  {
70  char name[std::numeric_limits<boost::uint16_t>::digits10 + 2];
71  sprintf(name, "%hu", value);
72  return name;
73  }
74 
75  /*!
76  \brief It converts an integer value to a string.
77 
78  \return The integer value converted to a string.
79  */
80  inline std::string Convert2String(boost::int32_t value)
81  {
82  char name[std::numeric_limits<boost::int32_t>::digits10 + 2];
83  sprintf(name, "%d", value);
84  return name;
85  }
86 
87  /*!
88  \brief It converts an unsigned integer value to a string.
89 
90  \return The unsigned integer value converted to a string.
91  */
92  inline std::string Convert2String(boost::uint32_t value)
93  {
94  char name[std::numeric_limits<boost::uint32_t>::digits10 + 2];
95  sprintf(name, "%u", value);
96  return name;
97  }
98 
99  /*!
100  \brief It converts a long value to a string.
101 
102  \return The long value converted to a string.
103  */
104  inline std::string Convert2String(boost::int64_t value)
105  {
106  char name[std::numeric_limits<boost::int64_t>::digits10 + 2];
107  sprintf(name, "%" PRId64 "", value);
108  return name;
109  }
110 
111  /*!
112  \brief It converts a unsigned long value to a string.
113 
114  \return The unsigned long value converted to a string.
115  */
116  inline std::string Convert2String(boost::uint64_t value)
117  {
118  char name[std::numeric_limits<boost::uint64_t>::digits10 + 2];
119  sprintf(name, "%" PRIu64 "", value);
120  return name;
121  }
122 
123  /*!
124  \brief It converts a double value to a string.
125 
126  \return The double value converted to a string.
127  */
128  inline std::string Convert2String(float value)
129  {
130  char name[std::numeric_limits<float>::digits10 + std::numeric_limits<float>::digits + 4];
131  sprintf(name, "%f", value);
132  return name;
133  }
134 
135  /*!
136  \brief It converts a double value to a string.
137 
138  \return The double value converted to a string.
139  */
140  inline std::string Convert2String(double value)
141  {
142  char name[std::numeric_limits<double>::digits10 + std::numeric_limits<double>::digits + 4];
143  sprintf(name, "%e", value);
144  return name;
145  }
146 
147  /*!
148  \brief It converts a double value to a string with a precision.
149 
150  \return The double value converted with the given precision to a string.
151 
152  \warning Max allowed precision is 20.
153  */
154  inline std::string Convert2String(double value, int precision)
155  {
156  char name[256];
157  sprintf(name, "%.*f", precision, value );
158  return name;
159  }
160 
161  /*!
162  \brief It converts a string to upper case.
163 
164  \param value The string value to be converted.
165 
166  \return The converted string.
167  */
168  inline std::string Convert2UCase(const std::string& value)
169  {
170  size_t size = value.size();
171 
172  std::string aux(size, ' ');
173 
174  for(size_t i = 0; i < size; ++i)
175  aux[i] = ((value[i] >= 97) && (value[i] <= 122)) ? (value[i] - 32) : value[i];
176 
177  return aux;
178  }
179 
180  /*!
181  \brief It converts a string to upper case in place (it doesn't allocate an auxiliar buffer).
182 
183  \param value The string value to be converted.
184 
185  \return The converted string.
186  */
187  inline void Convert2UCaseInPlace(std::string& value)
188  {
189  size_t size = value.size();
190 
191  for(size_t i = 0; i < size; ++i)
192  value[i] = ((value[i] >= 97) && (value[i] <= 122)) ? (value[i] - 32) : value[i];
193  }
194 
195  /*!
196  \brief It converts a string to lower case.
197 
198  \param value The string value to be converted.
199 
200  \return The converted string.
201  */
202  inline std::string Convert2LCase(const std::string& value)
203  {
204  size_t size = value.size();
205 
206  std::string aux(size, ' ');
207 
208  for(size_t i = 0; i < size; ++i)
209  aux[i] = ((value[i] >= 65) && (value[i] <= 90)) ? (value[i] + 32) : value[i];
210 
211  return aux;
212  }
213 
214  /*!
215  \brief It tokenizes a given string with a delimiter of your own choice.
216 
217  \param str The string to be tokenize.
218  \param tokens The output vector with the tokens.
219  \param delimiters The character delimiters.
220  */
221  inline void Tokenize(const std::string& str,
222  std::vector<std::string>& tokens,
223  const std::string& delimiters = " ")
224  {
225 // skip delimiters at beginning
226  size_t lastPos = str.find_first_not_of(delimiters, 0);
227 
228 // find first "non-delimiter"
229  size_t pos = str.find_first_of(delimiters, lastPos);
230 
231  while((std::string::npos != pos) ||
232  (std::string::npos != lastPos))
233  {
234 // found a token, add it to the vector
235  tokens.push_back(str.substr(lastPos, pos - lastPos));
236 
237 // skip delimiters. Note the "not_of"
238  lastPos = str.find_first_not_of(delimiters, pos);
239 
240 // find next "non-delimiter"
241  pos = str.find_first_of(delimiters, lastPos);
242  }
243  }
244 
245  /*!
246  \brief It extracts a key-value map from a string.
247 
248  \param kvpStr A string with key-value-pairs to be splitted.
249  \param kvp A map to output the pairs: (parameter-name, parameter-value).
250  \param kvpDelimiter The character used to delimit the key-value-pairs.
251  \param kvDelimiter The character used to delimit a key from its value.
252  \param toUpper If true the key will be converted to upper-case. The value case will not be touched.
253 
254  \pre The kvpStr must already be cleaned if necessary (see CleanQueryString function if needed).
255  */
256  inline void ExtractKVP(const std::string& kvpStr,
257  std::map<std::string, std::string>& kvp,
258  const std::string& kvpDelimiter = "&",
259  const std::string& kvDelimiter = "=",
260  bool toUpper = false)
261  {
262  if(kvpStr.empty())
263  return;
264 
265 // split query string
266  std::vector<std::string> tokens;
267 
268  Tokenize(kvpStr, tokens, kvpDelimiter);
269 
270 // for each token let's find a kvp
271  size_t size = tokens.size();
272 
273  for(size_t i = 0; i < size; ++i)
274  {
275  std::vector<std::string> kv;
276 
277  Tokenize(tokens[i], kv, kvDelimiter);
278 
279  //if(kvp.empty())
280  // continue;
281 
282  if(toUpper)
283  Convert2UCaseInPlace(kv[0]);
284 
285  if(!kv[0].empty())
286  kvp[kv[0]] = "";
287 
288  if(kv.size() > 1)
289  kvp[kv[0]] = kv[1];
290 
291  if(kv.size() > 2) // if we have >2 '=' ? concat
292  {
293  std::string aux("");
294 
295  for(size_t k = 1; k < kv.size(); ++k)
296  aux += kv[k] + "=";
297 
298  size_t pos = aux.size() - 1;
299 
300  kvp[kv[0]] = aux.erase(pos, 1);
301  }
302  }
303  }
304 
305  /*!
306  \brief It converts the C++ string to a C-string.
307 
308  \param s The C++ string to be converted to a char*.
309 
310  \return A C-string. The caller will take the ownership of the returned string and must use 'delete [] pointer' to release it when necessary.
311  */
312  inline char* CreateCString(const std::string& s)
313  {
314  char* cs = new char[s.length() + 1];
315  strncpy(cs, s.c_str(), s.length() + 1);
316  return cs;
317  }
318 
319  /*!
320  \brief It converts the C++ vector of string pointers to a C array os strings.
321 
322  \param vs The C++ vector of string pointers to be converted to a char**.
323 
324  \return A C array os strings NULL terminated. The caller will take the ownership of the returned array.
325  */
326  inline char** CreateCStringArray(const std::vector<std::string*>& vs)
327  {
328  std::size_t ns = vs.size() ;
329 
330  char** as = new char*[ns + 1];
331 
332  as[ns] = 0;
333 
334  for(std::size_t i = 0; i< ns; ++i)
335  {
336  as[i] = new char[vs[i]->length() + 1];
337 
338  strncpy(as[i], vs[i]->c_str(), vs[i]->length() + 1);
339  }
340 
341  return as;
342  }
343 
344  /*!
345  \brief It converts the C++ vector of string pointers to a C array os strings.
346 
347  \param vs The C++ vector of string pointers to be converted to a char**.
348 
349  \return A C array os strings NULL terminated. The caller will take the ownership of the returned array.
350  */
351  inline char** CreateCStringArray(const std::vector<std::string>& vs)
352  {
353  std::size_t ns = vs.size() ;
354 
355  char** as = new char*[ns + 1];
356 
357  as[ns] = 0;
358 
359  for(std::size_t i = 0; i< ns; ++i)
360  {
361  as[i] = new char[vs[i].length() + 1];
362 
363  strncpy(as[i], vs[i].c_str(), vs[i].length() + 1);
364  }
365 
366  return as;
367  }
368 
369  /*!
370  \brief It replace special characters of a string.
371 
372  \param str The string that will be verify.
373  \param changed Boolean that records whether there was a change.
374 
375  */
376  inline std::string ReplaceSpecialChars(const std::string& str, bool& changed)
377  {
378  std::string result;
379 
380  for (std::size_t i = 0; i < str.size(); ++i)
381  {
382  unsigned char c = (unsigned char)str.at(i);
383 
384  if (c <= 127)
385  {
386  if ((c == 95) ||
387  (c >= 48 && c <= 57) ||
388  (c >= 65 && c <= 90) ||
389  (c >= 97 && c <= 122))
390  {
391  result += c;
392  }
393  else if (c == 32)
394  {
395  unsigned char add = 95;
396  result += add;
397  }
398  }
399  else
400  {
401  unsigned char cAux = (unsigned char)str.at(i + 1);
402 
403  if (c == 195)
404  {
405  int value = -1;
406 
407  if (cAux >= 128 && cAux <= 133)
408  value = 65;
409  else if (cAux >= 136 && cAux <= 139)
410  value = 69;
411  else if (cAux >= 140 && cAux <= 143)
412  value = 73;
413  else if (cAux >= 146 && cAux <= 150)
414  value = 79;
415  else if (cAux >= 153 && cAux <= 156)
416  value = 85;
417  else if (cAux >= 160 && cAux <= 165)
418  value = 97;
419  else if (cAux >= 168 && cAux <= 171)
420  value = 101;
421  else if (cAux >= 172 && cAux <= 175)
422  value = 105;
423  else if (cAux >= 178 && cAux <= 182)
424  value = 111;
425  else if (cAux >= 185 && cAux <= 188)
426  value = 117;
427 
428  if (value >= 0)
429  {
430  unsigned char add = value;
431  result += add;
432  }
433  }
434 
435  ++i;
436 
437  }
438 
439  }
440 
441  changed = false;
442 
443  if (result != str)
444  changed = true;
445 
446  return result;
447  }
448 
449  TECOMMONEXPORT std::vector<std::string> SplitString(const std::string& str, const char& delimiter);
450 
451  } // end namespace common
452 } // end namespace te
453 
454 #endif // __TERRALIB_COMMON_INTERNAL_STRINGUTILS_H
455 
std::string Convert2LCase(const std::string &value)
It converts a string to lower case.
Definition: StringUtils.h:202
Configuration flags for the TerraLib Common Runtime module.
void ExtractKVP(const std::string &kvpStr, std::map< std::string, std::string > &kvp, const std::string &kvpDelimiter="&", const std::string &kvDelimiter="=", bool toUpper=false)
It extracts a key-value map from a string.
Definition: StringUtils.h:256
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
Definition: StringUtils.h:168
std::string ReplaceSpecialChars(const std::string &str, bool &changed)
It replace special characters of a string.
Definition: StringUtils.h:376
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
It tokenizes a given string with a delimiter of your own choice.
Definition: StringUtils.h:221
void Convert2UCaseInPlace(std::string &value)
It converts a string to upper case in place (it doesn't allocate an auxiliar buffer).
Definition: StringUtils.h:187
URI C++ Library.
#define TECOMMONEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:65
TECOMMONEXPORT std::vector< std::string > SplitString(const std::string &str, const char &delimiter)
char * CreateCString(const std::string &s)
It converts the C++ string to a C-string.
Definition: StringUtils.h:312
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:56
char ** CreateCStringArray(const std::vector< std::string * > &vs)
It converts the C++ vector of string pointers to a C array os strings.
Definition: StringUtils.h:326