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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma warning(disable: 4996)
|
|
|
|
|
|
|
|
#include "MeasureCalc.h"
|
|
|
|
#include "Rainmeter.h"
|
2009-08-31 17:47:09 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <ctime>
|
2009-02-10 18:37:48 +00:00
|
|
|
|
|
|
|
hqStrMap* CMeasureCalc::c_VarMap = NULL;
|
|
|
|
int CMeasureCalc::m_Loop = 0;
|
|
|
|
|
2009-08-31 17:47:09 +00:00
|
|
|
//======================================
|
|
|
|
//MattKing Code Edit :: Start
|
|
|
|
double CMeasureCalc::m_Random = 0;
|
|
|
|
double CMeasureCalc::m_LowBound = 0;
|
|
|
|
double CMeasureCalc::m_HighBound = 0;
|
|
|
|
//MattKing Code Edit :: End
|
|
|
|
|
2009-02-10 18:37:48 +00:00
|
|
|
/*
|
|
|
|
** CMeasureCalc
|
|
|
|
**
|
|
|
|
** The constructor
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
CMeasureCalc::CMeasureCalc(CMeterWindow* meterWindow) : CMeasure(meterWindow)
|
|
|
|
{
|
|
|
|
m_Parser = MathParser_Create(NULL);
|
2009-08-31 17:47:09 +00:00
|
|
|
|
|
|
|
//======================================
|
|
|
|
//MattKing Code Edit :: Start
|
|
|
|
srand((unsigned)time(0));
|
|
|
|
//MattKing Code Edit :: End
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
char* errMsg = MathParser_Parse(m_Parser, ConvertToAscii(m_Formula.c_str()).c_str(), &m_Value);
|
|
|
|
if (errMsg != NULL)
|
|
|
|
{
|
|
|
|
DebugLog(ConvertToWide(errMsg).c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
c_VarMap = Strmap_Create(sizeof(double), 0);
|
|
|
|
|
|
|
|
std::list<CMeasure*>& measures = meterWindow.GetMeasures();
|
|
|
|
|
|
|
|
std::list<CMeasure*>::iterator iter = measures.begin();
|
|
|
|
for( ; iter != measures.end(); iter++)
|
|
|
|
{
|
|
|
|
const char* name = (*iter)->GetANSIName();
|
|
|
|
double val = (*iter)->GetValue();
|
|
|
|
|
|
|
|
StrMap_AddString(c_VarMap, name, &val);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the counter
|
|
|
|
double counter = meterWindow.GetUpdateCounter();
|
|
|
|
StrMap_AddString(c_VarMap, "Counter", &counter);
|
2009-08-31 17:47:09 +00:00
|
|
|
StrMap_AddString(c_VarMap, "Random", &m_Random);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** ReadConfig
|
|
|
|
**
|
|
|
|
** Reads the measure specific configs.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void CMeasureCalc::ReadConfig(CConfigParser& parser, const WCHAR* section)
|
|
|
|
{
|
|
|
|
CMeasure::ReadConfig(parser, section);
|
|
|
|
|
|
|
|
m_Formula = parser.ReadString(section, L"Formula", L"");
|
2009-08-31 17:47:09 +00:00
|
|
|
|
|
|
|
//MattKing Code Edit :: Start
|
|
|
|
m_LowBound = parser.ReadFloat(section, L"LowBound", 95);
|
|
|
|
m_HighBound = parser.ReadFloat(section, L"HighBound", 400);
|
|
|
|
|
|
|
|
int range = (m_HighBound - m_LowBound);
|
|
|
|
|
|
|
|
m_Random = m_LowBound + int(range * rand()/(RAND_MAX + 1.0));
|
|
|
|
m_Random = m_LowBound + int(range * rand()/(RAND_MAX + 1.0));
|
|
|
|
// For some reason I need two calls to really randomize the number
|
|
|
|
// I don't have a fix as I dont know much about random numbers
|
|
|
|
|
|
|
|
//MattKing Code Edit :: End
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|