* Merged Shrink() code to Tokenize()
This commit is contained in:
spx 2013-02-06 19:09:17 +09:00
parent e96b02d3dc
commit 1c8b798928
4 changed files with 57 additions and 65 deletions

View File

@ -794,14 +794,14 @@ const std::wstring& CConfigParser::ReadString(LPCTSTR section, LPCTSTR key, LPCT
{ {
if (result.find(L'#') != std::wstring::npos) if (result.find(L'#') != std::wstring::npos)
{ {
SetCurrentSection(strSection); // Set temporarily m_CurrentSection->assign(strSection); // Set temporarily
if (ReplaceVariables(result)) if (ReplaceVariables(result))
{ {
m_LastReplaced = true; m_LastReplaced = true;
} }
ClearCurrentSection(); // Reset m_CurrentSection->clear(); // Reset
} }
else else
{ {
@ -852,16 +852,25 @@ CMeasure* CConfigParser::GetMeasure(const std::wstring& name)
std::vector<Gdiplus::REAL> CConfigParser::ReadFloats(LPCTSTR section, LPCTSTR key) std::vector<Gdiplus::REAL> CConfigParser::ReadFloats(LPCTSTR section, LPCTSTR key)
{ {
std::vector<Gdiplus::REAL> result; std::vector<Gdiplus::REAL> result;
const std::wstring& string = ReadString(section, key, L""); const std::wstring& str = ReadString(section, key, L"");
if (!string.empty()) if (!str.empty())
{ {
// Tokenize and parse the floats // Tokenize and parse the floats
std::vector<std::wstring> tokens = Tokenize(string, L";"); const WCHAR delimiter = L';';
std::vector<std::wstring>::const_iterator iter = tokens.begin(); size_t lastPos, pos = 0;
for ( ; iter != tokens.end(); ++iter) 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; 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<std::wstring> CConfigParser::Tokenize(const std::wstring& str, const std::wstring& delimiters) std::vector<std::wstring> CConfigParser::Tokenize(const std::wstring& str, const std::wstring& delimiters)
{ {
std::vector<std::wstring> tokens; std::vector<std::wstring> tokens;
std::wstring::size_type lastPos = str.find_first_not_of(delimiters, 0); // skip delimiters at beginning. size_t lastPos, pos = 0;
std::wstring::size_type pos = str.find_first_of(delimiters, lastPos); // find first "non-delimiter". do
while (std::wstring::npos != pos || std::wstring::npos != lastPos)
{ {
tokens.push_back(str.substr(lastPos, pos - lastPos)); // found a token, add it to the vector. lastPos = str.find_first_not_of(delimiters, pos);
lastPos = str.find_first_not_of(delimiters, pos); // skip delimiters. Note the "not_of" if (lastPos == std::wstring::npos) break;
pos = str.find_first_of(delimiters, lastPos); // find next "non-delimiter"
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; return tokens;
} }
/*
** Trims empty element in vector and white-space in each string.
**
*/
void CConfigParser::Shrink(std::vector<std::wstring>& vec)
{
if (!vec.empty())
{
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)
{
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. ** 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. ** If the given string is invalid format or causes overflow/underflow, returns given default value.

View File

@ -52,14 +52,11 @@ public:
const std::unordered_map<std::wstring, std::wstring>& GetVariables() { return m_Variables; } const std::unordered_map<std::wstring, std::wstring>& 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); 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 SetValue(const std::wstring& strSection, const std::wstring& strKey, const std::wstring& strValue);
void DeleteValue(const std::wstring& strSection, const std::wstring& strKey); 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(); } void ClearStyleTemplate() { m_StyleTemplate.clear(); }
bool GetLastReplaced() { return m_LastReplaced; } bool GetLastReplaced() { return m_LastReplaced; }
@ -89,7 +86,6 @@ public:
bool ReplaceMeasures(std::wstring& result); bool ReplaceMeasures(std::wstring& result);
static std::vector<std::wstring> Tokenize(const std::wstring& str, const std::wstring& delimiters); static std::vector<std::wstring> Tokenize(const std::wstring& str, const std::wstring& delimiters);
static void Shrink(std::vector<std::wstring>& vec);
static double ParseDouble(LPCTSTR string, double defValue); static double ParseDouble(LPCTSTR string, double defValue);
static int ParseInt(LPCTSTR string, int defValue); static int ParseInt(LPCTSTR string, int defValue);
static uint32_t ParseUInt(LPCTSTR string, uint32_t defValue); static uint32_t ParseUInt(LPCTSTR string, uint32_t defValue);

View File

@ -30,15 +30,9 @@ void CGroup::InitializeGroup(const std::wstring& groups)
if (!groups.empty()) if (!groups.empty())
{ {
std::vector<std::wstring> vGroups = CConfigParser::Tokenize(groups, L"|"); std::vector<std::wstring> vGroups = CConfigParser::Tokenize(groups, L"|");
for (auto iter = vGroups.begin(); iter != vGroups.end(); ++iter)
std::vector<std::wstring>::const_iterator iter = vGroups.begin();
for ( ; iter != vGroups.end(); ++iter)
{ {
std::wstring group = CreateGroup(*iter); m_Groups.insert(CreateGroup(*iter));
if (!group.empty())
{
m_Groups.insert(group);
}
} }
} }
} }
@ -46,10 +40,16 @@ void CGroup::InitializeGroup(const std::wstring& groups)
bool CGroup::BelongsToGroup(const std::wstring& group) const 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; std::wstring strTmp;
@ -59,7 +59,7 @@ std::wstring CGroup::CreateGroup(const std::wstring& str) const
// Trim white-space // Trim white-space
strTmp.assign(str, pos, str.find_last_not_of(L" \t\r\n") - pos + 1); strTmp.assign(str, pos, str.find_last_not_of(L" \t\r\n") - pos + 1);
_wcsupr(&strTmp[0]); CreateGroup(strTmp);
} }
return strTmp; return strTmp;

View File

@ -35,7 +35,8 @@ protected:
void InitializeGroup(const std::wstring& groups); void InitializeGroup(const std::wstring& groups);
private: 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<std::wstring> m_Groups; std::unordered_set<std::wstring> m_Groups;
std::wstring m_OldGroups; std::wstring m_OldGroups;