OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
string_util.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef BASE_STRING_UTIL_H__
6 #define BASE_STRING_UTIL_H__
7 
8 #include <string>
9 #include <sstream>
10 #include <vector>
11 
12 #include <boost/algorithm/string.hpp>
13 #include <boost/uuid/nil_generator.hpp>
14 #include <boost/uuid/uuid.hpp>
15 #include <boost/uuid/uuid_io.hpp>
16 
17 // Writes a number into a string
18 template <typename NumberType>
19 static inline const std::string integerToString(const NumberType &num) {
20  std::stringstream ss;
21  ss << num;
22  return ss.str();
23 }
24 
25 // int8_t must be handled specially because std::stringstream sees
26 // int8_t as a text type instead of an integer type
27 template <>
28 inline const std::string integerToString<>(const int8_t &num) {
29  std::stringstream ss;
30  ss << (int16_t)num;
31  return ss.str();
32 }
33 
34 // uint8_t must be handled specially because std::stringstream sees
35 // uint8_t as a text type instead of an integer type
36 template <>
37 inline const std::string integerToString<>(const uint8_t &num) {
38  std::stringstream ss;
39  ss << (uint16_t)num;
40  return ss.str();
41 }
42 
43 // Writes a number into a string as hex
44 template <typename NumberType>
45 static inline const std::string integerToHexString(const NumberType &num) {
46  std::stringstream ss;
47  ss << std::hex << num;
48  return ss.str();
49 }
50 
51 // signed int8 must be handled specially because std::stringstream sees
52 // int8_t as a text type instead of an integer type
53 template <>
54 inline const std::string integerToHexString<>(const int8_t &num) {
55  std::stringstream ss;
56  ss << std::hex << (int16_t)num;
57  return ss.str();
58 }
59 
60 // unsigned int8 must be handled specially because std::stringstream sees
61 // u_int8_t as a text type instead of an integer type
62 template <>
63 inline const std::string integerToHexString<>(const uint8_t &num) {
64  std::stringstream ss;
65  ss << std::hex << (uint16_t)num;
66  return ss.str();
67 }
68 
69 // Converts string into a number
70 template <typename NumberType>
71 inline bool stringToInteger(const std::string& str, NumberType &num) {
72  char *endptr;
73  num = strtoul(str.c_str(), &endptr, 10);
74  return endptr[0] == '\0';
75 }
76 
77 template <typename NumberType>
78 inline bool stringToLongLong(const std::string& str, NumberType &num) {
79  char *endptr;
80  num = strtoull(str.c_str(), &endptr, 10);
81  return endptr[0] == '\0';
82 }
83 
84 template <>
85 inline bool stringToInteger<>(const std::string& str, int64_t &num) {
86  return stringToLongLong(str, num);
87 }
88 
89 template <>
90 inline bool stringToInteger<>(const std::string& str, uint64_t &num) {
91  return stringToLongLong(str, num);
92 }
93 
94 template <>
95 inline bool stringToInteger<>(const std::string& str, double &num) {
96  char *endptr;
97  num = strtod(str.c_str(), &endptr);
98  return endptr[0] == '\0';
99 }
100 
101 //
102 // Split a the initial part of the string based on 'seperator' characters
103 // and return the resultant list of tokens after converting each token into
104 // NumberType elements
105 //
106 template<typename NumberType>
107 static inline bool stringToIntegerList(std::string input,
108  std::string seperator,
109  std::vector<NumberType> &entries) {
110  std::vector<std::string> tokens;
111 
112  boost::split(tokens, input, boost::is_any_of(seperator),
113  boost::token_compress_on);
114 
115  if (!tokens.size()) {
116  return false;
117  }
118 
119  std::vector<std::string>::iterator iter;
120 
121  for (iter = tokens.begin(); iter != tokens.end(); iter++) {
122  std::stringstream ss(*iter);
123  NumberType value;
124  ss >> value;
125 
126  //
127  // Bail if there is an error during the conversion.
128  //
129  if (ss.fail()) {
130  return false;
131  }
132  entries.push_back(value);
133  }
134 
135  return true;
136 }
137 
138 static inline std::string UuidToString(const boost::uuids::uuid &id)
139 {
140  std::stringstream uuidstring;
141  uuidstring << id;
142  return uuidstring.str();
143 }
144 
145 static inline boost::uuids::uuid StringToUuid(const std::string &str)
146 {
147  boost::uuids::uuid u = boost::uuids::nil_uuid();
148  std::stringstream uuidstring(str);
149  uuidstring >> u;
150  return u;
151 }
152 
153 static inline std::string BoolToString(const bool &val) {
154  if (val) {
155  return "true";
156  } else {
157  return "false";
158  }
159 }
160 
161 static inline bool StringToBool(const std::string &in_string) {
162  if (in_string.compare("true") == 0) {
163  return true;
164  }
165  if (in_string.compare("false") == 0) {
166  return false;
167  }
168  return false;
169 }
170 
171 #endif // BASE_STRING_UTIL_H__
static const std::string integerToHexString(const NumberType &num)
Definition: string_util.h:45
static boost::uuids::uuid StringToUuid(const std::string &str)
Definition: string_util.h:145
bool stringToInteger(const std::string &str, NumberType &num)
Definition: string_util.h:71
bool stringToLongLong(const std::string &str, NumberType &num)
Definition: string_util.h:78
boost::uuids::uuid uuid
static std::string UuidToString(const boost::uuids::uuid &id)
Definition: string_util.h:138
static const std::string integerToString(const NumberType &num)
Definition: string_util.h:19
static bool stringToIntegerList(std::string input, std::string seperator, std::vector< NumberType > &entries)
Definition: string_util.h:107
static bool StringToBool(const std::string &in_string)
Definition: string_util.h:161
static std::string BoolToString(const bool &val)
Definition: string_util.h:153