Revamped clean_file_name() function. Do NOT trim *leading* spaces from file name, as this is allowed (though ugly).

This commit is contained in:
LoRd_MuldeR 2016-12-15 00:26:37 +01:00
parent f9dd32c6e3
commit f5cd39eda3

View File

@ -494,44 +494,40 @@ void MUtils::natural_string_sort(QStringList &list, const bool bIgnoreCase)
// CLEAN FILE PATH // CLEAN FILE PATH
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
static const struct static const char FILENAME_ILLEGAL_CHARS[] = "\\/:*?<>\"";
{
const char *const search;
const char *const replace;
}
CLEAN_FILE_NAME[] =
{
{ "\\", "-" },
{ " / ", ", " },
{ "/", "," },
{ ":", "-" },
{ "*", "x" },
{ "?", "!" },
{ "<", "[" },
{ ">", "]" },
{ "|", "!" },
{ "\"", "'" },
{ NULL, NULL }
};
QString MUtils::clean_file_name(const QString &name) QString MUtils::clean_file_name(const QString &name)
{ {
QRegExp regExp("\"(.+)\""); QString result(name);
regExp.setMinimal(true); if (result.contains(QLatin1Char('"')))
QString str = QString(name).replace(regExp, "``\\1´´").trimmed();
for(size_t i = 0; CLEAN_FILE_NAME[i].search; i++)
{ {
str.replace(CLEAN_FILE_NAME[i].search, CLEAN_FILE_NAME[i].replace); QRegExp quoted("\"(.+)\"");
quoted.setMinimal(true);
result.replace(quoted, "``\\1´´");
} }
while(str.endsWith(QLatin1Char('.'))) for(QString::Iterator iter = result.begin(); iter != result.end(); iter++)
{ {
str.chop(1); if (iter->category() == QChar::Other_Control)
str = str.trimmed(); {
*iter = QLatin1Char('_');
}
} }
return str.trimmed(); for(size_t i = 0; FILENAME_ILLEGAL_CHARS[i]; i++)
{
result.replace(QLatin1Char(FILENAME_ILLEGAL_CHARS[i]), QLatin1Char('_'));
}
const QRegExp spaces("\\s+$");
result.remove(spaces);
while (result.endsWith(QLatin1Char('.')))
{
result.chop(1);
result.remove(spaces);
}
return result;
} }
static QPair<QString,QString> clean_file_path_get_prefix(const QString path) static QPair<QString,QString> clean_file_path_get_prefix(const QString path)