More tweaks

This commit is contained in:
Birunthan Mohanathas 2013-01-27 12:38:59 +02:00
parent 438f79bf5d
commit a0113cc74f
3 changed files with 29 additions and 75 deletions

View File

@ -52,12 +52,14 @@
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\Common\StringUtil.h" />
<ClInclude Include="..\..\Library\pcre-8.10\config.h" /> <ClInclude Include="..\..\Library\pcre-8.10\config.h" />
<ClInclude Include="..\..\Library\pcre-8.10\pcre.h" /> <ClInclude Include="..\..\Library\pcre-8.10\pcre.h" />
<ClInclude Include="..\..\Library\pcre-8.10\pcre_internal.h" /> <ClInclude Include="..\..\Library\pcre-8.10\pcre_internal.h" />
<ClInclude Include="..\..\Library\pcre-8.10\ucp.h" /> <ClInclude Include="..\..\Library\pcre-8.10\ucp.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\Common\StringUtil.cpp" />
<ClCompile Include="..\..\Library\pcre-8.10\pcre_globals.c" /> <ClCompile Include="..\..\Library\pcre-8.10\pcre_globals.c" />
<ClCompile Include="WebParser.cpp" /> <ClCompile Include="WebParser.cpp" />
</ItemGroup> </ItemGroup>

View File

@ -4,6 +4,9 @@
<Filter Include="pcre"> <Filter Include="pcre">
<UniqueIdentifier>{15b74478-8fbf-4a89-b1ed-5488d059708c}</UniqueIdentifier> <UniqueIdentifier>{15b74478-8fbf-4a89-b1ed-5488d059708c}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="Common">
<UniqueIdentifier>{a7ff5ecc-efd3-476f-83b8-d5a327f487b5}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\Library\pcre-8.10\config.h"> <ClInclude Include="..\..\Library\pcre-8.10\config.h">
@ -18,12 +21,18 @@
<ClInclude Include="..\..\Library\pcre-8.10\ucp.h"> <ClInclude Include="..\..\Library\pcre-8.10\ucp.h">
<Filter>pcre</Filter> <Filter>pcre</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\Common\StringUtil.h">
<Filter>Common</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="WebParser.cpp" /> <ClCompile Include="WebParser.cpp" />
<ClCompile Include="..\..\Library\pcre-8.10\pcre_globals.c"> <ClCompile Include="..\..\Library\pcre-8.10\pcre_globals.c">
<Filter>pcre</Filter> <Filter>pcre</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\Common\StringUtil.cpp">
<Filter>Common</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="PluginWebParser.rc" /> <ResourceCompile Include="PluginWebParser.rc" />

View File

@ -27,6 +27,7 @@
#include "../../Library/pcre-8.10/config.h" #include "../../Library/pcre-8.10/config.h"
#include "../../Library/pcre-8.10/pcre.h" #include "../../Library/pcre-8.10/pcre.h"
#include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point #include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point
#include "../../Common/StringUtil.h"
#include "../API/RainmeterAPI.h" #include "../API/RainmeterAPI.h"
void ShowError(int lineNumber, WCHAR* errorMsg = NULL); void ShowError(int lineNumber, WCHAR* errorMsg = NULL);
@ -257,70 +258,16 @@ static std::unordered_map<std::wstring, WCHAR> g_CERs;
#define OVECCOUNT 300 // should be a multiple of 3 #define OVECCOUNT 300 // should be a multiple of 3
std::wstring ConvertToWide(LPCSTR str, int codepage) std::string ConvertAsciiToUTF8(LPCSTR str, int strLen, int codepage)
{
std::wstring szWide;
if (str && *str)
{
int strLen = (int)strlen(str);
int bufLen = MultiByteToWideChar(codepage, 0, str, strLen, NULL, 0);
if (bufLen > 0)
{
szWide.resize(bufLen);
MultiByteToWideChar(codepage, 0, str, strLen, &szWide[0], bufLen);
}
}
return szWide;
}
std::string ConvertToAscii(LPCWSTR str, int codepage)
{
std::string szAscii;
if (str && *str)
{
int strLen = (int)wcslen(str);
int bufLen = WideCharToMultiByte(codepage, 0, str, strLen, NULL, 0, NULL, NULL);
if (bufLen > 0)
{
szAscii.resize(bufLen);
WideCharToMultiByte(codepage, 0, str, strLen, &szAscii[0], bufLen, NULL, NULL);
}
}
return szAscii;
}
std::string ConvertWideToAscii(LPCWSTR str)
{
return ConvertToAscii(str, CP_ACP);
}
std::wstring ConvertAsciiToWide(LPCSTR str)
{
return ConvertToWide(str, CP_ACP);
}
std::wstring ConvertUTF8ToWide(LPCSTR str)
{
return ConvertToWide(str, CP_UTF8);
}
std::string ConvertWideToUTF8(LPCWSTR str)
{
return ConvertToAscii(str, CP_UTF8);
}
std::string ConvertAsciiToUTF8(LPCSTR str, int codepage)
{ {
std::string szUTF8; std::string szUTF8;
if (str && *str) if (str && *str)
{ {
std::wstring wide = ConvertToWide(str, codepage); std::wstring wide = StringUtil::Widen(str, strLen, codepage);
if (!wide.empty()) if (!wide.empty())
{ {
szUTF8.swap(ConvertWideToUTF8(wide.c_str())); szUTF8.swap(StringUtil::NarrowUTF8(wide));
} }
} }
return szUTF8; return szUTF8;
@ -952,7 +899,7 @@ void ParseData(MeasureData* measure, LPCSTR parseData, DWORD dwSize)
// Compile the regular expression in the first argument // Compile the regular expression in the first argument
re = pcre_compile( re = pcre_compile(
ConvertWideToUTF8(measure->regExp.c_str()).c_str(), // the pattern StringUtil::NarrowUTF8(measure->regExp).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
@ -966,14 +913,14 @@ void ParseData(MeasureData* measure, LPCSTR parseData, DWORD dwSize)
if (measure->codepage == 1200) // 1200 = UTF-16LE if (measure->codepage == 1200) // 1200 = UTF-16LE
{ {
// Must convert the data to utf8 // Must convert the data to utf8
utf8Data = ConvertWideToUTF8((LPCWSTR)parseData); utf8Data = StringUtil::NarrowUTF8((LPCWSTR)parseData, dwSize);
parseData = utf8Data.c_str(); parseData = utf8Data.c_str();
dwSize = utf8Data.length(); dwSize = utf8Data.length();
} }
else if (measure->codepage != CP_UTF8 && measure->codepage != 0) // 0 = CP_ACP else if (measure->codepage != CP_UTF8 && measure->codepage != 0) // 0 = CP_ACP
{ {
// Must convert the data to utf8 // Must convert the data to utf8
utf8Data = ConvertAsciiToUTF8(parseData, measure->codepage); utf8Data = ConvertAsciiToUTF8(parseData, dwSize, measure->codepage);
parseData = utf8Data.c_str(); parseData = utf8Data.c_str();
dwSize = utf8Data.length(); dwSize = utf8Data.length();
} }
@ -1010,7 +957,6 @@ void ParseData(MeasureData* measure, LPCSTR parseData, DWORD dwSize)
const char* substring_start = parseData + ovector[2 * i]; const char* substring_start = parseData + ovector[2 * i];
int substring_length = ovector[2 * i + 1] - ovector[2 * i]; int substring_length = ovector[2 * i + 1] - ovector[2 * i];
substring_length = min(substring_length, 256); substring_length = min(substring_length, 256);
std::string tmpStr(substring_start, substring_length);
WCHAR buffer[32]; WCHAR buffer[32];
wsprintf(buffer, L"%2d", i); wsprintf(buffer, L"%2d", i);
@ -1020,7 +966,7 @@ void ParseData(MeasureData* measure, LPCSTR parseData, DWORD dwSize)
log += L"] (Index "; log += L"] (Index ";
log += buffer; log += buffer;
log += L") "; log += L") ";
log += ConvertUTF8ToWide(tmpStr.c_str()); log += StringUtil::WidenUTF8(substring_start, substring_length);
RmLog(LOG_DEBUG, log.c_str()); RmLog(LOG_DEBUG, log.c_str());
} }
} }
@ -1029,8 +975,8 @@ void ParseData(MeasureData* measure, LPCSTR parseData, DWORD dwSize)
int substring_length = ovector[2 * measure->stringIndex + 1] - ovector[2 * measure->stringIndex]; int substring_length = ovector[2 * measure->stringIndex + 1] - ovector[2 * measure->stringIndex];
EnterCriticalSection(&g_CriticalSection); EnterCriticalSection(&g_CriticalSection);
std::string szResult(substring_start, substring_length); measure->resultString = StringUtil::WidenUTF8(substring_start, substring_length);
measure->resultString = DecodeReferences(ConvertUTF8ToWide(szResult.c_str()), measure->decodeCharacterReference); DecodeReferences(measure->resultString, measure->decodeCharacterReference);
LeaveCriticalSection(&g_CriticalSection); LeaveCriticalSection(&g_CriticalSection);
} }
else else
@ -1073,14 +1019,12 @@ void ParseData(MeasureData* measure, LPCSTR parseData, DWORD dwSize)
const char* substring_start = parseData + ovector[2 * (*i)->stringIndex]; const char* substring_start = parseData + ovector[2 * (*i)->stringIndex];
int substring_length = ovector[2 * (*i)->stringIndex + 1] - ovector[2 * (*i)->stringIndex]; int substring_length = ovector[2 * (*i)->stringIndex + 1] - ovector[2 * (*i)->stringIndex];
std::string szResult(substring_start, substring_length);
if (!(*i)->regExp.empty()) if (!(*i)->regExp.empty())
{ {
// Change the index and parse the substring // Change the index and parse the substring
int index = (*i)->stringIndex; int index = (*i)->stringIndex;
(*i)->stringIndex = (*i)->stringIndex2; (*i)->stringIndex = (*i)->stringIndex2;
ParseData((*i), szResult.c_str(), szResult.length()); ParseData((*i), substring_start, substring_length);
(*i)->stringIndex = index; (*i)->stringIndex = index;
} }
else else
@ -1088,12 +1032,11 @@ void ParseData(MeasureData* measure, LPCSTR parseData, DWORD dwSize)
// Set the result // Set the result
EnterCriticalSection(&g_CriticalSection); EnterCriticalSection(&g_CriticalSection);
// Substitude the [measure] with szResult // Substitude the [measure] with result
std::wstring wzResult = ConvertUTF8ToWide(szResult.c_str()); std::wstring result = StringUtil::WidenUTF8(substring_start, substring_length);
std::wstring wzUrl = (*i)->url; (*i)->resultString = (*i)->url;
(*i)->resultString.replace((*i)->resultString.find(compareStr), compareStr.size(), result);
wzUrl.replace(wzUrl.find(compareStr), compareStr.size(), wzResult); DecodeReferences((*i)->resultString, (*i)->decodeCharacterReference);
(*i)->resultString = DecodeReferences(wzUrl, (*i)->decodeCharacterReference);
// Start download threads for the references // Start download threads for the references
if ((*i)->download) if ((*i)->download)
@ -1190,7 +1133,7 @@ void ParseData(MeasureData* measure, LPCSTR parseData, DWORD dwSize)
log += L"] PCRE compilation failed at offset "; log += L"] PCRE compilation failed at offset ";
log += buffer; log += buffer;
log += L": "; log += L": ";
log += ConvertAsciiToWide(error); log += StringUtil::Widen(error);
log += L"\n"; log += L"\n";
RmLog(LOG_ERROR, log.c_str()); RmLog(LOG_ERROR, log.c_str());
} }
@ -1680,7 +1623,7 @@ BYTE* DownloadUrl(HINTERNET handle, std::wstring& url, DWORD* dwDataSize, bool f
{ {
if (_wcsnicmp(url.c_str(), L"file://", 7) == 0) // file scheme if (_wcsnicmp(url.c_str(), L"file://", 7) == 0) // file scheme
{ {
std::string urlACP = ConvertWideToAscii(url.c_str()); const std::string urlACP = StringUtil::Narrow(url);
hUrlDump = InternetOpenUrlA(handle, urlACP.c_str(), NULL, NULL, flags, 0); hUrlDump = InternetOpenUrlA(handle, urlACP.c_str(), NULL, NULL, flags, 0);
} }