diff --git a/Library/ConfigParser.cpp b/Library/ConfigParser.cpp index ff314921..2ce7dd2d 100644 --- a/Library/ConfigParser.cpp +++ b/Library/ConfigParser.cpp @@ -794,14 +794,14 @@ const std::wstring& CConfigParser::ReadString(LPCTSTR section, LPCTSTR key, LPCT { if (result.find(L'#') != std::wstring::npos) { - SetCurrentSection(strSection); // Set temporarily + m_CurrentSection->assign(strSection); // Set temporarily if (ReplaceVariables(result)) { m_LastReplaced = true; } - ClearCurrentSection(); // Reset + m_CurrentSection->clear(); // Reset } else { @@ -852,16 +852,25 @@ CMeasure* CConfigParser::GetMeasure(const std::wstring& name) std::vector CConfigParser::ReadFloats(LPCTSTR section, LPCTSTR key) { std::vector result; - const std::wstring& string = ReadString(section, key, L""); - if (!string.empty()) + const std::wstring& str = ReadString(section, key, L""); + if (!str.empty()) { // Tokenize and parse the floats - std::vector tokens = Tokenize(string, L";"); - std::vector::const_iterator iter = tokens.begin(); - for ( ; iter != tokens.end(); ++iter) + const WCHAR delimiter = L';'; + size_t lastPos, pos = 0; + do { - result.push_back((Gdiplus::REAL)ParseDouble((*iter).c_str(), 0.0)); + lastPos = str.find_first_not_of(delimiter, pos); + if (lastPos == std::wstring::npos) break; + + pos = str.find_first_of(delimiter, lastPos + 1); + + result.push_back((Gdiplus::REAL)ParseDouble(str.substr(lastPos, pos - lastPos).c_str(), 0.0)); // (pos != std::wstring::npos) ? pos - lastPos : pos + if (pos == std::wstring::npos) break; + + ++pos; } + while (true); } return result; } @@ -1045,58 +1054,44 @@ RECT CConfigParser::ReadRECT(LPCTSTR section, LPCTSTR key, const RECT& defValue) } /* -** Splits the string from the delimiters +** Splits the string from the delimiters. +** Now trims empty element in vector and white-space in each string. ** -** http://www.digitalpeer.com/id/simple +** Modified from http://www.digitalpeer.com/id/simple */ std::vector CConfigParser::Tokenize(const std::wstring& str, const std::wstring& delimiters) { std::vector tokens; - std::wstring::size_type lastPos = str.find_first_not_of(delimiters, 0); // skip delimiters at beginning. - std::wstring::size_type pos = str.find_first_of(delimiters, lastPos); // find first "non-delimiter". - - while (std::wstring::npos != pos || std::wstring::npos != lastPos) + size_t lastPos, pos = 0; + do { - tokens.push_back(str.substr(lastPos, pos - lastPos)); // found a token, add it to the vector. - lastPos = str.find_first_not_of(delimiters, pos); // skip delimiters. Note the "not_of" - pos = str.find_first_of(delimiters, lastPos); // find next "non-delimiter" + lastPos = str.find_first_not_of(delimiters, pos); + if (lastPos == std::wstring::npos) break; + + pos = str.find_first_of(delimiters, lastPos + 1); + std::wstring token = str.substr(lastPos, pos - lastPos); // len = (pos != std::wstring::npos) ? pos - lastPos : pos + + size_t pos2 = token.find_first_not_of(L" \t\r\n"); + if (pos2 != std::wstring::npos) + { + size_t lastPos2 = token.find_last_not_of(L" \t\r\n"); + if (pos2 != 0 || lastPos2 != (token.size() - 1)) + { + // Trim white-space + token.assign(token, pos2, lastPos2 - pos2 + 1); + } + tokens.push_back(token); + } + + if (pos == std::wstring::npos) break; + ++pos; } + while (true); return tokens; } -/* -** Trims empty element in vector and white-space in each string. -** -*/ -void CConfigParser::Shrink(std::vector& vec) -{ - if (!vec.empty()) - { - std::vector::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) - { - std::wstring::size_type lastPos = (*iter).find_last_not_of(L" \t\r\n"); - if (pos != 0 || lastPos != ((*iter).size() - 1)) - { - // Trim white-space - (*iter).assign((*iter), pos, lastPos - pos + 1); - } - ++iter; - } - else - { - // Remove empty element - vec.erase((++iter).base()); - } - } - } -} - /* ** Helper method that parses the floating-point value from the given string. ** If the given string is invalid format or causes overflow/underflow, returns given default value. diff --git a/Library/ConfigParser.h b/Library/ConfigParser.h index 2b851946..78978e5c 100644 --- a/Library/ConfigParser.h +++ b/Library/ConfigParser.h @@ -52,14 +52,11 @@ public: const std::unordered_map& GetVariables() { return m_Variables; } - void SetCurrentSection(const std::wstring& strSection) { m_CurrentSection->assign(strSection); } - void ClearCurrentSection() { m_CurrentSection->clear(); } - const std::wstring& GetValue(const std::wstring& strSection, const std::wstring& strKey, const std::wstring& strDefault); void SetValue(const std::wstring& strSection, const std::wstring& strKey, const std::wstring& strValue); void DeleteValue(const std::wstring& strSection, const std::wstring& strKey); - void SetStyleTemplate(const std::wstring& strStyle) { static const std::wstring delim(1, L'|'); Tokenize(strStyle, delim).swap(m_StyleTemplate); Shrink(m_StyleTemplate); } + void SetStyleTemplate(const std::wstring& strStyle) { static const std::wstring delim(1, L'|'); Tokenize(strStyle, delim).swap(m_StyleTemplate); } void ClearStyleTemplate() { m_StyleTemplate.clear(); } bool GetLastReplaced() { return m_LastReplaced; } @@ -89,7 +86,6 @@ public: bool ReplaceMeasures(std::wstring& result); static std::vector Tokenize(const std::wstring& str, const std::wstring& delimiters); - static void Shrink(std::vector& vec); static double ParseDouble(LPCTSTR string, double defValue); static int ParseInt(LPCTSTR string, int defValue); static uint32_t ParseUInt(LPCTSTR string, uint32_t defValue); diff --git a/Library/Group.cpp b/Library/Group.cpp index e2c81167..fbb0b6f1 100644 --- a/Library/Group.cpp +++ b/Library/Group.cpp @@ -30,15 +30,9 @@ void CGroup::InitializeGroup(const std::wstring& groups) if (!groups.empty()) { std::vector vGroups = CConfigParser::Tokenize(groups, L"|"); - - std::vector::const_iterator iter = vGroups.begin(); - for ( ; iter != vGroups.end(); ++iter) + for (auto iter = vGroups.begin(); iter != vGroups.end(); ++iter) { - std::wstring group = CreateGroup(*iter); - if (!group.empty()) - { - m_Groups.insert(group); - } + m_Groups.insert(CreateGroup(*iter)); } } } @@ -46,10 +40,16 @@ void CGroup::InitializeGroup(const std::wstring& groups) bool CGroup::BelongsToGroup(const std::wstring& group) const { - return (m_Groups.find(CreateGroup(group)) != m_Groups.end()); + return (m_Groups.find(VerifyGroup(group)) != m_Groups.end()); } -std::wstring CGroup::CreateGroup(const std::wstring& str) const +std::wstring& CGroup::CreateGroup(std::wstring& str) const +{ + _wcsupr(&str[0]); + return str; +} + +std::wstring CGroup::VerifyGroup(const std::wstring& str) const { std::wstring strTmp; @@ -59,7 +59,7 @@ std::wstring CGroup::CreateGroup(const std::wstring& str) const // Trim white-space strTmp.assign(str, pos, str.find_last_not_of(L" \t\r\n") - pos + 1); - _wcsupr(&strTmp[0]); + CreateGroup(strTmp); } return strTmp; diff --git a/Library/Group.h b/Library/Group.h index e956bc77..cd84ea3a 100644 --- a/Library/Group.h +++ b/Library/Group.h @@ -35,7 +35,8 @@ protected: void InitializeGroup(const std::wstring& groups); private: - std::wstring CreateGroup(const std::wstring& str) const; + std::wstring& CreateGroup(std::wstring& str) const; + std::wstring VerifyGroup(const std::wstring& str) const; std::unordered_set m_Groups; std::wstring m_OldGroups;