Skip to content

Commit

Permalink
Tweaks and cosmetics
Browse files Browse the repository at this point in the history
Added "Dispose" method
  • Loading branch information
brianferguson committed Jan 31, 2022
1 parent 5bb02c1 commit 2da4a63
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
50 changes: 26 additions & 24 deletions Library/MeasureRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ MeasureRegistry::MeasureRegistry(Skin* skin, const WCHAR* name) : Measure(skin,

MeasureRegistry::~MeasureRegistry()
{
if (m_RegKey) RegCloseKey(m_RegKey);
Dispose();
}

void MeasureRegistry::Dispose()
{
if (m_RegKey)
{
RegCloseKey(m_RegKey);
}
}

/*
Expand All @@ -37,6 +45,9 @@ void MeasureRegistry::UpdateValue()
{
if (m_RegKey != nullptr)
{
m_Value = 0.0;
m_StringValue.clear();

if (m_OutputType != OutputType::Value)
{
auto getList = [&](const DWORD objNum, const int objMaxSize, auto* func) -> void
Expand All @@ -50,7 +61,7 @@ void MeasureRegistry::UpdateValue()
if (func(m_RegKey, i, objName, &objSize, nullptr, nullptr, nullptr, nullptr) == ERROR_SUCCESS)
{
m_StringValue += objName;
if (i < objNum - 1UL)
if (i < (objNum - 1UL))
{
m_StringValue += m_OutputDelimiter;
}
Expand All @@ -59,9 +70,6 @@ void MeasureRegistry::UpdateValue()
delete [] objName;
};

m_Value = 0.0;
m_StringValue.clear();

DWORD numSubKeys = 0UL;
DWORD numValues = 0UL;
if (ERROR_SUCCESS == RegQueryInfoKey(m_RegKey, nullptr, nullptr, nullptr, &numSubKeys,
Expand Down Expand Up @@ -101,7 +109,6 @@ void MeasureRegistry::UpdateValue()
{
case REG_DWORD:
m_Value = *((LPDWORD)data);
m_StringValue.clear();
break;

case REG_SZ:
Expand All @@ -113,7 +120,6 @@ void MeasureRegistry::UpdateValue()
case REG_MULTI_SZ:
{
m_Value = wcstod(data, nullptr);
m_StringValue.clear();

// |REG_MULTI_SZ| returns a sequence of null terminated strings, so convert the null
// separators from the BYTE array (returned from RegQueryValueEx) into a newline
Expand All @@ -122,48 +128,44 @@ void MeasureRegistry::UpdateValue()

for (ULONG pos = 0UL; pos < (dwSize - 1UL); ++pos)
{
if (data[pos]) m_StringValue[pos] = data[pos];
else m_StringValue[pos] = L'\n'; // Substitute newline for null
if (data[pos])
{
m_StringValue[pos] = data[pos];
}
else
{
m_StringValue[pos] = L'\n'; // Substitute newline for null
}
}
}
break;

case REG_QWORD:
m_Value = (double)((LARGE_INTEGER*)data)->QuadPart;
m_StringValue.clear();
break;

case REG_BINARY:
m_Value = 0.0;
m_StringValue.clear();

for (DWORD i = 0; i < size; ++i)
for (DWORD i = 0UL; i < size; ++i)
{
WCHAR buffer[3];
_snwprintf_s(buffer, 3, L"%02X", ((LPBYTE)data)[i]);
m_StringValue.append(buffer);
}

break;

default: // Other types are not supported
m_Value = 0.0;
m_StringValue.clear();
}
}
else
{
m_Value = 0.0;
m_StringValue.clear();
RegOpenKeyEx(m_HKey, m_RegKeyName.c_str(), 0, KEY_READ, &m_RegKey);
RegOpenKeyEx(m_HKey, m_RegKeyName.c_str(), 0UL, KEY_READ, &m_RegKey);
}

delete [] data;
}
}
else
{
RegOpenKeyEx(m_HKey, m_RegKeyName.c_str(), 0, KEY_READ, &m_RegKey);
RegOpenKeyEx(m_HKey, m_RegKeyName.c_str(), 0UL, KEY_READ, &m_RegKey);
}
}

Expand Down Expand Up @@ -235,8 +237,8 @@ void MeasureRegistry::ReadOptions(ConfigParser& parser, const WCHAR* section)
}

// Try to open the key
if (m_RegKey) RegCloseKey(m_RegKey);
RegOpenKeyEx(m_HKey, m_RegKeyName.c_str(), 0, KEY_READ, &m_RegKey);
Dispose();
RegOpenKeyEx(m_HKey, m_RegKeyName.c_str(), 0UL, KEY_READ, &m_RegKey);
}

/*
Expand Down
2 changes: 2 additions & 0 deletions Library/MeasureRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class MeasureRegistry : public Measure
ValueList
};

void Dispose();

OutputType m_OutputType;
std::wstring m_OutputDelimiter;
std::wstring m_RegKeyName;
Expand Down

0 comments on commit 2da4a63

Please sign in to comment.