summaryrefslogtreecommitdiffstats
path: root/tests/hawd/module.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/hawd/module.cpp')
-rw-r--r--tests/hawd/module.cpp170
1 files changed, 170 insertions, 0 deletions
diff --git a/tests/hawd/module.cpp b/tests/hawd/module.cpp
new file mode 100644
index 0000000..c020e06
--- /dev/null
+++ b/tests/hawd/module.cpp
@@ -0,0 +1,170 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "module.h"
21
22#include "modules/list.h"
23
24#include <QCoreApplication>
25
26#include <iostream>
27
28namespace HAWD
29{
30
31QVector<Module> Module::s_modules;
32
33Module::Syntax::Syntax()
34{
35}
36
37Module::Syntax::Syntax(const QString &k, std::function<bool(const QStringList &, State &)> l, bool e)
38 : keyword(k),
39 lambda(l),
40 eventDriven(e)
41{
42}
43
44Module::Module()
45{
46}
47
48void Module::loadModules()
49{
50 addModule(List());
51}
52
53void Module::printCommands()
54{
55 for (const Module &module: s_modules) {
56 printSyntax(1, module.syntax(), module.description());
57 }
58}
59
60void Module::printSyntax(uint indent, const Syntax &syntax, const QString &description)
61{
62 const std::string indentation(indent, '\t');
63 std::cout << indentation;
64
65 if (indent < 2) {
66 std::cout << "hawd ";
67 }
68
69 std::cout << syntax.keyword.toStdString();
70
71 if (!description.isEmpty()) {
72 std::cout << ": " << description.toStdString();
73 }
74
75 std::cout << std::endl;
76
77 for (const Syntax &child: syntax.children) {
78 printSyntax(indent + 1, child);
79 }
80}
81
82void Module::addModule(const Module &module)
83{
84 s_modules.append(module);
85}
86
87QVector<Module> Module::modules()
88{
89 return s_modules;
90}
91
92bool Module::match(const QStringList &commands, State &state)
93{
94 for (const Module &module: s_modules) {
95 if (module.matches(commands, state)) {
96 return true;
97 }
98 }
99
100 return false;
101}
102
103Module::Syntax Module::syntax() const
104{
105 return m_syntax;
106}
107
108void Module::setSyntax(const Syntax &syntax)
109{
110 m_syntax = syntax;
111}
112
113QString Module::description() const
114{
115 return m_description;
116}
117
118void Module::setDescription(const QString &description)
119{
120 m_description = description;
121}
122
123bool Module::matches(const QStringList &commands, State &state) const
124{
125 if (commands.isEmpty()) {
126 return false;
127 }
128
129 QStringListIterator commandIt(commands);
130
131 if (commandIt.next() != m_syntax.keyword) {
132 return false;
133 }
134
135 QListIterator<Syntax> syntaxIt(m_syntax.children);
136 const Syntax *syntax = &m_syntax;
137 QStringList tailCommands;
138 while (commandIt.hasNext() && syntaxIt.hasNext()) {
139 const QString word = commandIt.next();
140 while (syntaxIt.hasNext()) {
141 const Syntax &child = syntaxIt.next();
142 if (word == child.keyword) {
143 syntax = &child;
144 syntaxIt = child.children;
145 }
146 }
147
148 if (!syntaxIt.hasNext()) {
149 tailCommands << word;
150 break;
151 }
152 }
153
154 if (syntax && syntax->lambda) {
155 while (commandIt.hasNext()) {
156 tailCommands << commandIt.next();
157 }
158
159 bool rv = syntax->lambda(tailCommands, state);
160 if (rv && syntax->eventDriven) {
161 return QCoreApplication::instance()->exec();
162 }
163
164 return rv;
165 }
166
167 return false;
168}
169
170} // namespace HAWD