Code tweaks and cleanups.

This commit is contained in:
spx 2011-11-10 13:44:19 +00:00
parent f84491ec83
commit bbe733b437
6 changed files with 109 additions and 82 deletions

View File

@ -80,10 +80,9 @@ void CConfigParser::Initialize(LPCTSTR filename, CRainmeter* pRainmeter, CMeterW
SetBuiltInVariables(pRainmeter, meterWindow); SetBuiltInVariables(pRainmeter, meterWindow);
ResetMonitorVariables(meterWindow); ResetMonitorVariables(meterWindow);
std::vector<std::wstring> iniFileMappings; CSystem::UpdateIniFileMappingList();
CSystem::GetIniFileMappingList(iniFileMappings);
ReadIniFile(iniFileMappings, m_Filename, config); ReadIniFile(m_Filename, config);
ReadVariables(); ReadVariables();
// Clear and minimize // Clear and minimize
@ -449,35 +448,35 @@ bool CConfigParser::ReplaceVariables(std::wstring& result)
} }
// Check for variables (#VAR#) // Check for variables (#VAR#)
size_t start = 0; size_t start = 0, end;
size_t end = std::wstring::npos;
size_t pos = std::wstring::npos;
bool loop = true; bool loop = true;
do do
{ {
pos = result.find(L'#', start); start = result.find(L'#', start);
if (pos != std::wstring::npos) if (start != std::wstring::npos)
{ {
end = result.find(L'#', pos + 1); size_t si = start + 1;
end = result.find(L'#', si);
if (end != std::wstring::npos) if (end != std::wstring::npos)
{ {
if (result[pos + 1] == L'*' && result[end - 1] == L'*') size_t ei = end - 1;
if (result[si] == L'*' && result[ei] == L'*')
{ {
result.erase(pos + 1, 1); result.erase(ei, 1);
result.erase(end - 2, 1); result.erase(si, 1);
start = end - 1; start = ei;
} }
else else
{ {
std::wstring strVariable = result.substr(pos + 1, end - (pos + 1)); std::wstring strVariable = result.substr(si, end - si);
std::wstring strValue; std::wstring strValue;
if (GetVariable(strVariable, strValue)) if (GetVariable(strVariable, strValue))
{ {
// Variable found, replace it with the value // Variable found, replace it with the value
result.replace(pos, end - pos + 1, strValue); result.replace(start, end - start + 1, strValue);
start = pos + strValue.length(); start += strValue.length();
replaced = true; replaced = true;
} }
else else
@ -513,40 +512,40 @@ bool CConfigParser::ReplaceMeasures(std::wstring& result)
// Check for measures ([Measure]) // Check for measures ([Measure])
if (!m_Measures.empty()) if (!m_Measures.empty())
{ {
size_t start = 0; size_t start = 0, end, next;
size_t end = std::wstring::npos;
size_t pos = std::wstring::npos;
size_t pos2 = std::wstring::npos;
bool loop = true; bool loop = true;
do do
{ {
pos = result.find(L'[', start); start = result.find(L'[', start);
if (pos != std::wstring::npos) if (start != std::wstring::npos)
{ {
end = result.find(L']', pos + 1); size_t si = start + 1;
end = result.find(L']', si);
if (end != std::wstring::npos) if (end != std::wstring::npos)
{ {
pos2 = result.find(L'[', pos + 1); next = result.find(L'[', si);
if (pos2 == std::wstring::npos || end < pos2) if (next == std::wstring::npos || end < next)
{ {
if (result[pos + 1] == L'*' && result[end - 1] == L'*') size_t ei = end - 1;
if (result[si] == L'*' && result[ei] == L'*')
{ {
result.erase(pos + 1, 1); result.erase(ei, 1);
result.erase(end - 2, 1); result.erase(si, 1);
start = end - 1; start = ei;
} }
else else
{ {
std::wstring var = result.substr(pos + 1, end - (pos + 1)); std::wstring var = result.substr(si, end - si);
CMeasure* measure = GetMeasure(var); CMeasure* measure = GetMeasure(var);
if (measure) if (measure)
{ {
std::wstring value = measure->GetStringValue(AUTOSCALE_OFF, 1, -1, false); const std::wstring& value = measure->GetStringValue(AUTOSCALE_OFF, 1, -1, false);
// Measure found, replace it with the value // Measure found, replace it with the value
result.replace(pos, end - pos + 1, value); result.replace(start, end - start + 1, value);
start = pos + value.length(); start += value.length();
replaced = true; replaced = true;
} }
else else
@ -557,7 +556,7 @@ bool CConfigParser::ReplaceMeasures(std::wstring& result)
} }
else else
{ {
start = pos2; start = next;
} }
} }
else else
@ -835,8 +834,8 @@ void CConfigParser::Shrink(std::vector<std::wstring>& vec)
{ {
if (!vec.empty()) if (!vec.empty())
{ {
std::vector<std::wstring>::iterator iter = vec.begin(); std::vector<std::wstring>::reverse_iterator iter = vec.rbegin();
while (iter != vec.end()) while (iter != vec.rend())
{ {
std::wstring::size_type pos = (*iter).find_first_not_of(L" \t\r\n"); std::wstring::size_type pos = (*iter).find_first_not_of(L" \t\r\n");
if (pos != std::wstring::npos) if (pos != std::wstring::npos)
@ -852,7 +851,7 @@ void CConfigParser::Shrink(std::vector<std::wstring>& vec)
else else
{ {
// Remove empty element // Remove empty element
iter = vec.erase(iter); vec.erase((++iter).base());
} }
} }
} }
@ -1042,7 +1041,7 @@ RECT CConfigParser::ParseRECT(LPCTSTR string)
** **
** \param iniFile The ini file to be read. ** \param iniFile The ini file to be read.
*/ */
void CConfigParser::ReadIniFile(const std::vector<std::wstring>& iniFileMappings, const std::wstring& iniFile, LPCTSTR config, int depth) void CConfigParser::ReadIniFile(const std::wstring& iniFile, LPCTSTR config, int depth)
{ {
if (depth > 100) // Is 100 enough to assume the include loop never ends? if (depth > 100) // Is 100 enough to assume the include loop never ends?
{ {
@ -1058,7 +1057,7 @@ void CConfigParser::ReadIniFile(const std::vector<std::wstring>& iniFileMappings
} }
// Avoid "IniFileMapping" // Avoid "IniFileMapping"
std::wstring iniRead = CSystem::GetTemporaryFile(iniFileMappings, iniFile); std::wstring iniRead = CSystem::GetTemporaryFile(iniFile);
bool temporary = (!iniRead.empty() && (iniRead.size() != 1 || iniRead[0] != L'?')); bool temporary = (!iniRead.empty() && (iniRead.size() != 1 || iniRead[0] != L'?'));
if (temporary) if (temporary)
@ -1196,7 +1195,7 @@ void CConfigParser::ReadIniFile(const std::vector<std::wstring>& iniFileMappings
// It's a relative path so add the current path as a prefix // It's a relative path so add the current path as a prefix
value.insert(0, CRainmeter::ExtractPath(iniFile)); value.insert(0, CRainmeter::ExtractPath(iniFile));
} }
ReadIniFile(iniFileMappings, value, config, depth + 1); ReadIniFile(value, config, depth + 1);
} }
else else
{ {

View File

@ -100,7 +100,7 @@ private:
CMeasure* GetMeasure(const std::wstring& name); CMeasure* GetMeasure(const std::wstring& name);
void ReadIniFile(const std::vector<std::wstring>& iniFileMappings, const std::wstring& strFileName, LPCTSTR config = NULL, int depth = 0); void ReadIniFile(const std::wstring& strFileName, LPCTSTR config = NULL, int depth = 0);
void SetAutoSelectedMonitorVariables(CMeterWindow* meterWindow); void SetAutoSelectedMonitorVariables(CMeterWindow* meterWindow);

View File

@ -114,7 +114,7 @@ int CMeterString::GetX(bool abs)
switch (m_Align) switch (m_Align)
{ {
case ALIGN_CENTER: case ALIGN_CENTER:
x = x - (m_W / 2); x -= m_W / 2;
break; break;
case ALIGN_RIGHT: case ALIGN_RIGHT:

View File

@ -649,9 +649,8 @@ void CRainmeter::RainmeterWriteKeyValue(const WCHAR* arg)
} }
// Avoid "IniFileMapping" // Avoid "IniFileMapping"
std::vector<std::wstring> iniFileMappings; CSystem::UpdateIniFileMappingList();
CSystem::GetIniFileMappingList(iniFileMappings); std::wstring iniWrite = CSystem::GetTemporaryFile(iniFile);
std::wstring iniWrite = CSystem::GetTemporaryFile(iniFileMappings, iniFile);
if (iniWrite.size() == 1 && iniWrite[0] == L'?') // error occurred if (iniWrite.size() == 1 && iniWrite[0] == L'?') // error occurred
{ {
return; return;
@ -869,7 +868,7 @@ int CRainmeter::Initialize(HWND hParent, HINSTANCE hInstance, LPCWSTR szPath)
{ {
iniFile += L"Rainmeter.ini"; iniFile += L"Rainmeter.ini";
} }
else if (iniFile.length() <= 4 || _wcsicmp(iniFile.substr(iniFile.length() - 4).c_str(), L".ini") != 0) else if (iniFile.length() <= 4 || _wcsicmp(iniFile.c_str() + (iniFile.length() - 4), L".ini") != 0)
{ {
iniFile += L"\\Rainmeter.ini"; iniFile += L"\\Rainmeter.ini";
} }
@ -912,7 +911,7 @@ int CRainmeter::Initialize(HWND hParent, HINSTANCE hInstance, LPCWSTR szPath)
// Set the log file and stats file location // Set the log file and stats file location
m_LogFile = m_StatsFile = m_IniFile; m_LogFile = m_StatsFile = m_IniFile;
size_t logFileLen = m_LogFile.length(); size_t logFileLen = m_LogFile.length();
if (logFileLen > 4 && _wcsicmp(m_LogFile.substr(logFileLen - 4).c_str(), L".ini") == 0) if (logFileLen > 4 && _wcsicmp(m_LogFile.c_str() + (logFileLen - 4), L".ini") == 0)
{ {
m_LogFile.replace(logFileLen - 4, 4, L".log"); m_LogFile.replace(logFileLen - 4, 4, L".log");
m_StatsFile.replace(logFileLen - 4, 4, L".stats"); m_StatsFile.replace(logFileLen - 4, 4, L".stats");

View File

@ -36,9 +36,10 @@ enum TIMER
}; };
enum INTERVAL enum INTERVAL
{ {
INTERVAL_SHOWDESKTOP = 250, INTERVAL_SHOWDESKTOP = 250,
INTERVAL_NETSTATS = 60000, INTERVAL_RESTOREWINDOWS = 100,
INTERVAL_DELETELATER = 1000 INTERVAL_NETSTATS = 60000,
INTERVAL_DELETELATER = 1000
}; };
MULTIMONITOR_INFO CSystem::c_Monitors = { 0 }; MULTIMONITOR_INFO CSystem::c_Monitors = { 0 };
@ -54,6 +55,8 @@ OSPLATFORM CSystem::c_Platform = OSPLATFORM_UNKNOWN;
std::wstring CSystem::c_WorkingDirectory; std::wstring CSystem::c_WorkingDirectory;
std::vector<std::wstring> CSystem::c_IniFileMappings;
extern CRainmeter* Rainmeter; extern CRainmeter* Rainmeter;
/* /*
@ -909,12 +912,10 @@ bool CSystem::CheckDesktopState(HWND WorkerW)
if (c_ShowDesktop) if (c_ShowDesktop)
{ {
KillTimer(c_Window, TIMER_SHOWDESKTOP); SetTimer(c_Window, TIMER_SHOWDESKTOP, INTERVAL_RESTOREWINDOWS, NULL);
SetTimer(c_Window, TIMER_SHOWDESKTOP, 100, NULL);
} }
else else
{ {
KillTimer(c_Window, TIMER_SHOWDESKTOP);
SetTimer(c_Window, TIMER_SHOWDESKTOP, INTERVAL_SHOWDESKTOP, NULL); SetTimer(c_Window, TIMER_SHOWDESKTOP, INTERVAL_SHOWDESKTOP, NULL);
} }
} }
@ -1240,30 +1241,65 @@ bool CSystem::RemoveFolder(const std::wstring& strFolder)
} }
/* /*
** GetIniFileMappingList ** UpdateIniFileMappingList
** **
** Retrieves the "IniFileMapping" entries from Registry. ** Retrieves the "IniFileMapping" entries from Registry.
** **
*/ */
void CSystem::GetIniFileMappingList(std::vector<std::wstring>& iniFileMappings) void CSystem::UpdateIniFileMappingList()
{ {
HKEY hKey; static ULARGE_INTEGER s_LastWriteTime = {0};
LONG ret;
ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\IniFileMapping", 0, KEY_ENUMERATE_SUB_KEYS, &hKey); HKEY hKey;
LONG ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\IniFileMapping", 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hKey);
if (ret == ERROR_SUCCESS) if (ret == ERROR_SUCCESS)
{ {
WCHAR buffer[MAX_PATH]; DWORD numSubKeys;
DWORD index = 0, cch = MAX_PATH; ULARGE_INTEGER ftLastWriteTime;
bool changed = false;
while ((ret = RegEnumKeyEx(hKey, index++, buffer, &cch, NULL, NULL, NULL, NULL)) != ERROR_NO_MORE_ITEMS) ret = RegQueryInfoKey(hKey, NULL, NULL, NULL, &numSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, (LPFILETIME)&ftLastWriteTime);
if (ret == ERROR_SUCCESS)
{ {
if (ret == ERROR_SUCCESS) //LogWithArgs(LOG_DEBUG, L"IniFileMapping: numSubKeys=%u, ftLastWriteTime=%llu", numSubKeys, ftLastWriteTime.QuadPart);
if (ftLastWriteTime.QuadPart != s_LastWriteTime.QuadPart ||
numSubKeys != c_IniFileMappings.size())
{ {
iniFileMappings.push_back(buffer); s_LastWriteTime.QuadPart = ftLastWriteTime.QuadPart;
if (numSubKeys > c_IniFileMappings.capacity())
{
c_IniFileMappings.reserve(numSubKeys);
}
changed = true;
} }
cch = MAX_PATH;
} }
else
{
s_LastWriteTime.QuadPart = 0;
changed = true;
}
if (changed)
{
if (!c_IniFileMappings.empty())
{
c_IniFileMappings.clear();
}
WCHAR buffer[MAX_PATH];
DWORD index = 0, cch = MAX_PATH;
while ((ret = RegEnumKeyEx(hKey, index++, buffer, &cch, NULL, NULL, NULL, NULL)) != ERROR_NO_MORE_ITEMS)
{
if (ret == ERROR_SUCCESS)
{
c_IniFileMappings.push_back(buffer);
}
cch = MAX_PATH;
}
}
RegCloseKey(hKey); RegCloseKey(hKey);
} }
} }
@ -1276,28 +1312,19 @@ void CSystem::GetIniFileMappingList(std::vector<std::wstring>& iniFileMappings)
** Note that a temporary file must be deleted by caller. ** Note that a temporary file must be deleted by caller.
** **
*/ */
std::wstring CSystem::GetTemporaryFile(const std::vector<std::wstring>& iniFileMappings, const std::wstring& iniFile) std::wstring CSystem::GetTemporaryFile(const std::wstring& iniFile)
{ {
std::wstring temporary; std::wstring temporary;
if (!iniFileMappings.empty()) if (!c_IniFileMappings.empty())
{ {
std::wstring::size_type pos = iniFile.find_last_of(L"\\/"); std::wstring::size_type pos = iniFile.find_last_of(L"\\/");
std::wstring filename; const WCHAR* filename = iniFile.c_str() + ((pos != std::wstring::npos) ? pos + 1 : 0);
if (pos != std::wstring::npos) std::vector<std::wstring>::const_iterator iter = c_IniFileMappings.begin();
for ( ; iter != c_IniFileMappings.end(); ++iter)
{ {
filename.assign(iniFile, pos + 1, iniFile.length() - (pos + 1)); if (_wcsicmp((*iter).c_str(), filename) == 0)
}
else
{
filename = iniFile;
}
std::vector<std::wstring>::const_iterator iter = iniFileMappings.begin();
for ( ; iter != iniFileMappings.end(); ++iter)
{
if (_wcsicmp((*iter).c_str(), filename.c_str()) == 0)
{ {
WCHAR buffer[MAX_PATH]; WCHAR buffer[MAX_PATH];
@ -1307,7 +1334,7 @@ std::wstring CSystem::GetTemporaryFile(const std::vector<std::wstring>& iniFileM
{ {
temporary = buffer; temporary = buffer;
std::wstring tmp = GetTemporaryFile(iniFileMappings, temporary); std::wstring tmp = GetTemporaryFile(temporary);
if (tmp.empty() && CopyFiles(iniFile, temporary)) if (tmp.empty() && CopyFiles(iniFile, temporary))
{ {
return temporary; return temporary;

View File

@ -85,8 +85,8 @@ public:
static bool RemoveFile(const std::wstring& file); static bool RemoveFile(const std::wstring& file);
static bool RemoveFolder(const std::wstring& strFolder); static bool RemoveFolder(const std::wstring& strFolder);
static void GetIniFileMappingList(std::vector<std::wstring>& iniFileMappings); static void UpdateIniFileMappingList();
static std::wstring GetTemporaryFile(const std::vector<std::wstring>& iniFileMappings, const std::wstring& iniFile); static std::wstring GetTemporaryFile(const std::wstring& iniFile);
private: private:
static void CALLBACK MyWinEventProc(HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime); static void CALLBACK MyWinEventProc(HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime);
@ -118,6 +118,8 @@ private:
static OSPLATFORM c_Platform; static OSPLATFORM c_Platform;
static std::wstring c_WorkingDirectory; static std::wstring c_WorkingDirectory;
static std::vector<std::wstring> c_IniFileMappings;
}; };
#endif #endif