Line data Source code
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 1508395 : static inline const std::string integerToString(const NumberType &num) { 21 1508395 : std::stringstream ss; 22 1508774 : ss << num; 23 3017437 : return ss.str(); 24 1508763 : } 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 0 : inline const std::string integerToString<>(const int8_t &num) { 30 0 : std::stringstream ss; 31 0 : ss << (int16_t)num; 32 0 : return ss.str(); 33 0 : } 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 39407 : inline const std::string integerToString<>(const uint8_t &num) { 39 39407 : std::stringstream ss; 40 39407 : ss << (uint16_t)num; 41 78814 : return ss.str(); 42 39407 : } 43 : 44 : // Writes a number into a string as hex 45 : template <typename NumberType> 46 40869 : static inline const std::string integerToHexString(const NumberType &num) { 47 40869 : std::stringstream ss; 48 40869 : ss << std::hex << num; 49 81738 : return ss.str(); 50 40869 : } 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 1647 : inline bool stringToInteger(const std::string& str, NumberType &num) { 73 : char *endptr; 74 1647 : num = strtoul(str.c_str(), &endptr, 10); 75 1647 : return endptr[0] == '\0'; 76 : } 77 : 78 : template <typename NumberType> 79 5278 : inline bool stringToLongLong(const std::string& str, NumberType &num) { 80 : char *endptr; 81 5278 : num = strtoull(str.c_str(), &endptr, 10); 82 5278 : return endptr[0] == '\0'; 83 : } 84 : 85 : template <> 86 50 : inline bool stringToInteger<>(const std::string& str, int64_t &num) { 87 50 : return stringToLongLong(str, num); 88 : } 89 : 90 : template <> 91 5228 : inline bool stringToInteger<>(const std::string& str, uint64_t &num) { 92 5228 : return stringToLongLong(str, num); 93 : } 94 : 95 : template <> 96 8 : inline bool stringToInteger<>(const std::string& str, double &num) { 97 : char *endptr; 98 8 : num = strtod(str.c_str(), &endptr); 99 8 : 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 2 : static inline bool stringToIntegerList(std::string input, 109 : std::string seperator, 110 : std::vector<NumberType> &entries) { 111 2 : std::vector<std::string> tokens; 112 : 113 2 : boost::split(tokens, input, boost::is_any_of(seperator), 114 : boost::token_compress_on); 115 : 116 2 : if (!tokens.size()) { 117 0 : return false; 118 : } 119 : 120 2 : std::vector<std::string>::iterator iter; 121 : 122 10 : for (iter = tokens.begin(); iter != tokens.end(); iter++) { 123 4 : std::stringstream ss(*iter); 124 : NumberType value; 125 4 : ss >> value; 126 : 127 : // 128 : // Bail if there is an error during the conversion. 129 : // 130 4 : if (ss.fail()) { 131 0 : return false; 132 : } 133 4 : entries.push_back(value); 134 : } 135 : 136 2 : return true; 137 2 : } 138 : 139 14669 : static inline std::string UuidToString(const boost::uuids::uuid &id) 140 : { 141 14669 : std::stringstream uuidstring; 142 14669 : uuidstring << id; 143 29338 : return uuidstring.str(); 144 14669 : } 145 : 146 2160 : static inline boost::uuids::uuid StringToUuid(const std::string &str) 147 : { 148 2160 : boost::uuids::uuid u = boost::uuids::nil_uuid(); 149 2160 : std::stringstream uuidstring(str); 150 2160 : uuidstring >> u; 151 2160 : return u; 152 2160 : } 153 : 154 0 : static inline std::string BoolToString(const bool &val) { 155 0 : if (val) { 156 0 : return "true"; 157 : } else { 158 0 : return "false"; 159 : } 160 : } 161 : 162 0 : static inline bool StringToBool(const std::string &in_string) { 163 0 : if (in_string.compare("true") == 0) { 164 0 : return true; 165 : } 166 0 : if (in_string.compare("false") == 0) { 167 0 : return false; 168 : } 169 0 : return false; 170 : } 171 : 172 : #endif // BASE_STRING_UTIL_H__