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 <cstdio>
36 #include <cstring>
37 #include <limits>
38 #include <map>
39 #include <string>
40 #include <vector>
41 
42 // Boost
43 #include <boost/cstdint.hpp>
44 
45 namespace te
46 {
47  namespace common
48  {
49  /*!
50  \brief It converts a short integer value to a string.
51 
52  \return The integer value converted to a string.
53  */
54  inline std::string Convert2String(boost::int16_t value)
55  {
56  char name[std::numeric_limits<boost::int16_t>::digits10 + 2];
57  sprintf(name, "%hd", value);
58  return name;
59  }
60 
61  /*!
62  \brief It converts a unsigned short integer value to a string.
63 
64  \return The integer value converted to a string.
65  */
66  inline std::string Convert2String(boost::uint16_t value)
67  {
68  char name[std::numeric_limits<boost::uint16_t>::digits10 + 2];
69  sprintf(name, "%hu", value);
70  return name;
71  }
72 
73  /*!
74  \brief It converts an integer value to a string.
75 
76  \return The integer value converted to a string.
77  */
78  inline std::string Convert2String(boost::int32_t value)
79  {
80  char name[std::numeric_limits<boost::int32_t>::digits10 + 2];
81  sprintf(name, "%d", value);
82  return name;
83  }
84 
85  /*!
86  \brief It converts an unsigned integer value to a string.
87 
88  \return The unsigned integer value converted to a string.
89  */
90  inline std::string Convert2String(boost::uint32_t value)
91  {
92  char name[std::numeric_limits<boost::uint32_t>::digits10 + 2];
93  sprintf(name, "%u", value);
94  return name;
95  }
96 
97  /*!
98  \brief It converts a long value to a string.
99 
100  \return The long value converted to a string.
101  */
102  inline std::string Convert2String(boost::int64_t value)
103  {
104  char name[std::numeric_limits<boost::int64_t>::digits10 + 2];
105  sprintf(name, "%lli", value);
106  return name;
107  }
108 
109  /*!
110  \brief It converts a unsigned long value to a string.
111 
112  \return The unsigned long value converted to a string.
113  */
114  inline std::string Convert2String(boost::uint64_t value)
115  {
116  char name[std::numeric_limits<boost::uint64_t>::digits10 + 2];
117  sprintf(name, "%llu", value);
118  return name;
119  }
120 
121  /*!
122  \brief It converts a double value to a string.
123 
124  \return The double value converted to a string.
125  */
126  inline std::string Convert2String(float value)
127  {
128  char name[std::numeric_limits<float>::digits10 + std::numeric_limits<float>::digits + 4];
129  sprintf(name, "%f", value);
130  return name;
131  }
132 
133  /*!
134  \brief It converts a double value to a string.
135 
136  \return The double value converted to a string.
137  */
138  inline std::string Convert2String(double value)
139  {
140  char name[std::numeric_limits<double>::digits10 + std::numeric_limits<double>::digits + 4];
141  sprintf(name, "%e", value);
142  return name;
143  }
144 
145  /*!
146  \brief It converts a double value to a string with a precision.
147 
148  \return The double value converted with the given precision to a string.
149 
150  \warning Max allowed precision is 20.
151  */
152  inline std::string Convert2String(double value, int precision)
153  {
154  char name[256];
155  sprintf(name, "%.*f", precision, value );
156  return name;
157  }
158 
159  /*!
160  \brief It converts a string to upper case.
161 
162  \param value The string value to be converted.
163 
164  \return The converted string.
165  */
166  inline std::string Convert2UCase(const std::string& value)
167  {
168  size_t size = value.size();
169 
170  std::string aux(size, ' ');
171 
172  for(size_t i = 0; i < size; ++i)
173  aux[i] = ((value[i] >= 97) && (value[i] <= 122)) ? (value[i] - 32) : value[i];
174 
175  return aux;
176  }
177 
178  /*!
179  \brief It converts a string to upper case in place (it doesn't allocate an auxiliar buffer).
180 
181  \param value The string value to be converted.
182 
183  \return The converted string.
184  */
185  inline void Convert2UCaseInPlace(std::string& value)
186  {
187  size_t size = value.size();
188 
189  for(size_t i = 0; i < size; ++i)
190  value[i] = ((value[i] >= 97) && (value[i] <= 122)) ? (value[i] - 32) : value[i];
191  }
192 
193  /*!
194  \brief It converts a string to lower case.
195 
196  \param value The string value to be converted.
197 
198  \return The converted string.
199  */
200  inline std::string Convert2LCase(const std::string& value)
201  {
202  size_t size = value.size();
203 
204  std::string aux(size, ' ');
205 
206  for(size_t i = 0; i < size; ++i)
207  aux[i] = ((value[i] >= 65) && (value[i] <= 90)) ? (value[i] + 32) : value[i];
208 
209  return aux;
210  }
211 
212  /*!
213  \brief It tokenizes a given string with a delimiter of your own choice.
214 
215  \param str The string to be tokenize.
216  \param tokens The output vector with the tokens.
217  \param delimiters The character delimiters.
218  */
219  inline void Tokenize(const std::string& str,
220  std::vector<std::string>& tokens,
221  const std::string& delimiters = " ")
222  {
223 // skip delimiters at beginning
224  size_t lastPos = str.find_first_not_of(delimiters, 0);
225 
226 // find first "non-delimiter"
227  size_t pos = str.find_first_of(delimiters, lastPos);
228 
229  while((std::string::npos != pos) ||
230  (std::string::npos != lastPos))
231  {
232 // found a token, add it to the vector
233  tokens.push_back(str.substr(lastPos, pos - lastPos));
234 
235 // skip delimiters. Note the "not_of"
236  lastPos = str.find_first_not_of(delimiters, pos);
237 
238 // find next "non-delimiter"
239  pos = str.find_first_of(delimiters, lastPos);
240  }
241  }
242 
243  /*!
244  \brief It extracts a key-value map from a string.
245 
246  \param kvpStr A string with key-value-pairs to be splitted.
247  \param kvp A map to output the pairs: (parameter-name, parameter-value).
248  \param kvpDelimiter The character used to delimit the key-value-pairs.
249  \param kvDelimiter The character used to delimit a key from its value.
250  \param toUpper If true the key will be converted to upper-case. The value case will not be touched.
251 
252  \pre The kvpStr must already be cleaned if necessary (see CleanQueryString function if needed).
253  */
254  inline void ExtractKVP(const std::string& kvpStr,
255  std::map<std::string, std::string>& kvp,
256  const std::string& kvpDelimiter = "&",
257  const std::string& kvDelimiter = "=",
258  bool toUpper = false)
259  {
260  if(kvpStr.empty())
261  return;
262 
263 // split query string
264  std::vector<std::string> tokens;
265 
266  Tokenize(kvpStr, tokens, kvpDelimiter);
267 
268 // for each token let's find a kvp
269  size_t size = tokens.size();
270 
271  for(size_t i = 0; i < size; ++i)
272  {
273  std::vector<std::string> kv;
274 
275  Tokenize(tokens[i], kv, kvDelimiter);
276 
277  //if(kvp.empty())
278  // continue;
279 
280  if(toUpper)
281  Convert2UCaseInPlace(kv[0]);
282 
283  if(!kv[0].empty())
284  kvp[kv[0]] = "";
285 
286  if(kv.size() > 1)
287  kvp[kv[0]] = kv[1];
288 
289  if(kv.size() > 2) // if we have >2 '=' ? concat
290  {
291  std::string aux("");
292 
293  for(size_t k = 1; k < kv.size(); ++k)
294  aux += kv[k] + "=";
295 
296  size_t pos = aux.size() - 1;
297 
298  kvp[kv[0]] = aux.erase(pos, 1);
299  }
300  }
301  }
302 
303  /*!
304  \brief It converts the C++ string to a C-string.
305 
306  \param s The C++ string to be converted to a char*.
307 
308  \return A C-string. The caller will take the ownership of the returned string and must use 'delete [] pointer' to release it when necessary.
309  */
310  inline char* CreateCString(const std::string& s)
311  {
312  char* cs = new char[s.length() + 1];
313  strncpy(cs, s.c_str(), s.length() + 1);
314  return cs;
315  }
316 
317  /*!
318  \brief It converts the C++ vector of string pointers to a C array os strings.
319 
320  \param vs The C++ vector of string pointers to be converted to a char**.
321 
322  \return A C array os strings NULL terminated. The caller will take the ownership of the returned array.
323  */
324  inline char** CreateCStringArray(const std::vector<std::string*>& vs)
325  {
326  std::size_t ns = vs.size() ;
327 
328  char** as = new char*[ns + 1];
329 
330  as[ns] = 0;
331 
332  for(std::size_t i = 0; i< ns; ++i)
333  {
334  as[i] = new char[vs[i]->length() + 1];
335 
336  strncpy(as[i], vs[i]->c_str(), vs[i]->length() + 1);
337  }
338 
339  return as;
340  }
341 
342  /*!
343  \brief It converts the C++ vector of string pointers to a C array os strings.
344 
345  \param vs The C++ vector of string pointers to be converted to a char**.
346 
347  \return A C array os strings NULL terminated. The caller will take the ownership of the returned array.
348  */
349  inline char** CreateCStringArray(const std::vector<std::string>& vs)
350  {
351  std::size_t ns = vs.size() ;
352 
353  char** as = new char*[ns + 1];
354 
355  as[ns] = 0;
356 
357  for(std::size_t i = 0; i< ns; ++i)
358  {
359  as[i] = new char[vs[i].length() + 1];
360 
361  strncpy(as[i], vs[i].c_str(), vs[i].length() + 1);
362  }
363 
364  return as;
365  }
366 
367  /*!
368  \brief It build two string vectors with special upper case characters and the his respective normal characters
369 
370  \param vecUpperIn The vector with upper case special characters.
371  \param vecUpperOut The vector with upper case normal characters.
372 
373  */
374  TECOMMONEXPORT void GetAccentuatedUpperVector(std::vector<std::string> & vecUpperIn, std::vector<std::string> & vecUpperOut);
375 
376  /*!
377  \brief It build two string vectors with special lower case characters and the his respective normal characters
378 
379  \param vecUpperIn The vector with lower case special characters.
380  \param vecUpperOut The vector with lower case normal characters.
381 
382  */
383  TECOMMONEXPORT void GetAccentuatedLowerVector(std::vector<std::string> & vecLowerIn, std::vector<std::string> & vecLowerOut);
384 
385  /*!
386  \brief It build two string vectors with special characters and the his respective normal characters
387 
388  \param especialIn The vector with special characters.
389  \param especialOut The vector with normal characters.
390 
391  */
392  TECOMMONEXPORT void GetEspecialCharsFixVector(std::vector<std::string> & especialIn, std::vector<std::string> & especialOut);
393 
394  /*!
395  \brief It replace special characters of a string.
396 
397  \param str The string that will be verify.
398  \param changed Boolean that records whether there was a change.
399 
400  */
401  TECOMMONEXPORT std::string ReplaceSpecialChars(const std::string& str, bool& changed);
402 
403  } // end namespace common
404 } // end namespace te
405 
406 #endif // __TERRALIB_COMMON_INTERNAL_STRINGUTILS_H
407 
std::string Convert2LCase(const std::string &value)
It converts a string to lower case.
Definition: StringUtils.h:200
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:254
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
Definition: StringUtils.h:166
TECOMMONEXPORT void GetAccentuatedUpperVector(std::vector< std::string > &vecUpperIn, std::vector< std::string > &vecUpperOut)
It build two string vectors with special upper case characters and the his respective normal characte...
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:219
TECOMMONEXPORT std::string ReplaceSpecialChars(const std::string &str, bool &changed)
It replace special characters of a string.
void Convert2UCaseInPlace(std::string &value)
It converts a string to upper case in place (it doesn't allocate an auxiliar buffer).
Definition: StringUtils.h:185
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 void GetAccentuatedLowerVector(std::vector< std::string > &vecLowerIn, std::vector< std::string > &vecLowerOut)
It build two string vectors with special lower case characters and the his respective normal characte...
char * CreateCString(const std::string &s)
It converts the C++ string to a C-string.
Definition: StringUtils.h:310
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:54
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:324
TECOMMONEXPORT void GetEspecialCharsFixVector(std::vector< std::string > &especialIn, std::vector< std::string > &especialOut)
It build two string vectors with special characters and the his respective normal characters...