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