rainmeter-studio/Library/MeasureCalc.cpp

183 lines
4.3 KiB
C++
Raw Normal View History

2009-02-10 18:37:48 +00:00
/*
Copyright (C) 2004 Kimmo Pekkola
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2009-02-10 18:37:48 +00:00
*/
#include "StdAfx.h"
2009-02-10 18:37:48 +00:00
#include "MeasureCalc.h"
#include "Rainmeter.h"
#include "MathParser.h"
2010-09-21 22:47:53 +00:00
bool CMeasureCalc::c_RandSeeded = false;
2009-02-10 18:37:48 +00:00
/*
** The constructor
**
*/
2011-02-15 16:26:54 +00:00
CMeasureCalc::CMeasureCalc(CMeterWindow* meterWindow, const WCHAR* name) : CMeasure(meterWindow, name),
m_ParseError(false),
m_LowBound(),
m_HighBound(100),
m_UpdateRandom(false)
2009-02-10 18:37:48 +00:00
{
2011-03-29 19:21:57 +00:00
if (!c_RandSeeded)
{
c_RandSeeded = true;
2011-03-29 19:21:57 +00:00
srand((unsigned)time(0));
}
rand();
2009-02-10 18:37:48 +00:00
}
/*
** The destructor
**
*/
CMeasureCalc::~CMeasureCalc()
{
}
/*
** Updates the calculation
**
*/
void CMeasureCalc::UpdateValue()
2009-02-10 18:37:48 +00:00
{
const WCHAR* errMsg = MathParser::Parse(m_Formula.c_str(), this, &m_Value);
if (errMsg != NULL)
2009-02-10 18:37:48 +00:00
{
if (!m_ParseError)
{
2013-05-30 14:19:42 +00:00
LogErrorF(L"Calc: %s in [%s]", errMsg, m_Name.c_str());
m_ParseError = true;
}
2009-02-10 18:37:48 +00:00
}
else
{
m_ParseError = false;
}
2009-02-10 18:37:48 +00:00
}
/*
** Read the options specified in the ini file.
2009-02-10 18:37:48 +00:00
**
*/
2012-05-30 18:53:44 +00:00
void CMeasureCalc::ReadOptions(CConfigParser& parser, const WCHAR* section)
2009-02-10 18:37:48 +00:00
{
2012-05-30 18:53:44 +00:00
CMeasure::ReadOptions(parser, section);
2009-02-10 18:37:48 +00:00
// Store the current values so we know if the value needs to be updated
int oldLowBound = m_LowBound;
int oldHighBound = m_HighBound;
bool oldUpdateRandom = m_UpdateRandom;
2012-01-23 07:21:03 +00:00
std::wstring oldFormula = m_Formula;
2009-02-10 18:37:48 +00:00
m_Formula = parser.ReadString(section, L"Formula", L"");
2010-03-30 22:37:05 +00:00
m_LowBound = parser.ReadInt(section, L"LowBound", 0);
m_HighBound = parser.ReadInt(section, L"HighBound", 100);
m_UpdateRandom = 0!=parser.ReadInt(section, L"UpdateRandom", 0);
if (!m_Initialized ||
2012-01-23 07:21:03 +00:00
wcscmp(m_Formula.c_str(), oldFormula.c_str()) != 0 ||
oldLowBound != m_LowBound ||
oldHighBound != m_HighBound ||
oldUpdateRandom != m_UpdateRandom)
{
2012-01-23 09:30:51 +00:00
if (!m_UpdateRandom)
{
FormulaReplace();
}
2012-02-20 14:49:38 +00:00
const WCHAR* errMsg = MathParser::Check(m_Formula.c_str());
if (errMsg != NULL)
{
2013-05-30 14:19:42 +00:00
LogErrorF(L"Calc: %s in [%s]", errMsg, m_Name.c_str());
m_Formula.clear();
}
}
}
2012-01-23 09:35:32 +00:00
2012-01-23 09:30:51 +00:00
/*
** This replaces the word Random in the formula with a random number
**
*/
void CMeasureCalc::FormulaReplace()
{
size_t start = 0, pos;
do
{
pos = m_Formula.find_first_of(L"Rr", start);
if (pos != std::wstring::npos)
{
if (_wcsnicmp(L"random", m_Formula.c_str() + pos, 6) == 0 &&
(pos == 0 || MathParser::IsDelimiter((*(m_Formula.c_str() + pos - 1))) &&
(pos == (m_Formula.length() - 6) || MathParser::IsDelimiter((*(m_Formula.c_str() + pos + 6))))))
{
int randNumber = GetRandom();
WCHAR buffer[32];
_itow_s(randNumber, buffer, 10);
size_t len = wcslen(buffer);
m_Formula.replace(pos, 6, buffer, len);
start = pos + len;
}
else
{
start = pos + 1;
}
}
}
while (pos != std::wstring::npos);
}
2012-01-23 09:35:32 +00:00
2012-01-23 09:30:51 +00:00
bool CMeasureCalc::GetMeasureValue(const WCHAR* str, int len, double* value)
{
const std::vector<CMeasure*>& measures = m_MeterWindow->GetMeasures();
std::vector<CMeasure*>::const_iterator iter = measures.begin();
2012-01-23 07:21:03 +00:00
for ( ; iter != measures.end(); ++iter)
{
2012-01-23 09:35:32 +00:00
if ((*iter)->GetOriginalName().length() == len &&
_wcsnicmp(str, (*iter)->GetName(), len) == 0)
{
2012-01-23 07:21:03 +00:00
*value = (*iter)->GetValue();
2012-01-23 09:30:51 +00:00
return true;
}
}
2012-01-23 09:30:51 +00:00
if (_wcsnicmp(str, L"counter", len) == 0)
{
2012-01-23 07:21:03 +00:00
*value = m_MeterWindow->GetUpdateCounter();
2012-01-23 09:30:51 +00:00
return true;
}
2012-01-23 09:30:51 +00:00
else if (_wcsnicmp(str, L"random", len) == 0)
2012-01-23 07:21:03 +00:00
{
2012-01-23 09:30:51 +00:00
*value = GetRandom();
return true;
2012-01-23 07:21:03 +00:00
}
2012-01-23 09:30:51 +00:00
return false;
2012-01-23 07:21:03 +00:00
}
2012-01-23 09:30:51 +00:00
int CMeasureCalc::GetRandom()
2012-01-23 07:21:03 +00:00
{
double range = (m_HighBound - m_LowBound) + 1;
2012-01-23 07:21:03 +00:00
srand((unsigned)rand());
2012-01-23 09:30:51 +00:00
return m_LowBound + (int)(range * rand() / (RAND_MAX + 1.0));
}