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