From e84e7d534da3b38b5a125b0c8e535dd47426c7a5 Mon Sep 17 00:00:00 2001 From: Kimmo Pekkola Date: Fri, 18 Sep 2009 15:30:51 +0000 Subject: [PATCH] The skin files can read other files with @include statement. --- Library/ConfigParser.cpp | 146 ++++++++++++++++++++++++--------------- Library/ConfigParser.h | 5 +- Library/MeterWindow.cpp | 41 ++++------- Library/TrayWindow.cpp | 2 +- revision-number.h | 2 +- 5 files changed, 108 insertions(+), 88 deletions(-) diff --git a/Library/ConfigParser.cpp b/Library/ConfigParser.cpp index c870ecf8..5c3e2bc1 100644 --- a/Library/ConfigParser.cpp +++ b/Library/ConfigParser.cpp @@ -61,10 +61,10 @@ void CConfigParser::Initialize(LPCTSTR filename, CRainmeter* pRainmeter) m_Variables.clear(); m_Measures.clear(); + m_Keys.clear(); + m_Values.clear(); - ReadIniFile(m_Filename); - - // Set the default variables + // Set the default variables. Do this before the ini file is read so that the paths can be used with @include if (pRainmeter) { SetVariable(L"PROGRAMPATH", pRainmeter->GetPath()); @@ -92,6 +92,7 @@ void CConfigParser::Initialize(LPCTSTR filename, CRainmeter* pRainmeter) SetVariable(L"SCREENAREAHEIGHT", buffer); } + ReadIniFile(m_Filename); ReadVariables(); } @@ -110,22 +111,72 @@ void CConfigParser::ReadVariables() } } -//============================================================================== /** ** Sets a new value for the variable. The DynamicVariables must be set to 1 in the ** meter/measure for the changes to be applied. ** ** \param strVariable ** \param strValue -** \return void */ void CConfigParser::SetVariable(const std::wstring& strVariable, const std::wstring& strValue) { + // DebugLog(L"Variable: %s=%s (size=%i)", strVariable.c_str(), strValue.c_str(), m_Variables.size()); + std::wstring strTmp(strVariable); std::transform(strTmp.begin(), strTmp.end(), strTmp.begin(), ::tolower); m_Variables[strTmp] = strValue; } +/** +** Replaces environment and internal variables in the given string. +** +** \param result The string where the variables are returned. The string is modified. +*/ +void CConfigParser::ReplaceVariables(std::wstring& result) +{ + CRainmeter::ExpandEnvironmentVariables(result); + + // Check for variables (#VAR#) + size_t start = 0; + size_t end = std::wstring::npos; + size_t pos = std::wstring::npos; + bool loop = true; + + do + { + pos = result.find(L'#', start); + if (pos != std::wstring::npos) + { + end = result.find(L'#', pos + 1); + if (end != std::wstring::npos) + { + std::wstring strTmp(result.begin() + pos + 1, result.begin() + end); + std::transform(strTmp.begin(), strTmp.end(), strTmp.begin(), ::tolower); + + std::map::iterator iter = m_Variables.find(strTmp); + if (iter != m_Variables.end()) + { + // Variable found, replace it with the value + result.replace(result.begin() + pos, result.begin() + end + 1, (*iter).second); + start = pos + (*iter).second.length(); + } + else + { + start = end; + } + } + else + { + loop = false; + } + } + else + { + loop = false; + } + } while(loop); +} + /* ** ReadString ** @@ -175,56 +226,16 @@ const std::wstring& CConfigParser::ReadString(LPCTSTR section, LPCTSTR key, LPCT } } - CRainmeter::ExpandEnvironmentVariables(result); - - // Check for variables (#VAR#) - size_t start = 0; - size_t end = std::wstring::npos; - size_t pos = std::wstring::npos; - bool loop = true; - - do - { - pos = result.find(L'#', start); - if (pos != std::wstring::npos) - { - end = result.find(L'#', pos + 1); - if (end != std::wstring::npos) - { - std::wstring strTmp(result.begin() + pos + 1, result.begin() + end); - std::transform(strTmp.begin(), strTmp.end(), strTmp.begin(), ::tolower); - - std::map::iterator iter = m_Variables.find(strTmp); - if (iter != m_Variables.end()) - { - // Variable found, replace it with the value - result.replace(result.begin() + pos, result.begin() + end + 1, (*iter).second); - start = pos + (*iter).second.length(); - } - else - { - start = end; - } - } - else - { - loop = false; - } - } - else - { - loop = false; - } - } while(loop); + ReplaceVariables(result); // Check for measures ([Measure]) if (!m_Measures.empty() && bReplaceMeasures) { - start = 0; - end = std::wstring::npos; - pos = std::wstring::npos; + size_t start = 0; + size_t end = std::wstring::npos; + size_t pos = std::wstring::npos; size_t pos2 = std::wstring::npos; - loop = true; + bool loop = true; do { pos = result.find(L'[', start); @@ -459,10 +470,15 @@ Color CConfigParser::ParseColor(LPCTSTR string) ** ** \param iniFile The ini file to be read. */ -void CConfigParser::ReadIniFile(const std::wstring& iniFile) +void CConfigParser::ReadIniFile(const std::wstring& iniFile, int depth) { - m_Keys.clear(); - m_Values.clear(); + // DebugLog(L"Reading file: %s", iniFile.c_str()); + + if (depth > 100) // Is 100 enough to assume the include loop never ends? + { + MessageBox(NULL, L"It looks like you've made an infinite\nloop with the @include statements.\nPlease check your skin.", L"Rainmeter", MB_OK | MB_ICONERROR); + return; + } // Get all the sections (i.e. different meters) WCHAR* items = new WCHAR[MAX_LINE_LENGTH]; @@ -487,8 +503,10 @@ void CConfigParser::ReadIniFile(const std::wstring& iniFile) { std::wstring strTmp(pos); std::transform(strTmp.begin(), strTmp.end(), strTmp.begin(), ::tolower); - m_Keys[strTmp] = std::vector(); - + if (m_Keys.find(strTmp) == m_Keys.end()) + { + m_Keys[strTmp] = std::vector(); + } pos = pos + wcslen(pos) + 1; } @@ -526,7 +544,21 @@ void CConfigParser::ReadIniFile(const std::wstring& iniFile) buffer = new WCHAR[bufferSize]; }; - SetValue((*iter).first, strKey, buffer); + if (wcsnicmp(strKey.c_str(), L"@include", 8) == 0) + { + std::wstring strIncludeFile = buffer; + ReplaceVariables(strIncludeFile); + if (strIncludeFile.find(L':') == std::wstring::npos) + { + // It's a relative path so add the current path as a prefix + strIncludeFile = CRainmeter::ExtractPath(iniFile) + strIncludeFile; + } + ReadIniFile(strIncludeFile, depth + 1); + } + else + { + SetValue((*iter).first, strKey, buffer); + } pos = pos + wcslen(pos) + 1; } @@ -545,6 +577,8 @@ void CConfigParser::ReadIniFile(const std::wstring& iniFile) */ void CConfigParser::SetValue(const std::wstring& strSection, const std::wstring& strKey, const std::wstring& strValue) { + // DebugLog(L"[%s] %s=%s (size: %i)", strSection.c_str(), strKey.c_str(), strValue.c_str(), m_Values.size()); + std::wstring strTmpSection(strSection); std::wstring strTmpKey(strKey); std::transform(strTmpSection.begin(), strTmpSection.end(), strTmpSection.begin(), ::tolower); diff --git a/Library/ConfigParser.h b/Library/ConfigParser.h index 5bfa540a..7049a905 100644 --- a/Library/ConfigParser.h +++ b/Library/ConfigParser.h @@ -49,16 +49,17 @@ public: std::vector ReadFloats(LPCTSTR section, LPCTSTR key); std::wstring& GetFilename() { return m_Filename; } + std::vector GetSections(); private: void ReadVariables(); + void ReplaceVariables(std::wstring& result); Gdiplus::Color ParseColor(LPCTSTR string); std::vector Tokenize(const std::wstring& str, const std::wstring delimiters); - void ReadIniFile(const std::wstring& strFileName); + void ReadIniFile(const std::wstring& strFileName, int depth = 0); void SetValue(const std::wstring& strSection, const std::wstring& strKey, const std::wstring& strValue); const std::wstring& GetValue(const std::wstring& strSection, const std::wstring& strKey, const std::wstring& strDefault); - std::vector GetSections(); std::vector GetKeys(const std::wstring& strSection); std::map m_Variables; diff --git a/Library/MeterWindow.cpp b/Library/MeterWindow.cpp index 39b91c0c..5e01ad13 100644 --- a/Library/MeterWindow.cpp +++ b/Library/MeterWindow.cpp @@ -1506,42 +1506,30 @@ void CMeterWindow::ReadSkin() // Create the meters and measures - // Get all the sections (i.e. different meters) - WCHAR* items = new WCHAR[MAX_LINE_LENGTH]; - int size = MAX_LINE_LENGTH; + // Get all the sections (i.e. different meters, measures and the other stuff) + std::vector arraySections = m_Parser.GetSections(); - // Get all the sections - while(true) + for (size_t i = 0; i < arraySections.size(); i++) { - int res = GetPrivateProfileString( NULL, NULL, NULL, items, size, iniFile.c_str()); - if (res == 0) { delete [] items; return; } // File not found - if (res < size - 2) break; // Fits in the buffer + std::wstring strSection = arraySections[i]; - delete [] items; - size *= 2; - items = new WCHAR[size]; - }; - - WCHAR* pos = items; - while(wcslen(pos) > 0) - { - if(wcsicmp(L"Rainmeter", pos) != 0 && - wcsicmp(L"Variables", pos) != 0 && - wcsicmp(L"Metadata", pos) != 0) + if(wcsicmp(L"Rainmeter", strSection.c_str()) != 0 && + wcsicmp(L"Variables", strSection.c_str()) != 0 && + wcsicmp(L"Metadata", strSection.c_str()) != 0) { std::wstring meterName, measureName; // Check if the item is a meter or a measure (or perhaps something else) - measureName = m_Parser.ReadString(pos, L"Measure", L""); - meterName = m_Parser.ReadString(pos, L"Meter", L""); + measureName = m_Parser.ReadString(strSection.c_str(), L"Measure", L""); + meterName = m_Parser.ReadString(strSection.c_str(), L"Meter", L""); if (measureName.length() > 0) { try { // It's a measure CMeasure* measure = CMeasure::Create(measureName.c_str(), this); - measure->SetName(pos); - measure->ReadConfig(m_Parser, pos); + measure->SetName(strSection.c_str()); + measure->ReadConfig(m_Parser, strSection.c_str()); m_Measures.push_back(measure); m_Parser.AddMeasure(measure); @@ -1557,8 +1545,8 @@ void CMeterWindow::ReadSkin() { // It's a meter CMeter* meter = CMeter::Create(meterName.c_str(), this); - meter->SetName(pos); - meter->ReadConfig(pos); + meter->SetName(strSection.c_str()); + meter->ReadConfig(strSection.c_str()); m_Meters.push_back(meter); } catch (CError& error) @@ -1568,11 +1556,8 @@ void CMeterWindow::ReadSkin() } // If it's not a meter or measure it will be ignored } - pos = pos + wcslen(pos) + 1; } - delete [] items; - if (m_Meters.empty()) { MessageBox(m_Window, L"Your configuration file doesn't contain any meters!\nYour skin's ini-file might be out of date.", APPNAME, MB_OK | MB_TOPMOST | MB_ICONEXCLAMATION); diff --git a/Library/TrayWindow.cpp b/Library/TrayWindow.cpp index 55898f1a..18ccaf5c 100644 --- a/Library/TrayWindow.cpp +++ b/Library/TrayWindow.cpp @@ -493,7 +493,7 @@ LRESULT CALLBACK CTrayWindow::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA int pos = (wParam & 0x0ffff) - ID_THEME_FIRST; const std::vector& themes = Rainmeter->GetAllThemes(); - if (pos >= 0 && pos < themes.size()) + if (pos >= 0 && pos < (int)themes.size()) { std::wstring command = L"\"" + Rainmeter->GetPath(); command += L"\\Addons\\RainThemes\\RainThemes.exe\" /load \""; diff --git a/revision-number.h b/revision-number.h index bcb5b1c2..c50c43ef 100644 --- a/revision-number.h +++ b/revision-number.h @@ -1,2 +1,2 @@ #pragma once -const int revision_number = 192; \ No newline at end of file +const int revision_number = 213; \ No newline at end of file