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
///////////////////////////////////////////////////////////////////////////////
static const struct
{
const char *const search;
const char *const replace;
}
CLEAN_FILE_NAME[] =
{
{ "\\", "-" },
{ " / ", ", " },
{ "/", "," },
{ ":", "-" },
{ "*", "x" },
{ "?", "!" },
{ "<", "[" },
{ ">", "]" },
{ "|", "!" },
{ "\"", "'" },
{ NULL, NULL }
};
static const char FILENAME_ILLEGAL_CHARS[] = "\\/:*?<>\"";
QString MUtils::clean_file_name(const QString &name)
{
QRegExp regExp("\"(.+)\"");
regExp.setMinimal(true);
QString str = QString(name).replace(regExp, "``\\1´´").trimmed();
for(size_t i = 0; CLEAN_FILE_NAME[i].search; i++)
QString result(name);
if (result.contains(QLatin1Char('"')))
{
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);
str = str.trimmed();
if (iter->category() == QChar::Other_Control)
{
*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)