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"
|
2013-06-13 14:00:24 +00:00
|
|
|
#include "../Common/MathParser.h"
|
2009-02-10 18:37:48 +00:00
|
|
|
#include "MeasureCalc.h"
|
|
|
|
#include "Rainmeter.h"
|
2013-12-08 13:06:01 +00:00
|
|
|
#include <random>
|
2010-09-21 22:47:53 +00:00
|
|
|
|
2013-11-10 17:41:21 +00:00
|
|
|
const int DEFAULT_LOWER_BOUND = 0;
|
|
|
|
const int DEFAULT_UPPER_BOUND = 100;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2013-12-08 13:06:01 +00:00
|
|
|
std::mt19937& GetRandomEngine()
|
|
|
|
{
|
2013-12-08 13:07:45 +00:00
|
|
|
static std::unique_ptr<std::mt19937> s_Engine(new std::mt19937((uint32_t)time(nullptr)));
|
2013-12-08 13:06:01 +00:00
|
|
|
return *s_Engine;
|
|
|
|
}
|
2013-12-08 11:40:48 +00:00
|
|
|
|
2009-02-10 18:37:48 +00:00
|
|
|
/*
|
|
|
|
** The constructor
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
MeasureCalc::MeasureCalc(MeterWindow* meterWindow, const WCHAR* name) : Measure(meterWindow, name),
|
2012-04-07 07:06:58 +00:00
|
|
|
m_ParseError(false),
|
2013-08-16 00:12:36 +00:00
|
|
|
m_LowBound(DEFAULT_LOWER_BOUND),
|
|
|
|
m_HighBound(DEFAULT_UPPER_BOUND),
|
|
|
|
m_UpdateRandom(false),
|
2013-12-08 11:40:48 +00:00
|
|
|
m_UniqueRandom(false)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The destructor
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
MeasureCalc::~MeasureCalc()
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Updates the calculation
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void MeasureCalc::UpdateValue()
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2013-06-13 14:00:24 +00:00
|
|
|
const WCHAR* errMsg = MathParser::Parse(m_Formula.c_str(), &m_Value, GetMeasureValue, this);
|
2013-05-31 14:28:39 +00:00
|
|
|
if (errMsg != nullptr)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-04-07 07:06:58 +00:00
|
|
|
if (!m_ParseError)
|
2012-04-06 18:04:57 +00:00
|
|
|
{
|
2013-06-29 16:59:18 +00:00
|
|
|
LogErrorF(this, L"Calc: %s", errMsg);
|
2012-04-07 07:06:58 +00:00
|
|
|
m_ParseError = true;
|
2012-04-06 18:04:57 +00:00
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2012-04-07 07:06:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
m_ParseError = false;
|
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-06-01 13:06:36 +00:00
|
|
|
** Read the options specified in the ini file.
|
2009-02-10 18:37:48 +00:00
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void MeasureCalc::ReadOptions(ConfigParser& parser, const WCHAR* section)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2013-05-31 14:18:52 +00:00
|
|
|
Measure::ReadOptions(parser, section);
|
2009-02-10 18:37:48 +00:00
|
|
|
|
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;
|
2013-08-16 00:12:36 +00:00
|
|
|
bool oldUniqueRandom = m_UniqueRandom;
|
2010-08-11 18:08:41 +00:00
|
|
|
|
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
|
|
|
|
2013-08-16 00:12:36 +00:00
|
|
|
m_LowBound = parser.ReadInt(section, L"LowBound", DEFAULT_LOWER_BOUND);
|
|
|
|
m_HighBound = parser.ReadInt(section, L"HighBound", DEFAULT_UPPER_BOUND);
|
2013-07-31 10:36:53 +00:00
|
|
|
m_UpdateRandom = parser.ReadBool(section, L"UpdateRandom", false);
|
2013-08-16 19:19:22 +00:00
|
|
|
|
2013-08-16 00:12:36 +00:00
|
|
|
m_UniqueRandom = parser.ReadBool(section, L"UniqueRandom", false);
|
|
|
|
if (!m_UniqueRandom)
|
|
|
|
{
|
|
|
|
m_UniqueNumbers.clear();
|
|
|
|
}
|
2010-08-11 18:08:41 +00:00
|
|
|
|
|
|
|
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 ||
|
2013-08-16 00:12:36 +00:00
|
|
|
oldUpdateRandom != m_UpdateRandom ||
|
|
|
|
oldUniqueRandom != m_UniqueRandom)
|
2010-08-11 18:08:41 +00:00
|
|
|
{
|
2013-12-08 11:44:03 +00:00
|
|
|
// Reset bounds if |m_LowBound| is greater than |m_HighBound|.
|
|
|
|
if (m_LowBound > m_HighBound &&
|
|
|
|
(oldLowBound != m_LowBound || oldHighBound != m_HighBound))
|
2013-08-16 00:12:36 +00:00
|
|
|
{
|
2013-08-19 20:41:45 +00:00
|
|
|
LogErrorF(this, L"\"LowBound\" (%i) must be less then or equal to \"HighBound\" (%i)", m_LowBound, m_HighBound);
|
2013-08-16 00:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the list if the bounds are changed
|
|
|
|
if (m_UniqueRandom && (
|
|
|
|
oldLowBound != m_LowBound ||
|
|
|
|
oldHighBound != m_HighBound))
|
|
|
|
{
|
|
|
|
UpdateUniqueNumberList();
|
|
|
|
}
|
|
|
|
|
2012-01-23 09:30:51 +00:00
|
|
|
if (!m_UpdateRandom)
|
|
|
|
{
|
|
|
|
FormulaReplace();
|
|
|
|
}
|
2012-01-22 20:28:05 +00:00
|
|
|
|
2012-02-20 14:49:38 +00:00
|
|
|
const WCHAR* errMsg = MathParser::Check(m_Formula.c_str());
|
2013-05-31 14:28:39 +00:00
|
|
|
if (errMsg != nullptr)
|
2012-01-22 20:28:05 +00:00
|
|
|
{
|
2013-06-29 16:59:18 +00:00
|
|
|
LogErrorF(this, L"Calc: %s", errMsg);
|
2012-04-07 07:06:58 +00:00
|
|
|
m_Formula.clear();
|
2012-01-22 20:28:05 +00:00
|
|
|
}
|
2010-08-11 18:08:41 +00:00
|
|
|
}
|
2009-09-03 00:33:46 +00:00
|
|
|
}
|
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
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void MeasureCalc::FormulaReplace()
|
2012-01-23 09:30:51 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2013-06-13 14:00:24 +00:00
|
|
|
bool MeasureCalc::GetMeasureValue(const WCHAR* str, int len, double* value, void* context)
|
2009-09-03 00:33:46 +00:00
|
|
|
{
|
2013-06-13 14:00:24 +00:00
|
|
|
auto calc = (MeasureCalc*)context;
|
|
|
|
const std::vector<Measure*>& measures = calc->m_MeterWindow->GetMeasures();
|
2009-08-31 17:47:09 +00:00
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
std::vector<Measure*>::const_iterator iter = measures.begin();
|
2012-01-23 07:21:03 +00:00
|
|
|
for ( ; iter != measures.end(); ++iter)
|
2009-09-03 00:33:46 +00:00
|
|
|
{
|
2012-01-23 09:35:32 +00:00
|
|
|
if ((*iter)->GetOriginalName().length() == len &&
|
|
|
|
_wcsnicmp(str, (*iter)->GetName(), len) == 0)
|
2010-04-08 23:16:43 +00:00
|
|
|
{
|
2012-01-23 07:21:03 +00:00
|
|
|
*value = (*iter)->GetValue();
|
2012-01-23 09:30:51 +00:00
|
|
|
return true;
|
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 09:30:51 +00:00
|
|
|
if (_wcsnicmp(str, L"counter", len) == 0)
|
2010-08-11 18:08:41 +00:00
|
|
|
{
|
2013-06-13 14:00:24 +00:00
|
|
|
*value = calc->m_MeterWindow->GetUpdateCounter();
|
2012-01-23 09:30:51 +00:00
|
|
|
return true;
|
2010-08-11 18:08:41 +00:00
|
|
|
}
|
2012-01-23 09:30:51 +00:00
|
|
|
else if (_wcsnicmp(str, L"random", len) == 0)
|
2012-01-23 07:21:03 +00:00
|
|
|
{
|
2013-06-13 14:00:24 +00:00
|
|
|
*value = calc->GetRandom();
|
2012-01-23 09:30:51 +00:00
|
|
|
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
|
|
|
}
|
2010-08-11 18:08:41 +00:00
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
int MeasureCalc::GetRandom()
|
2012-01-23 07:21:03 +00:00
|
|
|
{
|
2013-12-08 11:44:03 +00:00
|
|
|
if (m_LowBound == m_HighBound || m_LowBound > m_HighBound)
|
2013-08-19 20:41:45 +00:00
|
|
|
{
|
2013-12-08 11:41:19 +00:00
|
|
|
return m_LowBound;
|
2013-08-19 20:41:45 +00:00
|
|
|
}
|
|
|
|
else if (m_UniqueRandom)
|
2013-08-16 00:12:36 +00:00
|
|
|
{
|
|
|
|
if (m_UniqueNumbers.empty())
|
|
|
|
{
|
|
|
|
UpdateUniqueNumberList();
|
|
|
|
}
|
|
|
|
|
2013-12-08 11:41:19 +00:00
|
|
|
const int value = m_UniqueNumbers.back();
|
2013-08-16 00:12:36 +00:00
|
|
|
m_UniqueNumbers.pop_back();
|
2013-12-08 11:41:19 +00:00
|
|
|
return value;
|
2013-08-16 00:12:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-08 11:40:48 +00:00
|
|
|
const std::uniform_int_distribution<int> distribution(m_LowBound, m_HighBound);
|
2013-12-08 13:06:01 +00:00
|
|
|
return distribution(GetRandomEngine());
|
2013-08-16 00:12:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeasureCalc::UpdateUniqueNumberList()
|
|
|
|
{
|
2013-12-08 11:41:19 +00:00
|
|
|
const size_t range = (m_HighBound - m_LowBound) + 1;
|
|
|
|
m_UniqueNumbers.resize(range);
|
2013-08-16 00:12:36 +00:00
|
|
|
|
2013-12-23 19:42:31 +00:00
|
|
|
for (int i = 0; i < (int)range; ++i)
|
2013-08-16 00:12:36 +00:00
|
|
|
{
|
2013-12-08 11:41:19 +00:00
|
|
|
m_UniqueNumbers[i] = m_LowBound + i;
|
2013-08-16 00:12:36 +00:00
|
|
|
}
|
|
|
|
|
2013-12-08 13:06:01 +00:00
|
|
|
std::shuffle(m_UniqueNumbers.begin(), m_UniqueNumbers.end(), GetRandomEngine());
|
2010-08-11 18:08:41 +00:00
|
|
|
}
|