diff --git a/Core/Debugger/WebSocket/WebSocketUtils.cpp b/Core/Debugger/WebSocket/WebSocketUtils.cpp index 3a3536684c..013121ff80 100644 --- a/Core/Debugger/WebSocket/WebSocketUtils.cpp +++ b/Core/Debugger/WebSocket/WebSocketUtils.cpp @@ -17,6 +17,7 @@ #include #include +#include "util/text/parsers.h" #include "Common/StringUtils.h" #include "Core/Debugger/WebSocket/WebSocketUtils.h" diff --git a/Core/TextureReplacer.cpp b/Core/TextureReplacer.cpp index 05dd6c44f9..f403deba44 100644 --- a/Core/TextureReplacer.cpp +++ b/Core/TextureReplacer.cpp @@ -26,6 +26,7 @@ #include "i18n/i18n.h" #include "ext/xxhash.h" #include "file/ini_file.h" +#include "util/text/parsers.h" #include "Common/ColorConv.h" #include "Common/FileUtil.h" #include "Core/Config.h" diff --git a/Windows/GEDebugger/GEDebugger.cpp b/Windows/GEDebugger/GEDebugger.cpp index aa0de48d05..5fb0fcb72b 100644 --- a/Windows/GEDebugger/GEDebugger.cpp +++ b/Windows/GEDebugger/GEDebugger.cpp @@ -21,6 +21,7 @@ #include #include "base/stringutil.h" +#include "util/text/parsers.h" #include "Common/ColorConv.h" #include "Core/Config.h" #include "Core/Screenshot.h" diff --git a/ext/native/base/stringutil.cpp b/ext/native/base/stringutil.cpp index 2861f844d3..d0ff0a71d7 100644 --- a/ext/native/base/stringutil.cpp +++ b/ext/native/base/stringutil.cpp @@ -22,8 +22,6 @@ #include "base/stringutil.h" #ifdef _WIN32 -// Function Cross-Compatibility -#define strcasecmp _stricmp void OutputDebugStringUTF8(const char *p) { wchar_t temp[4096]; @@ -84,46 +82,6 @@ void StringUpper(char *str, int len) { } } - -unsigned int parseHex(const char *_szValue) -{ - int Value = 0; - size_t Finish = strlen(_szValue); - if (Finish > 8 ) { Finish = 8; } - - for (size_t Count = 0; Count < Finish; Count++) { - Value = (Value << 4); - switch( _szValue[Count] ) { - case '0': break; - case '1': Value += 1; break; - case '2': Value += 2; break; - case '3': Value += 3; break; - case '4': Value += 4; break; - case '5': Value += 5; break; - case '6': Value += 6; break; - case '7': Value += 7; break; - case '8': Value += 8; break; - case '9': Value += 9; break; - case 'A': Value += 10; break; - case 'a': Value += 10; break; - case 'B': Value += 11; break; - case 'b': Value += 11; break; - case 'C': Value += 12; break; - case 'c': Value += 12; break; - case 'D': Value += 13; break; - case 'd': Value += 13; break; - case 'E': Value += 14; break; - case 'e': Value += 14; break; - case 'F': Value += 15; break; - case 'f': Value += 15; break; - default: - Value = (Value >> 4); - Count = Finish; - } - } - return Value; -} - void DataToHexString(const uint8_t *data, size_t size, std::string *output) { Buffer buffer; for (size_t i = 0; i < size; i++) { @@ -223,49 +181,6 @@ std::string ArrayToString(const uint8_t *data, uint32_t size, int line_len, bool return oss.str(); } -bool TryParse(const std::string &str, uint32_t *const output) -{ - char *endptr = NULL; - - // Holy crap this is ugly. - - // Reset errno to a value other than ERANGE - errno = 0; - - unsigned long value = strtoul(str.c_str(), &endptr, 0); - - if (!endptr || *endptr) - return false; - - if (errno == ERANGE) - return false; - - if (ULONG_MAX > UINT_MAX) { -#ifdef _MSC_VER -#pragma warning (disable:4309) -#endif - // Note: The typecasts avoid GCC warnings when long is 32 bits wide. - if (value >= static_cast(0x100000000ull) - && value <= static_cast(0xFFFFFFFF00000000ull)) - return false; - } - - *output = static_cast(value); - return true; -} - -bool TryParse(const std::string &str, bool *const output) -{ - if ("1" == str || !strcasecmp("true", str.c_str())) - *output = true; - else if ("0" == str || !strcasecmp("false", str.c_str())) - *output = false; - else - return false; - - return true; -} - void SplitString(const std::string& str, const char delim, std::vector& output) { size_t next = 0; diff --git a/ext/native/base/stringutil.h b/ext/native/base/stringutil.h index c16602946f..744a725088 100644 --- a/ext/native/base/stringutil.h +++ b/ext/native/base/stringutil.h @@ -9,6 +9,7 @@ #ifdef _MSC_VER #pragma warning (disable:4996) #define strncasecmp _strnicmp +#define strcasecmp _stricmp #else #include #endif @@ -59,10 +60,6 @@ inline void StringToHexString(const std::string &data, std::string *output) { DataToHexString((uint8_t *)(&data[0]), data.size(), output); } - -// highly unsafe and not recommended. -unsigned int parseHex(const char* _szValue); - std::string StringFromFormat(const char* format, ...); std::string StringFromInt(int value); std::string StringFromBool(bool value); @@ -72,23 +69,6 @@ std::string ArrayToString(const uint8_t *data, uint32_t size, int line_len = 20, std::string StripSpaces(const std::string &s); std::string StripQuotes(const std::string &s); -bool TryParse(const std::string &str, bool *const output); -bool TryParse(const std::string &str, uint32_t *const output); - -template -static bool TryParse(const std::string &str, N *const output) -{ - std::istringstream iss(str); - - N tmp = 0; - if (iss >> tmp) - { - *output = tmp; - return true; - } - else - return false; -} void SplitString(const std::string& str, const char delim, std::vector& output); void GetQuotedStrings(const std::string& str, std::vector& output); @@ -98,14 +78,6 @@ std::string ReplaceAll(std::string input, const std::string& src, const std::str // Compare two strings, ignore the difference between the ignorestr1 and the ignorestr2 in str1 and str2. int strcmpIgnore(std::string str1, std::string str2, std::string ignorestr1, std::string ignorestr2); -template -static std::string ValueToString(const N value) -{ - std::stringstream string; - string << value; - return string.str(); -} - void StringTrimEndNonAlphaNum(char *str); void SkipSpace(const char **ptr); void StringUpper(char *str); diff --git a/ext/native/file/file_util.cpp b/ext/native/file/file_util.cpp index 5b1087b425..d95dcef311 100644 --- a/ext/native/file/file_util.cpp +++ b/ext/native/file/file_util.cpp @@ -4,9 +4,6 @@ #define WIN32_LEAN_AND_MEAN #include #include -#ifndef strcasecmp -#define strcasecmp _stricmp -#endif #else #include #include @@ -23,6 +20,7 @@ #include "base/logging.h" #include "base/basictypes.h" +#include "base/stringutil.h" #include "file/file_util.h" #include "util/text/utf8.h" diff --git a/ext/native/file/ini_file.cpp b/ext/native/file/ini_file.cpp index 99bdcd163a..f8a96025a2 100644 --- a/ext/native/file/ini_file.cpp +++ b/ext/native/file/ini_file.cpp @@ -19,11 +19,10 @@ #include "base/stringutil.h" #include "file/ini_file.h" #include "file/vfs.h" +#include "util/text/parsers.h" #ifdef _WIN32 #include "../util/text/utf8.h" - // Function Cross-Compatibility -#define strcasecmp _stricmp #endif static bool ParseLineKey(const std::string &line, size_t &pos, std::string *keyOut) { diff --git a/ext/native/net/websocket_server.cpp b/ext/native/net/websocket_server.cpp index 42efb36647..dc9b361884 100644 --- a/ext/native/net/websocket_server.cpp +++ b/ext/native/net/websocket_server.cpp @@ -23,11 +23,6 @@ // TODO: Not a great cross dependency. #include "Common/Crypto/sha1.h" -#ifdef _WIN32 -// Function Cross-Compatibility -#define strcasecmp _stricmp -#endif - static const char *const WEBSOCKET_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; namespace net { diff --git a/ext/native/util/text/parsers.cpp b/ext/native/util/text/parsers.cpp index 8024324b08..8a381c0286 100644 --- a/ext/native/util/text/parsers.cpp +++ b/ext/native/util/text/parsers.cpp @@ -1,6 +1,7 @@ +#include #include -#include +#include "base/stringutil.h" #include "util/text/parsers.h" bool Version::ParseVersionString(std::string str) { @@ -37,3 +38,44 @@ bool ParseMacAddress(std::string str, uint8_t macAddr[6]) { } return true; } + +bool TryParse(const std::string &str, uint32_t *const output) { + char *endptr = NULL; + + // Holy crap this is ugly. + + // Reset errno to a value other than ERANGE + errno = 0; + + unsigned long value = strtoul(str.c_str(), &endptr, 0); + + if (!endptr || *endptr) + return false; + + if (errno == ERANGE) + return false; + + if (ULONG_MAX > UINT_MAX) { +#ifdef _MSC_VER +#pragma warning (disable:4309) +#endif + // Note: The typecasts avoid GCC warnings when long is 32 bits wide. + if (value >= static_cast(0x100000000ull) + && value <= static_cast(0xFFFFFFFF00000000ull)) + return false; + } + + *output = static_cast(value); + return true; +} + +bool TryParse(const std::string &str, bool *const output) { + if ("1" == str || !strcasecmp("true", str.c_str())) + *output = true; + else if ("0" == str || !strcasecmp("false", str.c_str())) + *output = false; + else + return false; + + return true; +} diff --git a/ext/native/util/text/parsers.h b/ext/native/util/text/parsers.h index fbf2c36d99..a2bf62dfd4 100644 --- a/ext/native/util/text/parsers.h +++ b/ext/native/util/text/parsers.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "base/basictypes.h" @@ -53,4 +54,19 @@ private: bool ParseVersionString(std::string str); }; -bool ParseMacAddress(std::string str, uint8_t macAddr[6]); \ No newline at end of file +bool ParseMacAddress(std::string str, uint8_t macAddr[6]); + +bool TryParse(const std::string &str, bool *const output); +bool TryParse(const std::string &str, uint32_t *const output); + +template +static bool TryParse(const std::string &str, N *const output) { + std::istringstream iss(str); + + N tmp = 0; + if (iss >> tmp) { + *output = tmp; + return true; + } else + return false; +}