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