diff options
Diffstat (limited to 'varparser.c')
-rw-r--r-- | varparser.c | 304 |
1 files changed, 145 insertions, 159 deletions
diff --git a/varparser.c b/varparser.c index 1f2ea07..a565dae 100644 --- a/varparser.c +++ b/varparser.c @@ -29,211 +29,197 @@ The project's page is at http://winni.vdr-developer.org/epgsearch bool cVarParser::Parse(const string& input) { - return ParseAssign(input); + return ParseAssign(input); } bool cVarParser::ParseAssign(const string& input) { - int assignPos = input.find("="); - if (assignPos >= 0) - { - string var(input.begin(), input.begin() + assignPos); - if (ParseVar(var)) - { - varName = Strip(var); - string assign(input.begin() + assignPos + 1, input.end()); - return ParseExp(assign); - } - } - LogFile.eSysLog("error parsing '%s'", input.c_str()); - return false; + int assignPos = input.find("="); + if (assignPos >= 0) { + string var(input.begin(), input.begin() + assignPos); + if (ParseVar(var)) { + varName = Strip(var); + string assign(input.begin() + assignPos + 1, input.end()); + return ParseExp(assign); + } + } + LogFile.eSysLog("error parsing '%s'", input.c_str()); + return false; } bool cVarParser::ParseExp(const string& input) { - // system call? - int sysPos = input.find("system"); - if (sysPos == 0) - return ParseShellCmd(input); - // connect command? - int conPos = input.find("connect"); - if (conPos == 0) - return ParseConnectCmd(input); - // length command? - int lenPos = input.find("length"); - if (lenPos == 0) - return ParseLengthCmd(input); - // conditional expression? - int varPos = Strip(input).find("%"); - if (varPos == 0) - { - int queryPos = input.find("?"); - if (queryPos >= 0) - { - int colonPos = input.find(":"); - if (colonPos > queryPos) - return ParseCondExp(input); - } - } - // composed expression - compExpr = input; - return true; + // system call? + int sysPos = input.find("system"); + if (sysPos == 0) + return ParseShellCmd(input); + // connect command? + int conPos = input.find("connect"); + if (conPos == 0) + return ParseConnectCmd(input); + // length command? + int lenPos = input.find("length"); + if (lenPos == 0) + return ParseLengthCmd(input); + // conditional expression? + int varPos = Strip(input).find("%"); + if (varPos == 0) { + int queryPos = input.find("?"); + if (queryPos >= 0) { + int colonPos = input.find(":"); + if (colonPos > queryPos) + return ParseCondExp(input); + } + } + // composed expression + compExpr = input; + return true; } bool cVarParser::ParseShellCmd(const string& input) { - int cmdPos = input.find("("); - int cmdArgsBegin = input.find(","); - int cmdArgsEnd = input.rfind(")"); - if (cmdPos == -1 || cmdArgsEnd == -1) return false; - string shellcmd(input.begin() + cmdPos + 1, input.begin() + (cmdArgsBegin >= 0?cmdArgsBegin:cmdArgsEnd)); - shellcmd = Strip(shellcmd); - - cmdArgs = ""; - if (cmdArgsBegin >= 0) - cmdArgs = string(input.begin() + cmdArgsBegin + 1, input.begin() + cmdArgsEnd); - - string cmdVDR = "varcmd: " + shellcmd; - cmd = new cCommand; - if (!cmd->Parse(cmdVDR.c_str())) - { - LogFile.eSysLog("error parsing command: %s", input.c_str()); - delete cmd; - cmd = NULL; - return false; - } - type = cVarParser::shellcmd; - return true; + int cmdPos = input.find("("); + int cmdArgsBegin = input.find(","); + int cmdArgsEnd = input.rfind(")"); + if (cmdPos == -1 || cmdArgsEnd == -1) return false; + string shellcmd(input.begin() + cmdPos + 1, input.begin() + (cmdArgsBegin >= 0 ? cmdArgsBegin : cmdArgsEnd)); + shellcmd = Strip(shellcmd); + + cmdArgs = ""; + if (cmdArgsBegin >= 0) + cmdArgs = string(input.begin() + cmdArgsBegin + 1, input.begin() + cmdArgsEnd); + + string cmdVDR = "varcmd: " + shellcmd; + cmd = new cCommand; + if (!cmd->Parse(cmdVDR.c_str())) { + LogFile.eSysLog("error parsing command: %s", input.c_str()); + delete cmd; + cmd = NULL; + return false; + } + type = cVarParser::shellcmd; + return true; } bool cVarParser::ParseConnectCmd(const string& input) { - int startCon = input.find("("); - int endCon = input.find(")"); - if (startCon == -1 || endCon == -1) return false; - string connect(input.begin() + startCon + 1, input.begin() + endCon); - std::stringstream ss(connect); - std::string item; - if (std::getline(ss, item, ',')) - connectAddr = item; - if (std::getline(ss, item, ',')) - connectPort = atoi(item.c_str()); - if (std::getline(ss, item)) - cmdArgs = item; - - connectAddr = Strip(connectAddr); - cmdArgs = Strip(cmdArgs); - - if (connectAddr.size() == 0 || connectPort == -1) - { - LogFile.eSysLog("error parsing command: %s", input.c_str()); - return false; - } - type = cVarParser::connectcmd; - return true; + int startCon = input.find("("); + int endCon = input.find(")"); + if (startCon == -1 || endCon == -1) return false; + string connect(input.begin() + startCon + 1, input.begin() + endCon); + std::stringstream ss(connect); + std::string item; + if (std::getline(ss, item, ',')) + connectAddr = item; + if (std::getline(ss, item, ',')) + connectPort = atoi(item.c_str()); + if (std::getline(ss, item)) + cmdArgs = item; + + connectAddr = Strip(connectAddr); + cmdArgs = Strip(cmdArgs); + + if (connectAddr.size() == 0 || connectPort == -1) { + LogFile.eSysLog("error parsing command: %s", input.c_str()); + return false; + } + type = cVarParser::connectcmd; + return true; } bool cVarParser::ParseLengthCmd(const string& input) { - int startLen = input.find("("); - int endLen = input.find(")"); - if (startLen == -1 || endLen == -1) return false; - string arg(input.begin() + startLen + 1, input.begin() + endLen); - compExpr = arg; - type = cVarParser::lengthcmd; - return true; + int startLen = input.find("("); + int endLen = input.find(")"); + if (startLen == -1 || endLen == -1) return false; + string arg(input.begin() + startLen + 1, input.begin() + endLen); + compExpr = arg; + type = cVarParser::lengthcmd; + return true; } bool cVarParser::ParseCondExp(const string& input) { - int condEndPos = input.find("?"); - string cond(input.begin(), input.begin() + condEndPos); - int condNeqPos = cond.find("!="); - int condEqPos = cond.find("=="); - - if (condEqPos == -1 && condNeqPos == -1) - { - cond += "!="; - condNeqPos = cond.find("!="); - } - - if (condEqPos >= 0 || condNeqPos >= 0) - { - if (!ParseEquality(cond)) - { - LogFile.eSysLog("error parsing '%s'", input.c_str()); - return false; - } - condOp = (condEqPos >= 0)?condEq:condNeq; - } - else - { - LogFile.eSysLog("error parsing '%s'", input.c_str()); - return false; - } - - string truefalse(input.begin() + condEndPos + 1, input.end()); - int elsePos = truefalse.find(":"); - if (elsePos >= 0) - { - string truePart(truefalse.begin(), truefalse.begin() + elsePos); - string falsePart(truefalse.begin() + elsePos + 1, truefalse.end()); - if (ParseVar(truePart) && ParseVar(falsePart)) - { - condvarTrue = Strip(truePart); - condvarFalse = Strip(falsePart); - type = cVarParser::condition; - return true; - } - } - LogFile.eSysLog("error parsing '%s'", input.c_str()); - condEqLeft = condEqRight = ""; - return false; + int condEndPos = input.find("?"); + string cond(input.begin(), input.begin() + condEndPos); + int condNeqPos = cond.find("!="); + int condEqPos = cond.find("=="); + + if (condEqPos == -1 && condNeqPos == -1) { + cond += "!="; + condNeqPos = cond.find("!="); + } + + if (condEqPos >= 0 || condNeqPos >= 0) { + if (!ParseEquality(cond)) { + LogFile.eSysLog("error parsing '%s'", input.c_str()); + return false; + } + condOp = (condEqPos >= 0) ? condEq : condNeq; + } else { + LogFile.eSysLog("error parsing '%s'", input.c_str()); + return false; + } + + string truefalse(input.begin() + condEndPos + 1, input.end()); + int elsePos = truefalse.find(":"); + if (elsePos >= 0) { + string truePart(truefalse.begin(), truefalse.begin() + elsePos); + string falsePart(truefalse.begin() + elsePos + 1, truefalse.end()); + if (ParseVar(truePart) && ParseVar(falsePart)) { + condvarTrue = Strip(truePart); + condvarFalse = Strip(falsePart); + type = cVarParser::condition; + return true; + } + } + LogFile.eSysLog("error parsing '%s'", input.c_str()); + condEqLeft = condEqRight = ""; + return false; } bool cVarParser::ParseEquality(const string& input) { - int condEqPos = input.find("=="); - int condNeqPos = input.find("!="); - int condOpPos = -1; - if (condEqPos >= 0) condOpPos = condEqPos; - if (condNeqPos >= 0) condOpPos = condNeqPos; - if (condOpPos == -1) return false; - string left(input.begin(), input.begin() + condOpPos); - string right(input.begin() + condOpPos + 2, input.end()); - if (ParseExp(left) && ParseExp(right)) - { - condEqLeft = Strip(left); - condEqRight = Strip(right); - return true; - } - return false; + int condEqPos = input.find("=="); + int condNeqPos = input.find("!="); + int condOpPos = -1; + if (condEqPos >= 0) condOpPos = condEqPos; + if (condNeqPos >= 0) condOpPos = condNeqPos; + if (condOpPos == -1) return false; + string left(input.begin(), input.begin() + condOpPos); + string right(input.begin() + condOpPos + 2, input.end()); + if (ParseExp(left) && ParseExp(right)) { + condEqLeft = Strip(left); + condEqRight = Strip(right); + return true; + } + return false; } bool cVarParser::ParseVar(const string& input) { - string str = Strip(input); - if (str.size() > 2 && str[0] == '%' && str[str.size()-1] == '%') - return true; - return false; + string str = Strip(input); + if (str.size() > 2 && str[0] == '%' && str[str.size() - 1] == '%') + return true; + return false; } bool cVarParser::IsCondExpr() { - return type == cVarParser::condition; + return type == cVarParser::condition; } bool cVarParser::IsShellCmd() { - return type == cVarParser::shellcmd; + return type == cVarParser::shellcmd; } bool cVarParser::IsConnectCmd() { - return type == cVarParser::connectcmd; + return type == cVarParser::connectcmd; } bool cVarParser::IsLengthCmd() { - return type == cVarParser::lengthcmd; + return type == cVarParser::lengthcmd; } |