Commit da0813eb authored by Oliver Heidmann's avatar Oliver Heidmann
Browse files

added error handling to cdo_apply, fixed unhandled errors

parent 67cfd640
......@@ -2,27 +2,39 @@
#include <iostream>
#include "modules.h"
#include "cdo_output.h"
#include "cdo_apply.h"
static int errState = 0;
static ApplyStatus errState = ApplyStatus::OK;
static std::string
parseArg(const std::string &oper)
{
if (oper == "")
{
printf("cdo apply: no operator given for apply.\n");
errState = -1;
fprintf(stderr, "cdo apply: no operator given for apply.\n");
errState = ApplyStatus::MISSING_ARG;
}
const auto mod = getModule(get_original(oper.c_str()));
if (mod.streamInCnt <= 0)
if (mod.streamInCnt != 1)
{
printf("cdo apply: operator %s can not be used with apply.\n", oper.c_str());
if (mod.streamInCnt == -1) printf(" %s has variable input.\n", oper.c_str());
if (mod.streamInCnt == 0) printf(" %s has no input.\n", oper.c_str());
fprintf(stderr, "cdo apply: operator %s can not be used with apply.\n", oper.c_str());
if (mod.streamInCnt == -1)
{
fprintf(stderr, " %s has variable input.\n", oper.c_str());
errState = ApplyStatus::ARG_VARIABLE_INPUT;
}
errState = -1;
if (mod.streamInCnt == 0)
{
fprintf(stderr, " %s has no input.\n", oper.c_str());
errState = ApplyStatus::ARG_NO_INPUT;
}
}
if (mod.streamInCnt > 1)
{
fprintf(stderr, " %s has more than one output.\n", oper.c_str());
errState = ApplyStatus::ARG_TOO_MANY_OUT;
}
return oper;
......@@ -35,7 +47,7 @@ generateTokens(const std::string &p_oper)
auto end = p_oper.find(' ');
auto start = 0;
while (end != std::string::npos)
while (end != std::string::npos && errState == ApplyStatus::OK)
{
auto oper = p_oper.substr(start, end - start);
if (oper[0] == '-') oper = parseArg(oper);
......@@ -60,15 +72,19 @@ expand(std::vector<std::string> p_oper, std::vector<std::string> &p_result, std:
auto currentArg = (*(argvIter));
if (currentArg[0] == '[')
{
printf("cdo apply: brackets not allowed for command apply.\n");
errState = -1;
fprintf(stderr, "cdo apply: brackets not allowed for command apply.\n");
errState = ApplyStatus::BRACKET_USED;
return argvIter;
}
if (currentArg[0] == '-' && false)
if (currentArg[0] == '-')
{
printf("cdo apply: operators not allowed for command apply.\n");
errState = -1;
return argvIter;
const auto mod = getModule(get_original(currentArg.c_str()));
if (mod.streamInCnt > 0)
{
fprintf(stderr, "cdo apply: operators with inputs are not allowed for command apply.\n");
errState = ApplyStatus::OPER_WITH_INPUT_USED;
return argvIter;
}
}
for (auto &oper : p_oper)
{
......@@ -78,10 +94,10 @@ expand(std::vector<std::string> p_oper, std::vector<std::string> &p_result, std:
++argvIter;
}
if ((*(argvIter))[0] != ']')
if (argvIter == p_argv.end() || (*(argvIter))[0] != ']')
{
printf("cdo apply: missing closing bracket.\n");
errState = -1;
fprintf(stderr, "cdo apply: missing closing bracket.\n");
errState = ApplyStatus::MISSING_CLOSING_BRACKET;
}
return argvIter;
......@@ -90,19 +106,18 @@ expand(std::vector<std::string> p_oper, std::vector<std::string> &p_result, std:
static std::vector<std::string>
scan(std::vector<std::string> p_argv)
{
bool firstIsNotApply = false;
std::vector<std::string> newArgv;
for (auto argvIter = p_argv.begin(); argvIter < p_argv.end(); argvIter++)
std::vector<std::string> newArgv = {};
if (p_argv[0].compare(0, strlen("-apply,"), "-apply,") == 0)
{
fprintf(stderr, "cdo apply: apply can not be first.\n");
errState = ApplyStatus::APPLY_USED_FIRST;
return p_argv;
}
for (auto argvIter = p_argv.begin(); argvIter < p_argv.end() && errState == ApplyStatus::OK; argvIter++)
{
std::string currentArgv = *argvIter;
if (currentArgv.compare(0, 7, "-apply,") == 0)
if (currentArgv.compare(0, 6, "-apply") == 0)
{
if (firstIsNotApply == false)
{
printf("cdo apply: apply can not be first.\n");
errState = -1;
break;
}
++argvIter;
if ((*(argvIter))[0] == '[')
{
......@@ -110,39 +125,39 @@ scan(std::vector<std::string> p_argv)
if (pos != std::string::npos)
{
auto parameter = currentArgv.substr(pos + 1);
// std::cout << parameter << std::endl;
if (parameter.empty())
{
printf("cdo apply: missing argument for apply.\n");
errState = -1;
fprintf(stderr, "cdo apply: missing argument for apply.\n");
errState = ApplyStatus::MISSING_ARG;
break;
}
else if (parameter[0] != '-')
{
printf("cdo apply: argument '%s' must start with minus sign (-%s).\n", parameter.c_str(), parameter.c_str());
errState = -1;
}
else
{
auto tokens = generateTokens(parameter);
argvIter = expand(tokens, newArgv, p_argv, ++argvIter);
fprintf(stderr, "missing pipe symbol in apply argument.\n");
errState = ApplyStatus::MISSING_PIPE_SYM;
break;
}
auto tokens = generateTokens(parameter);
if (errState != ApplyStatus::OK) break;
argvIter = expand(tokens, newArgv, p_argv, ++argvIter);
if (errState != ApplyStatus::OK) break;
}
else
{
printf("cdo apply: missing argument for apply.\n");
errState = -1;
fprintf(stderr, "cdo apply: missing argument for apply.\n");
errState = ApplyStatus::MISSING_ARG;
break;
}
}
else
{
printf("cdo apply: Missing bracket after apply, apply can only be used with [].\n");
errState = -1;
fprintf(stderr, "cdo apply: Missing bracket after apply, apply can only be used with [].\n");
errState = ApplyStatus::MISSING_BRACKET;
}
}
else
{
newArgv.push_back(*argvIter);
firstIsNotApply = true;
}
}
......@@ -150,9 +165,10 @@ scan(std::vector<std::string> p_argv)
}
std::vector<std::string>
expandApply(const std::vector<std::string> &p_argv)
expandApply(const std::vector<std::string> &p_argv, ApplyStatus &expandSuccess)
{
errState = ApplyStatus::OK;
auto result = scan(p_argv);
if (errState == -1) exit(EXIT_FAILURE);
expandSuccess = errState;
return result;
}
......@@ -23,7 +23,21 @@
* operators with number of inputs = 1 (no others)
* operators with arguments: written as -apply,-OPERNAME,arguments
*/
enum class ApplyStatus : int
{
OK = 0,
ARG_TOO_MANY_OUT = 1,
ARG_NO_INPUT = 2,
ARG_VARIABLE_INPUT = 3,
BRACKET_USED = 4,
OPER_WITH_INPUT_USED = 5,
MISSING_CLOSING_BRACKET = 6,
APPLY_USED_FIRST = 7,
MISSING_ARG = 8,
MISSING_BRACKET = 9,
MISSING_PIPE_SYM = 10
};
std::vector<std::string> expandApply(const std::vector<std::string> &p_argv);
std::vector<std::string> expandApply(const std::vector<std::string> &p_argv, ApplyStatus &p_status);
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment