1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
/***************************************************************************
Parser.cpp - parser for Kwave's internal commands
-------------------
begin : Sat Feb 3 2001
copyright : (C) 2001 by Thomas Eschenbacher
email : Thomas Eschenbacher <thomas.eschenbacher@gmx.de>
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "config.h"
#include <QChar>
#include <QLatin1Char>
#include <QRegularExpression>
#include <QString>
#include "libkwave/Parser.h"
#include "libkwave/String.h"
#include "libkwave/Utils.h"
//***************************************************************************
Kwave::Parser::Parser (const QString &init)
:m_command(_("")), m_param(), m_current(0), m_commands()
{
QString line = init.trimmed();
unsigned int level = 0;
bool escaped = false;
m_commands = splitCommands(line);
line = m_commands.first();
// --- parse the command ---
qsizetype pos = line.indexOf(QLatin1Char('('));
if (pos >= 0) {
// command present
m_command = line.left(pos).simplified();
line.remove(0, pos+1);
} else {
m_command.clear();
}
// --- parse the list of parameters ---
QString param;
while (line.length()) {
QChar c = line[0];
line.remove(0,1);
// the next character is escaped
if (!escaped && (c.toLatin1() == '\\')) {
escaped = true;
param += c;
continue;
}
// escaped character
if (escaped) {
escaped = false;
param += c;
continue;
}
switch (c.toLatin1()) {
case ',':
if (!level) {
m_param.append(unescape(param.trimmed()));
param.clear();
} else param += c;
break;
case '(':
level++;
param += c;
break;
case ')':
if (!level) {
param = unescape(param.trimmed());
if (param.length())
m_param.append(param);
// break, belongs to command, end of line
line.clear();
}
else
level--;
param += c;
break;
default:
param += c;
}
}
line = line.trimmed();
if (line.length()) {
qWarning("Parser: trailing garbage after command: '%s'", DBG(line));
}
}
//***************************************************************************
Kwave::Parser::~Parser ()
{
}
//***************************************************************************
QStringList Kwave::Parser::splitCommands(QString &line)
{
// split a line into commands
unsigned int level = 0;
QString cmd = _("");
QStringList commands;
bool escaped = false;
while (line.length()) {
QChar c = line[0];
line.remove(0,1);
// the next character is escaped
if (!escaped && (c.toLatin1() == '\\')) {
escaped = true;
cmd += c;
continue;
}
// escaped character
if (escaped) {
escaped = false;
cmd += c;
continue;
}
switch (c.toLatin1()) {
case ';':
if (!level) {
// next command in the list
commands.append(cmd.trimmed());
cmd = _("");
} else cmd += c;
break;
case '(':
level++;
cmd += c;
break;
case ')':
level--;
cmd += c;
break;
default:
cmd += c;
}
}
if (cmd.length()) {
commands.append(cmd.trimmed());
}
return commands;
}
//***************************************************************************
const QString &Kwave::Parser::firstParam()
{
m_current = 0;
return nextParam();
}
//***************************************************************************
const QString &Kwave::Parser::nextParam()
{
static const QString empty = _("");
if (m_current >= count()) return empty;
return m_param[m_current++];
}
//***************************************************************************
void Kwave::Parser::skipParam()
{
nextParam();
}
//***************************************************************************
QStringList Kwave::Parser::remainingParams()
{
QStringList list;
while (!isDone())
list.append(nextParam());
return list;
}
//***************************************************************************
bool Kwave::Parser::toBool()
{
const QString &p = nextParam();
// first test for "true" and "false"
if (p.toLower() == _("true")) return true;
if (p.toLower() == _("false")) return false;
// maybe numeric ?
bool ok;
int value = p.toInt(&ok);
if (ok) return (value != 0);
qWarning("Parser: invalid bool format: '%s'", DBG(p));
return false;
}
//***************************************************************************
int Kwave::Parser::toInt ()
{
const QString &p = nextParam();
bool ok;
int value = p.toInt(&ok);
if (!ok) {
qWarning("Parser: unable to parse int from '%s'", DBG(p));
value = 0;
}
return value;
}
//***************************************************************************
unsigned int Kwave::Parser::toUInt()
{
const QString &p = nextParam();
bool ok;
unsigned int value = p.toUInt(&ok);
if (!ok) {
qWarning("Parser: unable to parse unsigned int from '%s'", DBG(p));
value = 0;
}
return value;
}
//***************************************************************************
sample_index_t Kwave::Parser::toSampleIndex()
{
const QString &p = nextParam();
bool ok;
sample_index_t value = p.toULongLong(&ok);
if (!ok) {
qWarning("Parser: unable to parse unsigned int from '%s'", DBG(p));
value = 0;
}
return value;
}
//***************************************************************************
double Kwave::Parser::toDouble()
{
const QString &p = nextParam();
bool ok;
double value = p.toDouble(&ok);
if (!ok) {
qWarning("Parser: unable to parse double from '%s'", DBG(p));
value = 0.0;
}
return value;
}
//***************************************************************************
QString Kwave::Parser::escape(const QString &text)
{
static const QString special = _(":;<=>?[\\]^`");
QString escaped;
for (QString::ConstIterator it = text.begin(); it != text.end(); ++it) {
const QChar c(*it);
if ((c.toLatin1() < '.') || (c.toLatin1() > 'z') || special.contains(c))
escaped += _("\\");
escaped += c;
}
return escaped;
}
//***************************************************************************
QString Kwave::Parser::escapeForFileName(const QString &text)
{
QString result = text;
// convert all kinds of (multiple) whitespaces, including tabs
// and newlines into single spaces
QRegularExpression rx(_("[\\s\\\t\\\r\\\n]+"));
result.replace(rx, QChar(0x0020));
// NOTE: this escapes any kind of special chars, but not a slash "/"
result = escape(result);
// special handling for slashes "/" -> replace with unicode FRACTION SLASH
result.replace(QChar(0x002F), QChar(0x2044));
return result;
}
//***************************************************************************
QString Kwave::Parser::unescape(const QString &text)
{
QString unescaped;
bool esc = false;
for (QString::ConstIterator it = text.begin(); it != text.end(); ++it) {
const QChar c(*it);
if (!esc && (c.toLatin1() == '\\')) {
// this is the leading escape character -> skip it
esc = true;
continue;
}
esc = false;
unescaped += c;
}
return unescaped;
}
//***************************************************************************
QUrl Kwave::Parser::toUrl(const QString &command)
{
QUrl url;
url.setScheme(Kwave::urlScheme());
Parser parser(command);
// encode the command as "path"
url.setPath(QString::fromLatin1(QUrl::toPercentEncoding(parser.command())));
// encode the parameter list into a comma separated string
unsigned int count = parser.count();
QByteArray params;
for (unsigned int i = 0; i < count; ++i) {
QString param = parser.nextParam();
if (params.length()) params += ',';
params += QUrl::toPercentEncoding(param);
}
url.setQuery(QString::fromLatin1(params));
return url;
}
//***************************************************************************
QString Kwave::Parser::fromUrl(const QUrl &url)
{
if (url.scheme().toLower() != Kwave::urlScheme()) return QString();
// get the command name (path)
QString command = QUrl::fromPercentEncoding(url.path().toLatin1());
// get the parameter list
command += _("(");
QStringList params = url.query().split(_(","));
if (!params.isEmpty()) {
bool first = true;
foreach (const QString ¶m, params) {
if (!first) command += _(",");
command += QUrl::fromPercentEncoding(param.toLatin1());
first = false;
}
}
command += _(")");
return command;
}
//***************************************************************************
//***************************************************************************
|