예제 #1
0
static int GetW(lua_State* L)
{
	CMeter* self = GetSelf(L);
	lua_pushnumber(L, self->GetW());

	return 1;
}
예제 #2
0
/*
** Gets the value of a section variable. Returns true if strValue is set.
** The selector is stripped from strVariable.
**
*/
bool CConfigParser::GetSectionVariable(std::wstring& strVariable, std::wstring& strValue)
{
    size_t colonPos = strVariable.find_last_of(L':');
    if (colonPos == std::wstring::npos)
    {
        return false;
    }

    const std::wstring selector = strVariable.substr(colonPos + 1);
    const WCHAR* selectorSz = selector.c_str();
    strVariable.resize(colonPos);

    bool isKeySelector = (!selector.empty() && iswalpha(selectorSz[0]));

    if (isKeySelector)
    {
        // [Meter:X], [Meter:Y], [Meter:W], [Meter:H]
        CMeter* meter = m_MeterWindow->GetMeter(strVariable);
        if (meter)
        {
            WCHAR buffer[32];
            if (_wcsicmp(selectorSz, L"X") == 0)
            {
                _itow_s(meter->GetX(), buffer, 10);
            }
            else if (_wcsicmp(selectorSz, L"Y") == 0)
            {
                _itow_s(meter->GetY(), buffer, 10);
            }
            else if (_wcsicmp(selectorSz, L"W") == 0)
            {
                _itow_s(meter->GetW(), buffer, 10);
            }
            else if (_wcsicmp(selectorSz, L"H") == 0)
            {
                _itow_s(meter->GetH(), buffer, 10);
            }
            else
            {
                return false;
            }

            strValue = buffer;
            return true;
        }
    }

    // Number: [Measure:], [Measure:dec]
    // Percentual: [Measure:%], [Measure:%, dec]
    // Scale: [Measure:/scale], [Measure:/scale, dec]
    // Max/Min: [Measure:MaxValue], [Measure:MaxValue:/scale, dec] ('%' cannot be used)
    enum VALUETYPE
    {
        RAW        = 0,
        PERCENTUAL = 1,
        MAX        = 2,
        MIN        = 3
    } valueType = RAW;

    if (isKeySelector)
    {
        if (_wcsicmp(selectorSz, L"MaxValue") == 0)
        {
            valueType = MAX;
        }
        else if (_wcsicmp(selectorSz, L"MinValue") == 0)
        {
            valueType = MIN;
        }
        else
        {
            return false;
        }

        selectorSz = L"";
    }
    else
    {
        colonPos = strVariable.find_last_of(L':');
        if (colonPos != std::wstring::npos)
        {
            do
            {
                const WCHAR* keySelectorSz = strVariable.c_str() + colonPos + 1;

                if (_wcsicmp(keySelectorSz, L"MaxValue") == 0)
                {
                    valueType = MAX;
                }
                else if (_wcsicmp(keySelectorSz, L"MinValue") == 0)
                {
                    valueType = MIN;
                }
                else
                {
                    // Section name contains ':' ?
                    break;
                }

                strVariable.resize(colonPos);
            }
            while (0);
        }
    }

    CMeasure* measure = m_MeterWindow->GetMeasure(strVariable);
    if (measure)
    {
        int scale = 1;

        const WCHAR* decimalsSz = wcschr(selectorSz, L',');
        if (decimalsSz)
        {
            ++decimalsSz;
        }

        if (*selectorSz == L'%')  // Percentual
        {
            if (valueType == MAX || valueType == MIN)  // '%' cannot be used with MAX/MIN value
            {
                return false;
            }

            valueType = PERCENTUAL;
        }
        else if (*selectorSz == L'/')  // Scale
        {
            errno = 0;
            scale = _wtoi(selectorSz + 1);
            if (errno == EINVAL || scale == 0)  // Invalid scale value
            {
                return false;
            }
        }
        else
        {
            if (decimalsSz)
            {
                return false;
            }

            decimalsSz = selectorSz;
        }

        double value = (valueType == PERCENTUAL) ? measure->GetRelativeValue() * 100.0
                       : (valueType == MAX) ? measure->GetMaxValue() / scale
                       : (valueType == MIN) ? measure->GetMinValue() / scale
                       : measure->GetValue() / scale;
        int decimals = 10;

        if (decimalsSz)
        {
            while (iswspace(*decimalsSz)) ++decimalsSz;

            if (*decimalsSz)
            {
                decimals = _wtoi(decimalsSz);
                decimals = max(0, decimals);
                decimals = min(32, decimals);
            }
            else
            {
                decimalsSz = NULL;
            }
        }

        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 false;
}