swift
Loading...
Searching...
No Matches
simplecommandparser.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include <algorithm>
7
8#include <QList>
9#include <QSet>
10#include <QStringBuilder>
11
12#include "misc/pq/pqstring.h"
13#include "misc/stringutils.h"
14
15using namespace swift::misc::physical_quantities;
16
17namespace swift::misc
18{
19 QList<CSimpleCommandParser::CommandHtmlHelp> CSimpleCommandParser::s_commands;
20 QSet<QString> CSimpleCommandParser::s_registered;
21
23 {
24 this->setCheckedCommandList(knownCommands);
25 }
26
27 void CSimpleCommandParser::parse(const QString &commandLine)
28 {
29 m_knownCommand = false;
30 m_originalLine = commandLine;
31 m_cleanedLine = commandLine.trimmed().simplified();
32 if (!m_cleanedLine.isEmpty())
33 {
34 m_splitParts = m_cleanedLine.split(' ');
35 if (!m_splitParts.isEmpty())
36 {
37 const QString &first = m_splitParts.constFirst();
38 const QString formatted = formatCommand(first);
39 if (isCommand(first))
40 {
41 m_commandPart = formatted;
42 m_knownCommand = m_knownCommands.contains(formatted);
43 }
44 }
45 }
46 }
47
48 const QString &CSimpleCommandParser::part(int index) const
49 {
50 static const QString empty("");
51 if (index < 0 || index >= m_splitParts.size()) return empty;
52 return m_splitParts.at(index);
53 }
54
56 {
57 if (index < 0)
58 {
59 return m_cleanedLine;
60 }
61 const QString p = this->part(index);
62 const int from = index < 1 ? 0 : nthIndexOf(m_cleanedLine, ' ', index, Qt::CaseInsensitive);
63 const int fi = m_cleanedLine.indexOf(p, from, Qt::CaseInsensitive);
64 if (fi < 0)
65 {
66 return {};
67 }
68 return m_originalLine.mid(fi).trimmed();
69 }
70
71 int CSimpleCommandParser::countParts() const { return m_splitParts.count(); }
72
73 bool CSimpleCommandParser::hasPart(int index) const { return index >= 0 && index < this->countParts(); }
74
76 {
77 int c = countParts();
78 return c > 0 ? c - 1 : 0;
79 }
80
81 bool CSimpleCommandParser::isInt(int index) const
82 {
83 const QString p = this->part(index);
84 if (p.isEmpty())
85 {
86 return false;
87 }
88 bool ok = false;
89 p.toInt(&ok); // cppcheck-suppress ignoredReturnValue
90 return ok;
91 }
92
93 bool CSimpleCommandParser::isDouble(int index) const
94 {
95 const QString p = this->part(index);
96 if (p.isEmpty())
97 {
98 return false;
99 }
100 bool ok = false;
102 return ok;
103 }
104
105 int CSimpleCommandParser::toInt(int index, int def) const
106 {
107 const QString p = this->part(index);
108 if (p.isEmpty())
109 {
110 return def;
111 }
112 bool ok = false;
113 int i = p.toInt(&ok);
114 return ok ? i : def;
115 }
116
117 bool CSimpleCommandParser::toBool(int index, bool def) const
118 {
119 const QString p = this->part(index);
120 if (p.isEmpty())
121 {
122 return def;
123 }
124 const bool b = stringToBool(p);
125 return b;
126 }
127
128 double CSimpleCommandParser::toDouble(int index, double def) const
129 {
130 const QString p = this->part(index);
131 if (p.isEmpty())
132 {
133 return def;
134 }
135 bool ok = false;
137 return ok ? d : def;
138 }
139
140 bool CSimpleCommandParser::matchesPart(int index, const QString &toMatch, Qt::CaseSensitivity cs) const
141 {
142 if (toMatch.isEmpty())
143 {
144 return false;
145 }
146 if (!this->hasPart(index))
147 {
148 return false;
149 }
150 const QString p(this->part(index));
151 if (p.isEmpty())
152 {
153 return false;
154 }
155 return (p.length() == toMatch.length() && p.startsWith(toMatch, cs));
156 }
157
159 {
160 for (const CommandHtmlHelp &help : std::as_const(s_commands))
161 {
162 // avoid duplicates
163 if (help.command == command.command)
164 {
165 return;
166 }
167 }
168 s_commands.append(command);
169 }
170
172 {
173 if (s_registered.contains(helpContext))
174 {
175 return true;
176 }
177 s_registered.insert(helpContext);
178 return false;
179 }
180
182 {
183 if (s_commands.isEmpty())
184 {
185 return {};
186 }
187
188 static const QString html("<table style=\"font-size: 8pt; white-space: nowrap;\">\n%1\n</table>");
189 static const QString row("<td>%1</td><td>%2</td>");
190 static const QString rowHeader("<th>%1</th><th>%2</th><th>%3</th><th>%4</th>");
191
192 QString rows;
193 QList<CommandHtmlHelp> cmds(s_commands);
194 std::ranges::sort(cmds, CommandHtmlHelp::less);
195
196 static const QString cmdCol(QString().fill('-', 20));
197 static const QString textCol(QString().fill('-', 40));
198 rows += u"<tr>" % rowHeader.arg("command", "synopsis", "command", "synopsis") % u"</tr>\n" % u"<tr>" %
199 rowHeader.arg(cmdCol, textCol, cmdCol, textCol) % u"</tr>\n";
200
201 for (int i = 0; i < cmds.size(); i++)
202 {
203 CommandHtmlHelp help = cmds[i];
204 rows += u"<tr>" % row.arg(help.command, help.help);
205 i++;
206 if (i < cmds.size())
207 {
208 help = cmds[i];
209 rows += row.arg(help.command, help.help);
210 }
211 else
212 {
213 rows += row.arg("", "");
214 }
215 rows += "</tr>\n";
216 }
217 return html.arg(rows);
218 }
219
220 QString CSimpleCommandParser::removeLeadingDot(const QString &candidate)
221 {
222 if (!candidate.startsWith('.'))
223 {
224 return candidate;
225 }
226 return candidate.right(candidate.length() - 1);
227 }
228
229 QString CSimpleCommandParser::formatCommand(const QString &command)
230 {
231 return removeLeadingDot(command.trimmed().toLower());
232 }
233
234 bool CSimpleCommandParser::isCommand(const QString &candidate) { return candidate.startsWith('.'); }
235
236 bool CSimpleCommandParser::matchesCommand(const QString &checkCommand, const QString &alias1, const QString &alias2)
237 {
238 if (m_commandPart.isEmpty())
239 {
240 return false;
241 }
242 if (!checkCommand.isEmpty() && formatCommand(checkCommand) == m_commandPart)
243 {
244 return true;
245 }
246 if (!alias1.isEmpty() && formatCommand(alias1) == m_commandPart)
247 {
248 return true;
249 }
250 if (!alias2.isEmpty() && formatCommand(alias2) == m_commandPart)
251 {
252 return true;
253 }
254 return false;
255 }
256
257 bool CSimpleCommandParser::commandStartsWith(const QString &startPattern) const
258 {
259 if (m_commandPart.isEmpty())
260 {
261 return false;
262 }
263 return m_commandPart.startsWith(formatCommand(startPattern));
264 }
265
266 bool CSimpleCommandParser::commandEndsWith(const QString &endPattern) const
267 {
268 if (m_commandPart.isEmpty())
269 {
270 return false;
271 }
272 return m_commandPart.endsWith(endPattern);
273 }
274
275 void CSimpleCommandParser::setCheckedCommandList(const QStringList &commands)
276 {
277 for (const QString &c : commands)
278 {
279 m_knownCommands.append(formatCommand(c));
280 }
281 }
282} // namespace swift::misc
static void registerCommand(const CommandHtmlHelp &command)
Register a command.
static QString commandsHtmlHelp()
HTML commands HELP.
int countPartsWithoutCommand() const
Count parts, command excluded.
int toInt(int index, int def=-1) const
Part as integer.
bool isInt(int index) const
Is part an integer?
bool toBool(int index, bool def=false) const
Part as bool.
bool commandStartsWith(const QString &startPattern) const
Command starting with pattern?
CSimpleCommandParser(const QStringList &knownCommands)
Constructor.
bool matchesPart(int index, const QString &toMatch, Qt::CaseSensitivity cs=Qt::CaseInsensitive) const
Matches given part.
bool isDouble(int index) const
Is part a double?
void parse(const QString &commandLine)
Parse.
QString partAndRemainingStringAfter(int index) const
Part and remaing string after.
double toDouble(int index, double def=-1.0) const
Part as double.
bool matchesCommand(const QString &checkCommand, const QString &alias1="", const QString &alias2="")
Is given command current command of command line.
const QString & part(int index) const
Get part, 0 is command itself.
bool hasPart(int index) const
Existing part.
static bool registered(const QString &helpContext)
Help already registered.
bool commandEndsWith(const QString &endPattern) const
Command ending with pattern?
static double parseNumber(const QString &number, bool &success, SeparatorMode mode=SeparatorBestGuess)
Locale aware parsing.
Definition pqstring.cpp:118
Free functions in swift::misc.
SWIFT_MISC_EXPORT qsizetype nthIndexOf(const QString &string, QChar ch, int nth=1, Qt::CaseSensitivity cs=Qt::CaseInsensitive)
nth index of ch
SWIFT_MISC_EXPORT bool stringToBool(const QString &boolString)
Convert string to bool.
void append(QList< T > &&value)
qsizetype size() const const
QString arg(Args &&... args) const const
const QChar at(qsizetype position) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype length() const const
QString right(qsizetype n) &&
QString simplified() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
int toInt(bool *ok, int base) const const
QString toLower() const const
QString trimmed() const const
CaseInsensitive
static bool less(const CommandHtmlHelp &a, const CommandHtmlHelp &b)
Compare by command.