This commit is contained in:
Birunthan Mohanathas 2013-01-27 12:14:37 +02:00
parent 654599d192
commit fdcd5a9757
12 changed files with 134 additions and 88 deletions

65
Common/StringUtil.cpp Normal file
View File

@ -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

41
Common/StringUtil.h Normal file
View File

@ -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 <Windows.h>
#include <string>
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

View File

@ -66,6 +66,7 @@
<ClCompile Include="..\Common\ControlTemplate.cpp" /> <ClCompile Include="..\Common\ControlTemplate.cpp" />
<ClCompile Include="..\Common\Dialog.cpp" /> <ClCompile Include="..\Common\Dialog.cpp" />
<ClCompile Include="..\Common\MenuTemplate.cpp" /> <ClCompile Include="..\Common\MenuTemplate.cpp" />
<ClCompile Include="..\Common\StringUtil.cpp" />
<ClCompile Include="ConfigParser.cpp"> <ClCompile Include="ConfigParser.cpp">
<PrecompiledHeader>Use</PrecompiledHeader> <PrecompiledHeader>Use</PrecompiledHeader>
</ClCompile> </ClCompile>
@ -274,6 +275,7 @@
<ClInclude Include="..\Common\ControlTemplate.h" /> <ClInclude Include="..\Common\ControlTemplate.h" />
<ClInclude Include="..\Common\Dialog.h" /> <ClInclude Include="..\Common\Dialog.h" />
<ClInclude Include="..\Common\MenuTemplate.h" /> <ClInclude Include="..\Common\MenuTemplate.h" />
<ClInclude Include="..\Common\StringUtil.h" />
<ClInclude Include="ConfigParser.h" /> <ClInclude Include="ConfigParser.h" />
<ClInclude Include="DialogAbout.h" /> <ClInclude Include="DialogAbout.h" />
<ClInclude Include="DisableThreadLibraryCalls.h" /> <ClInclude Include="DisableThreadLibraryCalls.h" />

View File

@ -333,6 +333,9 @@
<ClCompile Include="..\Common\MenuTemplate.cpp"> <ClCompile Include="..\Common\MenuTemplate.cpp">
<Filter>Common</Filter> <Filter>Common</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\Common\StringUtil.cpp">
<Filter>Common</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="ConfigParser.h"> <ClInclude Include="ConfigParser.h">
@ -572,6 +575,9 @@
<ClInclude Include="..\Common\MenuTemplate.h"> <ClInclude Include="..\Common\MenuTemplate.h">
<Filter>Common</Filter> <Filter>Common</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\Common\StringUtil.h">
<Filter>Common</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="Library.rc"> <ResourceCompile Include="Library.rc">

View File

@ -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) void LogInternal(int nLevel, ULONGLONG elapsed, LPCTSTR pszMessage)
{ {
// Add timestamp // Add timestamp
@ -211,7 +143,7 @@ void LogInternal(int nLevel, ULONGLONG elapsed, LPCTSTR pszMessage)
message += L'\n'; message += L'\n';
#ifdef _DEBUG #ifdef _DEBUG
_RPT0(_CRT_WARN, ConvertToAscii(message.c_str()).c_str()); _RPT0(_CRT_WARN, StringUtil::Narrow(message).c_str());
if (!Rainmeter->GetLogging()) return; if (!Rainmeter->GetLogging()) return;
#endif #endif

View File

@ -22,6 +22,7 @@
#include <windows.h> #include <windows.h>
#include <comdef.h> #include <comdef.h>
#include <string> #include <string>
#include "../Common/StringUtil.h"
#include "Error.h" #include "Error.h"
enum LOGLEVEL enum LOGLEVEL
@ -40,11 +41,6 @@ UINT GetUniqueID();
template <typename T> template <typename T>
UINT TypeID() { static UINT id = GetUniqueID(); return id; } 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 Log(int nLevel, const WCHAR* message);
void LogWithArgs(int nLevel, const WCHAR* format, ...); void LogWithArgs(int nLevel, const WCHAR* format, ...);
void LogError(CError& error); void LogError(CError& error);

View File

@ -239,7 +239,7 @@ const WCHAR* CMeasure::CheckSubstitute(const WCHAR* buffer)
} }
else // Contains a RegEx else // Contains a RegEx
{ {
std::string utf8str = ConvertToUTF8(buffer); std::string utf8str = StringUtil::NarrowUTF8(buffer);
int* ovector = new int[OVECCOUNT]; int* ovector = new int[OVECCOUNT];
for (size_t i = 0, isize = m_Substitute.size(); i < isize ; i += 2) 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; int offset = 0;
re = pcre_compile( 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 flags, // default options
&error, // for error message &error, // for error message
&erroffset, // for error offset &erroffset, // for error offset
@ -283,7 +283,7 @@ const WCHAR* CMeasure::CheckSubstitute(const WCHAR* buffer)
} }
else else
{ {
std::string result = ConvertToUTF8(m_Substitute[i + 1].c_str()); std::string result = StringUtil::NarrowUTF8(m_Substitute[i + 1]);
if (rc > 1) if (rc > 1)
{ {
@ -324,7 +324,7 @@ const WCHAR* CMeasure::CheckSubstitute(const WCHAR* buffer)
delete [] ovector; delete [] ovector;
str = ConvertUTF8ToWide(utf8str.c_str()); str = StringUtil::WidenUTF8(utf8str);
} }
return str.c_str(); return str.c_str();

View File

@ -143,12 +143,12 @@ void CMeasureScript::ReadOptions(CConfigParser& parser, const WCHAR* section)
lua_pop(L, 1); lua_pop(L, 1);
const char* strKey = lua_tostring(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""); const std::wstring& wstrValue = parser.ReadString(section, wstrKey.c_str(), L"");
if (!wstrValue.empty()) if (!wstrValue.empty())
{ {
std::string strStrVal = ConvertToAscii(wstrValue.c_str()); std::string strStrVal = StringUtil::Narrow(wstrValue);
const char* strValue = strStrVal.c_str(); const char* strValue = strStrVal.c_str();
lua_pushstring(L, strValue); lua_pushstring(L, strValue);
@ -186,7 +186,7 @@ void CMeasureScript::ReadOptions(CConfigParser& parser, const WCHAR* section)
*/ */
void CMeasureScript::Command(const std::wstring& command) 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()); m_LuaScript.RunString(str.c_str());
} }

View File

@ -64,7 +64,7 @@ void CheckVersion(void* dummy)
return version; return version;
}; };
std::wstring tmpSz = ConvertToWide(urlData); std::wstring tmpSz = StringUtil::Widen(urlData);
const WCHAR* version = tmpSz.c_str(); const WCHAR* version = tmpSz.c_str();
int availableVersion = parseVersion(version); int availableVersion = parseVersion(version);

View File

@ -69,16 +69,19 @@ void LuaManager::ReportErrors(lua_State* L, const std::wstring& file)
} }
std::wstring str(file, file.find_last_of(L'\\') + 1); 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()); LogWithArgs(LOG_ERROR, L"Script: %s", str.c_str());
} }
void LuaManager::PushWide(lua_State* L, const WCHAR* 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) 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);
} }

View File

@ -206,8 +206,9 @@ int LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, std
} }
else if (type == LUA_TSTRING) else if (type == LUA_TSTRING)
{ {
const char* str = lua_tostring(L, -1); size_t strLen = 0;
strValue = ConvertToWide(str); const char* str = lua_tolstring(L, -1, &strLen);
strValue = StringUtil::Widen(str, (int)strLen);
numValue = strtod(str, NULL); numValue = strtod(str, NULL);
} }

View File

@ -53,7 +53,7 @@ static int Print(lua_State* L)
lua_pop(L, 1); lua_pop(L, 1);
} }
Log(LOG_DEBUG, ConvertToWide(message.c_str()).c_str()); Log(LOG_DEBUG, StringUtil::Widen(message).c_str());
return 0; return 0;
} }