radarlib  1.4.4
string.hpp
Go to the documentation of this file.
1 /*
2  * Radar Library
3  *
4  * Copyright (C) 2009-2010 ARPA-SIM <urpsim@smr.arpa.emr.it>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Author: Guido Billi <guidobilli@gmail.com>
21  */
22 
23 
28 #ifndef __RADAR_STRING_HPP__
29 #define __RADAR_STRING_HPP__
30 
31 #include <cstring>
32 #include <string>
33 #include <sstream>
34 #include <vector>
35 #include <utility>
36 #include <stdexcept>
37 #include <ctime>
38 
39 #include <radarlib/defs.h>
40 
41 #if defined(WIN32)
42 #define snprintf _snprintf
43 #endif
44 
45 namespace Radar {
46 
49 namespace stringutils {
50 
51 /*===========================================================================*/
52 /* SPLIT */
53 /*===========================================================================*/
54 
62 RADAR_API void split (const std::string& str, std::string& first, std::string& second, const char separator = ',');
71 RADAR_API void split (const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters = " ");
72 
73 /*===========================================================================*/
74 /* TRIM */
75 /*===========================================================================*/
76 
84 RADAR_API std::string& trimleft (std::string& str);
92 RADAR_API std::string trimleft (const std::string& str);
100 RADAR_API std::string& trimright (std::string& str);
108 RADAR_API std::string trimright (const std::string& str);
116 RADAR_API std::string& trim (std::string& str);
124 RADAR_API std::string trim (const std::string& str);
125 
126 /*===========================================================================*/
127 /* TO STRING */
128 /*===========================================================================*/
129 
139 template<class T> std::string toString(const T& value)
140 {
141  std::ostringstream stream; stream << std::fixed << value; return stream.str();
142 }
143 
151 RADAR_API std::string toString(bool val);
159 RADAR_API std::string toString(int val);
167 RADAR_API std::string toString(long val);
175 RADAR_API std::string toString(float val);
183 RADAR_API std::string toString(double val);
191 RADAR_API std::string toString(size_t val);
192 
204 template<class T> std::string toString(const std::vector<T>& val, const char* sep = ",")
205 {
206  std::ostringstream ss;
207  ss << std::fixed;
208  for (size_t i=0; i<val.size(); i++)
209  {
210  if (i)
211  ss << sep;
212  ss << val[i];
213  }
214  return ss.str();
215 }
216 
227 RADAR_API std::string toString(const std::vector<bool>& val, const char* sep = ",");
238 RADAR_API std::string toString(const std::vector<int>& val, const char* sep = ",");
249 RADAR_API std::string toString(const std::vector<long>& val, const char* sep = ",");
260 RADAR_API std::string toString(const std::vector<float>& val, const char* sep = ",");
271 RADAR_API std::string toString(const std::vector<double>& val, const char* sep = ",");
282 template <class T> std::string toString(const std::vector<std::pair<T,T> > value, const char* sep = ",")
283 {
284  std::ostringstream ss;
285  for (size_t i=0; i<value.size(); i++)
286  {
287  if (i)
288  ss << sep;
289  ss << toString<T>(value[i].first) << ":" << toString<T>(value[i].second);
290  }
291  return ss.str();
292 }
293 
294 /*===========================================================================*/
295 /* PARSE */
296 /*===========================================================================*/
297 
308 template <class T> static T parse(const std::string& str, const std::string& typestr)
309 {
310  if (!str.empty())
311  {
312  T result;
313  std::istringstream ss(str);
314  if ((ss >> result))
315  return result;
316  }
317  throw std::invalid_argument("'" + str + "' is not a valid " + typestr + " value");
318 }
319 
327 RADAR_API bool parseBool (const std::string& str) throw(std::invalid_argument);
335 RADAR_API int parseInt (const std::string& str) throw(std::invalid_argument);
343 RADAR_API float parseFloat (const std::string& str) throw(std::invalid_argument);
351 RADAR_API double parseDouble (const std::string& str) throw(std::invalid_argument);
352 
364 RADAR_API void parseSeq (const std::string& str, std::vector<bool>& val, const char* sep = ",", const bool allowEmptyStr = true);
376 RADAR_API void parseSeq (const std::string& str, std::vector<int>& val, const char* sep = ",", const bool allowEmptyStr = true);
388 RADAR_API void parseSeq (const std::string& str, std::vector<double>& val, const char* sep = ",", const bool allowEmptyStr = true);
400 RADAR_API void parseSeq (const std::string& str, std::vector<std::string>& val, const char* sep = ",", const bool allowEmptyStr = true);
401 
402 /*===========================================================================*/
403 
410 RADAR_API bool isInt (const std::string& str);
411 
412 /*===========================================================================*/
413 
414 } }
415 
416 
417 
418 #endif
419 
420 
421 
422 
423 
424 
425 
426 
427 
428 
429 
430 
431 
432 
433 
434 
435 
436 
437 
438 
439 
440 
441 
442 
443 
444 
445 
446 
447 
448 
449 
450 
451 
452 
453 
454 
455 
456 
457 
458 
459 
460 
461 
462 
463 
464 
465 
466 
467 
468 
469 
470 
471 
472 
473 
double parseDouble(const std::string &s)
Parse a std::string to a double value.
Definition: string.cpp:156
std::string & trimright(std::string &s)
removes all spaces to the right of a string
Definition: string.cpp:72
void split(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters)
Splits a string into substrings using the chacatars of a given string as tokens separators.
Definition: string.cpp:34
void parseSeq(const std::string &s, std::vector< bool > &val, const char *sep, const bool allowEmptyStr)
Parse a string sequence of boolean values to a std::vector.
Definition: string.cpp:158
Internal library macros.
std::string toString(bool value)
Convert a boolean value to its string rapresentation (0/1).
Definition: string.cpp:109
static T parse(const std::string &str, const std::string &typestr)
Parse a std::string to a given type value.
Definition: string.hpp:308
std::string & trim(std::string &s)
remove all space to the left and the right of a string
Definition: string.cpp:91
int parseInt(const std::string &s)
Parse a std::string to an int value.
Definition: string.cpp:154
float parseFloat(const std::string &s)
Parse a std::string to a float value.
Definition: string.cpp:155
std::string & trimleft(std::string &s)
removes all spaces to the left of a string
Definition: string.cpp:55
bool isInt(const std::string &s)
Check if the string is a number.
Definition: string.cpp:128
bool parseBool(const std::string &s)
Parse a std::string to a boolean value.
Definition: string.cpp:135