MathParser: Fixed issue with measure names less than 6 chars.

This commit is contained in:
Birunthan Mohanathas 2012-01-27 15:36:46 +00:00
parent 35be827071
commit fd48b9f24a
2 changed files with 7 additions and 162 deletions

View File

@ -27,7 +27,6 @@ static const double M_PI = 3.14159265358979323846;
typedef double (*OneArgProc)(double arg); typedef double (*OneArgProc)(double arg);
typedef WCHAR* (*MultiArgProc)(int paramcnt, double* args, double* result); typedef WCHAR* (*MultiArgProc)(int paramcnt, double* args, double* result);
typedef double (*FunctionProc)(double);
enum OperationType enum OperationType
{ {
@ -91,7 +90,7 @@ struct Operation
struct Function struct Function
{ {
WCHAR* name; WCHAR* name;
FunctionProc proc; OneArgProc proc;
BYTE length; BYTE length;
}; };
@ -116,7 +115,7 @@ static Function g_Functions[] =
{ L"trunc", &trunc, 5 }, { L"trunc", &trunc, 5 },
{ L"floor", &floor, 5 }, { L"floor", &floor, 5 },
{ L"ceil", &ceil, 4 }, { L"ceil", &ceil, 4 },
{ L"round", (FunctionProc)&round, 5 }, { L"round", (OneArgProc)&round, 5 },
{ L"asin", &asin, 4 }, { L"asin", &asin, 4 },
{ L"acos", &acos, 4 }, { L"acos", &acos, 4 },
{ L"sgn", &sgn, 4 }, { L"sgn", &sgn, 4 },
@ -129,6 +128,7 @@ static const int FUNC_MAX_LEN = 5;
static const int FUNC_ROUND = 13; static const int FUNC_ROUND = 13;
static const int FUNC_E = 18; static const int FUNC_E = 18;
static const int FUNC_PI = 19; static const int FUNC_PI = 19;
static const BYTE FUNC_INVALID = UCHAR_MAX;
static const Operation g_BrOp = { OP_OBR, 0, 0}; static const Operation g_BrOp = { OP_OBR, 0, 0};
static const Operation g_NegOp = { OP_FUNC_ONEARG, 17, 0 }; static const Operation g_NegOp = { OP_FUNC_ONEARG, 17, 0 };
@ -166,7 +166,7 @@ static const BYTE g_OpPriorities[OP_FUNC_MULTIARG + 1] =
}; };
static CharType GetCharType(WCHAR ch); static CharType GetCharType(WCHAR ch);
static int GetFunction(const WCHAR* str, size_t len); static BYTE GetFunctionIndex(const WCHAR* str, BYTE len);
static int FindSymbol(const WCHAR* str); static int FindSymbol(const WCHAR* str);
struct Parser struct Parser
@ -364,7 +364,7 @@ WCHAR* MathParser::Parse(const WCHAR* formula, CMeasureCalc* calc, double* resul
{ {
Operation op; Operation op;
if (lexer.nameLen <= FUNC_MAX_LEN && if (lexer.nameLen <= FUNC_MAX_LEN &&
((op.funcIndex = GetFunction(lexer.name, lexer.nameLen)) >= 0)) ((op.funcIndex = GetFunctionIndex(lexer.name, lexer.nameLen)) != FUNC_INVALID))
{ {
switch (op.funcIndex) switch (op.funcIndex)
{ {
@ -735,7 +735,7 @@ bool MathParser::IsDelimiter(WCHAR ch)
return type == CH_SYMBOL || type == CH_SEPARAT; return type == CH_SYMBOL || type == CH_SEPARAT;
} }
int GetFunction(const WCHAR* str, size_t len) BYTE GetFunctionIndex(const WCHAR* str, BYTE len)
{ {
const int funcCount = sizeof(g_Functions) / sizeof(Function); const int funcCount = sizeof(g_Functions) / sizeof(Function);
for (int i = 0; i < funcCount; ++i) for (int i = 0; i < funcCount; ++i)
@ -747,7 +747,7 @@ int GetFunction(const WCHAR* str, size_t len)
} }
} }
return -1; return FUNC_INVALID;
} }
int FindSymbol(const WCHAR* str) int FindSymbol(const WCHAR* str)

View File

@ -94,156 +94,6 @@ void ExecuteBang(LPCTSTR szBang)
} }
} }
/*
** ReadConfigString
**
** Reads a config string. Used by the plugins.
**
*/
LPCTSTR ReadConfigString(LPCTSTR section, LPCTSTR key, LPCTSTR defValue)
{
if (Rainmeter)
{
CConfigParser* parser = Rainmeter->GetCurrentParser();
if (parser)
{
// NULL checking
if (section == NULL) section = L"";
if (key == NULL) key = L"";
if (defValue == NULL) defValue = L"";
return parser->ReadString(section, key, defValue, false).c_str();
}
}
return NULL;
}
/*
** PluginBridge
**
** Receives a command and data from a plugin and returns a result. Used by plugins.
**
*/
LPCTSTR PluginBridge(LPCTSTR _sCommand, LPCTSTR _sData)
{
if (Rainmeter)
{
static std::wstring result;
if (_sCommand == NULL || *_sCommand == L'\0')
{
return L"noop";
}
if (_sData == NULL) _sData = L"";
// Command GetConfig
// Data unquoted full path and filename given to the plugin on initialize
// (note: this is CaSe-SeNsItIvE!)
// Execution none
// Result the config name if found or a blank string if not
if (_wcsicmp(_sCommand, L"GetConfig") == 0)
{
// returns the config name, lookup by INI file
CMeterWindow *meterWindow = Rainmeter->GetMeterWindowByINI(_sData);
if (meterWindow)
{
result = L'"';
result += meterWindow->GetSkinName();
result += L'"';
return result.c_str();
}
return L"";
}
// Command GetWindow
// Data [the config name]
// Execution none
// Result the HWND to the specified config window if found, 'error' otherwise
if (_wcsicmp(_sCommand, L"GetWindow") == 0)
{
std::vector<std::wstring> subStrings = CRainmeter::ParseString(_sData);
if (subStrings.size() >= 1)
{
const std::wstring& config = subStrings[0];
CMeterWindow *meterWindow = Rainmeter->GetMeterWindow(config);
if (meterWindow)
{
WCHAR buf1[64];
_snwprintf_s(buf1, _TRUNCATE, L"%lu", PtrToUlong(meterWindow->GetWindow()));
result = buf1;
return result.c_str();
}
}
return L"error";
}
// Command GetVariable
// Data [the config name]
// Execution none
// Result the value of the variable
if (_wcsicmp(_sCommand, L"GetVariable") == 0)
{
std::vector<std::wstring> subStrings = CRainmeter::ParseString(_sData);
if (subStrings.size() >= 2)
{
const std::wstring& config = subStrings[0];
CMeterWindow *meterWindow = Rainmeter->GetMeterWindow(config);
if (meterWindow)
{
const std::wstring& variable = subStrings[1];
if (meterWindow->GetParser().GetVariable(variable, result))
{
return result.c_str();
}
}
}
return L"";
}
// Command SetVariable
// Data [the config name] [variable data]
// Execution the indicated variable is updated
// Result 'success' if the config was found, 'error' otherwise
if (_wcsicmp(_sCommand, L"SetVariable") == 0)
{
std::vector<std::wstring> subStrings = CRainmeter::ParseString(_sData);
if (subStrings.size() >= 2)
{
const std::wstring& config = subStrings[0];
std::wstring arguments;
for (size_t i = 1, isize = subStrings.size(); i < isize; ++i)
{
if (i != 1) arguments += L' ';
arguments += subStrings[i];
}
CMeterWindow *meterWindow = Rainmeter->GetMeterWindow(config);
if (meterWindow)
{
meterWindow->RunBang(BANG_SETVARIABLE, arguments.c_str());
return L"success";
}
}
return L"error";
}
return L"noop";
}
return L"error:no rainmeter!";
}
/* /*
** ParseString ** ParseString
** **
@ -1369,11 +1219,6 @@ CMeterWindow* CRainmeter::GetMeterWindow(const std::wstring& config)
return NULL; return NULL;
} }
// Added by Peter Souza IV / psouza4 / 2010.12.13
//
// Returns a CMeterWindow object given a config's INI path and filename. Since plugins
// get the full path and filename of an INI file on Initialize(), but not the name of
// the config, this is used to convert the INI filename to a config name.
CMeterWindow* CRainmeter::GetMeterWindowByINI(const std::wstring& ini_searching) CMeterWindow* CRainmeter::GetMeterWindowByINI(const std::wstring& ini_searching)
{ {
if (_wcsnicmp(m_SkinPath.c_str(), ini_searching.c_str(), m_SkinPath.length()) == 0) if (_wcsnicmp(m_SkinPath.c_str(), ini_searching.c_str(), m_SkinPath.length()) == 0)