summaryrefslogtreecommitdiff
path: root/src/FbTk/LogicCommands.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/LogicCommands.cc')
-rw-r--r--src/FbTk/LogicCommands.cc93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/FbTk/LogicCommands.cc b/src/FbTk/LogicCommands.cc
index 99b5b53..1c6f238 100644
--- a/src/FbTk/LogicCommands.cc
+++ b/src/FbTk/LogicCommands.cc
@@ -23,8 +23,101 @@
23 23
24#include "LogicCommands.hh" 24#include "LogicCommands.hh"
25 25
26#include "CommandRegistry.hh"
27#include "StringUtil.hh"
28
29using FbTk::StringUtil::removeFirstWhitespace;
30using FbTk::StringUtil::toLower;
31using std::string;
32
26namespace FbTk { 33namespace FbTk {
27 34
35namespace {
36
37template <typename M>
38M *addCommands(M *macro, const string &args, bool trusted) {
39 int pos = 0, err = 0;
40 string cmd;
41
42 while (true) {
43 RefCount<BoolCommand> tmp(0);
44 err = StringUtil::getStringBetween(cmd, args.c_str() + pos,
45 '{', '}', " \t\n", true);
46 pos += err;
47 if (err == 0)
48 break;
49
50 tmp = CommandRegistry::instance().parseBoolLine(cmd, trusted);
51 if (*tmp)
52 macro->add(tmp);
53 }
54
55 if (macro->size() > 0)
56 return macro;
57 delete macro;
58 return 0;
59}
60
61BoolCommand *parseLogicCommand(const string &command, const string &args,
62 bool trusted) {
63 if (command == "not") {
64 BoolCommand *boolcmd =
65 CommandRegistry::instance().parseBoolLine(args, trusted);
66 if (!boolcmd)
67 return 0;
68 RefCount<BoolCommand> ref(boolcmd);
69 return new NotCommand(ref);
70 } else if (command == "and")
71 return addCommands<AndCommand>(new AndCommand(), args, trusted);
72 else if (command == "or")
73 return addCommands<OrCommand>(new OrCommand(), args, trusted);
74 else if (command == "xor")
75 return addCommands<XorCommand>(new XorCommand(), args, trusted);
76 return 0;
77}
78
79REGISTER_BOOLCOMMAND_PARSER(not, parseLogicCommand);
80REGISTER_BOOLCOMMAND_PARSER(and, parseLogicCommand);
81REGISTER_BOOLCOMMAND_PARSER(or, parseLogicCommand);
82REGISTER_BOOLCOMMAND_PARSER(xor, parseLogicCommand);
83
84}; // end anonymous namespace
85
86Command *IfCommand::parse(const std::string &command, const std::string &args,
87 bool trusted) {
88 std::string cmd;
89 int err = 0, pos = 0;
90 RefCount<BoolCommand> cond(0);
91 RefCount<Command> t(0), f(0);
92
93 err = StringUtil::getStringBetween(cmd, args.c_str(),
94 '{', '}', " \t\n", true);
95 if (err > 0)
96 cond = CommandRegistry::instance().parseBoolLine(cmd, trusted);
97 if (err == 0 || *cond == 0)
98 return 0;
99
100 pos = err;
101 err = StringUtil::getStringBetween(cmd, args.c_str() + pos,
102 '{', '}', " \t\n", true);
103 if (err == 0)
104 return 0;
105 t = CommandRegistry::instance().parseLine(cmd, trusted);
106
107 pos += err;
108 err = StringUtil::getStringBetween(cmd, args.c_str() + pos,
109 '{', '}', " \t\n", true);
110 if (err > 0)
111 f = CommandRegistry::instance().parseLine(cmd, trusted);
112 if (err == 0 || *t == 0 && *f == 0)
113 return 0;
114
115 return new IfCommand(cond, t, f);
116}
117
118REGISTER_COMMAND_PARSER(if, IfCommand::parse);
119REGISTER_COMMAND_PARSER(cond, IfCommand::parse);
120
28void OrCommand::add(RefCount<BoolCommand> &com) { 121void OrCommand::add(RefCount<BoolCommand> &com) {
29 m_commandlist.push_back(com); 122 m_commandlist.push_back(com);
30} 123}