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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
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"
|
2010-09-21 22:47:53 +00:00
|
|
|
|
2009-02-10 18:37:48 +00:00
|
|
|
hqStrMap* CMeasureCalc::c_VarMap = NULL;
|
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),
|
2011-01-29 00:11:01 +00:00
|
|
|
m_Parser(MathParser_Create(NULL)),
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
MathParser_Destroy(m_Parser);
|
|
|
|
|
|
|
|
if (c_VarMap)
|
|
|
|
{
|
|
|
|
StrMap_Destroy(c_VarMap);
|
|
|
|
c_VarMap = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Update
|
|
|
|
**
|
|
|
|
** Updates the calculation
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
bool CMeasureCalc::Update()
|
|
|
|
{
|
|
|
|
if (!CMeasure::PreUpdate()) return false;
|
|
|
|
|
|
|
|
m_Parser->Parameters = c_VarMap;
|
2010-08-11 18:08:41 +00:00
|
|
|
if (m_UpdateRandom)
|
2009-09-03 00:33:46 +00:00
|
|
|
{
|
2010-08-10 15:37:35 +00:00
|
|
|
FormulaReplace();
|
2009-09-03 00:33:46 +00:00
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
|
|
|
|
char* errMsg = MathParser_Parse(m_Parser, ConvertToAscii(m_Formula.c_str()).c_str(), &m_Value);
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMeasureCalc::UpdateVariableMap(CMeterWindow& meterWindow)
|
|
|
|
{
|
|
|
|
// Delete the old map
|
|
|
|
if (c_VarMap)
|
|
|
|
{
|
|
|
|
StrMap_Destroy(c_VarMap);
|
|
|
|
c_VarMap = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the variable map
|
2011-03-29 19:21:57 +00:00
|
|
|
c_VarMap = Strmap_Create(sizeof(double), 0);
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2011-01-24 10:15:05 +00:00
|
|
|
const std::list<CMeasure*>& measures = meterWindow.GetMeasures();
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2010-03-30 22:37:05 +00:00
|
|
|
std::list<CMeasure*>::const_iterator iter = measures.begin();
|
2011-03-29 19:21:57 +00:00
|
|
|
for ( ; iter != measures.end(); ++iter)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-11-09 08:52:32 +00:00
|
|
|
const char* name = (*iter)->GetAsciiName();
|
2009-02-10 18:37:48 +00:00
|
|
|
double val = (*iter)->GetValue();
|
|
|
|
|
|
|
|
StrMap_AddString(c_VarMap, name, &val);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the counter
|
|
|
|
double counter = meterWindow.GetUpdateCounter();
|
|
|
|
StrMap_AddString(c_VarMap, "Counter", &counter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** 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;
|
|
|
|
|
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 ||
|
2011-11-27 08:30:39 +00:00
|
|
|
wcscmp(m_FormulaHolder.c_str(), m_Formula.c_str()) != 0 ||
|
2010-08-11 18:08:41 +00:00
|
|
|
oldLowBound != m_LowBound ||
|
|
|
|
oldHighBound != m_HighBound ||
|
|
|
|
oldUpdateRandom != m_UpdateRandom)
|
|
|
|
{
|
|
|
|
// Hold onto the formula, we are going to change it
|
|
|
|
m_FormulaHolder = m_Formula;
|
2009-08-31 17:47:09 +00:00
|
|
|
|
2010-08-11 18:08:41 +00:00
|
|
|
if (!m_UpdateRandom)
|
|
|
|
{
|
|
|
|
FormulaReplace();
|
|
|
|
}
|
|
|
|
}
|
2009-09-03 00:33:46 +00:00
|
|
|
}
|
2009-08-31 17:47:09 +00:00
|
|
|
|
2009-09-03 00:33:46 +00:00
|
|
|
/*
|
2010-08-10 15:37:35 +00:00
|
|
|
** FormulaReplace
|
2009-09-03 00:33:46 +00:00
|
|
|
**
|
|
|
|
** This replaces the word Random in m_Formula with a random number
|
|
|
|
**
|
|
|
|
*/
|
2010-08-10 15:37:35 +00:00
|
|
|
void CMeasureCalc::FormulaReplace()
|
2009-09-03 00:33:46 +00:00
|
|
|
{
|
|
|
|
//To implement random numbers the word "Random" in the string
|
|
|
|
//formula is being replaced by the random number value
|
|
|
|
m_Formula = m_FormulaHolder;
|
2011-12-09 03:28:19 +00:00
|
|
|
size_t start = 0, pos;
|
2009-08-31 17:47:09 +00:00
|
|
|
|
2011-12-09 03:28:19 +00:00
|
|
|
do
|
2009-09-03 00:33:46 +00:00
|
|
|
{
|
2011-12-09 03:28:19 +00:00
|
|
|
pos = m_Formula.find_first_of(L"Rr", start);
|
|
|
|
if (pos != std::wstring::npos)
|
2010-04-08 23:16:43 +00:00
|
|
|
{
|
2011-12-09 03:28:19 +00:00
|
|
|
if (_wcsnicmp(L"Random", m_Formula.c_str() + pos, 6) == 0 &&
|
|
|
|
(pos == 0 || IsDelimiter(*(m_Formula.c_str() + pos - 1))) &&
|
|
|
|
(pos == (m_Formula.length() - 6) || IsDelimiter(*(m_Formula.c_str() + pos + 6))))
|
|
|
|
{
|
|
|
|
int range = (m_HighBound - m_LowBound) + 1;
|
|
|
|
srand((unsigned) rand());
|
|
|
|
int randNumber = m_LowBound + (int)(range * rand() / (RAND_MAX + 1.0));
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2010-04-08 23:16:43 +00:00
|
|
|
}
|
2009-09-03 00:33:46 +00:00
|
|
|
}
|
2011-12-09 03:28:19 +00:00
|
|
|
while (pos != std::wstring::npos);
|
2010-08-11 18:08:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** IsDelimiter
|
|
|
|
**
|
|
|
|
** Checks whether the given character is a operator or a delimiter.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
bool CMeasureCalc::IsDelimiter(WCHAR ch)
|
|
|
|
{
|
2011-01-24 10:15:05 +00:00
|
|
|
const WCHAR* symbols = L" \t\n()+-/*^~<>%$,?:=&|;";
|
2010-08-11 18:08:41 +00:00
|
|
|
|
2011-01-24 10:15:05 +00:00
|
|
|
for (const WCHAR* sch = symbols; *sch != L'\0'; ++sch)
|
2010-08-11 18:08:41 +00:00
|
|
|
{
|
2011-01-24 10:15:05 +00:00
|
|
|
if (ch == *sch)
|
2010-08-11 18:08:41 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|