Tweaks and cosmetics

This commit is contained in:
Birunthan Mohanathas 2012-08-05 15:51:58 +03:00
parent 47781848e7
commit e0ec47f9a0
2 changed files with 58 additions and 124 deletions

View File

@ -192,63 +192,42 @@ bool CConfigParser::GetVariable(const std::wstring& strVariable, std::wstring& s
} }
/* /*
** Meter/Measure psuedo-variables. [MeasureName:] returns numeric value of measure. ** Gets the value of a section variable. Returns true if strValue is set.
** ie. [MeterName:X], [MeasureName:], [MeasureName:%, 2], [MeasureName:3] ** The selector is stripped from strVariable.
**
*/ */
bool CConfigParser::GetSectionVariables(const std::wstring& strVariable, std::wstring& strValue) bool CConfigParser::GetSectionVariable(std::wstring& strVariable, std::wstring& strValue)
{ {
WCHAR buffer[MAX_LINE_LENGTH]; size_t colonPos = strVariable.find_last_of(L':');
int len; if (colonPos == std::wstring::npos)
std::vector<std::wstring> strToken = Tokenize(strVariable, L":");
if (strToken.size() == 1 && !m_Measures.empty())
{ {
// [MeasureName:] returns number value return false;
CMeasure* measure = m_MeterWindow->GetMeasure(strToken[0]);
if (measure)
{
len = _snwprintf_s(buffer, _TRUNCATE, L"%lli", (LONGLONG)measure->GetValue());
strValue.assign(buffer, len);
return true;
}
} }
else if (strToken.size() == 2)
const std::wstring selector = strVariable.substr(colonPos + 1);
const WCHAR* selectorSz = selector.c_str();
strVariable.resize(colonPos);
if (!selector.empty() && iswalpha(selectorSz[0]))
{ {
auto stripSpaces = [](std::wstring& str) // [Meter:X], [Meter:Y], [Meter:W], [Meter:H]
{ CMeter* meter = m_MeterWindow->GetMeter(strVariable);
// Strip off leading and trailing spaces
size_t startPos = str.find_first_not_of(L" \t");
size_t endPos = str.find_last_not_of(L" \t");
if (std::wstring::npos != startPos || std::wstring::npos != endPos)
{
str = str.substr(startPos, endPos - startPos + 1);
}
};
std::wstring args = strToken[1];
stripSpaces(args);
// Check meters first
// Format: [MeterName:X]
CMeter* meter = m_MeterWindow->GetMeter(strToken[0]);
if (meter) if (meter)
{ {
const WCHAR* arg = args.c_str(); WCHAR buffer[32];
if (_wcsicmp(selectorSz, L"X") == 0)
if (_wcsicmp(arg, L"X") == 0)
{ {
_itow_s(meter->GetX(), buffer, 10); _itow_s(meter->GetX(), buffer, 10);
} }
else if (_wcsicmp(arg, L"Y") == 0) else if (_wcsicmp(selectorSz, L"Y") == 0)
{ {
_itow_s(meter->GetY(), buffer, 10); _itow_s(meter->GetY(), buffer, 10);
} }
else if (_wcsicmp(arg, L"W") == 0) else if (_wcsicmp(selectorSz, L"W") == 0)
{ {
_itow_s(meter->GetW(), buffer, 10); _itow_s(meter->GetW(), buffer, 10);
} }
else if (_wcsicmp(arg, L"H") == 0) else if (_wcsicmp(selectorSz, L"H") == 0)
{ {
_itow_s(meter->GetH(), buffer, 10); _itow_s(meter->GetH(), buffer, 10);
} }
@ -260,110 +239,65 @@ bool CConfigParser::GetSectionVariables(const std::wstring& strVariable, std::ws
strValue = buffer; strValue = buffer;
return true; return true;
} }
}
// Check measures else
// Format: [MeasureName:/1000,2] [MeasureName:%,2] [MeasureName:10] {
CMeasure* measure = m_MeterWindow->GetMeasure(strToken[0]); // Number: [Measure:], [Measure:dec]
// Percentual: [Measure:%], [Measure:%, dec]
// Scale: [Measure:/scale], [Measure:/scale, dec]
CMeasure* measure = m_MeterWindow->GetMeasure(strVariable);
if (measure) if (measure)
{ {
double scale = 1.0; double scale = 1.0;
int numOfDecimals = -1;
bool foundDecimal = false;
bool percentual = false; bool percentual = false;
WCHAR format[32];
std::vector<std::wstring> measureOptions = Tokenize(args, L","); const WCHAR* decimalsSz = wcschr(selectorSz, L',');
std::wstring tempStr; if (decimalsSz)
{
++decimalsSz;
}
if (wcscmp(measureOptions[0].c_str(), L"%") == 0) // Percentual if (*selectorSz == L'%') // Percentual
{ {
percentual = true; percentual = true;
} }
else if (!measureOptions[0].empty() && measureOptions[0][0] == L'/') // Scale else if (*selectorSz == L'/') // Scale
{ {
std::wstring tempScale = measureOptions[0].substr(1, measureOptions[0].length() - 1); // RVO
stripSpaces(tempScale);
errno = 0; errno = 0;
scale = _wtoi(tempScale.c_str()); scale = _wtoi(selectorSz);
if (errno == EINVAL) if (errno == EINVAL)
{ {
scale = 1.0; scale = 1.0;
} }
} }
else // NumOfDecimals
{
tempStr = measureOptions[0];
stripSpaces(tempStr);
errno = 0;
numOfDecimals = _wtoi(tempStr.c_str());
if (errno == EINVAL || numOfDecimals < -1)
{
numOfDecimals = -1;
}
foundDecimal = true;
}
// NumOfDecimals (for Percentual and Scale)
if (measureOptions.size() == 2 && !foundDecimal)
{
tempStr = measureOptions[1];
stripSpaces(tempStr);
errno = 0;
numOfDecimals = _wtoi(tempStr.c_str());
// _wtoi returns 0 if there is an error.
if (errno == EINVAL || numOfDecimals < -1)
{
numOfDecimals = -1;
}
}
len = 0;
if (percentual)
{
double val = 100.0 * measure->GetRelativeValue();
if (numOfDecimals <= 0)
{
_itow_s((int)val, buffer, 10);
len = (int)wcslen(buffer);
}
else
{
_snwprintf_s(format, _TRUNCATE, L"%%.%if", numOfDecimals);
len = _snwprintf_s(buffer, _TRUNCATE, format, val);
}
}
else else
{ {
double val = measure->GetValue() / scale; if (decimalsSz)
if (numOfDecimals == 0)
{ {
val += (val >= 0) ? 0.5 : -0.5; return false;
len = _snwprintf_s(buffer, _TRUNCATE, L"%lli", (LONGLONG)val);
}
else if (numOfDecimals < 0)
{
len = _snwprintf_s(buffer, _TRUNCATE, L"%.15f", val);
measure->RemoveTrailingZero(buffer, len);
len = (int)wcslen(buffer);
} }
else else
{ {
_snwprintf_s(format, _TRUNCATE, L"%%.%if", numOfDecimals); decimalsSz = selectorSz;
len = _snwprintf_s(buffer, _TRUNCATE, format, val);
} }
} }
strValue.assign(buffer, len); int decimals = decimalsSz && *decimalsSz ? _wtoi(decimalsSz) : 15;
double value = percentual ? measure->GetRelativeValue() * 100.0 : measure->GetValue() / scale;
WCHAR format[32];
WCHAR buffer[128];
_snwprintf_s(format, _TRUNCATE, L"%%.%if", decimals);
int bufferLen = _snwprintf_s(buffer, _TRUNCATE, format, value);
if (!decimalsSz)
{
// Remove trailing zeros if decimal count was not specified.
measure->RemoveTrailingZero(buffer, bufferLen);
bufferLen = (int)wcslen(buffer);
}
strValue.assign(buffer, bufferLen);
return true; return true;
} }
} }
@ -719,9 +653,9 @@ bool CConfigParser::ReplaceMeasures(std::wstring& result)
else else
{ {
std::wstring value; std::wstring value;
if (GetSectionVariables(var, value)) if (GetSectionVariable(var, value))
{ {
// Measure found, replace it with the value // Replace section variable with the value.
result.replace(start, end - start + 1, value); result.replace(start, end - start + 1, value);
start += value.length(); start += value.length();
replaced = true; replaced = true;

View File

@ -110,7 +110,7 @@ private:
void SetAutoSelectedMonitorVariables(CMeterWindow* meterWindow); void SetAutoSelectedMonitorVariables(CMeterWindow* meterWindow);
bool GetSectionVariables(const std::wstring& strVariable, std::wstring& strValue); bool GetSectionVariable(std::wstring& strVariable, std::wstring& strValue);
static void SetVariable(std::unordered_map<std::wstring, std::wstring>& variables, const std::wstring& strVariable, const std::wstring& strValue); static void SetVariable(std::unordered_map<std::wstring, std::wstring>& variables, const std::wstring& strVariable, const std::wstring& strValue);
static void SetVariable(std::unordered_map<std::wstring, std::wstring>& variables, const WCHAR* strVariable, const WCHAR* strValue); static void SetVariable(std::unordered_map<std::wstring, std::wstring>& variables, const WCHAR* strVariable, const WCHAR* strValue);