Commit 593de04f authored by Thomas Eschenbacher's avatar Thomas Eschenbacher

moved escapeForFileName() from saveblocks plugin to Kwave::Parser,

to make it reusable in other plugins
parent 20ec314b
......@@ -18,7 +18,9 @@
#include "config.h"
#include <QChar>
#include <QLatin1Char>
#include <QRegExp>
#include <QString>
#include "libkwave/Parser.h"
......@@ -289,6 +291,25 @@ QString Kwave::Parser::escape(const QString &text)
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
QRegExp 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)
{
......
......@@ -144,6 +144,15 @@ namespace Kwave
*/
static QString escape(const QString &text);
/**
* Escape all parts of a string that might be dangerous for using
* within a file name, like "/" and similar.
*
* @param text a unicode text to escape
* @return escaped string, which should be safe to use in a file name
*/
static QString escapeForFileName(const QString &text);
/**
* Un-escapes all characters in a string previously escaped with
* escape()
......
......@@ -185,7 +185,7 @@ int Kwave::SaveBlocksPlugin::start(QStringList &params)
{
qDebug("SaveBlocksPlugin::start()");
// interprete the parameters
// interpret the parameters
int result = interpreteParameters(params);
if (result) return result;
......@@ -497,25 +497,6 @@ void Kwave::SaveBlocksPlugin::scanBlocksToSave(const QString &base,
}
}
//***************************************************************************
QString Kwave::SaveBlocksPlugin::escapeForFileName(const QString &text)
{
QString result = text;
// convert all kinds of (multiple) whitespaces, including tabs
// and newlines into single spaces
QRegExp rx(_("[\\s\\\t\\\r\\\n]+"));
result.replace(rx, QChar(0x0020));
// NOTE: this escapes any kind of special chars, but not a slash "/"
result = Kwave::Parser::escape(result);
// special handling for slashes "/" -> replace with unicode FRACTION SLASH
result.replace(QChar(0x002F), QChar(0x2044));
return result;
}
//***************************************************************************
QString Kwave::SaveBlocksPlugin::createFileName(const QString &base,
const QString &ext, const QString &pattern,
......@@ -605,7 +586,7 @@ QString Kwave::SaveBlocksPlugin::createFileName(const QString &base,
value = value + _(" ");
}
}
value = escapeForFileName(value);
value = Kwave::Parser::escapeForFileName(value);
}
}
......@@ -623,7 +604,7 @@ QString Kwave::SaveBlocksPlugin::createFileName(const QString &base,
if ((idx >= 0) && (idx < m_block_info.count()))
title = m_block_info[idx].m_title;
if (title.length()) {
title = escapeForFileName(title);
title = Kwave::Parser::escapeForFileName(title);
p.replace(rx_title, title);
}
}
......
......@@ -173,14 +173,6 @@ namespace Kwave
QString createDisplayList(const QStringList &list,
unsigned int max_entries) const;
/**
* Escape all parts of a string that might be dangerous for using
* within a file name, like "/" and similar.
* @param text unescaped text
* @return escaped string, which should be safe to use in a file name
*/
QString escapeForFileName(const QString &text);
private:
/** the URL of the first file (user selection) */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment