kwave  18.07.70
Kwave::Parser Class Reference

#include <Parser.h>

Collaboration diagram for Kwave::Parser:
Collaboration graph

Public Member Functions

 Parser (const QString &init)
 
virtual ~Parser ()
 
QString command ()
 
const QStringList & params ()
 
bool hasParams ()
 
bool hasMultipleCommands ()
 
QStringList commandList ()
 
bool isDone () const
 
unsigned int count () const
 
const QString & firstParam ()
 
const QString & nextParam ()
 
QStringList remainingParams ()
 
void skipParam ()
 
bool toBool ()
 
int toInt ()
 
unsigned int toUInt ()
 
sample_index_t toSampleIndex ()
 
double toDouble ()
 

Static Public Member Functions

static QString escape (const QString &text)
 
static QString escapeForFileName (const QString &text)
 
static QString unescape (const QString &text)
 
static QUrl toUrl (const QString &command)
 
static QString fromUrl (const QUrl &url)
 

Protected Member Functions

QStringList splitCommands (QString &line)
 

Private Attributes

QString m_command
 
QStringList m_param
 
unsigned int m_current
 
QStringList m_commands
 

Detailed Description

Definition at line 34 of file Parser.h.

Constructor & Destructor Documentation

◆ Parser()

Kwave::Parser::Parser ( const QString &  init)

Constructor. Parses the given string into an optional command part and a list of parameters.

Definition at line 31 of file Parser.cpp.

References DBG, m_command, m_commands, m_param, splitCommands(), and unescape().

32  :m_command(_("")), m_param(), m_current(0), m_commands()
33 {
34  QString line = init.trimmed();
35  unsigned int level = 0;
36  bool escaped = false;
37  int pos;
38 
39  m_commands = splitCommands(line);
40  line = m_commands.first();
41 
42  // --- parse the command ---
43  pos = line.indexOf(QLatin1Char('('));
44  if (pos >= 0) {
45  // command present
46  m_command = line.left(pos).simplified();
47  line.remove(0, pos+1);
48  } else {
49  m_command.clear();
50  }
51 
52  // --- parse the list of parameters ---
53  QString param;
54  while (line.length()) {
55  QChar c = line[0];
56  line.remove(0,1);
57 
58  // the next character is escaped
59  if (!escaped && (c.toLatin1() == '\\')) {
60  escaped = true;
61  param += c;
62  continue;
63  }
64 
65  // escaped character
66  if (escaped) {
67  escaped = false;
68  param += c;
69  continue;
70  }
71 
72  switch (c.toLatin1()) {
73  case ',':
74  if (!level) {
75  m_param.append(unescape(param.trimmed()));
76  param.clear();
77  } else param += c;
78  break;
79 
80  case '(':
81  level++;
82  param += c;
83  break;
84  case ')':
85  if (!level) {
86  param = unescape(param.trimmed());
87  if (param.length())
88  m_param.append(param);
89  // break, belongs to command, end of line
90  line.clear();
91  }
92  else
93  level--;
94  param += c;
95  break;
96  default:
97  param += c;
98  }
99  }
100 
101  line = line.trimmed();
102  if (line.length()) {
103  qWarning("Parser: trailing garbage after command: '%s'", DBG(line));
104  }
105 }
QStringList m_commands
Definition: Parser.h:196
unsigned int m_current
Definition: Parser.h:193
static QString unescape(const QString &text)
Definition: Parser.cpp:314
QStringList splitCommands(QString &line)
Definition: Parser.cpp:113
QStringList m_param
Definition: Parser.h:190
#define _(m)
Definition: memcpy.c:66
#define DBG(qs)
Definition: String.h:55
QString m_command
Definition: Parser.h:187
Here is the call graph for this function:

◆ ~Parser()

Kwave::Parser::~Parser ( )
virtual

Destructor.

Definition at line 108 of file Parser.cpp.

109 {
110 }

Member Function Documentation

◆ command()

QString Kwave::Parser::command ( )
inline

Returns the command part of the line

Definition at line 47 of file Parser.h.

Referenced by Kwave::FileContext::delegateCommand(), Kwave::TopWidget::executeCommand(), Kwave::App::executeCommand(), Kwave::FileContext::executeCommand(), fromUrl(), Kwave::FileContext::parseCommands(), Kwave::MenuItem::specialCommand(), Kwave::MenuNode::specialCommand(), and toUrl().

47  {
48  return m_command;
49  }
QString m_command
Definition: Parser.h:187
Here is the caller graph for this function:

◆ commandList()

QStringList Kwave::Parser::commandList ( )
inline

Returns the list of commands

Definition at line 67 of file Parser.h.

Referenced by Kwave::FileContext::executeCommand().

67 { return m_commands; }
QStringList m_commands
Definition: Parser.h:196
Here is the caller graph for this function:

◆ count()

unsigned int Kwave::Parser::count ( ) const
inline

Returns the number of parameters.

Definition at line 75 of file Parser.h.

References Kwave::toInt().

Referenced by Kwave::FileContext::delegateCommand(), Kwave::FileContext::executeCommand(), nextParam(), and toUrl().

75  {
76  return m_param.count();
77  }
QStringList m_param
Definition: Parser.h:190
Here is the call graph for this function:
Here is the caller graph for this function:

◆ escape()

QString Kwave::Parser::escape ( const QString &  text)
static

Escapes all characters that might be critical when parsing

Parameters
texta unicode text to escape
Returns
text with certain characters escaped

Definition at line 277 of file Parser.cpp.

References _.

Referenced by Kwave::MenuManager::addNumberedMenuEntry(), Kwave::K3BExportPlugin::detectBlockMetaData(), Kwave::TopWidget::dropEvent(), Kwave::SignalView::dropEvent(), Kwave::AsciiEncoder::encode(), escapeForFileName(), Kwave::K3BExportPlugin::saveBlocks(), Kwave::MainWidget::saveLabels(), Kwave::SaveBlocksPlugin::setup(), and Kwave::K3BExportPlugin::setup().

278 {
279  static const QString special = _(":;<=>?[\\]^`");
280  QString escaped;
281 
282  for (QString::ConstIterator it = text.begin(); it != text.end(); ++it) {
283  const QChar c(*it);
284 
285  if ((c.toLatin1() < '.') || (c.toLatin1() > 'z') || special.contains(c))
286  escaped += _("\\");
287 
288  escaped += c;
289  }
290 
291  return escaped;
292 }
#define _(m)
Definition: memcpy.c:66
Here is the caller graph for this function:

◆ escapeForFileName()

QString Kwave::Parser::escapeForFileName ( const QString &  text)
static

Escape all parts of a string that might be dangerous for using within a file name, like "/" and similar.

Parameters
texta unicode text to escape
Returns
escaped string, which should be safe to use in a file name

Definition at line 295 of file Parser.cpp.

References _, and escape().

Referenced by Kwave::SaveBlocksPlugin::createFileName(), and Kwave::K3BExportPlugin::saveBlocks().

296 {
297  QString result = text;
298 
299  // convert all kinds of (multiple) whitespaces, including tabs
300  // and newlines into single spaces
301  QRegExp rx(_("[\\s\\\t\\\r\\\n]+"));
302  result.replace(rx, QChar(0x0020));
303 
304  // NOTE: this escapes any kind of special chars, but not a slash "/"
305  result = escape(result);
306 
307  // special handling for slashes "/" -> replace with unicode FRACTION SLASH
308  result.replace(QChar(0x002F), QChar(0x2044));
309 
310  return result;
311 }
#define _(m)
Definition: memcpy.c:66
static QString escape(const QString &text)
Definition: Parser.cpp:277
Here is the call graph for this function:
Here is the caller graph for this function:

◆ firstParam()

const QString & Kwave::Parser::firstParam ( )

Returns the first parameter and sets the current position to the next.

Definition at line 168 of file Parser.cpp.

References m_current, and nextParam().

Referenced by Kwave::MenuManager::executeCommand(), Kwave::SignalManager::executeCommand(), Kwave::TopWidget::executeCommand(), Kwave::FileContext::executeCommand(), Kwave::Curve::fromCommand(), Kwave::TopWidget::openRecent(), Kwave::FileContext::parseCommands(), Kwave::MenuItem::specialCommand(), and Kwave::MenuNode::specialCommand().

169 {
170  m_current = 0;
171  return nextParam();
172 }
unsigned int m_current
Definition: Parser.h:193
const QString & nextParam()
Definition: Parser.cpp:175
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromUrl()

QString Kwave::Parser::fromUrl ( const QUrl &  url)
static

Converts a kwave:// URL into a string command

Parameters
urla URL using the kwave:// scheme
Returns
a Kwave string command parsed from the URL

Definition at line 360 of file Parser.cpp.

References _, command(), params(), and Kwave::urlScheme().

Referenced by Kwave::TopWidget::loadFile().

361 {
362  if (url.scheme().toLower() != Kwave::urlScheme()) return QString();
363 
364  // get the command name (path)
365  QString command = QUrl::fromPercentEncoding(url.path().toLatin1());
366 
367  // get the parameter list
368  command += _("(");
369  QStringList params = url.query().split(_(","));
370  if (!params.isEmpty()) {
371  bool first = true;
372  foreach (const QString &param, params) {
373  if (!first) command += _(",");
374  command += QUrl::fromPercentEncoding(param.toLatin1());
375  first = false;
376  }
377  }
378  command += _(")");
379 
380  return command;
381 }
#define _(m)
Definition: memcpy.c:66
const QStringList & params()
Definition: Parser.h:52
QString Q_DECL_EXPORT urlScheme()
Definition: Utils.cpp:177
QString command()
Definition: Parser.h:47
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hasMultipleCommands()

bool Kwave::Parser::hasMultipleCommands ( )
inline

Returns true if a list of commands was parsed

Definition at line 62 of file Parser.h.

Referenced by Kwave::FileContext::executeCommand().

62  {
63  return (m_commands.count() > 1);
64  }
QStringList m_commands
Definition: Parser.h:196
Here is the caller graph for this function:

◆ hasParams()

bool Kwave::Parser::hasParams ( )
inline

Returns true if the parameter list is not empty.

Definition at line 57 of file Parser.h.

Referenced by Kwave::App::executeCommand(), and Kwave::MainWidget::executeCommand().

57  {
58  return (m_param.count() != 0);
59  }
QStringList m_param
Definition: Parser.h:190
Here is the caller graph for this function:

◆ isDone()

bool Kwave::Parser::isDone ( ) const
inline

Returns true if the end of the parameter list has been reached.

Definition at line 70 of file Parser.h.

Referenced by Kwave::MainWidget::executeCommand(), Kwave::Curve::fromCommand(), remainingParams(), and Kwave::AmplifyFreePlugin::setup().

70  {
71  return (static_cast<int>(m_current) >= m_param.count());
72  }
unsigned int m_current
Definition: Parser.h:193
QStringList m_param
Definition: Parser.h:190
Here is the caller graph for this function:

◆ nextParam()

const QString & Kwave::Parser::nextParam ( )

Returns the next parameter and increases the current position if not already at the end of the parameter list. If the end of the parameter list has been reached, the return value will be a zero-length string.

Definition at line 175 of file Parser.cpp.

References _, count(), m_current, and m_param.

Referenced by Kwave::MenuManager::executeCommand(), Kwave::SignalManager::executeCommand(), Kwave::TopWidget::executeCommand(), Kwave::MainWidget::executeCommand(), Kwave::FileContext::executeCommand(), Kwave::Filter::Filter(), firstParam(), remainingParams(), Kwave::AmplifyFreePlugin::setup(), skipParam(), Kwave::MenuItem::specialCommand(), Kwave::MenuNode::specialCommand(), toBool(), toDouble(), toInt(), toSampleIndex(), toUInt(), and toUrl().

176 {
177  static const QString empty = _("");
178  if (m_current >= count()) return empty;
179  return m_param[m_current++];
180 }
unsigned int m_current
Definition: Parser.h:193
QStringList m_param
Definition: Parser.h:190
#define _(m)
Definition: memcpy.c:66
unsigned int count() const
Definition: Parser.h:75
Here is the call graph for this function:
Here is the caller graph for this function:

◆ params()

const QStringList& Kwave::Parser::params ( )
inline

Returns the complete list of parameters

Definition at line 52 of file Parser.h.

Referenced by Kwave::App::executeCommand(), fromUrl(), and toUrl().

52  {
53  return m_param;
54  }
QStringList m_param
Definition: Parser.h:190
Here is the caller graph for this function:

◆ remainingParams()

QStringList Kwave::Parser::remainingParams ( )

Returns all remaining parameters in a string list

Definition at line 189 of file Parser.cpp.

References isDone(), and nextParam().

Referenced by Kwave::FileContext::delegateCommand(), and Kwave::FileContext::executeCommand().

190 {
191  QStringList list;
192  while (!isDone())
193  list.append(nextParam());
194  return list;
195 }
bool isDone() const
Definition: Parser.h:70
const QString & nextParam()
Definition: Parser.cpp:175
Here is the call graph for this function:
Here is the caller graph for this function:

◆ skipParam()

void Kwave::Parser::skipParam ( )

Skips a parameter and advances the current position by one if the end has not already been reached.

Definition at line 183 of file Parser.cpp.

References nextParam().

184 {
185  nextParam();
186 }
const QString & nextParam()
Definition: Parser.cpp:175
Here is the call graph for this function:

◆ splitCommands()

QStringList Kwave::Parser::splitCommands ( QString &  line)
protected

Splits a line into a list of commands

Definition at line 113 of file Parser.cpp.

References _.

Referenced by Parser().

114 {
115  // split a line into commands
116  unsigned int level = 0;
117  QString cmd = _("");
118  QStringList commands;
119  bool escaped = false;
120 
121  while (line.length()) {
122  QChar c = line[0];
123  line.remove(0,1);
124 
125  // the next character is escaped
126  if (!escaped && (c.toLatin1() == '\\')) {
127  escaped = true;
128  cmd += c;
129  continue;
130  }
131 
132  // escaped character
133  if (escaped) {
134  escaped = false;
135  cmd += c;
136  continue;
137  }
138 
139  switch (c.toLatin1()) {
140  case ';':
141  if (!level) {
142  // next command in the list
143  commands.append(cmd.trimmed());
144  cmd = _("");
145  } else cmd += c;
146  break;
147  case '(':
148  level++;
149  cmd += c;
150  break;
151  case ')':
152  level--;
153  cmd += c;
154  break;
155  default:
156  cmd += c;
157  }
158  }
159 
160  if (cmd.length()) {
161  commands.append(cmd.trimmed());
162  }
163 
164  return commands;
165 }
#define _(m)
Definition: memcpy.c:66
Here is the caller graph for this function:

◆ toBool()

bool Kwave::Parser::toBool ( )

Gets the next parameter through calling nextParam() and interpretes it as a "bool" value. It will recognize the strings "true" and "false" (not case-sensitive) and numeric values (true means not zero). On errors the return value will be false.

Definition at line 198 of file Parser.cpp.

References _, DBG, and nextParam().

199 {
200  const QString &p = nextParam();
201 
202  // first test for "true" and "false"
203  if (p.toLower() == _("true")) return true;
204  if (p.toLower() == _("false")) return false;
205 
206  // maybe numeric ?
207  bool ok;
208  int value = p.toInt(&ok);
209  if (ok) return (value != 0);
210 
211  qWarning("Parser: invalid bool format: '%s'", DBG(p));
212  return false;
213 }
#define _(m)
Definition: memcpy.c:66
#define DBG(qs)
Definition: String.h:55
const QString & nextParam()
Definition: Parser.cpp:175
Here is the call graph for this function:

◆ toDouble()

double Kwave::Parser::toDouble ( )

Gets the next parameter through calling nextParam() and interpretes it as a "double" value. On errors the return value will be zero.

Definition at line 262 of file Parser.cpp.

References DBG, and nextParam().

Referenced by Kwave::TopWidget::executeCommand(), Kwave::Filter::Filter(), and Kwave::Curve::fromCommand().

263 {
264  const QString &p = nextParam();
265  bool ok;
266  double value = p.toDouble(&ok);
267 
268  if (!ok) {
269  qWarning("Parser: unable to parse double from '%s'", DBG(p));
270  value = 0.0;
271  }
272 
273  return value;
274 }
#define DBG(qs)
Definition: String.h:55
const QString & nextParam()
Definition: Parser.cpp:175
Here is the call graph for this function:
Here is the caller graph for this function:

◆ toInt()

int Kwave::Parser::toInt ( )

Gets the next parameter through calling nextParam() and interpretes it as an "int" value. On errors the return value will be zero.

Definition at line 216 of file Parser.cpp.

References DBG, and nextParam().

Referenced by Kwave::SignalManager::executeCommand(), Kwave::MainWidget::executeCommand(), and Kwave::Filter::Filter().

217 {
218  const QString &p = nextParam();
219  bool ok;
220  int value = p.toInt(&ok);
221 
222  if (!ok) {
223  qWarning("Parser: unable to parse int from '%s'", DBG(p));
224  value = 0;
225  }
226 
227  return value;
228 }
#define DBG(qs)
Definition: String.h:55
const QString & nextParam()
Definition: Parser.cpp:175
Here is the call graph for this function:
Here is the caller graph for this function:

◆ toSampleIndex()

sample_index_t Kwave::Parser::toSampleIndex ( )

Gets the next parameter through calling nextParam() and interpretes it as a "sample_index_t" value. On errors the return value will be zero.

Definition at line 246 of file Parser.cpp.

References DBG, and nextParam().

Referenced by Kwave::SignalManager::executeCommand(), Kwave::TopWidget::executeCommand(), and Kwave::MainWidget::executeCommand().

247 {
248  const QString &p = nextParam();
249  bool ok;
250  sample_index_t value = p.toULongLong(&ok);
251 
252  if (!ok) {
253  qWarning("Parser: unable to parse unsigned int from '%s'", DBG(p));
254  value = 0;
255  }
256 
257  return value;
258 }
quint64 sample_index_t
Definition: Sample.h:28
#define DBG(qs)
Definition: String.h:55
const QString & nextParam()
Definition: Parser.cpp:175
Here is the call graph for this function:
Here is the caller graph for this function:

◆ toUInt()

unsigned int Kwave::Parser::toUInt ( )

Gets the next parameter through calling nextParam() and interpretes it as an "unsigned int" value. On errors the return value will be zero.

Definition at line 231 of file Parser.cpp.

References DBG, and nextParam().

Referenced by Kwave::SignalManager::executeCommand(), and Kwave::TopWidget::executeCommand().

232 {
233  const QString &p = nextParam();
234  bool ok;
235  unsigned int value = p.toUInt(&ok);
236 
237  if (!ok) {
238  qWarning("Parser: unable to parse unsigned int from '%s'", DBG(p));
239  value = 0;
240  }
241 
242  return value;
243 }
#define DBG(qs)
Definition: String.h:55
const QString & nextParam()
Definition: Parser.cpp:175
Here is the call graph for this function:
Here is the caller graph for this function:

◆ toUrl()

QUrl Kwave::Parser::toUrl ( const QString &  command)
static

Converts a string command into a URL suitable for passing to Kwave as command line parameter

Parameters
commandthe string command to encode as URL
Returns
a QUrl with the kwave:// scheme

Definition at line 336 of file Parser.cpp.

References command(), count(), nextParam(), params(), and Kwave::urlScheme().

Referenced by Kwave::TopWidget::newSignal().

337 {
338  QUrl url;
339 
340  url.setScheme(Kwave::urlScheme());
341  Parser parser(command);
342 
343  // encode the command as "path"
344  url.setPath(QString::fromLatin1(QUrl::toPercentEncoding(parser.command())));
345 
346  // encode the parameter list into a comma separated string
347  unsigned int count = parser.count();
348  QByteArray params;
349  for (unsigned int i = 0; i < count; ++i) {
350  QString param = parser.nextParam();
351  if (params.length()) params += ',';
352  params += QUrl::toPercentEncoding(param);
353  }
354  url.setQuery(QString::fromLatin1(params));
355 
356  return url;
357 }
Parser(const QString &init)
Definition: Parser.cpp:31
const QStringList & params()
Definition: Parser.h:52
QString Q_DECL_EXPORT urlScheme()
Definition: Utils.cpp:177
unsigned int count() const
Definition: Parser.h:75
QString command()
Definition: Parser.h:47
Here is the call graph for this function:
Here is the caller graph for this function:

◆ unescape()

QString Kwave::Parser::unescape ( const QString &  text)
static

Un-escapes all characters in a string previously escaped with escape()

Parameters
texta unicode text to un-escape
Returns
the original text without escaped characters

Definition at line 314 of file Parser.cpp.

Referenced by Kwave::SaveBlocksPlugin::interpreteParameters(), Kwave::K3BExportPlugin::interpreteParameters(), Kwave::AsciiDecoder::open(), Parser(), Kwave::SaveBlocksPlugin::start(), and Kwave::SaveBlocksPlugin::updateExample().

315 {
316  QString unescaped;
317 
318  bool esc = false;
319  for (QString::ConstIterator it = text.begin(); it != text.end(); ++it) {
320  const QChar c(*it);
321 
322  if (!esc && (c.toLatin1() == '\\')) {
323  // this is the leading escape character -> skip it
324  esc = true;
325  continue;
326  }
327 
328  esc = false;
329  unescaped += c;
330  }
331 
332  return unescaped;
333 }
Here is the caller graph for this function:

Member Data Documentation

◆ m_command

QString Kwave::Parser::m_command
private

the command part of the line

Definition at line 187 of file Parser.h.

Referenced by Parser().

◆ m_commands

QStringList Kwave::Parser::m_commands
private

if it has multiple commands, the list of command strings

Definition at line 196 of file Parser.h.

Referenced by Parser().

◆ m_current

unsigned int Kwave::Parser::m_current
private

number of the "current" parameter

Definition at line 193 of file Parser.h.

Referenced by firstParam(), and nextParam().

◆ m_param

QStringList Kwave::Parser::m_param
private

list of parsed parameters

Definition at line 190 of file Parser.h.

Referenced by nextParam(), and Parser().


The documentation for this class was generated from the following files: