From 2a166b520f7a1ac5f6169a6d56b4d0ab6a5802d3 Mon Sep 17 00:00:00 2001 From: spx Date: Sat, 5 Nov 2011 09:01:06 +0000 Subject: [PATCH] Minor tweaks and cleanup. --- Library/ConfigParser.cpp | 67 ++++++++++++++++++------------------ Library/MeasureDiskSpace.cpp | 3 +- Library/MeterImage.cpp | 4 +-- Library/MeterWindow.cpp | 4 +-- Library/Rainmeter.cpp | 9 +++-- Library/System.h | 4 +++ 6 files changed, 46 insertions(+), 45 deletions(-) diff --git a/Library/ConfigParser.cpp b/Library/ConfigParser.cpp index 2ff6daf6..bfdacd61 100644 --- a/Library/ConfigParser.cpp +++ b/Library/ConfigParser.cpp @@ -591,45 +591,41 @@ const std::wstring& CConfigParser::ReadString(LPCTSTR section, LPCTSTR key, LPCT const std::wstring strSection = section; const std::wstring strKey = key; - std::wstring strDefault = defValue; - - // If the template is defined read the value first from there. - if (m_StyleTemplate.size() > 0) - { - std::vector::const_reverse_iterator iter = m_StyleTemplate.rbegin(); - for ( ; iter != m_StyleTemplate.rend(); ++iter) - { - const std::wstring& strStyle = GetValue((*iter), strKey, strDefault); - - //LogWithArgs(LOG_DEBUG, L"[%s] %s (from [%s]) : strDefault=%s (0x%p), strStyle=%s (0x%p)", - // section, key, (*iter).c_str(), strDefault.c_str(), &strDefault, strStyle.c_str(), &strStyle); - - if (&strStyle != &strDefault) - { - strDefault = strStyle; - m_LastUsedStyle = (*iter); - break; - } - } - } + const std::wstring strDefault = defValue; const std::wstring& strValue = GetValue(strSection, strKey, strDefault); - result = strValue; - - if (m_LastUsedStyle.size() > 0) + if (&strValue == &strDefault) { - if (&strValue != &strDefault) + // If the template is defined read the value from there. + if (m_StyleTemplate.size() > 0) { - m_LastUsedStyle.clear(); + std::vector::const_reverse_iterator iter = m_StyleTemplate.rbegin(); + for ( ; iter != m_StyleTemplate.rend(); ++iter) + { + const std::wstring& strStyleValue = GetValue((*iter), strKey, strDefault); + + //LogWithArgs(LOG_DEBUG, L"StyleTemplate: [%s] %s (from [%s]) : strDefault=%s (0x%p), strStyleValue=%s (0x%p)", + // section, key, (*iter).c_str(), strDefault.c_str(), &strDefault, strStyleValue.c_str(), &strStyleValue); + + if (&strStyleValue != &strDefault) + { + result = strStyleValue; + m_LastUsedStyle = (*iter); + break; + } + } + } + + if (m_LastUsedStyle.size() == 0) // No template found + { + result = strDefault; + m_LastDefaultUsed = true; + return result; } } else { - if (&strValue == &strDefault) - { - m_LastDefaultUsed = true; - return result; - } + result = strValue; } if (result.size() > 0) @@ -1144,6 +1140,7 @@ void CConfigParser::ReadIniFile(const std::vector& iniFileMappings { std::unordered_set foundKeys; bool isVariables = (_wcsicmp((*iter).c_str(), L"Variables") == 0); + bool isMetadata = (config == NULL && _wcsicmp((*iter).c_str(), L"Metadata") == 0); // Read all "key=value" from the section while (true) @@ -1190,8 +1187,7 @@ void CConfigParser::ReadIniFile(const std::vector& iniFileMappings { ReadVariables(); ReplaceVariables(value); - if (value.find(L':') == std::wstring::npos && - (valueLen < 2 || (value[0] != L'\\' && value[0] != L'/') || (value[1] != L'\\' && value[1] != L'/'))) + if (!CSystem::IsAbsolutePath(value)) { // It's a relative path so add the current path as a prefix value.insert(0, CRainmeter::ExtractPath(iniFile)); @@ -1200,7 +1196,10 @@ void CConfigParser::ReadIniFile(const std::vector& iniFileMappings } else { - SetValue((*iter), key, value); + if (!isMetadata) // Uncache Metadata's key-value pair in the skin + { + SetValue((*iter), key, value); + } if (isVariables) { diff --git a/Library/MeasureDiskSpace.cpp b/Library/MeasureDiskSpace.cpp index 1f9eba53..6075db02 100644 --- a/Library/MeasureDiskSpace.cpp +++ b/Library/MeasureDiskSpace.cpp @@ -19,6 +19,7 @@ #include "StdAfx.h" #include "MeasureDiskSpace.h" #include "Rainmeter.h" +#include "System.h" /* ** CMeasureDiskSpace @@ -156,7 +157,7 @@ void CMeasureDiskSpace::ReadConfig(CConfigParser& parser, const WCHAR* section) m_OldTotalBytes = 0; m_LabelName = L""; } - else if (m_Drive[m_Drive.length() - 1] != L'\\') // E.g. "C:" + else if (!CSystem::IsPathSeparator(m_Drive[m_Drive.length() - 1])) // E.g. "C:" { m_Drive += L"\\"; // A trailing backslash is required. } diff --git a/Library/MeterImage.cpp b/Library/MeterImage.cpp index d079e314..f20af84b 100644 --- a/Library/MeterImage.cpp +++ b/Library/MeterImage.cpp @@ -21,6 +21,7 @@ #include "Measure.h" #include "Error.h" #include "Rainmeter.h" +#include "System.h" extern CRainmeter* Rainmeter; @@ -129,8 +130,7 @@ void CMeterImage::ReadConfig(CConfigParser& parser, const WCHAR* section) m_Path = parser.ReadString(section, L"Path", L""); if (!m_Path.empty()) { - WCHAR ch = m_Path[m_Path.length() - 1]; - if (ch != L'\\' && ch != L'/') + if (!CSystem::IsPathSeparator(m_Path[m_Path.length() - 1])) { m_Path += L"\\"; } diff --git a/Library/MeterWindow.cpp b/Library/MeterWindow.cpp index b8d014ed..1cf18264 100644 --- a/Library/MeterWindow.cpp +++ b/Library/MeterWindow.cpp @@ -5094,9 +5094,7 @@ std::wstring CMeterWindow::MakePathAbsolute(const std::wstring& path) { std::wstring absolute; - if (path.empty() || - path.find(L":\\") != std::wstring::npos || path.find(L":/") != std::wstring::npos || - (path.length() >= 2 && (path[0] == L'\\' || path[0] == L'/') && (path[1] == L'\\' || path[1] == L'/'))) // UNC + if (path.empty() || CSystem::IsAbsolutePath(path)) { absolute = path; // It's already absolute path (or it's empty) } diff --git a/Library/Rainmeter.cpp b/Library/Rainmeter.cpp index 56041a6f..2c9adcc7 100644 --- a/Library/Rainmeter.cpp +++ b/Library/Rainmeter.cpp @@ -865,7 +865,7 @@ int CRainmeter::Initialize(HWND hParent, HINSTANCE hInstance, LPCWSTR szPath) ExpandEnvironmentVariables(iniFile); - if (iniFile.empty() || iniFile[iniFile.length() - 1] == L'\\') + if (iniFile.empty() || CSystem::IsPathSeparator(iniFile[iniFile.length() - 1])) { iniFile += L"Rainmeter.ini"; } @@ -874,7 +874,7 @@ int CRainmeter::Initialize(HWND hParent, HINSTANCE hInstance, LPCWSTR szPath) iniFile += L"\\Rainmeter.ini"; } - if (iniFile[0] != L'\\' && iniFile[0] != L'/' && iniFile.find_first_of(L':') == std::wstring::npos) + if (!CSystem::IsPathSeparator(iniFile[0]) && iniFile.find_first_of(L':') == std::wstring::npos) { // Make absolute path iniFile.insert(0, m_Path); @@ -990,8 +990,7 @@ int CRainmeter::Initialize(HWND hParent, HINSTANCE hInstance, LPCWSTR szPath) if (!m_SkinPath.empty()) { - WCHAR ch = m_SkinPath[m_SkinPath.size() - 1]; - if (ch != L'\\' && ch != L'/') + if (!CSystem::IsPathSeparator(m_SkinPath[m_SkinPath.size() - 1])) { m_SkinPath += L"\\"; } @@ -1054,7 +1053,7 @@ int CRainmeter::Initialize(HWND hParent, HINSTANCE hInstance, LPCWSTR szPath) { m_Drive.assign(m_Path, 0, loc + 1); } - else if (m_Path.length() >= 2 && (m_Path[0] == L'\\' || m_Path[0] == L'/') && (m_Path[1] == L'\\' || m_Path[1] == L'/')) + else if (CSystem::IsUNCPath(m_Path)) { if ((loc = m_Path.find_first_of(L"\\/", 2)) != std::wstring::npos) { diff --git a/Library/System.h b/Library/System.h index e2614564..bfe9147c 100644 --- a/Library/System.h +++ b/Library/System.h @@ -72,6 +72,10 @@ public: static OSPLATFORM GetOSPlatform(); static ULONGLONG GetTickCount64(); + static bool IsPathSeparator(WCHAR ch) { return (ch == L'\\' || ch == L'/'); } + static bool IsUNCPath(const std::wstring& path) { return (path.length() >= 2 && IsPathSeparator(path[0]) && IsPathSeparator(path[1])); } + static bool IsAbsolutePath(const std::wstring& path) { return (path.find(L":\\") != std::wstring::npos || path.find(L":/") != std::wstring::npos || IsUNCPath(path)); } + static HMODULE RmLoadLibrary(LPCWSTR lpLibFileName, DWORD* dwError = NULL, bool ignoreErrors = false); static void ResetWorkingDirectory();