From 333557da84393944cb5418de997f9b1c5f603725 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sat, 25 May 2013 17:03:37 -0700 Subject: [PATCH] Split string utilities from utils.h --- .../memory/ripple_StringUtilities.cpp | 264 ++++++++++++++++ .../memory/ripple_StringUtilities.h | 208 +++++++++++++ modules/ripple_basics/ripple_basics.cpp | 3 + modules/ripple_basics/ripple_basics.h | 4 + newcoin.vcxproj | 7 + newcoin.vcxproj.filters | 6 + src/cpp/ripple/utils.cpp | 292 +----------------- src/cpp/ripple/utils.h | 196 ------------ 8 files changed, 496 insertions(+), 484 deletions(-) create mode 100644 modules/ripple_basics/memory/ripple_StringUtilities.cpp create mode 100644 modules/ripple_basics/memory/ripple_StringUtilities.h diff --git a/modules/ripple_basics/memory/ripple_StringUtilities.cpp b/modules/ripple_basics/memory/ripple_StringUtilities.cpp new file mode 100644 index 0000000000..13ab9c0748 --- /dev/null +++ b/modules/ripple_basics/memory/ripple_StringUtilities.cpp @@ -0,0 +1,264 @@ + +char charHex(int iDigit) +{ + return iDigit < 10 ? '0' + iDigit : 'A' - 10 + iDigit; +} + +int charUnHex(char cDigit) +{ + return cDigit >= '0' && cDigit <= '9' + ? cDigit - '0' + : cDigit >= 'A' && cDigit <= 'F' + ? cDigit - 'A' + 10 + : cDigit >= 'a' && cDigit <= 'f' + ? cDigit - 'a' + 10 + : -1; +} + +int strUnHex(std::string& strDst, const std::string& strSrc) +{ + int iBytes = (strSrc.size()+1)/2; + + strDst.resize(iBytes); + + const char* pSrc = &strSrc[0]; + char* pDst = &strDst[0]; + + if (strSrc.size() & 1) + { + int c = charUnHex(*pSrc++); + + if (c < 0) + { + iBytes = -1; + } + else + { + *pDst++ = c; + } + } + + for (int i=0; iBytes >= 0 && i != iBytes; i++) + { + int cHigh = charUnHex(*pSrc++); + int cLow = charUnHex(*pSrc++); + + if (cHigh < 0 || cLow < 0) + { + iBytes = -1; + } + else + { + strDst[i] = (cHigh << 4) | cLow; + } + } + + if (iBytes < 0) + strDst.clear(); + + return iBytes; +} + +std::vector strUnHex(const std::string& strSrc) +{ + std::string strTmp; + + strUnHex(strTmp, strSrc); + + return strCopy(strTmp); +} + +uint64_t uintFromHex(const std::string& strSrc) +{ + uint64_t uValue = 0; + + BOOST_FOREACH(char c, strSrc) + uValue = (uValue << 4) | charUnHex(c); + + return uValue; +} + +// +// Misc string +// + +std::vector strCopy(const std::string& strSrc) +{ + std::vector vucDst; + + vucDst.resize(strSrc.size()); + + std::copy(strSrc.begin(), strSrc.end(), vucDst.begin()); + + return vucDst; +} + +std::string strCopy(const std::vector& vucSrc) +{ + std::string strDst; + + strDst.resize(vucSrc.size()); + + std::copy(vucSrc.begin(), vucSrc.end(), strDst.begin()); + + return strDst; + +} + +extern std::string urlEncode(const std::string& strSrc) +{ + std::string strDst; + int iOutput = 0; + int iSize = strSrc.length(); + + strDst.resize(iSize*3); + + for (int iInput = 0; iInput < iSize; iInput++) { + unsigned char c = strSrc[iInput]; + + if (c == ' ') + { + strDst[iOutput++] = '+'; + } + else if (isalnum(c)) + { + strDst[iOutput++] = c; + } + else + { + strDst[iOutput++] = '%'; + strDst[iOutput++] = charHex(c >> 4); + strDst[iOutput++] = charHex(c & 15); + } + } + + strDst.resize(iOutput); + + return strDst; +} + +// +// IP Port parsing +// +// <-- iPort: "" = -1 +bool parseIpPort(const std::string& strSource, std::string& strIP, int& iPort) +{ + boost::smatch smMatch; + bool bValid = false; + + static boost::regex reEndpoint("\\`\\s*(\\S+)(?:\\s+(\\d+))?\\s*\\'"); + + if (boost::regex_match(strSource, smMatch, reEndpoint)) + { + boost::system::error_code err; + std::string strIPRaw = smMatch[1]; + std::string strPortRaw = smMatch[2]; + + boost::asio::ip::address addrIP = boost::asio::ip::address::from_string(strIPRaw, err); + + bValid = !err; + if (bValid) + { + strIP = addrIP.to_string(); + iPort = strPortRaw.empty() ? -1 : boost::lexical_cast(strPortRaw); + } + } + + return bValid; +} + +bool parseUrl(const std::string& strUrl, std::string& strScheme, std::string& strDomain, int& iPort, std::string& strPath) +{ + // scheme://username:password@hostname:port/rest + static boost::regex reUrl("(?i)\\`\\s*([[:alpha:]][-+.[:alpha:][:digit:]]*)://([^:/]+)(?::(\\d+))?(/.*)?\\s*?\\'"); + boost::smatch smMatch; + + bool bMatch = boost::regex_match(strUrl, smMatch, reUrl); // Match status code. + + if (bMatch) + { + std::string strPort; + + strScheme = smMatch[1]; + strDomain = smMatch[2]; + strPort = smMatch[3]; + strPath = smMatch[4]; + + boost::algorithm::to_lower(strScheme); + + iPort = strPort.empty() ? -1 : lexical_cast_s(strPort); + // std::cerr << strUrl << " : " << bMatch << " : '" << strDomain << "' : '" << strPort << "' : " << iPort << " : '" << strPath << "'" << std::endl; + } + // std::cerr << strUrl << " : " << bMatch << " : '" << strDomain << "' : '" << strPath << "'" << std::endl; + + return bMatch; +} + +// +// Quality parsing +// - integers as is. +// - floats multiplied by a billion +bool parseQuality(const std::string& strSource, uint32& uQuality) +{ + uQuality = lexical_cast_s(strSource); + + if (!uQuality) + { + float fQuality = lexical_cast_s(strSource); + + if (fQuality) + uQuality = (uint32)(QUALITY_ONE*fQuality); + } + + return !!uQuality; +} + +BOOST_AUTO_TEST_SUITE( Utils) + +BOOST_AUTO_TEST_CASE( ParseUrl ) +{ + std::string strScheme; + std::string strDomain; + int iPort; + std::string strPath; + + if (!parseUrl("lower://domain", strScheme, strDomain, iPort, strPath)) + BOOST_FAIL("parseUrl: lower://domain failed"); + + if (strScheme != "lower") + BOOST_FAIL("parseUrl: lower://domain : scheme failed"); + + if (strDomain != "domain") + BOOST_FAIL("parseUrl: lower://domain : domain failed"); + + if (iPort != -1) + BOOST_FAIL("parseUrl: lower://domain : port failed"); + + if (strPath != "") + BOOST_FAIL("parseUrl: lower://domain : path failed"); + + if (!parseUrl("UPPER://domain:234/", strScheme, strDomain, iPort, strPath)) + BOOST_FAIL("parseUrl: UPPER://domain:234/ failed"); + + if (strScheme != "upper") + BOOST_FAIL("parseUrl: UPPER://domain:234/ : scheme failed"); + + if (iPort != 234) + BOOST_FAIL(boost::str(boost::format("parseUrl: UPPER://domain:234/ : port failed: %d") % iPort)); + + if (strPath != "/") + BOOST_FAIL("parseUrl: UPPER://domain:234/ : path failed"); + + if (!parseUrl("Mixed://domain/path", strScheme, strDomain, iPort, strPath)) + BOOST_FAIL("parseUrl: Mixed://domain/path failed"); + + if (strScheme != "mixed") + BOOST_FAIL("parseUrl: Mixed://domain/path tolower failed"); + + if (strPath != "/path") + BOOST_FAIL("parseUrl: Mixed://domain/path path failed"); +} + +BOOST_AUTO_TEST_SUITE_END() + +// vim:ts=4 diff --git a/modules/ripple_basics/memory/ripple_StringUtilities.h b/modules/ripple_basics/memory/ripple_StringUtilities.h new file mode 100644 index 0000000000..bc18d67318 --- /dev/null +++ b/modules/ripple_basics/memory/ripple_StringUtilities.h @@ -0,0 +1,208 @@ +#ifndef RIPPLE_STRINGUTILITIES_H +#define RIPPLE_STRINGUTILITIES_H + +/** String utility functions. +*/ + +//------------------------------------------------------------------------------ + +// Ripple specific constant used for parsing qualities and other things +// +#define QUALITY_ONE 1000000000 // 10e9 + +//------------------------------------------------------------------------------ + +// Terminal output color codes +#define vt_f_black "\033[30m" +#define vt_f_red "\033[31m" +#define vt_f_green "\033[32m" +#define vt_f_yellow "\033[33m" +#define vt_f_blue "\033[34m" +#define vt_f_megenta "\033[35m" +#define vt_f_cyan "\033[36m" +#define vt_f_white "\033[37m" +#define vt_f_default "\033[39m" + +#define vt_b_black "\033[40m" +#define vt_b_red "\033[41m" +#define vt_b_green "\033[42m" +#define vt_b_yellow "\033[43m" +#define vt_b_blue "\033[44m" +#define vt_b_megenta "\033[45m" +#define vt_b_cyan "\033[46m" +#define vt_b_white "\033[47m" +#define vt_b_default "\033[49m" + +#define vt_f_bold_black "\033[1m\033[30m" +#define vt_f_bold_red "\033[1m\033[31m" +#define vt_f_bold_green "\033[1m\033[32m" +#define vt_f_bold_yellow "\033[1m\033[33m" +#define vt_f_bold_blue "\033[1m\033[34m" +#define vt_f_bold_megenta "\033[1m\033[35m" +#define vt_f_bold_cyan "\033[1m\033[36m" +#define vt_f_bold_white "\033[1m\033[37m" +#define vt_f_bold_default "\033[1m\033[39m" + +#define vt_bold "\033[1m" +#define vt_dim "\033[2m" // does not work for xterm +#define vt_normal "\033[22m" // intensity + +#define vt_n_enable "\033[7m" // negative +#define vt_n_disable "\033[27m" + +#define vt_u_single "\033[4m" // underline +#define vt_u_double "\033[21m" // does not work for xterm +#define vt_u_disable "\033[24m" + +#define vt_reset vt_f_default vt_b_default vt_normal vt_n_disable vt_u_disable + +//------------------------------------------------------------------------------ + +extern std::string urlEncode(const std::string& strSrc); + +template +std::string strJoin(Iterator first, Iterator last, std::string strSeperator) +{ + std::ostringstream ossValues; + + for (Iterator start = first; first != last; first++) + { + ossValues << str(boost::format("%s%s") % (start == first ? "" : strSeperator) % *first); + } + + return ossValues.str(); +} + +char charHex(int iDigit); + +template +std::string strHex(Iterator first, int iSize) +{ + std::string strDst; + + strDst.resize(iSize*2); + + for (int i = 0; i < iSize; i++) { + unsigned char c = *first++; + + strDst[i*2] = charHex(c >> 4); + strDst[i*2+1] = charHex(c & 15); + } + + return strDst; +} + +inline const std::string strHex(const std::string& strSrc) +{ + return strHex(strSrc.begin(), strSrc.size()); +} + +inline std::string strHex(const std::vector& vucData) +{ + return strHex(vucData.begin(), vucData.size()); +} + +inline std::string strHex(const uint64 uiHost) +{ + uint64_t uBig = htobe64(uiHost); + + return strHex((unsigned char*) &uBig, sizeof(uBig)); +} + +inline static std::string sqlEscape(const std::string& strSrc) +{ + static boost::format f("X'%s'"); + return str(boost::format(f) % strHex(strSrc)); +} + +inline static std::string sqlEscape(const std::vector& vecSrc) +{ + size_t size = vecSrc.size(); + if (size == 0) + return "X''"; + + std::string j(size * 2 + 3, 0); + + unsigned char *oPtr = reinterpret_cast(&*j.begin()); + const unsigned char *iPtr = &vecSrc[0]; + + *oPtr++ = 'X'; + *oPtr++ = '\''; + + for (int i = size; i != 0; --i) + { + unsigned char c = *iPtr++; + *oPtr++ = charHex(c >> 4); + *oPtr++ = charHex(c & 15); + } + + *oPtr++ = '\''; + return j; +} + +template +bool isZero(Iterator first, int iSize) +{ + while (iSize && !*first++) + --iSize; + + return !iSize; +} + +int charUnHex(char cDigit); +int strUnHex(std::string& strDst, const std::string& strSrc); + +uint64_t uintFromHex(const std::string& strSrc); + +std::vector strUnHex(const std::string& strSrc); + +std::vector strCopy(const std::string& strSrc); +std::string strCopy(const std::vector& vucSrc); + +bool parseIpPort(const std::string& strSource, std::string& strIP, int& iPort); +bool parseQuality(const std::string& strSource, uint32& uQuality); + +inline std::string strGetEnv(const std::string& strKey) +{ + return getenv(strKey.c_str()) ? getenv(strKey.c_str()) : ""; +} + +template T lexical_cast_s(const std::string& string) +{ // lexically cast a string to the selected type. Does not throw + try + { + return boost::lexical_cast(string); + } + catch (...) + { + return 0; + } +} + +template std::string lexical_cast_i(const T& t) +{ // lexicaly cast the selected type to a string. Does not throw + try + { + return boost::lexical_cast(t); + } + catch (...) + { + return ""; + } +} + +template T lexical_cast_st(const std::string& string) +{ // lexically cast a string to the selected type. Does throw + return boost::lexical_cast(string); +} + +template std::string lexical_cast_it(const T& t) +{ // lexicaly cast the selected type to a string. Does not throw + return boost::lexical_cast(t); +} + +bool parseUrl(const std::string& strUrl, std::string& strScheme, std::string& strDomain, int& iPort, std::string& strPath); + +#endif + +// vim:ts=4 diff --git a/modules/ripple_basics/ripple_basics.cpp b/modules/ripple_basics/ripple_basics.cpp index 88a9a576b3..f808b9c929 100644 --- a/modules/ripple_basics/ripple_basics.cpp +++ b/modules/ripple_basics/ripple_basics.cpp @@ -30,6 +30,8 @@ #include #include #include +#include // for stupid parseIpPort +#include // VFALCO: TODO, fix these warnings! #ifdef _MSC_VER @@ -49,6 +51,7 @@ #include "Winsock2.h" // for ripple_ByteOrder.cpp #endif #include "memory/ripple_ByteOrder.cpp" +#include "memory/ripple_StringUtilities.cpp" #ifdef _MSC_VER //#pragma warning (pop) diff --git a/modules/ripple_basics/ripple_basics.h b/modules/ripple_basics/ripple_basics.h index 2954f076e4..1cc4476432 100644 --- a/modules/ripple_basics/ripple_basics.h +++ b/modules/ripple_basics/ripple_basics.h @@ -79,6 +79,9 @@ namespace boost { # include #endif +// StringUtilities +#include + #include "../ripple_json/ripple_json.h" @@ -93,6 +96,7 @@ namespace boost { #include "containers/ripple_TaggedCache.h" #include "memory/ripple_ByteOrder.h" +#include "memory/ripple_StringUtilities.h" #include "events/ripple_UptimeTimer.h" diff --git a/newcoin.vcxproj b/newcoin.vcxproj index a431b7352f..eccd7ece33 100644 --- a/newcoin.vcxproj +++ b/newcoin.vcxproj @@ -203,6 +203,12 @@ true true + + true + true + true + true + @@ -1188,6 +1194,7 @@ + diff --git a/newcoin.vcxproj.filters b/newcoin.vcxproj.filters index c741c72669..4cf26a8e8d 100644 --- a/newcoin.vcxproj.filters +++ b/newcoin.vcxproj.filters @@ -759,6 +759,9 @@ 1. Modules\ripple_basics\memory + + 1. Modules\ripple_basics\memory + @@ -1409,6 +1412,9 @@ 1. Modules\ripple_basics\memory + + 1. Modules\ripple_basics\memory + diff --git a/src/cpp/ripple/utils.cpp b/src/cpp/ripple/utils.cpp index c1b712cc4c..fc6737f597 100644 --- a/src/cpp/ripple/utils.cpp +++ b/src/cpp/ripple/utils.cpp @@ -17,11 +17,11 @@ #include -#include -#include +//#include +//#include #include -#include -#include +//#include +//#include #include @@ -77,148 +77,6 @@ uint64_t utFromSeconds(int iSeconds) return tdDelta.total_seconds(); } -// -// Hex suport -// - -char charHex(int iDigit) -{ - return iDigit < 10 ? '0' + iDigit : 'A' - 10 + iDigit; -} - -int charUnHex(char cDigit) -{ - return cDigit >= '0' && cDigit <= '9' - ? cDigit - '0' - : cDigit >= 'A' && cDigit <= 'F' - ? cDigit - 'A' + 10 - : cDigit >= 'a' && cDigit <= 'f' - ? cDigit - 'a' + 10 - : -1; -} - -int strUnHex(std::string& strDst, const std::string& strSrc) -{ - int iBytes = (strSrc.size()+1)/2; - - strDst.resize(iBytes); - - const char* pSrc = &strSrc[0]; - char* pDst = &strDst[0]; - - if (strSrc.size() & 1) - { - int c = charUnHex(*pSrc++); - - if (c < 0) - { - iBytes = -1; - } - else - { - *pDst++ = c; - } - } - - for (int i=0; iBytes >= 0 && i != iBytes; i++) - { - int cHigh = charUnHex(*pSrc++); - int cLow = charUnHex(*pSrc++); - - if (cHigh < 0 || cLow < 0) - { - iBytes = -1; - } - else - { - strDst[i] = (cHigh << 4) | cLow; - } - } - - if (iBytes < 0) - strDst.clear(); - - return iBytes; -} - -std::vector strUnHex(const std::string& strSrc) -{ - std::string strTmp; - - strUnHex(strTmp, strSrc); - - return strCopy(strTmp); -} - -uint64_t uintFromHex(const std::string& strSrc) -{ - uint64_t uValue = 0; - - BOOST_FOREACH(char c, strSrc) - uValue = (uValue << 4) | charUnHex(c); - - return uValue; -} - -// -// Misc string -// - -std::vector strCopy(const std::string& strSrc) -{ - std::vector vucDst; - - vucDst.resize(strSrc.size()); - - std::copy(strSrc.begin(), strSrc.end(), vucDst.begin()); - - return vucDst; -} - -std::string strCopy(const std::vector& vucSrc) -{ - std::string strDst; - - strDst.resize(vucSrc.size()); - - std::copy(vucSrc.begin(), vucSrc.end(), strDst.begin()); - - return strDst; - -} - -extern std::string urlEncode(const std::string& strSrc) -{ - std::string strDst; - int iOutput = 0; - int iSize = strSrc.length(); - - strDst.resize(iSize*3); - - for (int iInput = 0; iInput < iSize; iInput++) { - unsigned char c = strSrc[iInput]; - - if (c == ' ') - { - strDst[iOutput++] = '+'; - } - else if (isalnum(c)) - { - strDst[iOutput++] = c; - } - else - { - strDst[iOutput++] = '%'; - strDst[iOutput++] = charHex(c >> 4); - strDst[iOutput++] = charHex(c & 15); - } - } - - strDst.resize(iOutput); - - return strDst; -} - // // DH support // @@ -251,100 +109,6 @@ DH* DH_der_load(const std::string& strDer) return d2i_DHparams(NULL, &pbuf, strDer.size()); } -// -// IP Port parsing -// -// <-- iPort: "" = -1 -bool parseIpPort(const std::string& strSource, std::string& strIP, int& iPort) -{ - boost::smatch smMatch; - bool bValid = false; - - static boost::regex reEndpoint("\\`\\s*(\\S+)(?:\\s+(\\d+))?\\s*\\'"); - - if (boost::regex_match(strSource, smMatch, reEndpoint)) - { - boost::system::error_code err; - std::string strIPRaw = smMatch[1]; - std::string strPortRaw = smMatch[2]; - - boost::asio::ip::address addrIP = boost::asio::ip::address::from_string(strIPRaw, err); - - bValid = !err; - if (bValid) - { - strIP = addrIP.to_string(); - iPort = strPortRaw.empty() ? -1 : boost::lexical_cast(strPortRaw); - } - } - - return bValid; -} - -bool parseUrl(const std::string& strUrl, std::string& strScheme, std::string& strDomain, int& iPort, std::string& strPath) -{ - // scheme://username:password@hostname:port/rest - static boost::regex reUrl("(?i)\\`\\s*([[:alpha:]][-+.[:alpha:][:digit:]]*)://([^:/]+)(?::(\\d+))?(/.*)?\\s*?\\'"); - boost::smatch smMatch; - - bool bMatch = boost::regex_match(strUrl, smMatch, reUrl); // Match status code. - - if (bMatch) - { - std::string strPort; - - strScheme = smMatch[1]; - strDomain = smMatch[2]; - strPort = smMatch[3]; - strPath = smMatch[4]; - - boost::algorithm::to_lower(strScheme); - - iPort = strPort.empty() ? -1 : lexical_cast_s(strPort); - // std::cerr << strUrl << " : " << bMatch << " : '" << strDomain << "' : '" << strPort << "' : " << iPort << " : '" << strPath << "'" << std::endl; - } - // std::cerr << strUrl << " : " << bMatch << " : '" << strDomain << "' : '" << strPath << "'" << std::endl; - - return bMatch; -} - -// -// Quality parsing -// - integers as is. -// - floats multiplied by a billion -bool parseQuality(const std::string& strSource, uint32& uQuality) -{ - uQuality = lexical_cast_s(strSource); - - if (!uQuality) - { - float fQuality = lexical_cast_s(strSource); - - if (fQuality) - uQuality = (uint32)(QUALITY_ONE*fQuality); - } - - return !!uQuality; -} - -/* -void intIPtoStr(int ip,std::string& retStr) -{ - unsigned char bytes[4]; - bytes[0] = ip & 0xFF; - bytes[1] = (ip >> 8) & 0xFF; - bytes[2] = (ip >> 16) & 0xFF; - bytes[3] = (ip >> 24) & 0xFF; - - retStr=str(boost::format("%d.%d.%d.%d") % bytes[3] % bytes[2] % bytes[1] % bytes[0] ); -} - -int strIPtoInt(std::string& ipStr) -{ - -} -*/ - #ifdef PR_SET_NAME #define HAVE_NAME_THREAD extern void NameThread(const char* n) @@ -451,52 +215,4 @@ std::string StopSustain() { return std::string(); } #endif -BOOST_AUTO_TEST_SUITE( Utils) - -BOOST_AUTO_TEST_CASE( ParseUrl ) -{ - std::string strScheme; - std::string strDomain; - int iPort; - std::string strPath; - - if (!parseUrl("lower://domain", strScheme, strDomain, iPort, strPath)) - BOOST_FAIL("parseUrl: lower://domain failed"); - - if (strScheme != "lower") - BOOST_FAIL("parseUrl: lower://domain : scheme failed"); - - if (strDomain != "domain") - BOOST_FAIL("parseUrl: lower://domain : domain failed"); - - if (iPort != -1) - BOOST_FAIL("parseUrl: lower://domain : port failed"); - - if (strPath != "") - BOOST_FAIL("parseUrl: lower://domain : path failed"); - - if (!parseUrl("UPPER://domain:234/", strScheme, strDomain, iPort, strPath)) - BOOST_FAIL("parseUrl: UPPER://domain:234/ failed"); - - if (strScheme != "upper") - BOOST_FAIL("parseUrl: UPPER://domain:234/ : scheme failed"); - - if (iPort != 234) - BOOST_FAIL(boost::str(boost::format("parseUrl: UPPER://domain:234/ : port failed: %d") % iPort)); - - if (strPath != "/") - BOOST_FAIL("parseUrl: UPPER://domain:234/ : path failed"); - - if (!parseUrl("Mixed://domain/path", strScheme, strDomain, iPort, strPath)) - BOOST_FAIL("parseUrl: Mixed://domain/path failed"); - - if (strScheme != "mixed") - BOOST_FAIL("parseUrl: Mixed://domain/path tolower failed"); - - if (strPath != "/path") - BOOST_FAIL("parseUrl: Mixed://domain/path path failed"); -} - -BOOST_AUTO_TEST_SUITE_END() - // vim:ts=4 diff --git a/src/cpp/ripple/utils.h b/src/cpp/ripple/utils.h index fc4c7b9892..ed97374c23 100644 --- a/src/cpp/ripple/utils.h +++ b/src/cpp/ripple/utils.h @@ -2,7 +2,6 @@ #define __UTILS__ #include -#include #include #if BOOST_VERSION < 104700 @@ -11,8 +10,6 @@ #include -#define QUALITY_ONE 1000000000 // 10e9 - #define nothing() do {} while (0) #define fallthru() do {} while (0) #define NUMBER(x) (sizeof(x)/sizeof((x)[0])) @@ -21,163 +18,11 @@ #define isSetBit(x,y) (!!((x) & (y))) -#define vt_f_black "\033[30m" -#define vt_f_red "\033[31m" -#define vt_f_green "\033[32m" -#define vt_f_yellow "\033[33m" -#define vt_f_blue "\033[34m" -#define vt_f_megenta "\033[35m" -#define vt_f_cyan "\033[36m" -#define vt_f_white "\033[37m" -#define vt_f_default "\033[39m" - -#define vt_b_black "\033[40m" -#define vt_b_red "\033[41m" -#define vt_b_green "\033[42m" -#define vt_b_yellow "\033[43m" -#define vt_b_blue "\033[44m" -#define vt_b_megenta "\033[45m" -#define vt_b_cyan "\033[46m" -#define vt_b_white "\033[47m" -#define vt_b_default "\033[49m" - -#define vt_f_bold_black "\033[1m\033[30m" -#define vt_f_bold_red "\033[1m\033[31m" -#define vt_f_bold_green "\033[1m\033[32m" -#define vt_f_bold_yellow "\033[1m\033[33m" -#define vt_f_bold_blue "\033[1m\033[34m" -#define vt_f_bold_megenta "\033[1m\033[35m" -#define vt_f_bold_cyan "\033[1m\033[36m" -#define vt_f_bold_white "\033[1m\033[37m" -#define vt_f_bold_default "\033[1m\033[39m" - -#define vt_bold "\033[1m" -#define vt_dim "\033[2m" // does not work for xterm -#define vt_normal "\033[22m" // intensity - -#define vt_n_enable "\033[7m" // negative -#define vt_n_disable "\033[27m" - -#define vt_u_single "\033[4m" // underline -#define vt_u_double "\033[21m" // does not work for xterm -#define vt_u_disable "\033[24m" - -#define vt_reset vt_f_default vt_b_default vt_normal vt_n_disable vt_u_disable - boost::posix_time::ptime ptEpoch(); int iToSeconds(boost::posix_time::ptime ptWhen); boost::posix_time::ptime ptFromSeconds(int iSeconds); uint64_t utFromSeconds(int iSeconds); -extern std::string urlEncode(const std::string& strSrc); - -/* -void intIPtoStr(int ip,std::string& retStr); -int strIPtoInt(std::string& ipStr); -*/ - -template -std::string strJoin(Iterator first, Iterator last, std::string strSeperator) -{ - std::ostringstream ossValues; - - for (Iterator start = first; first != last; first++) - { - ossValues << str(boost::format("%s%s") % (start == first ? "" : strSeperator) % *first); - } - - return ossValues.str(); -} - -char charHex(int iDigit); - -template -std::string strHex(Iterator first, int iSize) -{ - std::string strDst; - - strDst.resize(iSize*2); - - for (int i = 0; i < iSize; i++) { - unsigned char c = *first++; - - strDst[i*2] = charHex(c >> 4); - strDst[i*2+1] = charHex(c & 15); - } - - return strDst; -} - -inline const std::string strHex(const std::string& strSrc) -{ - return strHex(strSrc.begin(), strSrc.size()); -} - -inline std::string strHex(const std::vector& vucData) -{ - return strHex(vucData.begin(), vucData.size()); -} - -inline std::string strHex(const uint64 uiHost) -{ - uint64_t uBig = htobe64(uiHost); - - return strHex((unsigned char*) &uBig, sizeof(uBig)); -} - -inline static std::string sqlEscape(const std::string& strSrc) -{ - static boost::format f("X'%s'"); - return str(boost::format(f) % strHex(strSrc)); -} - -inline static std::string sqlEscape(const std::vector& vecSrc) -{ - size_t size = vecSrc.size(); - if (size == 0) - return "X''"; - - std::string j(size * 2 + 3, 0); - - unsigned char *oPtr = reinterpret_cast(&*j.begin()); - const unsigned char *iPtr = &vecSrc[0]; - - *oPtr++ = 'X'; - *oPtr++ = '\''; - - for (int i = size; i != 0; --i) - { - unsigned char c = *iPtr++; - *oPtr++ = charHex(c >> 4); - *oPtr++ = charHex(c & 15); - } - - *oPtr++ = '\''; - return j; -} - -template -bool isZero(Iterator first, int iSize) -{ - while (iSize && !*first++) - --iSize; - - return !iSize; -} - -int charUnHex(char cDigit); -int strUnHex(std::string& strDst, const std::string& strSrc); - -uint64_t uintFromHex(const std::string& strSrc); - -std::vector strUnHex(const std::string& strSrc); - -std::vector strCopy(const std::string& strSrc); -std::string strCopy(const std::vector& vucSrc); - -bool parseIpPort(const std::string& strSource, std::string& strIP, int& iPort); -bool parseQuality(const std::string& strSource, uint32& uQuality); - DH* DH_der_load(const std::string& strDer); std::string DH_der_gen(int iKeyLength); @@ -185,45 +30,6 @@ void getRand(unsigned char *buf, int num); inline static void getRand(char *buf, int num) { return getRand(reinterpret_cast(buf), num); } inline static void getRand(void *buf, int num) { return getRand(reinterpret_cast(buf), num); } -inline std::string strGetEnv(const std::string& strKey) -{ - return getenv(strKey.c_str()) ? getenv(strKey.c_str()) : ""; -} - -template T lexical_cast_s(const std::string& string) -{ // lexically cast a string to the selected type. Does not throw - try - { - return boost::lexical_cast(string); - } - catch (...) - { - return 0; - } -} - -template std::string lexical_cast_i(const T& t) -{ // lexicaly cast the selected type to a string. Does not throw - try - { - return boost::lexical_cast(t); - } - catch (...) - { - return ""; - } -} - -template T lexical_cast_st(const std::string& string) -{ // lexically cast a string to the selected type. Does throw - return boost::lexical_cast(string); -} - -template std::string lexical_cast_it(const T& t) -{ // lexicaly cast the selected type to a string. Does not throw - return boost::lexical_cast(t); -} - template T range_check(const T& value, const T& minimum, const T& maximum) { if ((value < minimum) || (value > maximum)) @@ -252,8 +58,6 @@ template T range_check_cast(const U& value, const T& min return static_cast(value); } -bool parseUrl(const std::string& strUrl, std::string& strScheme, std::string& strDomain, int& iPort, std::string& strPath); - extern void NameThread(const char *); extern bool HaveSustain();