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);
ResetMonitorVariables(meterWindow);
std::vector<std::wstring> iniFileMappings;
CSystem::GetIniFileMappingList(iniFileMappings);
CSystem::UpdateIniFileMappingList();
ReadIniFile(iniFileMappings, m_Filename, config);
ReadIniFile(m_Filename, config);
ReadVariables();
// Clear and minimize
@ -449,35 +448,35 @@ bool CConfigParser::ReplaceVariables(std::wstring& result)
}
// Check for variables (#VAR#)
size_t start = 0;
size_t end = std::wstring::npos;
size_t pos = std::wstring::npos;
size_t start = 0, end;
bool loop = true;
do
{
pos = result.find(L'#', start);
if (pos != std::wstring::npos)
start = result.find(L'#', start);
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 (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(end - 2, 1);
start = end - 1;
result.erase(ei, 1);
result.erase(si, 1);
start = ei;
}
else
{
std::wstring strVariable = result.substr(pos + 1, end - (pos + 1));
std::wstring strVariable = result.substr(si, end - si);
std::wstring strValue;
if (GetVariable(strVariable, strValue))
{
// Variable found, replace it with the value
result.replace(pos, end - pos + 1, strValue);
start = pos + strValue.length();
result.replace(start, end - start + 1, strValue);
start += strValue.length();
replaced = true;
}
else
@ -513,40 +512,40 @@ bool CConfigParser::ReplaceMeasures(std::wstring& result)
// Check for measures ([Measure])
if (!m_Measures.empty())
{
size_t start = 0;
size_t end = std::wstring::npos;
size_t pos = std::wstring::npos;
size_t pos2 = std::wstring::npos;
size_t start = 0, end, next;
bool loop = true;
do
{
pos = result.find(L'[', start);
if (pos != std::wstring::npos)
start = result.find(L'[', start);
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)
{
pos2 = result.find(L'[', pos + 1);
if (pos2 == std::wstring::npos || end < pos2)
next = result.find(L'[', si);
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(end - 2, 1);
start = end - 1;
result.erase(ei, 1);
result.erase(si, 1);
start = ei;
}
else
{
std::wstring var = result.substr(pos + 1, end - (pos + 1));
std::wstring var = result.substr(si, end - si);
CMeasure* measure = GetMeasure(var);
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
result.replace(pos, end - pos + 1, value);
start = pos + value.length();
result.replace(start, end - start + 1, value);
start += value.length();
replaced = true;
}
else
@ -557,7 +556,7 @@ bool CConfigParser::ReplaceMeasures(std::wstring& result)
}
else
{
start = pos2;
start = next;
}
}
else
@ -835,8 +834,8 @@ void CConfigParser::Shrink(std::vector<std::wstring>& vec)
{
if (!vec.empty())
{
std::vector<std::wstring>::iterator iter = vec.begin();
while (iter != vec.end())
std::vector<std::wstring>::reverse_iterator iter = vec.rbegin();
while (iter != vec.rend())
{
std::wstring::size_type pos = (*iter).find_first_not_of(L" \t\r\n");
if (pos != std::wstring::npos)
@ -852,7 +851,7 @@ void CConfigParser::Shrink(std::vector<std::wstring>& vec)
else
{
// 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.
*/
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?
{
@ -1058,7 +1057,7 @@ void CConfigParser::ReadIniFile(const std::vector<std::wstring>& iniFileMappings
}
// 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'?'));
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
value.insert(0, CRainmeter::ExtractPath(iniFile));
}
ReadIniFile(iniFileMappings, value, config, depth + 1);
ReadIniFile(value, config, depth + 1);
}
else
{

View File

@ -100,7 +100,7 @@ private:
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);

View File

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

View File

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

View File

@ -37,6 +37,7 @@ enum TIMER
enum INTERVAL
{
INTERVAL_SHOWDESKTOP = 250,
INTERVAL_RESTOREWINDOWS = 100,
INTERVAL_NETSTATS = 60000,
INTERVAL_DELETELATER = 1000
};
@ -54,6 +55,8 @@ OSPLATFORM CSystem::c_Platform = OSPLATFORM_UNKNOWN;
std::wstring CSystem::c_WorkingDirectory;
std::vector<std::wstring> CSystem::c_IniFileMappings;
extern CRainmeter* Rainmeter;
/*
@ -909,12 +912,10 @@ bool CSystem::CheckDesktopState(HWND WorkerW)
if (c_ShowDesktop)
{
KillTimer(c_Window, TIMER_SHOWDESKTOP);
SetTimer(c_Window, TIMER_SHOWDESKTOP, 100, NULL);
SetTimer(c_Window, TIMER_SHOWDESKTOP, INTERVAL_RESTOREWINDOWS, NULL);
}
else
{
KillTimer(c_Window, TIMER_SHOWDESKTOP);
SetTimer(c_Window, TIMER_SHOWDESKTOP, INTERVAL_SHOWDESKTOP, NULL);
}
}
@ -1240,19 +1241,52 @@ bool CSystem::RemoveFolder(const std::wstring& strFolder)
}
/*
** GetIniFileMappingList
** UpdateIniFileMappingList
**
** Retrieves the "IniFileMapping" entries from Registry.
**
*/
void CSystem::GetIniFileMappingList(std::vector<std::wstring>& iniFileMappings)
void CSystem::UpdateIniFileMappingList()
{
HKEY hKey;
LONG ret;
static ULARGE_INTEGER s_LastWriteTime = {0};
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)
{
DWORD numSubKeys;
ULARGE_INTEGER ftLastWriteTime;
bool changed = false;
ret = RegQueryInfoKey(hKey, NULL, NULL, NULL, &numSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, (LPFILETIME)&ftLastWriteTime);
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())
{
s_LastWriteTime.QuadPart = ftLastWriteTime.QuadPart;
if (numSubKeys > c_IniFileMappings.capacity())
{
c_IniFileMappings.reserve(numSubKeys);
}
changed = true;
}
}
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;
@ -1260,10 +1294,12 @@ void CSystem::GetIniFileMappingList(std::vector<std::wstring>& iniFileMappings)
{
if (ret == ERROR_SUCCESS)
{
iniFileMappings.push_back(buffer);
c_IniFileMappings.push_back(buffer);
}
cch = MAX_PATH;
}
}
RegCloseKey(hKey);
}
}
@ -1276,28 +1312,19 @@ void CSystem::GetIniFileMappingList(std::vector<std::wstring>& iniFileMappings)
** 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;
if (!iniFileMappings.empty())
if (!c_IniFileMappings.empty())
{
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));
}
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)
if (_wcsicmp((*iter).c_str(), filename) == 0)
{
WCHAR buffer[MAX_PATH];
@ -1307,7 +1334,7 @@ std::wstring CSystem::GetTemporaryFile(const std::vector<std::wstring>& iniFileM
{
temporary = buffer;
std::wstring tmp = GetTemporaryFile(iniFileMappings, temporary);
std::wstring tmp = GetTemporaryFile(temporary);
if (tmp.empty() && CopyFiles(iniFile, temporary))
{
return temporary;

View File

@ -85,8 +85,8 @@ public:
static bool RemoveFile(const std::wstring& file);
static bool RemoveFolder(const std::wstring& strFolder);
static void GetIniFileMappingList(std::vector<std::wstring>& iniFileMappings);
static std::wstring GetTemporaryFile(const std::vector<std::wstring>& iniFileMappings, const std::wstring& iniFile);
static void UpdateIniFileMappingList();
static std::wstring GetTemporaryFile(const std::wstring& iniFile);
private:
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 std::wstring c_WorkingDirectory;
static std::vector<std::wstring> c_IniFileMappings;
};
#endif