mirror of
https://github.com/chibicitiberiu/rainmeter-studio.git
synced 2024-02-24 04:33:31 +00:00
Tweaks
This commit is contained in:
parent
654599d192
commit
fdcd5a9757
65
Common/StringUtil.cpp
Normal file
65
Common/StringUtil.cpp
Normal 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
41
Common/StringUtil.h
Normal 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
|
@ -66,6 +66,7 @@
|
||||
<ClCompile Include="..\Common\ControlTemplate.cpp" />
|
||||
<ClCompile Include="..\Common\Dialog.cpp" />
|
||||
<ClCompile Include="..\Common\MenuTemplate.cpp" />
|
||||
<ClCompile Include="..\Common\StringUtil.cpp" />
|
||||
<ClCompile Include="ConfigParser.cpp">
|
||||
<PrecompiledHeader>Use</PrecompiledHeader>
|
||||
</ClCompile>
|
||||
@ -274,6 +275,7 @@
|
||||
<ClInclude Include="..\Common\ControlTemplate.h" />
|
||||
<ClInclude Include="..\Common\Dialog.h" />
|
||||
<ClInclude Include="..\Common\MenuTemplate.h" />
|
||||
<ClInclude Include="..\Common\StringUtil.h" />
|
||||
<ClInclude Include="ConfigParser.h" />
|
||||
<ClInclude Include="DialogAbout.h" />
|
||||
<ClInclude Include="DisableThreadLibraryCalls.h" />
|
||||
|
@ -333,6 +333,9 @@
|
||||
<ClCompile Include="..\Common\MenuTemplate.cpp">
|
||||
<Filter>Common</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Common\StringUtil.cpp">
|
||||
<Filter>Common</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="ConfigParser.h">
|
||||
@ -572,6 +575,9 @@
|
||||
<ClInclude Include="..\Common\MenuTemplate.h">
|
||||
<Filter>Common</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\Common\StringUtil.h">
|
||||
<Filter>Common</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ResourceCompile Include="Library.rc">
|
||||
|
@ -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
|
||||
|
||||
|
@ -22,6 +22,7 @@
|
||||
#include <windows.h>
|
||||
#include <comdef.h>
|
||||
#include <string>
|
||||
#include "../Common/StringUtil.h"
|
||||
#include "Error.h"
|
||||
|
||||
enum LOGLEVEL
|
||||
@ -40,11 +41,6 @@ UINT GetUniqueID();
|
||||
template <typename T>
|
||||
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);
|
||||
|
@ -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();
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user