From 87003fef8d08fc8ebdc5a71272e5a75726c2795c Mon Sep 17 00:00:00 2001 From: mrexodia Date: Tue, 22 Nov 2016 00:07:35 +0100 Subject: [PATCH] removed some dependencies --- btparser/btparser.vcxproj | 7 +- btparser/btparser.vcxproj.filters | 21 +-- btparser/dynamicmem.h | 71 ----------- btparser/filehelper.cpp | 43 ------- btparser/filehelper.h | 18 --- btparser/handle.h | 51 -------- btparser/helpers.h | 147 +++++++++++++++++++++ btparser/lexer.cpp | 4 +- btparser/main.cpp | 2 +- btparser/parser.cpp | 1 - btparser/stringutils.cpp | 204 ------------------------------ btparser/stringutils.h | 35 ----- 12 files changed, 154 insertions(+), 450 deletions(-) delete mode 100644 btparser/dynamicmem.h delete mode 100644 btparser/filehelper.cpp delete mode 100644 btparser/filehelper.h delete mode 100644 btparser/handle.h create mode 100644 btparser/helpers.h delete mode 100644 btparser/stringutils.cpp delete mode 100644 btparser/stringutils.h diff --git a/btparser/btparser.vcxproj b/btparser/btparser.vcxproj index af2e056..4811b58 100644 --- a/btparser/btparser.vcxproj +++ b/btparser/btparser.vcxproj @@ -19,21 +19,16 @@ - - - - - + - diff --git a/btparser/btparser.vcxproj.filters b/btparser/btparser.vcxproj.filters index 526b5cc..a7b5f10 100644 --- a/btparser/btparser.vcxproj.filters +++ b/btparser/btparser.vcxproj.filters @@ -18,12 +18,6 @@ Source Files - - Source Files - - - Source Files - Source Files @@ -32,21 +26,9 @@ - - Header Files - Header Files - - Header Files - - - Header Files - - - Header Files - Header Files @@ -59,6 +41,9 @@ Header Files + + Header Files + diff --git a/btparser/dynamicmem.h b/btparser/dynamicmem.h deleted file mode 100644 index 50a8773..0000000 --- a/btparser/dynamicmem.h +++ /dev/null @@ -1,71 +0,0 @@ -#pragma once - -static void* emalloc(size_t size) -{ - return malloc(size); -} - -static void efree(void* ptr) -{ - free(ptr); -} - -static void* erealloc(void* ptr, size_t size) -{ - return realloc(ptr, size); -} - -template -class Memory -{ -public: - // - // This class guarantees that the returned allocated memory - // will always be zeroed - // - explicit Memory(const char* Reason = "Memory:???") - { - m_Ptr = nullptr; - m_Size = 0; - m_Reason = Reason; - } - - explicit Memory(size_t Size, const char* Reason = "Memory:???") - { - m_Ptr = reinterpret_cast(emalloc(Size)); - m_Size = Size; - m_Reason = Reason; - - memset(m_Ptr, 0, Size); - } - - ~Memory() - { - if(m_Ptr) - efree(m_Ptr); - } - - T realloc(size_t Size, const char* Reason = "Memory:???") - { - m_Ptr = reinterpret_cast(erealloc(m_Ptr, Size)); - m_Size = Size; - m_Reason = Reason; - - return (T)memset(m_Ptr, 0, m_Size); - } - - size_t size() - { - return m_Size; - } - - T operator()() - { - return m_Ptr; - } - -private: - T m_Ptr; - size_t m_Size; - const char* m_Reason; -}; \ No newline at end of file diff --git a/btparser/filehelper.cpp b/btparser/filehelper.cpp deleted file mode 100644 index 260ed9c..0000000 --- a/btparser/filehelper.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "filehelper.h" -#include "handle.h" -#include "stringutils.h" - -bool FileHelper::ReadAllData(const String & fileName, std::vector & content) -{ - Handle hFile = CreateFileW(StringUtils::Utf8ToUtf16(fileName).c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); - if(hFile == INVALID_HANDLE_VALUE) - return false; - unsigned int filesize = GetFileSize(hFile, nullptr); - if(!filesize) - { - content.clear(); - return true; - } - content.resize(filesize); - DWORD read = 0; - return !!ReadFile(hFile, content.data(), filesize, &read, nullptr); -} - -bool FileHelper::WriteAllData(const String & fileName, const void* data, size_t size) -{ - Handle hFile = CreateFileW(StringUtils::Utf8ToUtf16(fileName).c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr); - if(hFile == INVALID_HANDLE_VALUE) - return false; - DWORD written = 0; - return !!WriteFile(hFile, data, DWORD(size), &written, nullptr); -} - -bool FileHelper::ReadAllText(const String & fileName, String & content) -{ - std::vector data; - if(!ReadAllData(fileName, data)) - return false; - data.push_back(0); - content = String((const char*)data.data()); - return true; -} - -bool FileHelper::WriteAllText(const String & fileName, const String & content) -{ - return WriteAllData(fileName, content.c_str(), content.length()); -} diff --git a/btparser/filehelper.h b/btparser/filehelper.h deleted file mode 100644 index a0297dd..0000000 --- a/btparser/filehelper.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef _FILEREADER_H -#define _FILEREADER_H - -#include -#include - -using String = std::string; - -class FileHelper -{ -public: - static bool ReadAllData(const String & fileName, std::vector & content); - static bool WriteAllData(const String & fileName, const void* data, size_t size); - static bool ReadAllText(const String & fileName, String & content); - static bool WriteAllText(const String & fileName, const String & content); -}; - -#endif //_FILEREADER_H \ No newline at end of file diff --git a/btparser/handle.h b/btparser/handle.h deleted file mode 100644 index f98b556..0000000 --- a/btparser/handle.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef _HANDLE_H -#define _HANDLE_H - -#include - -class Handle -{ -public: - Handle(HANDLE h = nullptr) - { - mHandle = h; - } - - ~Handle() - { - Close(); - } - - void Close() - { - DWORD dwFlags = 0; - if(GetHandleInformation(mHandle, &dwFlags) && !(dwFlags & HANDLE_FLAG_PROTECT_FROM_CLOSE)) - CloseHandle(mHandle); - mHandle = INVALID_HANDLE_VALUE; - } - - const HANDLE & operator=(const HANDLE & h) - { - return mHandle = h; - } - - operator HANDLE & () - { - return mHandle; - } - - bool operator!() const - { - return (!mHandle || mHandle == INVALID_HANDLE_VALUE); - } - - operator bool() const - { - return !this; - } - -private: - HANDLE mHandle; -}; - -#endif //_HANDLE_H \ No newline at end of file diff --git a/btparser/helpers.h b/btparser/helpers.h new file mode 100644 index 0000000..34280e9 --- /dev/null +++ b/btparser/helpers.h @@ -0,0 +1,147 @@ +#pragma once + +#include +#include +#include +#include + +namespace StringUtils +{ + static std::string sprintf(const char* format, ...) + { + va_list args; + va_start(args, format); + std::vector buffer(256, '\0'); + while(true) + { + int res = _vsnprintf_s(buffer.data(), buffer.size(), _TRUNCATE, format, args); + if(res == -1) + { + buffer.resize(buffer.size() * 2); + continue; + } + else + break; + } + va_end(args); + return std::string(buffer.data()); + } + + static std::string Escape(const std::string & s) + { + auto escape = [](unsigned char ch) -> std::string + { + char buf[8] = ""; + switch(ch) + { + case '\0': + return "\\0"; + case '\t': + return "\\t"; + case '\f': + return "\\f"; + case '\v': + return "\\v"; + case '\n': + return "\\n"; + case '\r': + return "\\r"; + case '\\': + return "\\\\"; + case '\"': + return "\\\""; + default: + if(!isprint(ch)) //unknown unprintable character + sprintf_s(buf, "\\x%02X", ch); + else + *buf = ch; + return buf; + } + }; + std::string escaped; + escaped.reserve(s.length() + s.length() / 2); + for(size_t i = 0; i < s.length(); i++) + escaped.append(escape((unsigned char)s[i])); + return escaped; + } + + static std::string Utf16ToUtf8(const std::wstring & wstr) + { + std::string convertedString; + auto requiredSize = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr); + if(requiredSize > 0) + { + std::vector buffer(requiredSize); + WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &buffer[0], requiredSize, nullptr, nullptr); + convertedString.assign(buffer.begin(), buffer.end() - 1); + } + return convertedString; + } + + static std::wstring Utf8ToUtf16(const std::string & str) + { + std::wstring convertedString; + int requiredSize = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0); + if(requiredSize > 0) + { + std::vector buffer(requiredSize); + MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &buffer[0], requiredSize); + convertedString.assign(buffer.begin(), buffer.end() - 1); + } + return convertedString; + } +}; + +namespace FileHelper +{ + static bool ReadAllData(const std::string & fileName, std::vector & content) + { + auto hFile = CreateFileW(StringUtils::Utf8ToUtf16(fileName).c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); + auto result = false; + if(hFile != INVALID_HANDLE_VALUE) + { + unsigned int filesize = GetFileSize(hFile, nullptr); + if(!filesize) + { + content.clear(); + result = true; + } + else + { + content.resize(filesize); + DWORD read = 0; + result = !!ReadFile(hFile, content.data(), filesize, &read, nullptr); + } + CloseHandle(hFile); + } + return result; + } + + static bool WriteAllData(const std::string & fileName, const void* data, size_t size) + { + auto hFile = CreateFileW(StringUtils::Utf8ToUtf16(fileName).c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr); + auto result = false; + if(hFile != INVALID_HANDLE_VALUE) + { + DWORD written = 0; + result = !!WriteFile(hFile, data, DWORD(size), &written, nullptr); + CloseHandle(hFile); + } + return result; + } + + static bool ReadAllText(const std::string & fileName, std::string & content) + { + std::vector data; + if(!ReadAllData(fileName, data)) + return false; + data.push_back(0); + content = std::string((const char*)data.data()); + return true; + } + + static bool WriteAllText(const std::string & fileName, const std::string & content) + { + return WriteAllData(fileName, content.c_str(), content.length()); + } +}; \ No newline at end of file diff --git a/btparser/lexer.cpp b/btparser/lexer.cpp index efa7e53..82c177e 100644 --- a/btparser/lexer.cpp +++ b/btparser/lexer.cpp @@ -1,7 +1,7 @@ #include "lexer.h" -#include "stringutils.h" #include -#include "filehelper.h" +#include +#include "helpers.h" #define MAKE_OP_TRIPLE(ch1, ch2, ch3) (ch3 << 16 | ch2 << 8 | ch1) #define MAKE_OP_DOUBLE(ch1, ch2) (ch2 << 8 | ch1) diff --git a/btparser/main.cpp b/btparser/main.cpp index 0a42ad1..cdcc96c 100644 --- a/btparser/main.cpp +++ b/btparser/main.cpp @@ -2,8 +2,8 @@ #include #include "testfiles.h" #include "lexer.h" -#include "filehelper.h" #include "parser.h" +#include "helpers.h" bool TestLexer(Lexer & lexer, const std::string & filename) { diff --git a/btparser/parser.cpp b/btparser/parser.cpp index 167fec3..1eba52a 100644 --- a/btparser/parser.cpp +++ b/btparser/parser.cpp @@ -1,5 +1,4 @@ #include "parser.h" -#include "stringutils.h" using namespace AST; diff --git a/btparser/stringutils.cpp b/btparser/stringutils.cpp deleted file mode 100644 index 81e3e1b..0000000 --- a/btparser/stringutils.cpp +++ /dev/null @@ -1,204 +0,0 @@ -#include -#include "stringutils.h" -#include "memory.h" -#include "dynamicmem.h" -#include -#include - -StringList StringUtils::Split(const String & s, char delim, std::vector & elems) -{ - std::stringstream ss(s); - String item; - while(std::getline(ss, item, delim)) - { - if(!item.length()) - continue; - elems.push_back(item); - } - return elems; -} - -StringList StringUtils::Split(const String & s, char delim) -{ - std::vector elems; - Split(s, delim, elems); - return elems; -} - -String StringUtils::Escape(const String & s) -{ - String escaped = ""; - for(size_t i = 0; i < s.length(); i++) - { - auto ch = uint8_t(s[i]); - switch(ch) - { - case '\0': - escaped += "\\0"; - break; - case '\t': - escaped += "\\t"; - break; - case '\f': - escaped += "\\f"; - break; - case '\v': - escaped += "\\v"; - break; - case '\n': - escaped += "\\n"; - break; - case '\r': - escaped += "\\r"; - break; - case '\\': - escaped += "\\\\"; - break; - case '\"': - escaped += "\\\""; - break; - default: - if(!isprint(ch)) //unknown unprintable character - { - char buf[16] = ""; - sprintf_s(buf, "\\x%02X", ch); - escaped += buf; - } - else - escaped += ch; - break; - } - } - return escaped; -} - -//Trim functions taken from: http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring/16743707#16743707 -const String StringUtils::WHITESPACE = " \n\r\t"; - -String StringUtils::Trim(const String & s) -{ - return TrimRight(TrimLeft(s)); -} - -String StringUtils::TrimLeft(const String & s) -{ - size_t startpos = s.find_first_not_of(StringUtils::WHITESPACE); - return (startpos == String::npos) ? "" : s.substr(startpos); -} - -String StringUtils::TrimRight(const String & s) -{ - size_t endpos = s.find_last_not_of(StringUtils::WHITESPACE); - return (endpos == String::npos) ? "" : s.substr(0, endpos + 1); -} - -//Conversion functions taken from: http://www.nubaria.com/en/blog/?p=289 -String StringUtils::Utf16ToUtf8(const WString & wstr) -{ - String convertedString; - auto requiredSize = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr); - if(requiredSize > 0) - { - std::vector buffer(requiredSize); - WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &buffer[0], requiredSize, nullptr, nullptr); - convertedString.assign(buffer.begin(), buffer.end() - 1); - } - return convertedString; -} - -String StringUtils::Utf16ToUtf8(const wchar_t* wstr) -{ - return Utf16ToUtf8(wstr ? WString(wstr) : WString()); -} - -WString StringUtils::Utf8ToUtf16(const String & str) -{ - WString convertedString; - int requiredSize = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0); - if(requiredSize > 0) - { - std::vector buffer(requiredSize); - MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &buffer[0], requiredSize); - convertedString.assign(buffer.begin(), buffer.end() - 1); - } - return convertedString; -} - -WString StringUtils::Utf8ToUtf16(const char* str) -{ - return Utf8ToUtf16(str ? String(str) : String()); -} - -//Taken from: http://stackoverflow.com/a/24315631 -void StringUtils::ReplaceAll(String & s, const String & from, const String & to) -{ - size_t start_pos = 0; - while((start_pos = s.find(from, start_pos)) != std::string::npos) - { - s.replace(start_pos, from.length(), to); - start_pos += to.length(); // Handles case where 'to' is a substring of 'from' - } -} - -void StringUtils::ReplaceAll(WString & s, const WString & from, const WString & to) -{ - size_t start_pos = 0; - while((start_pos = s.find(from, start_pos)) != std::string::npos) - { - s.replace(start_pos, from.length(), to); - start_pos += to.length(); // Handles case where 'to' is a substring of 'from' - } -} - -String StringUtils::sprintf(const char* format, ...) -{ - va_list args; - va_start(args, format); - Memory buffer(256 * sizeof(char), "StringUtils::sprintf"); - while(true) - { - int res = _vsnprintf_s(buffer(), buffer.size(), _TRUNCATE, format, args); - if(res == -1) - { - buffer.realloc(buffer.size() * 2, "StringUtils::sprintf"); - continue; - } - else - break; - } - va_end(args); - return String(buffer()); -} - -WString StringUtils::sprintf(const wchar_t* format, ...) -{ - va_list args; - va_start(args, format); - Memory buffer(256 * sizeof(wchar_t), "StringUtils::sprintf"); - while(true) - { - int res = _vsnwprintf_s(buffer(), buffer.size(), _TRUNCATE, format, args); - if(res == -1) - { - buffer.realloc(buffer.size() * 2, "StringUtils::sprintf"); - continue; - } - else - break; - } - va_end(args); - return WString(buffer()); -} - -String StringUtils::ToLower(const String & s) -{ - auto result = s; - for(size_t i = 0; i < result.size(); i++) - result[i] = tolower(result[i]); - return result; -} - -bool StringUtils::StartsWith(const String & h, const String & n) -{ - return strstr(h.c_str(), n.c_str()) == h.c_str(); -} diff --git a/btparser/stringutils.h b/btparser/stringutils.h deleted file mode 100644 index 04b53b5..0000000 --- a/btparser/stringutils.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef _STRINGUTILS_H -#define _STRINGUTILS_H - -#include - -typedef std::string String; -typedef std::wstring WString; -typedef std::vector StringList; -typedef std::vector WStringList; - -class StringUtils -{ -public: - static StringList Split(const String & s, char delim, std::vector & elems); - static StringList Split(const String & s, char delim); - static String Escape(const String & s); - static String Trim(const String & s); - static String TrimLeft(const String & s); - static String TrimRight(const String & s); - static String Utf16ToUtf8(const WString & wstr); - static String Utf16ToUtf8(const wchar_t* wstr); - static WString Utf8ToUtf16(const String & str); - static WString Utf8ToUtf16(const char* str); - static void ReplaceAll(String & s, const String & from, const String & to); - static void ReplaceAll(WString & s, const WString & from, const WString & to); - static String sprintf(const char* format, ...); - static WString sprintf(const wchar_t* format, ...); - static String ToLower(const String & s); - static bool StartsWith(const String & h, const String & n); - -private: - static const String WHITESPACE; -}; - -#endif //_STRINGUTILS_H \ No newline at end of file