- Fixed: Multibang [] not assumed when [Measure] is replaced in CConfigParser::ReadString().

- Minor changes of ConvertToXXX(): Get an appropriate buffer size to convert the string. And using [] when deleting arrays.
- Fixed a few memory leaks.
- It's now possible to send the !BANG command when all windows are "On Desktop". (Rainmeter.exe and WebParser)
This commit is contained in:
spx 2009-09-12 11:11:40 +00:00
parent 8ea3c6780a
commit 9d96ec61c1
10 changed files with 155 additions and 82 deletions

View File

@ -58,7 +58,7 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
hWnd=InitInstance(hInstance, WinClass, WinName); hWnd=InitInstance(hInstance, WinClass, WinName);
if(!hWnd) return FALSE; if(!hWnd) return FALSE;
if (lpCmdLine[0] == '!') if (lpCmdLine && lpCmdLine[0] == L'!')
{ {
// It's a !bang // It's a !bang
Bang(hWnd, lpCmdLine); Bang(hWnd, lpCmdLine);
@ -158,8 +158,17 @@ HWND InitInstance(HINSTANCE hInstance, const WCHAR* WinClass, const WCHAR* WinNa
*/ */
void Bang(HWND hWnd, const WCHAR* command) void Bang(HWND hWnd, const WCHAR* command)
{ {
// Check if Rainlendar is running // Check if Rainmeter is running
HWND wnd = FindWindow(L"RainmeterMeterWindow", NULL); HWND wnd = FindWindow(L"RainmeterMeterWindow", NULL);
if (wnd == NULL)
{
// Check if all windows are "On Desktop"
HWND ProgmanHwnd = FindWindow(L"Progman", L"Program Manager");
if (ProgmanHwnd)
{
wnd = FindWindowEx(ProgmanHwnd, NULL, L"RainmeterMeterWindow", NULL);
}
}
if (wnd != NULL) if (wnd != NULL)
{ {
@ -169,7 +178,7 @@ void Bang(HWND hWnd, const WCHAR* command)
copyData.cbData = (DWORD)((wcslen(command) + 1) * sizeof(WCHAR)); copyData.cbData = (DWORD)((wcslen(command) + 1) * sizeof(WCHAR));
copyData.lpData = (void*)command; copyData.lpData = (void*)command;
// Send the bang to the Rainlendar window // Send the bang to the Rainmeter window
SendMessage(wnd, WM_COPYDATA, (WPARAM)hWnd, (LPARAM)&copyData); SendMessage(wnd, WM_COPYDATA, (WPARAM)hWnd, (LPARAM)&copyData);
} }
else else

View File

@ -22,7 +22,6 @@
#include "ConfigParser.h" #include "ConfigParser.h"
#include "Litestep.h" #include "Litestep.h"
#include "Rainmeter.h" #include "Rainmeter.h"
#include <TCHAR.H>
#include <algorithm> #include <algorithm>
extern CRainmeter* Rainmeter; extern CRainmeter* Rainmeter;
@ -189,7 +188,7 @@ const std::wstring& CConfigParser::ReadString(LPCTSTR section, LPCTSTR key, LPCT
pos = result.find(L'#', start); pos = result.find(L'#', start);
if (pos != std::wstring::npos) if (pos != std::wstring::npos)
{ {
size_t end = result.find(L'#', pos + 1); end = result.find(L'#', pos + 1);
if (end != std::wstring::npos) if (end != std::wstring::npos)
{ {
std::wstring strTmp(result.begin() + pos + 1, result.begin() + end); std::wstring strTmp(result.begin() + pos + 1, result.begin() + end);
@ -224,29 +223,38 @@ const std::wstring& CConfigParser::ReadString(LPCTSTR section, LPCTSTR key, LPCT
start = 0; start = 0;
end = std::wstring::npos; end = std::wstring::npos;
pos = std::wstring::npos; pos = std::wstring::npos;
size_t pos2 = std::wstring::npos;
loop = true; loop = true;
do do
{ {
pos = result.find(L'[', start); pos = result.find(L'[', start);
if (pos != std::wstring::npos) if (pos != std::wstring::npos)
{ {
size_t end = result.find(L']', pos + 1); end = result.find(L']', pos + 1);
if (end != std::wstring::npos) if (end != std::wstring::npos)
{ {
std::wstring var(result.begin() + pos + 1, result.begin() + end); pos2 = result.find(L'[', pos + 1);
if (pos2 == std::wstring::npos || end < pos2)
std::map<std::wstring, CMeasure*>::iterator iter = m_Measures.find(var);
if (iter != m_Measures.end())
{ {
std::wstring value = (*iter).second->GetStringValue(true, 1, 5, false); std::wstring var(result.begin() + pos + 1, result.begin() + end);
// Measure found, replace it with the value std::map<std::wstring, CMeasure*>::iterator iter = m_Measures.find(var);
result.replace(result.begin() + pos, result.begin() + end + 1, value); if (iter != m_Measures.end())
start = pos + value.length(); {
std::wstring value = (*iter).second->GetStringValue(true, 1, 5, false);
// Measure found, replace it with the value
result.replace(result.begin() + pos, result.begin() + end + 1, value);
start = pos + value.length();
}
else
{
start = end;
}
} }
else else
{ {
start = end; start = pos2;
} }
} }
else else
@ -465,7 +473,7 @@ void CConfigParser::ReadIniFile(const std::wstring& iniFile)
{ {
items[0] = 0; items[0] = 0;
int res = GetPrivateProfileString( NULL, NULL, NULL, items, size, iniFile.c_str()); int res = GetPrivateProfileString( NULL, NULL, NULL, items, size, iniFile.c_str());
if (res == 0) return; // File not found if (res == 0) { delete [] items; return; } // File not found
if (res < size - 2) break; // Fits in the buffer if (res < size - 2) break; // Fits in the buffer
delete [] items; delete [] items;
@ -486,7 +494,7 @@ void CConfigParser::ReadIniFile(const std::wstring& iniFile)
// Read the keys and values // Read the keys and values
int bufferSize = MAX_LINE_LENGTH; int bufferSize = MAX_LINE_LENGTH;
TCHAR* buffer = new TCHAR[bufferSize]; WCHAR* buffer = new WCHAR[bufferSize];
stdext::hash_map<std::wstring, std::vector<std::wstring> >::iterator iter = m_Keys.begin(); stdext::hash_map<std::wstring, std::vector<std::wstring> >::iterator iter = m_Keys.begin();
for ( ; iter != m_Keys.end(); iter++) for ( ; iter != m_Keys.end(); iter++)

View File

@ -471,13 +471,18 @@ std::string ConvertToAscii(LPCTSTR str)
{ {
std::string szAscii; std::string szAscii;
if (str) if (str && *str)
{ {
size_t len = (wcslen(str) + 1); int strLen = (int)wcslen(str) + 1;
char* tmpSz = new char[len * 2]; int bufLen = WideCharToMultiByte(CP_ACP, 0, str, strLen, NULL, 0, NULL, NULL);
WideCharToMultiByte(CP_ACP, 0, str, -1, tmpSz, (int)len * 2, NULL, FALSE); if (bufLen > 0)
szAscii = tmpSz; {
delete tmpSz; char* tmpSz = new char[bufLen];
tmpSz[0] = 0;
WideCharToMultiByte(CP_ACP, 0, str, strLen, tmpSz, bufLen, NULL, NULL);
szAscii = tmpSz;
delete [] tmpSz;
}
} }
return szAscii; return szAscii;
} }
@ -486,13 +491,18 @@ std::wstring ConvertToWide(LPCSTR str)
{ {
std::wstring szWide; std::wstring szWide;
if (str) if (str && *str)
{ {
size_t len = strlen(str) + 1; int strLen = (int)strlen(str) + 1;
WCHAR* wideSz = new WCHAR[len * 2]; int bufLen = MultiByteToWideChar(CP_ACP, 0, str, strLen, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, str, (int)len, wideSz, (int)len * 2); if (bufLen > 0)
szWide = wideSz; {
delete wideSz; WCHAR* wideSz = new WCHAR[bufLen];
wideSz[0] = 0;
MultiByteToWideChar(CP_ACP, 0, str, strLen, wideSz, bufLen);
szWide = wideSz;
delete [] wideSz;
}
} }
return szWide; return szWide;
} }

View File

@ -74,7 +74,6 @@ public:
const WCHAR* GetName() { return m_Name.c_str(); }; const WCHAR* GetName() { return m_Name.c_str(); };
static CMeter* Create(const WCHAR* meter, CMeterWindow* meterWindow); static CMeter* Create(const WCHAR* meter, CMeterWindow* meterWindow);
static WCHAR* ConvertToWide(const char* string);
static void DrawBevel(Gdiplus::Graphics& graphics, Gdiplus::Rect& rect, Gdiplus::Pen& light, Gdiplus::Pen& dark); static void DrawBevel(Gdiplus::Graphics& graphics, Gdiplus::Rect& rect, Gdiplus::Pen& light, Gdiplus::Pen& dark);

View File

@ -1514,7 +1514,7 @@ void CMeterWindow::ReadSkin()
while(true) while(true)
{ {
int res = GetPrivateProfileString( NULL, NULL, NULL, items, size, iniFile.c_str()); int res = GetPrivateProfileString( NULL, NULL, NULL, items, size, iniFile.c_str());
if (res == 0) return; // File not found if (res == 0) { delete [] items; return; } // File not found
if (res < size - 2) break; // Fits in the buffer if (res < size - 2) break; // Fits in the buffer
delete [] items; delete [] items;

View File

@ -682,11 +682,11 @@ int CRainmeter::Initialize(HWND Parent, HINSTANCE Instance, LPCSTR szPath)
WCHAR* pos = wcsrchr(tmpName, L'\\'); WCHAR* pos = wcsrchr(tmpName, L'\\');
if(pos) if(pos)
{ {
*(pos + 1)='\0'; *(pos + 1) = L'\0';
} }
else else
{ {
tmpName[0]='\0'; tmpName[0] = L'\0';
} }
if(!c_DummyLitestep) InitalizeLitestep(); if(!c_DummyLitestep) InitalizeLitestep();
@ -1770,7 +1770,7 @@ PLATFORM CRainmeter::IsNT()
{ {
// You got NT // You got NT
if(osvi.dwMajorVersion <= 4) return PLATFORM_NT4; if(osvi.dwMajorVersion <= 4) return PLATFORM_NT4;
if(osvi.dwMajorVersion == 5) return PLATFORM_2K; if(osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0) return PLATFORM_2K;
return PLATFORM_XP; return PLATFORM_XP;
} }

View File

@ -82,13 +82,18 @@ std::string ConvertToAscii(LPCTSTR str)
{ {
std::string szAscii; std::string szAscii;
if (str) if (str && *str)
{ {
size_t len = (wcslen(str) + 1); int strLen = (int)wcslen(str) + 1;
char* tmpSz = new char[len * 2]; int bufLen = WideCharToMultiByte(CP_ACP, 0, str, strLen, NULL, 0, NULL, NULL);
WideCharToMultiByte(CP_ACP, 0, str, -1, tmpSz, (int)len * 2, NULL, FALSE); if (bufLen > 0)
szAscii = tmpSz; {
delete tmpSz; char* tmpSz = new char[bufLen];
tmpSz[0] = 0;
WideCharToMultiByte(CP_ACP, 0, str, strLen, tmpSz, bufLen, NULL, NULL);
szAscii = tmpSz;
delete [] tmpSz;
}
} }
return szAscii; return szAscii;
} }

View File

@ -57,13 +57,18 @@ std::string ConvertToAscii(LPCTSTR str)
{ {
std::string szAscii; std::string szAscii;
if (str) if (str && *str)
{ {
size_t len = (wcslen(str) + 1); int strLen = (int)wcslen(str) + 1;
char* tmpSz = new char[len * 2]; int bufLen = WideCharToMultiByte(CP_ACP, 0, str, strLen, NULL, 0, NULL, NULL);
WideCharToMultiByte(CP_ACP, 0, str, -1, tmpSz, (int)len * 2, NULL, FALSE); if (bufLen > 0)
szAscii = tmpSz; {
delete tmpSz; char* tmpSz = new char[bufLen];
tmpSz[0] = 0;
WideCharToMultiByte(CP_ACP, 0, str, strLen, tmpSz, bufLen, NULL, NULL);
szAscii = tmpSz;
delete [] tmpSz;
}
} }
return szAscii; return szAscii;
} }
@ -72,13 +77,18 @@ std::wstring ConvertToWide(LPCSTR str)
{ {
std::wstring szWide; std::wstring szWide;
if (str) if (str && *str)
{ {
size_t len = strlen(str) + 1; int strLen = (int)strlen(str) + 1;
WCHAR* wideSz = new WCHAR[len * 2]; int bufLen = MultiByteToWideChar(CP_ACP, 0, str, strLen, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, str, (int)len, wideSz, (int)len * 2); if (bufLen > 0)
szWide = wideSz; {
delete wideSz; WCHAR* wideSz = new WCHAR[bufLen];
wideSz[0] = 0;
MultiByteToWideChar(CP_ACP, 0, str, strLen, wideSz, bufLen);
szWide = wideSz;
delete [] wideSz;
}
} }
return szWide; return szWide;
} }

View File

@ -226,13 +226,18 @@ std::wstring ConvertToWide(LPCSTR str)
{ {
std::wstring szWide; std::wstring szWide;
if (str) if (str && *str)
{ {
size_t len = strlen(str) + 1; int strLen = (int)strlen(str) + 1;
WCHAR* wideSz = new WCHAR[len * 2]; int bufLen = MultiByteToWideChar(CP_ACP, 0, str, strLen, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, str, (int)len, wideSz, (int)len * 2); if (bufLen > 0)
szWide = wideSz; {
delete wideSz; WCHAR* wideSz = new WCHAR[bufLen];
wideSz[0] = 0;
MultiByteToWideChar(CP_ACP, 0, str, strLen, wideSz, bufLen);
szWide = wideSz;
delete [] wideSz;
}
} }
return szWide; return szWide;
} }

View File

@ -86,14 +86,18 @@ std::string ConvertToUTF8(LPCWSTR str)
{ {
std::string szAscii; std::string szAscii;
if (str) if (str && *str)
{ {
size_t len = (wcslen(str) + 1); int strLen = (int)wcslen(str) + 1;
char* tmpSz = new char[len * 2]; int bufLen = WideCharToMultiByte(CP_UTF8, 0, str, strLen, NULL, 0, NULL, NULL);
tmpSz[0] = 0; if (bufLen > 0)
WideCharToMultiByte(CP_UTF8, 0, str, -1, tmpSz, (int)len * 2, NULL, FALSE); {
szAscii = tmpSz; char* tmpSz = new char[bufLen];
delete tmpSz; tmpSz[0] = 0;
WideCharToMultiByte(CP_UTF8, 0, str, strLen, tmpSz, bufLen, NULL, NULL);
szAscii = tmpSz;
delete [] tmpSz;
}
} }
return szAscii; return szAscii;
} }
@ -102,14 +106,18 @@ std::string ConvertToUTF8(LPCSTR str, int codepage)
{ {
std::string szUTF8; std::string szUTF8;
if (str) if (str && *str)
{ {
size_t len = strlen(str) + 1; int strLen = (int)strlen(str) + 1;
WCHAR* wideSz = new WCHAR[len * 2]; int bufLen = MultiByteToWideChar(codepage, 0, str, strLen, NULL, 0);
wideSz[0] = 0; if (bufLen > 0)
MultiByteToWideChar(codepage, 0, str, (int)len, wideSz, (int)len * 2); {
szUTF8 = ConvertToUTF8(wideSz); WCHAR* wideSz = new WCHAR[bufLen];
delete wideSz; wideSz[0] = 0;
MultiByteToWideChar(codepage, 0, str, strLen, wideSz, bufLen);
szUTF8 = ConvertToUTF8(wideSz);
delete [] wideSz;
}
} }
return szUTF8; return szUTF8;
} }
@ -118,18 +126,37 @@ std::wstring ConvertToWide(LPCSTR str)
{ {
std::wstring szWide; std::wstring szWide;
if (str) if (str && *str)
{ {
size_t len = strlen(str) + 1; int strLen = (int)strlen(str) + 1;
WCHAR* wideSz = new WCHAR[len * 2]; int bufLen = MultiByteToWideChar(CP_ACP, 0, str, strLen, NULL, 0);
wideSz[0] = 0; if (bufLen > 0)
MultiByteToWideChar(CP_UTF8, 0, str, (int)len, wideSz, (int)len * 2); {
szWide = wideSz; WCHAR* wideSz = new WCHAR[bufLen];
delete wideSz; wideSz[0] = 0;
MultiByteToWideChar(CP_ACP, 0, str, strLen, wideSz, bufLen);
szWide = wideSz;
delete [] wideSz;
}
} }
return szWide; return szWide;
} }
HWND FindMeterWindow()
{
HWND wnd = FindWindow(L"RainmeterMeterWindow", NULL);
if (wnd == NULL)
{
// Check if all windows are "On Desktop"
HWND ProgmanHwnd = FindWindow(L"Progman", L"Program Manager");
if (ProgmanHwnd)
{
wnd = FindWindowEx(ProgmanHwnd, NULL, L"RainmeterMeterWindow", NULL);
}
}
return wnd;
}
/* /*
This function is called when the measure is initialized. This function is called when the measure is initialized.
The function must return the maximum value that can be measured. The function must return the maximum value that can be measured.
@ -540,7 +567,7 @@ void ParseData(UrlData* urlData, LPCSTR parseData)
{ {
if (!urlData->finishAction.empty()) if (!urlData->finishAction.empty())
{ {
HWND wnd = FindWindow(L"RainmeterMeterWindow", NULL); HWND wnd = FindMeterWindow();
if (wnd != NULL) if (wnd != NULL)
{ {
@ -641,7 +668,7 @@ DWORD WINAPI NetworkDownloadThreadProc(LPVOID pParam)
if (!urlData->finishAction.empty()) if (!urlData->finishAction.empty())
{ {
HWND wnd = FindWindow(L"RainmeterMeterWindow", NULL); HWND wnd = FindMeterWindow();
if (wnd != NULL) if (wnd != NULL)
{ {