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
|
2012-01-23 06:36:15 +00:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2009-02-10 18:37:48 +00:00
|
|
|
*/
|
|
|
|
|
2009-10-07 16:45:14 +00:00
|
|
|
#include "StdAfx.h"
|
2009-02-10 18:37:48 +00:00
|
|
|
#include "MeasureCalc.h"
|
|
|
|
#include "Rainmeter.h"
|
2012-01-22 20:28:05 +00:00
|
|
|
#include "MathParser.h"
|
2010-09-21 22:47:53 +00:00
|
|
|
|
2010-02-28 14:19:35 +00:00
|
|
|
bool CMeasureCalc::c_RandSeeded = false;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
** CMeasureCalc
|
|
|
|
**
|
|
|
|
** The constructor
|
|
|
|
**
|
|
|
|
*/
|
2011-02-15 16:26:54 +00:00
|
|
|
CMeasureCalc::CMeasureCalc(CMeterWindow* meterWindow, const WCHAR* name) : CMeasure(meterWindow, name),
|
2012-01-23 07:21:03 +00:00
|
|
|
m_Random(),
|
2011-01-29 00:11:01 +00:00
|
|
|
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)
|
2010-02-28 14:19:35 +00:00
|
|
|
{
|
|
|
|
c_RandSeeded = true;
|
2011-03-29 19:21:57 +00:00
|
|
|
srand((unsigned)time(0));
|
2010-02-28 14:19:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rand();
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** ~CMeasureCalc
|
|
|
|
**
|
|
|
|
** The destructor
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
CMeasureCalc::~CMeasureCalc()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Update
|
|
|
|
**
|
|
|
|
** Updates the calculation
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
bool CMeasureCalc::Update()
|
|
|
|
{
|
|
|
|
if (!CMeasure::PreUpdate()) return false;
|
|
|
|
|
2012-01-23 07:21:03 +00:00
|
|
|
if (m_UpdateRandom) UpdateRandom();
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-23 07:21:03 +00:00
|
|
|
char* errMsg = MathParser::Parse(ConvertToAscii(m_Formula.c_str()).c_str(), this, &m_Value);
|
2009-02-10 18:37:48 +00:00
|
|
|
if (errMsg != NULL)
|
|
|
|
{
|
2011-08-29 21:57:36 +00:00
|
|
|
std::wstring error = L"Calc: ";
|
|
|
|
error += ConvertToWide(errMsg);
|
2011-11-09 09:27:06 +00:00
|
|
|
error += L" in [";
|
2011-08-29 21:57:36 +00:00
|
|
|
error += m_Name;
|
2011-12-04 22:18:40 +00:00
|
|
|
error += L']';
|
2011-08-29 21:57:36 +00:00
|
|
|
Log(LOG_ERROR, error.c_str());
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return PostUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** ReadConfig
|
|
|
|
**
|
|
|
|
** Reads the measure specific configs.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void CMeasureCalc::ReadConfig(CConfigParser& parser, const WCHAR* section)
|
|
|
|
{
|
|
|
|
CMeasure::ReadConfig(parser, section);
|
|
|
|
|
2010-08-11 18:08:41 +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"");
|
2009-08-31 17:47:09 +00:00
|
|
|
|
2010-03-30 22:37:05 +00:00
|
|
|
m_LowBound = parser.ReadInt(section, L"LowBound", 0);
|
|
|
|
m_HighBound = parser.ReadInt(section, L"HighBound", 100);
|
2010-08-11 18:08:41 +00:00
|
|
|
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 ||
|
2010-08-11 18:08:41 +00:00
|
|
|
oldLowBound != m_LowBound ||
|
|
|
|
oldHighBound != m_HighBound ||
|
|
|
|
oldUpdateRandom != m_UpdateRandom)
|
|
|
|
{
|
2012-01-23 07:21:03 +00:00
|
|
|
if (!m_UpdateRandom) UpdateRandom();
|
2012-01-22 20:28:05 +00:00
|
|
|
|
|
|
|
char* errMsg = MathParser::Check(ConvertToAscii(m_Formula.c_str()).c_str());
|
|
|
|
if (errMsg != NULL)
|
|
|
|
{
|
|
|
|
std::wstring error = L"Calc: ";
|
|
|
|
error += ConvertToWide(errMsg);
|
|
|
|
error += L" in [";
|
|
|
|
error += m_Name;
|
|
|
|
error += L']';
|
|
|
|
throw CError(error);
|
|
|
|
}
|
2010-08-11 18:08:41 +00:00
|
|
|
}
|
2009-09-03 00:33:46 +00:00
|
|
|
}
|
2009-08-31 17:47:09 +00:00
|
|
|
|
2012-01-23 07:21:03 +00:00
|
|
|
bool CMeasureCalc::GetMeasureValue(const char* str, int len, double* value)
|
2009-09-03 00:33:46 +00:00
|
|
|
{
|
2012-01-23 07:21:03 +00:00
|
|
|
const std::list<CMeasure*>& measures = m_MeterWindow->GetMeasures();
|
2009-08-31 17:47:09 +00:00
|
|
|
|
2012-01-23 07:21:03 +00:00
|
|
|
std::list<CMeasure*>::const_iterator iter = measures.begin();
|
|
|
|
for ( ; iter != measures.end(); ++iter)
|
2009-09-03 00:33:46 +00:00
|
|
|
{
|
2012-01-23 07:21:03 +00:00
|
|
|
if (_strnicmp(str, (*iter)->GetAsciiName(), len) == 0)
|
2010-04-08 23:16:43 +00:00
|
|
|
{
|
2012-01-23 07:21:03 +00:00
|
|
|
*value = (*iter)->GetValue();
|
|
|
|
return 1;
|
2010-04-08 23:16:43 +00:00
|
|
|
}
|
2009-09-03 00:33:46 +00:00
|
|
|
}
|
2010-08-11 18:08:41 +00:00
|
|
|
|
2012-01-23 07:21:03 +00:00
|
|
|
if (_strnicmp(str, "counter", len) == 0)
|
2010-08-11 18:08:41 +00:00
|
|
|
{
|
2012-01-23 07:21:03 +00:00
|
|
|
*value = m_MeterWindow->GetUpdateCounter();
|
|
|
|
return 1;
|
2010-08-11 18:08:41 +00:00
|
|
|
}
|
2012-01-23 07:21:03 +00:00
|
|
|
else if (_strnicmp(str, "random", len) == 0)
|
|
|
|
{
|
|
|
|
*value = (double)m_Random;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2010-08-11 18:08:41 +00:00
|
|
|
|
2012-01-23 07:21:03 +00:00
|
|
|
void CMeasureCalc::UpdateRandom()
|
|
|
|
{
|
|
|
|
int range = (m_HighBound - m_LowBound) + 1;
|
|
|
|
srand((unsigned)rand());
|
|
|
|
m_Random = m_LowBound + (int)(range * rand() / (RAND_MAX + 1.0));
|
2010-08-11 18:08:41 +00:00
|
|
|
}
|