Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…4&p=59935

- Removed unneeded NULL checks.
- Other code tweaks and cleanups.
  • Loading branch information
spx268 committed Nov 8, 2011
1 parent 10f7504 commit a209bf1
Show file tree
Hide file tree
Showing 17 changed files with 239 additions and 255 deletions.
44 changes: 24 additions & 20 deletions Library/ConfigParser.cpp
Expand Up @@ -64,13 +64,11 @@ void CConfigParser::Initialize(LPCTSTR filename, CRainmeter* pRainmeter, CMeterW
{
m_Filename = filename;

m_BuiltInVariables.clear();
m_Variables.clear();
m_Measures.clear();
m_Values.clear();
m_Sections.clear();
m_FoundSections.clear();
m_ListVariables.clear();
m_Values.clear();
m_BuiltInVariables.clear();
m_Variables.clear();

m_StyleTemplate.clear();
m_LastUsedStyle.clear();
Expand All @@ -89,6 +87,7 @@ void CConfigParser::Initialize(LPCTSTR filename, CRainmeter* pRainmeter, CMeterW
ReadVariables();

// Clear and minimize
std::unordered_set<std::wstring>().swap(m_FoundSections);
std::vector<std::wstring>().swap(m_ListVariables);
}

Expand Down Expand Up @@ -126,9 +125,10 @@ void CConfigParser::SetBuiltInVariables(CRainmeter* pRainmeter, CMeterWindow* me
*/
void CConfigParser::ReadVariables()
{
for (size_t i = 0, isize = m_ListVariables.size(); i < isize; ++i)
std::vector<std::wstring>::const_iterator iter = m_ListVariables.begin();
for ( ; iter != m_ListVariables.end(); ++iter)
{
SetVariable(m_ListVariables[i], ReadString(L"Variables", m_ListVariables[i].c_str(), L"", false));
SetVariable((*iter), ReadString(L"Variables", (*iter).c_str(), L"", false));
}
}

Expand Down Expand Up @@ -495,7 +495,8 @@ bool CConfigParser::ReplaceVariables(std::wstring& result)
{
loop = false;
}
} while (loop);
}
while (loop);

return replaced;
}
Expand Down Expand Up @@ -568,7 +569,8 @@ bool CConfigParser::ReplaceMeasures(std::wstring& result)
{
loop = false;
}
} while (loop);
}
while (loop);
}

return replaced;
Expand Down Expand Up @@ -703,9 +705,10 @@ std::vector<Gdiplus::REAL> CConfigParser::ReadFloats(LPCTSTR section, LPCTSTR ke

// Tokenize and parse the floats
std::vector<std::wstring> tokens = Tokenize(tmp, L";");
for (size_t i = 0, isize = tokens.size(); i < isize; ++i)
std::vector<std::wstring>::const_iterator iter = tokens.begin();
for ( ; iter != tokens.end(); ++iter)
{
result.push_back((Gdiplus::REAL)ParseDouble(tokens[i], 0));
result.push_back((Gdiplus::REAL)ParseDouble((*iter), 0));
}
}
return result;
Expand Down Expand Up @@ -1078,7 +1081,7 @@ void CConfigParser::ReadIniFile(const std::vector<std::wstring>& iniFileMappings
if (config == NULL)
{
// Get all the sections
while (true)
do
{
items[0] = 0;
DWORD res = GetPrivateProfileSectionNames(items, itemsSize, iniRead.c_str());
Expand All @@ -1098,6 +1101,7 @@ void CConfigParser::ReadIniFile(const std::vector<std::wstring>& iniFileMappings
itemsSize *= 2;
items = new WCHAR[itemsSize];
}
while (true);

// Read the sections
pos = items;
Expand Down Expand Up @@ -1143,7 +1147,7 @@ void CConfigParser::ReadIniFile(const std::vector<std::wstring>& iniFileMappings
bool isMetadata = (config == NULL && _wcsicmp((*iter).c_str(), L"Metadata") == 0);

// Read all "key=value" from the section
while (true)
do
{
items[0] = 0;
DWORD res = GetPrivateProfileSection((*iter).c_str(), items, itemsSize, iniRead.c_str());
Expand All @@ -1157,6 +1161,7 @@ void CConfigParser::ReadIniFile(const std::vector<std::wstring>& iniFileMappings
itemsSize *= 2;
items = new WCHAR[itemsSize];
}
while (true);

pos = items;
while (pos < epos)
Expand All @@ -1170,8 +1175,7 @@ void CConfigParser::ReadIniFile(const std::vector<std::wstring>& iniFileMappings
std::wstring value = key.substr(sep + 1, len - sep);
key.erase(sep);

std::wstring lowerKey = StrToLower(key);
if (foundKeys.insert(lowerKey).second)
if (foundKeys.insert(StrToLowerC(key)).second)
{
// Trim surrounded quotes from value
std::wstring::size_type valueLen = value.length();
Expand All @@ -1183,7 +1187,7 @@ void CConfigParser::ReadIniFile(const std::vector<std::wstring>& iniFileMappings
value.assign(value, 1, valueLen);
}

if (wcsncmp(lowerKey.c_str(), L"@include", 8) == 0)
if (wcsncmp(key.c_str(), L"@include", 8) == 0)
{
ReadVariables();
ReplaceVariables(value);
Expand All @@ -1203,7 +1207,7 @@ void CConfigParser::ReadIniFile(const std::vector<std::wstring>& iniFileMappings

if (isVariables)
{
m_ListVariables.push_back(lowerKey);
m_ListVariables.push_back(key);
}
}
}
Expand Down Expand Up @@ -1235,7 +1239,7 @@ void CConfigParser::SetValue(const std::wstring& strSection, const std::wstring&
std::wstring strTmp = strSection + L"::";
strTmp += strKey;

m_Values[StrToLower(strTmp)] = strValue;
m_Values[StrToLowerC(strTmp)] = strValue;
}

//==============================================================================
Expand All @@ -1251,7 +1255,7 @@ void CConfigParser::DeleteValue(const std::wstring& strSection, const std::wstri
std::wstring strTmp = strSection + L"::";
strTmp += strKey;

std::unordered_map<std::wstring, std::wstring>::iterator iter = m_Values.find(StrToLower(strTmp));
std::unordered_map<std::wstring, std::wstring>::iterator iter = m_Values.find(StrToLowerC(strTmp));
if (iter != m_Values.end())
{
m_Values.erase(iter);
Expand All @@ -1272,7 +1276,7 @@ const std::wstring& CConfigParser::GetValue(const std::wstring& strSection, cons
std::wstring strTmp = strSection + L"::";
strTmp += strKey;

std::unordered_map<std::wstring, std::wstring>::const_iterator iter = m_Values.find(StrToLower(strTmp));
std::unordered_map<std::wstring, std::wstring>::const_iterator iter = m_Values.find(StrToLowerC(strTmp));
if (iter != m_Values.end())
{
return (*iter).second;
Expand Down
8 changes: 5 additions & 3 deletions Library/ConfigParser.h
Expand Up @@ -109,8 +109,9 @@ class CConfigParser
static void SetMultiMonitorVariables(bool reset);
static void SetMonitorVariable(const std::wstring& strVariable, const std::wstring& strValue) { SetVariable(c_MonitorVariables, strVariable, strValue); }

static std::wstring StrToLower(const std::wstring& str) { std::wstring strTmp(str); std::transform(strTmp.begin(), strTmp.end(), strTmp.begin(), ::towlower); return strTmp; }
static std::wstring StrToLower(const WCHAR* str) { std::wstring strTmp(str); std::transform(strTmp.begin(), strTmp.end(), strTmp.begin(), ::towlower); return strTmp; }
static std::wstring StrToLower(const std::wstring& str) { std::wstring strTmp(str); return StrToLowerC(strTmp); }
static std::wstring StrToLower(const WCHAR* str) { std::wstring strTmp(str); return StrToLowerC(strTmp); }
static std::wstring& StrToLowerC(std::wstring& str) { std::transform(str.begin(), str.end(), str.begin(), ::towlower); return str; }

std::wstring m_Filename;

Expand All @@ -125,9 +126,10 @@ class CConfigParser
bool m_LastValueDefined;

std::vector<std::wstring> m_Sections; // The sections must be an ordered array
std::unordered_map<std::wstring, std::wstring> m_Values;

std::unordered_set<std::wstring> m_FoundSections;
std::vector<std::wstring> m_ListVariables;
std::unordered_map<std::wstring, std::wstring> m_Values;

std::unordered_map<std::wstring, std::wstring> m_BuiltInVariables; // Built-in variables
std::unordered_map<std::wstring, std::wstring> m_Variables; // User-defined variables
Expand Down
15 changes: 7 additions & 8 deletions Library/Measure.cpp
Expand Up @@ -207,7 +207,8 @@ bool CMeasure::MakePlainSubstitute(std::wstring& str, size_t index)
str.replace(pos, m_Substitute[index].first.length(), m_Substitute[index].second);
start = pos + m_Substitute[index].second.length();
}
} while (pos != std::wstring::npos);
}
while (pos != std::wstring::npos);

return true;
}
Expand Down Expand Up @@ -313,7 +314,8 @@ const WCHAR* CMeasure::CheckSubstitute(const WCHAR* buffer)
utf8str.replace(start, length, result);
offset = start + result.length();
}
} while (true);
}
while (true);

// Release memory used for the compiled pattern
pcre_free(re);
Expand Down Expand Up @@ -343,7 +345,7 @@ bool CMeasure::ParseSubstitute(std::wstring buffer)
{
if (buffer.empty()) return true;

while (!buffer.empty())
do
{
std::wstring word1 = ExtractWord(buffer);
std::wstring sep = ExtractWord(buffer);
Expand All @@ -358,6 +360,7 @@ bool CMeasure::ParseSubstitute(std::wstring buffer)
sep = ExtractWord(buffer);
if (!sep.empty() && sep != L",") return false;
}
while (!buffer.empty());

return true;
}
Expand Down Expand Up @@ -772,11 +775,7 @@ CMeasure* CMeasure::Create(const WCHAR* measure, CMeterWindow* meterWindow, cons
{
// Comparison is caseinsensitive

if (*measure == L'\0')
{
return NULL;
}
else if (_wcsicmp(L"CPU", measure) == 0)
if (_wcsicmp(L"CPU", measure) == 0)
{
return new CMeasureCPU(meterWindow, name);
}
Expand Down
3 changes: 2 additions & 1 deletion Library/MeasureCPU.cpp
Expand Up @@ -185,7 +185,8 @@ bool CMeasureCPU::Update()
buf = new BYTE[bufSize];
}
++loop;
} while (loop < 5);
}
while (loop < 5);

if (status != STATUS_SUCCESS) // failed
{
Expand Down
7 changes: 2 additions & 5 deletions Library/MeasureScript.cpp
Expand Up @@ -54,11 +54,8 @@ CMeasureScript::~CMeasureScript()

void CMeasureScript::DeleteLuaScript()
{
if (m_LuaScript)
{
delete m_LuaScript;
m_LuaScript = NULL;
}
delete m_LuaScript;
m_LuaScript = NULL;

m_HasInitializeFunction = false;
m_HasUpdateFunction = false;
Expand Down

0 comments on commit a209bf1

Please sign in to comment.