From fdcd5a9757e6c44a8fdc5331c0842c47adc34643 Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Sun, 27 Jan 2013 12:14:37 +0200 Subject: [PATCH] Tweaks --- Common/StringUtil.cpp | 65 ++++++++++++++++++++++++++++++ Common/StringUtil.h | 41 +++++++++++++++++++ Library/Library.vcxproj | 2 + Library/Library.vcxproj.filters | 6 +++ Library/Litestep.cpp | 70 +-------------------------------- Library/Litestep.h | 6 +-- Library/Measure.cpp | 8 ++-- Library/MeasureScript.cpp | 6 +-- Library/UpdateCheck.cpp | 2 +- Library/lua/LuaManager.cpp | 9 +++-- Library/lua/LuaScript.cpp | 5 ++- Library/lua/glue/LuaGlobal.cpp | 2 +- 12 files changed, 134 insertions(+), 88 deletions(-) create mode 100644 Common/StringUtil.cpp create mode 100644 Common/StringUtil.h diff --git a/Common/StringUtil.cpp b/Common/StringUtil.cpp new file mode 100644 index 00000000..0c85fc0d --- /dev/null +++ b/Common/StringUtil.cpp @@ -0,0 +1,65 @@ +/* + Copyright (C) 2013 Birunthan Mohanathas + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "StringUtil.h" + +namespace StringUtil { + +std::string Narrow(const WCHAR* str, int strLen, int cp) +{ + std::string narrowStr; + + if (str && *str) + { + if (strLen == -1) + { + strLen = (int)wcslen(str); + } + + int bufLen = WideCharToMultiByte(cp, 0, str, strLen, NULL, 0, NULL, NULL); + if (bufLen > 0) + { + narrowStr.resize(bufLen); + WideCharToMultiByte(cp, 0, str, strLen, &narrowStr[0], bufLen, NULL, NULL); + } + } + return narrowStr; +} + +std::wstring Widen(const char* str, int strLen, int cp) +{ + std::wstring wideStr; + + if (str && *str) + { + if (strLen == -1) + { + strLen = strlen(str); + } + + int bufLen = MultiByteToWideChar(cp, 0, str, strLen, NULL, 0); + if (bufLen > 0) + { + wideStr.resize(bufLen); + MultiByteToWideChar(cp, 0, str, strLen, &wideStr[0], bufLen); + } + } + return wideStr; +} + +} // namespace StringUtil diff --git a/Common/StringUtil.h b/Common/StringUtil.h new file mode 100644 index 00000000..87c76712 --- /dev/null +++ b/Common/StringUtil.h @@ -0,0 +1,41 @@ +/* + Copyright (C) 2013 Birunthan Mohanathas + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#ifndef RM_COMMON_STRINGUTIL_H_ +#define RM_COMMON_STRINGUTIL_H_ + +#include +#include + +namespace StringUtil { + +std::string Narrow(const WCHAR* str, int strLen = -1, int cp = CP_ACP); +inline std::string Narrow(const std::wstring& str, int cp = CP_ACP) { return Narrow(str.c_str(), (int)str.length(), cp); } + +inline std::string NarrowUTF8(const WCHAR* str, int strLen = -1) { return Narrow(str, strLen, CP_UTF8); } +inline std::string NarrowUTF8(const std::wstring& str) { return Narrow(str.c_str(), (int)str.length(), CP_UTF8); } + +std::wstring Widen(const char* str, int strLen = -1, int cp = CP_ACP); +inline std::wstring Widen(const std::string& str, int cp = CP_ACP) { return Widen(str.c_str(), (int)str.length(), cp); } + +inline std::wstring WidenUTF8(const char* str, int strLen = -1) { return Widen(str, strLen, CP_UTF8); } +inline std::wstring WidenUTF8(const std::string& str) { return Widen(str.c_str(), (int)str.length(), CP_UTF8); } + +} // namespace StringUtil + +#endif diff --git a/Library/Library.vcxproj b/Library/Library.vcxproj index 3774f52c..18fccff3 100644 --- a/Library/Library.vcxproj +++ b/Library/Library.vcxproj @@ -66,6 +66,7 @@ + Use @@ -274,6 +275,7 @@ + diff --git a/Library/Library.vcxproj.filters b/Library/Library.vcxproj.filters index 241c76b7..d839bd13 100644 --- a/Library/Library.vcxproj.filters +++ b/Library/Library.vcxproj.filters @@ -333,6 +333,9 @@ Common + + Common + @@ -572,6 +575,9 @@ Common + + Common + diff --git a/Library/Litestep.cpp b/Library/Litestep.cpp index b3db2532..0d4ffdc0 100644 --- a/Library/Litestep.cpp +++ b/Library/Litestep.cpp @@ -104,74 +104,6 @@ void RunFile(const WCHAR* file, const WCHAR* args) } } -std::string ConvertToAscii(LPCTSTR str) -{ - std::string szAscii; - - if (str && *str) - { - int strLen = (int)wcslen(str); - int bufLen = WideCharToMultiByte(CP_ACP, 0, str, strLen, NULL, 0, NULL, NULL); - if (bufLen > 0) - { - szAscii.resize(bufLen); - WideCharToMultiByte(CP_ACP, 0, str, strLen, &szAscii[0], bufLen, NULL, NULL); - } - } - return szAscii; -} - -std::wstring ConvertToWide(LPCSTR str) -{ - std::wstring szWide; - - if (str && *str) - { - int strLen = (int)strlen(str); - int bufLen = MultiByteToWideChar(CP_ACP, 0, str, strLen, NULL, 0); - if (bufLen > 0) - { - szWide.resize(bufLen); - MultiByteToWideChar(CP_ACP, 0, str, strLen, &szWide[0], bufLen); - } - } - return szWide; -} - -std::string ConvertToUTF8(LPCWSTR str) -{ - std::string szAscii; - - if (str && *str) - { - int strLen = (int)wcslen(str); - int bufLen = WideCharToMultiByte(CP_UTF8, 0, str, strLen, NULL, 0, NULL, NULL); - if (bufLen > 0) - { - szAscii.resize(bufLen); - WideCharToMultiByte(CP_UTF8, 0, str, strLen, &szAscii[0], bufLen, NULL, NULL); - } - } - return szAscii; -} - -std::wstring ConvertUTF8ToWide(LPCSTR str) -{ - std::wstring szWide; - - if (str && *str) - { - int strLen = (int)strlen(str); - int bufLen = MultiByteToWideChar(CP_UTF8, 0, str, strLen, NULL, 0); - if (bufLen > 0) - { - szWide.resize(bufLen); - MultiByteToWideChar(CP_UTF8, 0, str, strLen, &szWide[0], bufLen); - } - } - return szWide; -} - void LogInternal(int nLevel, ULONGLONG elapsed, LPCTSTR pszMessage) { // Add timestamp @@ -211,7 +143,7 @@ void LogInternal(int nLevel, ULONGLONG elapsed, LPCTSTR pszMessage) message += L'\n'; #ifdef _DEBUG - _RPT0(_CRT_WARN, ConvertToAscii(message.c_str()).c_str()); + _RPT0(_CRT_WARN, StringUtil::Narrow(message).c_str()); if (!Rainmeter->GetLogging()) return; #endif diff --git a/Library/Litestep.h b/Library/Litestep.h index bd34db17..a5b54eaa 100644 --- a/Library/Litestep.h +++ b/Library/Litestep.h @@ -22,6 +22,7 @@ #include #include #include +#include "../Common/StringUtil.h" #include "Error.h" enum LOGLEVEL @@ -40,11 +41,6 @@ UINT GetUniqueID(); template UINT TypeID() { static UINT id = GetUniqueID(); return id; } -std::string ConvertToAscii(LPCTSTR str); -std::wstring ConvertToWide(LPCSTR str); -std::string ConvertToUTF8(LPCWSTR str); -std::wstring ConvertUTF8ToWide(LPCSTR str); - void Log(int nLevel, const WCHAR* message); void LogWithArgs(int nLevel, const WCHAR* format, ...); void LogError(CError& error); diff --git a/Library/Measure.cpp b/Library/Measure.cpp index 088f86cf..d0fbad1a 100644 --- a/Library/Measure.cpp +++ b/Library/Measure.cpp @@ -239,7 +239,7 @@ const WCHAR* CMeasure::CheckSubstitute(const WCHAR* buffer) } else // Contains a RegEx { - std::string utf8str = ConvertToUTF8(buffer); + std::string utf8str = StringUtil::NarrowUTF8(buffer); int* ovector = new int[OVECCOUNT]; for (size_t i = 0, isize = m_Substitute.size(); i < isize ; i += 2) @@ -252,7 +252,7 @@ const WCHAR* CMeasure::CheckSubstitute(const WCHAR* buffer) int offset = 0; re = pcre_compile( - ConvertToUTF8(m_Substitute[i].c_str()).c_str(), // the pattern + StringUtil::NarrowUTF8(m_Substitute[i]).c_str(), // the pattern flags, // default options &error, // for error message &erroffset, // for error offset @@ -283,7 +283,7 @@ const WCHAR* CMeasure::CheckSubstitute(const WCHAR* buffer) } else { - std::string result = ConvertToUTF8(m_Substitute[i + 1].c_str()); + std::string result = StringUtil::NarrowUTF8(m_Substitute[i + 1]); if (rc > 1) { @@ -324,7 +324,7 @@ const WCHAR* CMeasure::CheckSubstitute(const WCHAR* buffer) delete [] ovector; - str = ConvertUTF8ToWide(utf8str.c_str()); + str = StringUtil::WidenUTF8(utf8str); } return str.c_str(); diff --git a/Library/MeasureScript.cpp b/Library/MeasureScript.cpp index fd33ca31..7608dbc4 100644 --- a/Library/MeasureScript.cpp +++ b/Library/MeasureScript.cpp @@ -143,12 +143,12 @@ void CMeasureScript::ReadOptions(CConfigParser& parser, const WCHAR* section) lua_pop(L, 1); const char* strKey = lua_tostring(L, -1); - std::wstring wstrKey = ConvertToWide(strKey); + std::wstring wstrKey = StringUtil::Widen(strKey); const std::wstring& wstrValue = parser.ReadString(section, wstrKey.c_str(), L""); if (!wstrValue.empty()) { - std::string strStrVal = ConvertToAscii(wstrValue.c_str()); + std::string strStrVal = StringUtil::Narrow(wstrValue); const char* strValue = strStrVal.c_str(); lua_pushstring(L, strValue); @@ -186,7 +186,7 @@ void CMeasureScript::ReadOptions(CConfigParser& parser, const WCHAR* section) */ void CMeasureScript::Command(const std::wstring& command) { - std::string str = ConvertToAscii(command.c_str()); + std::string str = StringUtil::Narrow(command); m_LuaScript.RunString(str.c_str()); } diff --git a/Library/UpdateCheck.cpp b/Library/UpdateCheck.cpp index b89a9ca0..150a44a3 100644 --- a/Library/UpdateCheck.cpp +++ b/Library/UpdateCheck.cpp @@ -64,7 +64,7 @@ void CheckVersion(void* dummy) return version; }; - std::wstring tmpSz = ConvertToWide(urlData); + std::wstring tmpSz = StringUtil::Widen(urlData); const WCHAR* version = tmpSz.c_str(); int availableVersion = parseVersion(version); diff --git a/Library/lua/LuaManager.cpp b/Library/lua/LuaManager.cpp index f130e384..bad11a20 100644 --- a/Library/lua/LuaManager.cpp +++ b/Library/lua/LuaManager.cpp @@ -69,16 +69,19 @@ void LuaManager::ReportErrors(lua_State* L, const std::wstring& file) } std::wstring str(file, file.find_last_of(L'\\') + 1); - str += ConvertToWide(error); + str += StringUtil::Widen(error); LogWithArgs(LOG_ERROR, L"Script: %s", str.c_str()); } void LuaManager::PushWide(lua_State* L, const WCHAR* str) { - lua_pushstring(L, ConvertToAscii(str).c_str()); + const std::string tmpStr = StringUtil::Narrow(str); + lua_pushlstring(L, tmpStr.c_str(), tmpStr.length()); } std::wstring LuaManager::ToWide(lua_State* L, int narg) { - return ConvertToWide(lua_tostring(L, narg)); + size_t strLen = 0; + const char* str = lua_tolstring(L, narg, &strLen); + return StringUtil::Widen(str, (int)strLen); } diff --git a/Library/lua/LuaScript.cpp b/Library/lua/LuaScript.cpp index 53ba6be5..1e5a35c8 100644 --- a/Library/lua/LuaScript.cpp +++ b/Library/lua/LuaScript.cpp @@ -206,8 +206,9 @@ int LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, std } else if (type == LUA_TSTRING) { - const char* str = lua_tostring(L, -1); - strValue = ConvertToWide(str); + size_t strLen = 0; + const char* str = lua_tolstring(L, -1, &strLen); + strValue = StringUtil::Widen(str, (int)strLen); numValue = strtod(str, NULL); } diff --git a/Library/lua/glue/LuaGlobal.cpp b/Library/lua/glue/LuaGlobal.cpp index df70f0f5..e54515c9 100644 --- a/Library/lua/glue/LuaGlobal.cpp +++ b/Library/lua/glue/LuaGlobal.cpp @@ -53,7 +53,7 @@ static int Print(lua_State* L) lua_pop(L, 1); } - Log(LOG_DEBUG, ConvertToWide(message.c_str()).c_str()); + Log(LOG_DEBUG, StringUtil::Widen(message).c_str()); return 0; }