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 18898895 : static inline const std::string integerToString(const NumberType &num) { 21 18898895 : std::stringstream ss; 22 18899415 : ss << num; 23 37798520 : return ss.str(); 24 18899372 : } 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 1182019 : inline const std::string integerToString<>(const uint8_t &num) { 39 1182019 : std::stringstream ss; 40 1182023 : ss << (uint16_t)num; 41 2364041 : return ss.str(); 42 1182023 : } 43 : 44 : // Writes a number into a string as hex 45 : template <typename NumberType> 46 40839 : static inline const std::string integerToHexString(const NumberType &num) { 47 40839 : std::stringstream ss; 48 40839 : ss << std::hex << num; 49 81678 : return ss.str(); 50 40839 : } 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 264351 : inline bool stringToInteger(const std::string& str, NumberType &num) { 73 : char *endptr; 74 264351 : num = strtoul(str.c_str(), &endptr, 10); 75 264351 : return endptr[0] == '\0'; 76 : } 77 : 78 : template <typename NumberType> 79 17693 : inline bool stringToLongLong(const std::string& str, NumberType &num) { 80 : char *endptr; 81 17693 : num = strtoull(str.c_str(), &endptr, 10); 82 17692 : return endptr[0] == '\0'; 83 : } 84 : 85 : template <> 86 122 : inline bool stringToInteger<>(const std::string& str, int64_t &num) { 87 122 : return stringToLongLong(str, num); 88 : } 89 : 90 : template <> 91 17571 : inline bool stringToInteger<>(const std::string& str, uint64_t &num) { 92 17571 : 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 728 : static inline bool stringToIntegerList(std::string input, 109 : std::string seperator, 110 : std::vector<NumberType> &entries) { 111 728 : std::vector<std::string> tokens; 112 : 113 728 : boost::split(tokens, input, boost::is_any_of(seperator), 114 : boost::token_compress_on); 115 : 116 727 : if (!tokens.size()) { 117 0 : return false; 118 : } 119 : 120 727 : std::vector<std::string>::iterator iter; 121 : 122 3635 : for (iter = tokens.begin(); iter != tokens.end(); iter++) { 123 1453 : std::stringstream ss(*iter); 124 : NumberType value; 125 1455 : ss >> value; 126 : 127 : // 128 : // Bail if there is an error during the conversion. 129 : // 130 1455 : if (ss.fail()) { 131 0 : return false; 132 : } 133 1455 : entries.push_back(value); 134 : } 135 : 136 728 : return true; 137 728 : } 138 : 139 1248 : static inline std::string UuidToString(const boost::uuids::uuid &id) 140 : { 141 1248 : std::stringstream uuidstring; 142 1248 : uuidstring << id; 143 2496 : return uuidstring.str(); 144 1248 : } 145 : 146 29892 : static inline boost::uuids::uuid StringToUuid(const std::string &str) 147 : { 148 29892 : boost::uuids::uuid u = boost::uuids::nil_uuid(); 149 29892 : std::stringstream uuidstring(str); 150 29892 : uuidstring >> u; 151 29892 : return u; 152 29892 : } 153 : 154 26 : static inline std::string BoolToString(const bool &val) { 155 26 : if (val) { 156 1 : return "true"; 157 : } else { 158 25 : return "false"; 159 : } 160 : } 161 : 162 2 : static inline bool StringToBool(const std::string &in_string) { 163 2 : if (in_string.compare("true") == 0) { 164 1 : return true; 165 : } 166 1 : if (in_string.compare("false") == 0) { 167 1 : return false; 168 : } 169 0 : return false; 170 : } 171 : 172 : #endif // BASE_STRING_UTIL_H__