Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 240 lines (210 sloc) 6.064 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
/*
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.
*/

#include "StdAfx.h"
#include "../Common/MathParser.h"
#include "MeasureCalc.h"
#include "Rainmeter.h"
#include <random>

const int DEFAULT_LOWER_BOUND = 0;
const int DEFAULT_UPPER_BOUND = 100;
const int DEFAULT_UNIQUELIMIT = 65535;

std::mt19937& GetRandomEngine()
{
static std::unique_ptr<std::mt19937> s_Engine(new std::mt19937((uint32_t)time(nullptr)));
return *s_Engine;
}

/*
** The constructor
**
*/
MeasureCalc::MeasureCalc(MeterWindow* meterWindow, const WCHAR* name) : Measure(meterWindow, name),
m_ParseError(false),
m_LowBound(DEFAULT_LOWER_BOUND),
m_HighBound(DEFAULT_UPPER_BOUND),
m_UpdateRandom(false),
m_UniqueRandom(false)
{
}

/*
** The destructor
**
*/
MeasureCalc::~MeasureCalc()
{
}

/*
** Updates the calculation
**
*/
void MeasureCalc::UpdateValue()
{
const WCHAR* errMsg = MathParser::Parse(m_Formula.c_str(), &m_Value, GetMeasureValue, this);
if (errMsg != nullptr)
{
if (!m_ParseError)
{
LogErrorF(this, L"Calc: %s", errMsg);
m_ParseError = true;
}
}
else
{
m_ParseError = false;
}
}

/*
** Read the options specified in the ini file.
**
*/
void MeasureCalc::ReadOptions(ConfigParser& parser, const WCHAR* section)
{
Measure::ReadOptions(parser, section);

// 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;
bool oldUniqueRandom = m_UniqueRandom;

std::wstring oldFormula = m_Formula;
m_Formula = parser.ReadString(section, L"Formula", L"");

m_LowBound = parser.ReadInt(section, L"LowBound", DEFAULT_LOWER_BOUND);
m_HighBound = parser.ReadInt(section, L"HighBound", DEFAULT_UPPER_BOUND);
m_UpdateRandom = parser.ReadBool(section, L"UpdateRandom", false);
const size_t range = (m_HighBound - m_LowBound) + 1;

m_UniqueRandom = (range <= DEFAULT_UNIQUELIMIT) && parser.ReadBool(section, L"UniqueRandom", false);
if (!m_UniqueRandom)
{
m_UniqueNumbers.clear();
}

if (!m_Initialized ||
wcscmp(m_Formula.c_str(), oldFormula.c_str()) != 0 ||
oldLowBound != m_LowBound ||
oldHighBound != m_HighBound ||
oldUpdateRandom != m_UpdateRandom ||
oldUniqueRandom != m_UniqueRandom)
{
// Reset bounds if |m_LowBound| is greater than |m_HighBound|.
if (m_LowBound > m_HighBound &&
(oldLowBound != m_LowBound || oldHighBound != m_HighBound))
{
LogErrorF(this, L"\"LowBound\" (%i) must be less then or equal to \"HighBound\" (%i)", m_LowBound, m_HighBound);
}

// Reset the list if the bounds are changed
if (m_UniqueRandom && (
oldLowBound != m_LowBound ||
oldHighBound != m_HighBound))
{
UpdateUniqueNumberList();
}

if (!m_UpdateRandom)
{
FormulaReplace();
}

const WCHAR* errMsg = MathParser::Check(m_Formula.c_str());
if (errMsg != nullptr)
{
LogErrorF(this, L"Calc: %s", errMsg);
m_Formula.clear();
}
}
}

/*
** This replaces the word Random in the formula with a random number
**
*/
void MeasureCalc::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);
}

bool MeasureCalc::GetMeasureValue(const WCHAR* str, int len, double* value, void* context)
{
auto calc = (MeasureCalc*)context;
const std::vector<Measure*>& measures = calc->m_MeterWindow->GetMeasures();

std::vector<Measure*>::const_iterator iter = measures.begin();
for ( ; iter != measures.end(); ++iter)
{
if ((*iter)->GetOriginalName().length() == len &&
_wcsnicmp(str, (*iter)->GetName(), len) == 0)
{
*value = (*iter)->GetValue();
return true;
}
}

if (_wcsnicmp(str, L"counter", len) == 0)
{
*value = calc->m_MeterWindow->GetUpdateCounter();
return true;
}
else if (_wcsnicmp(str, L"random", len) == 0)
{
*value = calc->GetRandom();
return true;
}

return false;
}

int MeasureCalc::GetRandom()
{
if (m_LowBound == m_HighBound || m_LowBound > m_HighBound)
{
return m_LowBound;
}
else if (m_UniqueRandom)
{
if (m_UniqueNumbers.empty())
{
UpdateUniqueNumberList();
}

const int value = m_UniqueNumbers.back();
m_UniqueNumbers.pop_back();
return value;
}
else
{
const std::uniform_int_distribution<int> distribution(m_LowBound, m_HighBound);
return distribution(GetRandomEngine());
}
}

void MeasureCalc::UpdateUniqueNumberList()
{
const size_t range = (m_HighBound - m_LowBound) + 1;
m_UniqueNumbers.resize(range);

for (int i = 0; i < (int)range; ++i)
{
m_UniqueNumbers[i] = m_LowBound + i;
}

std::shuffle(m_UniqueNumbers.begin(), m_UniqueNumbers.end(), GetRandomEngine());
}
Something went wrong with that request. Please try again.