From 957e37fbf232090178f07ea9afc5facfcfb5a76b Mon Sep 17 00:00:00 2001 From: Brian Ferguson Date: Sat, 4 Jan 2014 09:06:07 -0700 Subject: [PATCH] Added [Measure:EscapeRegExp] section variable for measures. This will escape any regular expression meta-characters for use with IfMatch. --- Library/ConfigParser.cpp | 51 +++++++++++++++++++++++++++++++++++++++- Library/ConfigParser.h | 2 ++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/Library/ConfigParser.cpp b/Library/ConfigParser.cpp index 166a7ca6..20052a7c 100644 --- a/Library/ConfigParser.cpp +++ b/Library/ConfigParser.cpp @@ -230,12 +230,14 @@ bool ConfigParser::GetSectionVariable(std::wstring& strVariable, std::wstring& s // Percentual: [Measure:%], [Measure:%, dec] // Scale: [Measure:/scale], [Measure:/scale, dec] // Max/Min: [Measure:MaxValue], [Measure:MaxValue:/scale, dec] ('%' cannot be used) + // EscapeRegExp: [Measure:EscapeRegExp] (Escapes regular expression syntax, used for 'IfMatch') enum VALUETYPE { RAW = 0, PERCENTUAL = 1, MAX = 2, - MIN = 3 + MIN = 3, + SPECIAL = 4 } valueType = RAW; if (isKeySelector) @@ -248,6 +250,10 @@ bool ConfigParser::GetSectionVariable(std::wstring& strVariable, std::wstring& s { valueType = MIN; } + else if (_wcsicmp(selectorSz, L"EscapeRegExp") == 0) + { + valueType = SPECIAL; + } else { return false; @@ -287,6 +293,19 @@ bool ConfigParser::GetSectionVariable(std::wstring& strVariable, std::wstring& s Measure* measure = m_MeterWindow->GetMeasure(strVariable); if (measure) { + if (valueType == SPECIAL) // "Special" functions: EscapeRegExp + { + if (_wcsicmp(selector.c_str(), L"EscapeRegExp") == 0) + { + std::wstring str = measure->GetStringValue(); + EscapeRegExp(str); + strValue.assign(str); + } + //else if (_wcsicmp(selector.c_str(), L"") == 0) + + return true; + } + int scale = 1; const WCHAR* decimalsSz = wcschr(selectorSz, L','); @@ -364,6 +383,36 @@ bool ConfigParser::GetSectionVariable(std::wstring& strVariable, std::wstring& s return false; } +/* +** Used to escape regular expression metacharacters for use in IfMatch +** +*/ +void ConfigParser::EscapeRegExp(std::wstring& str) +{ + auto replace = [&str](WCHAR reservedChar) + { + size_t start = 0; + while ((start = str.find(reservedChar, start)) != std::wstring::npos) + { + str.insert(start, L"\\"); + start += 2; + } + }; + + replace(L'\\'); + replace(L'^'); + replace(L'$'); + replace(L'|'); + replace(L'('); + replace(L')'); + replace(L'['); + replace(L'{'); + replace(L'.'); + replace(L'+'); + replace(L'*'); + replace(L'?'); +} + void ConfigParser::ResetMonitorVariables(MeterWindow* meterWindow) { // Set the SCREENAREA/WORKAREA variables diff --git a/Library/ConfigParser.h b/Library/ConfigParser.h index d8c6d54a..589c254b 100644 --- a/Library/ConfigParser.h +++ b/Library/ConfigParser.h @@ -110,6 +110,8 @@ private: bool GetSectionVariable(std::wstring& strVariable, std::wstring& strValue); + void EscapeRegex(std::wstring& str); + static void SetVariable(std::unordered_map& variables, const std::wstring& strVariable, const std::wstring& strValue); static void SetVariable(std::unordered_map& variables, const WCHAR* strVariable, const WCHAR* strValue);