LP-92 Remove Feed Forward : flight / ground
[librepilot.git] / ground / gcs / src / libs / extensionsystem / optionsparser.cpp
blobaa01196bc446fe26d915651abf1b280ace99f39a
1 /**
2 ******************************************************************************
4 * @file optionsparser.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
7 * @brief
8 * @see The GNU Public License (GPL) Version 3
9 * @defgroup
10 * @{
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "optionsparser.h"
31 #include <QtCore/QCoreApplication>
33 using namespace ExtensionSystem;
34 using namespace ExtensionSystem::Internal;
36 static const char *END_OF_OPTIONS = "--";
37 const char *OptionsParser::NO_LOAD_OPTION = "-noload";
38 const char *OptionsParser::TEST_OPTION = "-test";
40 OptionsParser::OptionsParser(const QStringList &args,
41 const QMap<QString, bool> &appOptions,
42 QMap<QString, QString> *foundAppOptions,
43 QString *errorString,
44 PluginManagerPrivate *pmPrivate)
45 : m_args(args), m_appOptions(appOptions),
46 m_foundAppOptions(foundAppOptions),
47 m_errorString(errorString),
48 m_pmPrivate(pmPrivate),
49 m_it(m_args.constBegin()),
50 m_end(m_args.constEnd()),
51 m_isDependencyRefreshNeeded(false),
52 m_hasError(false)
54 ++m_it; // jump over program name
55 if (m_errorString) {
56 m_errorString->clear();
58 if (m_foundAppOptions) {
59 m_foundAppOptions->clear();
61 m_pmPrivate->arguments.clear();
64 bool OptionsParser::parse()
66 while (!m_hasError) {
67 if (!nextToken()) { // move forward
68 break;
70 if (checkForEndOfOptions()) {
71 break;
73 if (checkForNoLoadOption()) {
74 continue;
76 if (checkForTestOption()) {
77 continue;
79 if (checkForAppOption()) {
80 continue;
82 if (checkForPluginOption()) {
83 continue;
85 if (checkForUnknownOption()) {
86 break;
88 // probably a file or something
89 m_pmPrivate->arguments << m_currentArg;
91 if (m_isDependencyRefreshNeeded) {
92 m_pmPrivate->resolveDependencies();
94 return !m_hasError;
97 bool OptionsParser::checkForEndOfOptions()
99 if (m_currentArg != QLatin1String(END_OF_OPTIONS)) {
100 return false;
102 while (nextToken()) {
103 m_pmPrivate->arguments << m_currentArg;
105 return true;
108 bool OptionsParser::checkForTestOption()
110 if (m_currentArg != QLatin1String(TEST_OPTION)) {
111 return false;
113 if (nextToken(RequiredToken)) {
114 PluginSpec *spec = m_pmPrivate->pluginByName(m_currentArg);
115 if (!spec) {
116 if (m_errorString) {
117 *m_errorString = QCoreApplication::translate("PluginManager",
118 "The plugin '%1' does not exist.").arg(m_currentArg);
120 m_hasError = true;
121 } else {
122 m_pmPrivate->testSpecs.append(spec);
125 return true;
128 bool OptionsParser::checkForNoLoadOption()
130 if (m_currentArg != QLatin1String(NO_LOAD_OPTION)) {
131 return false;
133 if (nextToken(RequiredToken)) {
134 PluginSpec *spec = m_pmPrivate->pluginByName(m_currentArg);
135 if (!spec) {
136 if (m_errorString) {
137 *m_errorString = QCoreApplication::translate("PluginManager",
138 "The plugin '%1' does not exist.").arg(m_currentArg);
140 m_hasError = true;
141 } else {
142 m_pmPrivate->pluginSpecs.removeAll(spec);
143 delete spec;
144 m_isDependencyRefreshNeeded = true;
147 return true;
150 bool OptionsParser::checkForAppOption()
152 if (!m_appOptions.contains(m_currentArg)) {
153 return false;
155 QString option = m_currentArg;
156 QString argument;
157 if (m_appOptions.value(m_currentArg) && nextToken(RequiredToken)) {
158 // argument required
159 argument = m_currentArg;
161 if (m_foundAppOptions) {
162 m_foundAppOptions->insert(option, argument);
164 return true;
167 bool OptionsParser::checkForPluginOption()
169 bool requiresParameter;
170 PluginSpec *spec = m_pmPrivate->pluginForOption(m_currentArg, &requiresParameter);
172 if (!spec) {
173 return false;
175 spec->addArgument(m_currentArg);
176 if (requiresParameter && nextToken(RequiredToken)) {
177 spec->addArgument(m_currentArg);
179 return true;
182 bool OptionsParser::checkForUnknownOption()
184 if (!m_currentArg.startsWith(QLatin1Char('-'))) {
185 return false;
187 if (m_errorString) {
188 *m_errorString = QCoreApplication::translate("PluginManager",
189 "Unknown option %1").arg(m_currentArg);
191 m_hasError = true;
192 return true;
195 bool OptionsParser::nextToken(OptionsParser::TokenType type)
197 if (m_it == m_end) {
198 if (type == OptionsParser::RequiredToken) {
199 m_hasError = true;
200 if (m_errorString) {
201 *m_errorString = QCoreApplication::translate("PluginManager",
202 "The option %1 requires an argument.").arg(m_currentArg);
205 return false;
207 m_currentArg = *m_it;
208 ++m_it;
209 return true;