diff --git a/src/engine/helper/HL_String.cpp b/src/engine/helper/HL_String.cpp index bdbaf246..c0d819f6 100644 --- a/src/engine/helper/HL_String.cpp +++ b/src/engine/helper/HL_String.cpp @@ -17,88 +17,88 @@ std::string StringHelper::extractFilename(const std::string& path) { - // Look for separator from end of string - for (int i = path.size() - 1; i >= 0; i--) - { - if (path[i] == '/' || path[i] == '\\') - return path.substr(i+1); - } - return ""; - + // Look for separator from end of string + for (int i = path.size() - 1; i >= 0; i--) + { + if (path[i] == '/' || path[i] == '\\') + return path.substr(i+1); + } + return ""; + } std::string StringHelper::makeUtf8(const std::tstring &arg) { #if defined(TARGET_WIN) - size_t required = WideCharToMultiByte(CP_UTF8, 0, arg.c_str(), -1, NULL, 0, NULL, NULL); - char *result = (char*)_alloca(required + 1); - WideCharToMultiByte(CP_UTF8, 0, arg.c_str(), -1, result, required+1, NULL, NULL); - return result; + size_t required = WideCharToMultiByte(CP_UTF8, 0, arg.c_str(), -1, NULL, 0, NULL, NULL); + char *result = (char*)_alloca(required + 1); + WideCharToMultiByte(CP_UTF8, 0, arg.c_str(), -1, result, required+1, NULL, NULL); + return result; #else - return arg; + return arg; #endif } std::tstring StringHelper::makeTstring(const std::string& arg) { #if defined(TARGET_WIN) - size_t count = MultiByteToWideChar(CP_UTF8, 0, arg.c_str(), -1, NULL, 0); - wchar_t* result = (wchar_t*)_alloca(count * 2); - MultiByteToWideChar(CP_UTF8, 0, arg.c_str(), -1, result, count); - return result; + size_t count = MultiByteToWideChar(CP_UTF8, 0, arg.c_str(), -1, NULL, 0); + wchar_t* result = (wchar_t*)_alloca(count * 2); + MultiByteToWideChar(CP_UTF8, 0, arg.c_str(), -1, result, count); + return result; #else - return arg; + return arg; #endif } int StringHelper::toInt(const char *s, int defaultValue, bool* isOk) { - int result; - if (sscanf(s, "%d", &result) != 1) - { - if (isOk) - *isOk = false; - result = defaultValue; - } - else - if (isOk) - *isOk = true; + int result; + if (sscanf(s, "%d", &result) != 1) + { + if (isOk) + *isOk = false; + result = defaultValue; + } + else + if (isOk) + *isOk = true; - return result; + return result; } uint64_t StringHelper::toUint64(const char* s, uint64_t def, bool *isOk) { - uint64_t result = def; + uint64_t result = def; #if defined(TARGET_WIN) - if (sscanf(s, "%I64d", &result) != 1) + if (sscanf(s, "%I64d", &result) != 1) #else - if (sscanf(s, "%llu", &result) != 1) + if (sscanf(s, "%llu", &result) != 1) #endif - { - if (isOk) - *isOk = false; - result = def; - } - else - if (isOk) - *isOk = true; + { + if (isOk) + *isOk = false; + result = def; + } + else + if (isOk) + *isOk = true; - return result; + return result; } std::string StringHelper::toHex(unsigned int value) { - char buffer[32]; - sprintf(buffer, "%x", value); - return buffer; + char buffer[32]; + sprintf(buffer, "%x", value); + return buffer; } std::string StringHelper::toHex(const void *ptr) { - std::ostringstream oss; - oss << std::hex << ptr; - return oss.str(); + std::ostringstream oss; + oss << std::hex << ptr; + return oss.str(); } //must be lowercase for MD5 @@ -106,334 +106,340 @@ static const char hexmap[] = "0123456789abcdef"; std::string StringHelper::toHex(const uint8_t* input, size_t inputLength) { - std::string result; result.resize(inputLength * 2); + std::string result; result.resize(inputLength * 2); - const char* p = (const char*)input; - char* r = &result[0]; - for (size_t i=0; i < inputLength; ++i) - { - unsigned char temp = *p++; + const char* p = (const char*)input; + char* r = &result[0]; + for (size_t i=0; i < inputLength; ++i) + { + unsigned char temp = *p++; - int hi = (temp & 0xf0)>>4; - int low = (temp & 0xf); + int hi = (temp & 0xf0)>>4; + int low = (temp & 0xf); - *r++ = hexmap[hi]; - *r++ = hexmap[low]; - } - *r = 0; + *r++ = hexmap[hi]; + *r++ = hexmap[low]; + } + *r = 0; - return result; + return result; } std::string StringHelper::prefixLines(const std::string &source, const std::string &prefix) { - // Read source line by line - std::istringstream iss(source); - std::ostringstream oss; - std::string line; - while (std::getline(iss,line)) - { - oss << prefix << line << std::endl; - } - return oss.str(); + // Read source line by line + std::istringstream iss(source); + std::ostringstream oss; + std::string line; + while (std::getline(iss,line)) + { + oss << prefix << line << std::endl; + } + return oss.str(); } std::string StringHelper::doubleToString(double value, int precision) { - std::stringstream ss; - ss << std::fixed << std::setprecision(precision) << value; - return ss.str(); + std::stringstream ss; + ss << std::fixed << std::setprecision(precision) << value; + return ss.str(); } const char* StringHelper::findSubstring(const char* buffer, const char* substring, size_t bufferLength) { #if defined(TARGET_WIN) - return (const char*)strstr(buffer, substring); + return (const char*)strstr(buffer, substring); #else - return (const char*)memmem(buffer, bufferLength, substring, strlen(substring)); + return (const char*)memmem(buffer, bufferLength, substring, strlen(substring)); #endif } void StringHelper::split(const std::string& src, std::vector& dst, const std::string& delims) { - dst.clear(); - std::string::size_type p = 0; - while (p < src.size()) - { - std::string::size_type f = src.find_first_of(delims, p); - if (f == std::string::npos) + dst.clear(); + std::string::size_type p = 0; + while (p < src.size()) { - std::string t = src.substr(p); - if (!t.empty()) - dst.push_back(t); - p = src.size(); + std::string::size_type f = src.find_first_of(delims, p); + if (f == std::string::npos) + { + std::string t = src.substr(p); + if (!t.empty()) + dst.push_back(t); + p = src.size(); + } + else + { + std::string t = src.substr(p, f-p); + if (!t.empty()) + dst.push_back(t); + p = f + 1; + } } - else - { - std::string t = src.substr(p, f-p); - if (!t.empty()) - dst.push_back(t); - p = f + 1; - } - } } std::pair StringHelper::parseHost(const std::string& host, int defaultPort) { - std::pair result; - std::size_t p = host.find(':'); - if (p != std::string::npos) - { - result.first = host.substr(0, p); - result.second = StringHelper::toInt(host.c_str() + p + 1, defaultPort); - } - else - { - result.first = host; - result.second = defaultPort; - } - return result; + std::pair result; + std::size_t p = host.find(':'); + if (p != std::string::npos) + { + result.first = host.substr(0, p); + result.second = StringHelper::toInt(host.c_str() + p + 1, defaultPort); + } + else + { + result.first = host; + result.second = defaultPort; + } + return result; } std::pair StringHelper::parseAssignment(const std::string& s, bool trimQuotes) { - std::pair result; + std::pair result; - std::string::size_type p = s.find('='); - if (p != std::string::npos) - { - result.first = StringHelper::trim(s.substr(0, p)); - result.second = StringHelper::trim(s.substr(p+1)); - if (trimQuotes && result.second.size() >= 2) + std::string::size_type p = s.find('='); + if (p != std::string::npos) { - if (result.second[0] == '"' && result.second[result.second.size()-1] == '"' || - result.second[0] == '\'' && result.second[result.second.size()-1] == '\'') - result.second = result.second.substr(1, result.second.size() - 2); + result.first = StringHelper::trim(s.substr(0, p)); + result.second = StringHelper::trim(s.substr(p+1)); + if (trimQuotes && result.second.size() >= 2) + { + if (result.second[0] == '"' && result.second[result.second.size()-1] == '"' || + result.second[0] == '\'' && result.second[result.second.size()-1] == '\'') + result.second = result.second.substr(1, result.second.size() - 2); + } } - } - else - result.first = StringHelper::trim(s); + else + result.first = StringHelper::trim(s); - return result; + return result; } std::string StringHelper::intToString(int value) { - char buffer[32]; - sprintf(buffer, "%d", value); - return buffer; + char buffer[32]; + sprintf(buffer, "%d", value); + return buffer; } float StringHelper::toFloat(const std::string &s, float v, bool* isOk) { - float result = 0.0; - int code = sscanf(s.c_str(), "%f", &result); - if (code == 1) - { - if (isOk) - *isOk = true; - } - else - { - result = v; - if (isOk) - *isOk = false; - } + float result = 0.0; + int code = sscanf(s.c_str(), "%f", &result); + if (code == 1) + { + if (isOk) + *isOk = true; + } + else + { + result = v; + if (isOk) + *isOk = false; + } - return result; + return result; } std::string StringHelper::trim(const std::string &s) { - auto wsfront = std::find_if_not(s.begin(), s.end(), [](int c) { return std::isspace(c); }); - auto wsback = std::find_if_not(s.rbegin(), s.rend(), [](int c) { return std::isspace(c); }).base(); - return (wsback <= wsfront ? std::string() : std::string(wsfront,wsback)); + auto wsfront = std::find_if_not(s.begin(), s.end(), [](int c) { return std::isspace(c); }); + auto wsback = std::find_if_not(s.rbegin(), s.rend(), [](int c) { return std::isspace(c); }).base(); + return (wsback <= wsfront ? std::string() : std::string(wsfront,wsback)); } std::string StringHelper::timeToString(time_t t) { - char buffer[96]; - struct tm lt; + char buffer[96]; + struct tm lt; #if defined(TARGET_LINUX) || defined(TARGET_OSX) || defined(TARGET_ANDROID) - localtime_r(&t, <); + localtime_r(&t, <); #else - lt = *localtime(&t); + lt = *localtime(&t); #endif - strftime(buffer, sizeof(buffer)-1, "%Y-%m-%d %H:%M:%S", <); - return buffer; + strftime(buffer, sizeof(buffer)-1, "%Y-%m-%d %H:%M:%S", <); + return buffer; } std::string StringHelper::millisecondsToString(uint64_t t) { - return timeToString(t/1000); + return timeToString(t/1000); } int StringHelper::fromHex2Int(const std::string &s) { - int result = 0; - sscanf(s.c_str(), "%x", &result); - return result; + int result = 0; + sscanf(s.c_str(), "%x", &result); + return result; } static int hex2code(char s) { - if (s >= '0' && s <= '9') - return s - '0'; - if (s >= 'a' && s <= 'f') - return s - 'a' + 10; - if (s >= 'A' && s <= 'F') - return s - 'A' + 10; - return 0; + if (s >= '0' && s <= '9') + return s - '0'; + if (s >= 'a' && s <= 'f') + return s - 'a' + 10; + if (s >= 'A' && s <= 'F') + return s - 'A' + 10; + return 0; } static int hex2code(const char* s) { - return (hex2code(s[0]) << 4) + hex2code(s[1]); + return (hex2code(s[0]) << 4) + hex2code(s[1]); } std::string StringHelper::fromHex2String(const std::string& s) { - std::string result; result.resize(s.size() / 2); - const char* t = s.c_str(); - for (size_t i = 0; i < result.size(); i++) - result[i] = hex2code(t[i*2]); + std::string result; result.resize(s.size() / 2); + const char* t = s.c_str(); + for (size_t i = 0; i < result.size(); i++) + result[i] = hex2code(t[i*2]); - return result; + return result; } std::string StringHelper::replace(const std::string& s, char f, char r) { - std::string result(s); - for (std::string::size_type i = 0; i < result.size(); i++) - if (result[i] == 'f') - result[i] = r; + std::string result(s); + for (std::string::size_type i = 0; i < result.size(); i++) + if (result[i] == 'f') + result[i] = r; - return result; + return result; } std::string StringHelper::replace(const std::string& s, const std::string& tmpl, const std::string& n) { - std::string result(s); - std::string::size_type p; - while ( (p = result.find(tmpl)) != std::string::npos) - { - result.replace(p, tmpl.size(), n); - } + std::string result(s); + std::string::size_type p; + while ( (p = result.find(tmpl)) != std::string::npos) + { + result.replace(p, tmpl.size(), n); + } - return result; + return result; } std::string StringHelper::decodeUri(const std::string& s) { - std::string ret; - ret.reserve(s.size()); + std::string ret; + ret.reserve(s.size()); - char ch; + char ch; - int i, ii; - for (i=0; i<(int)s.length(); i++) - { - if (s[i] == 37) + int i, ii; + for (i=0; i<(int)s.length(); i++) { - sscanf(s.substr(i+1,2).c_str(), "%x", &ii); - ch = static_cast(ii); - ret += ch; - i += 2; + if (s[i] == 37) + { + sscanf(s.substr(i+1,2).c_str(), "%x", &ii); + ch = static_cast(ii); + ret += ch; + i += 2; + } + else + ret += s[i]; } - else - ret += s[i]; - } - return ret; + return ret; +} + +bool StringHelper::startsWith(const std::string& s, const std::string& prefix) +{ + std::string::size_type p = s.find(prefix); + return p == 0; } #define XML_HEADER "" // --------------------- XcapHelper ----------------- std::string XcapHelper::buildBuddyList(std::string listName, std::vector buddies) { - std::ostringstream result; - result << XML_HEADER << - "" << - ""; - - // to test CT only! - //result << ""; - //result << ""; + std::ostringstream result; + result << XML_HEADER << + "" << + ""; - for (unsigned i = 0; i"; - } - result << ""; - return result.str(); + // to test CT only! + //result << ""; + //result << ""; + + for (unsigned i = 0; i"; + } + result << ""; + return result.str(); } std::string XcapHelper::buildRules(std::vector buddies) { - std::ostringstream result; - result << XML_HEADER << - "" << - "" << - ""; + std::ostringstream result; + result << XML_HEADER << + "" << + "" << + ""; - for (unsigned i = 0; i"; - } - result << "" << - "" << - "" << - "allow" << - "" << - "" << - "" << - "" << - "" << - "" << - "" << - "" << - "" << - "" << - "" << - "" << - "" << - "" << - ""; - return result.str(); + for (unsigned i = 0; i"; + } + result << "" << + "" << + "" << + "allow" << + "" << + "" << + "" << + "" << + "" << + "" << + "" << + "" << + "" << + "" << + "" << + "" << + "" << + "" << + ""; + return result.str(); } std::string XcapHelper::buildServices(std::string serviceUri, std::string listRef) { - std::ostringstream result; - - result << "" << std::endl << - "" << std::endl << - "" << std::endl << - "" << listRef.c_str() << "" << std::endl << - "" << std::endl << - "presence" << std::endl << - "" << std::endl << - "" << std::endl << - ""; + std::ostringstream result; - return result.str(); + result << "" << std::endl << + "" << std::endl << + "" << std::endl << + "" << listRef.c_str() << "" << std::endl << + "" << std::endl << + "presence" << std::endl << + "" << std::endl << + "" << std::endl << + ""; + + return result.str(); } std::string XcapHelper::normalizeSipUri(std::string uri) { - if (uri.length()) - { - if (uri[0] == '<') - uri.erase(0,1); if (uri.length()) { - if (uri[uri.length()-1] == '>') - uri.erase(uri.length()-1, 1); + if (uri[0] == '<') + uri.erase(0,1); + if (uri.length()) + { + if (uri[uri.length()-1] == '>') + uri.erase(uri.length()-1, 1); + } } - } - return uri; + return uri; } diff --git a/src/engine/helper/HL_String.h b/src/engine/helper/HL_String.h index 63c091a1..f6f39b77 100644 --- a/src/engine/helper/HL_String.h +++ b/src/engine/helper/HL_String.h @@ -19,54 +19,55 @@ class StringHelper { public: - static std::string extractFilename(const std::string& path); - static std::string makeUtf8(const std::tstring& arg); - static std::tstring makeTstring(const std::string& arg); - static int toInt(const char* s, int defaultValue, bool* isOk = nullptr); - static uint64_t toUint64(const char* s, uint64_t def, bool *isOk = nullptr); - static std::string toHex(unsigned int value); - static std::string toHex(const void* ptr); - static std::string toHex(const uint8_t* input, size_t inputLength); - static std::string intToString(int value); - static std::string prefixLines(const std::string& source, const std::string& prefix); - static std::string doubleToString(double value, int precision); + static std::string extractFilename(const std::string& path); + static std::string makeUtf8(const std::tstring& arg); + static std::tstring makeTstring(const std::string& arg); + static int toInt(const char* s, int defaultValue, bool* isOk = nullptr); + static uint64_t toUint64(const char* s, uint64_t def, bool *isOk = nullptr); + static std::string toHex(unsigned int value); + static std::string toHex(const void* ptr); + static std::string toHex(const uint8_t* input, size_t inputLength); + static std::string intToString(int value); + static std::string prefixLines(const std::string& source, const std::string& prefix); + static std::string doubleToString(double value, int precision); - static const char* findSubstring(const char* buffer, const char* substring, size_t bufferLength); - static void split(const std::string& src, std::vector& dst, const std::string& delims); + static const char* findSubstring(const char* buffer, const char* substring, size_t bufferLength); + static void split(const std::string& src, std::vector& dst, const std::string& delims); - template - static std::string join(const std::vector& v, const std::string& delimiter) - { - std::ostringstream s; - for (const auto& i : v) + template + static std::string join(const std::vector& v, const std::string& delimiter) { - if (&i != &v[0]) - s << delimiter; - s << i; + std::ostringstream s; + for (const auto& i : v) + { + if (&i != &v[0]) + s << delimiter; + s << i; + } + return s.str(); } - return s.str(); - } - static std::pair parseHost(const std::string& host, int defaultPort); - static std::pair parseAssignment(const std::string& s, bool trimQuotes = true); - static float toFloat(const std::string& s, float defaultValue = 0.0, bool* isOk = nullptr); - static std::string trim(const std::string& s); - static std::string timeToString(time_t t); - static std::string millisecondsToString(uint64_t t); - static int fromHex2Int(const std::string& s); - static std::string fromHex2String(const std::string& s); - static std::string replace(const std::string& s, char f, char r); - static std::string replace(const std::string& s, const std::string& tmpl, const std::string& n); - static std::string decodeUri(const std::string& s); + static std::pair parseHost(const std::string& host, int defaultPort); + static std::pair parseAssignment(const std::string& s, bool trimQuotes = true); + static float toFloat(const std::string& s, float defaultValue = 0.0, bool* isOk = nullptr); + static std::string trim(const std::string& s); + static std::string timeToString(time_t t); + static std::string millisecondsToString(uint64_t t); + static int fromHex2Int(const std::string& s); + static std::string fromHex2String(const std::string& s); + static std::string replace(const std::string& s, char f, char r); + static std::string replace(const std::string& s, const std::string& tmpl, const std::string& n); + static std::string decodeUri(const std::string& s); + static bool startsWith(const std::string& s, const std::string& prefix); }; class XcapHelper { public: - static std::string buildBuddyList(std::string listName, std::vector buddies); - static std::string buildRules(std::vector buddies); - static std::string buildServices(std::string serviceUri, std::string listRef); - static std::string normalizeSipUri(std::string uri); + static std::string buildBuddyList(std::string listName, std::vector buddies); + static std::string buildRules(std::vector buddies); + static std::string buildServices(std::string serviceUri, std::string listRef); + static std::string normalizeSipUri(std::string uri); };