improve invalid path name

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97
2024-05-30 13:49:23 +03:00
parent 428889a68d
commit a7a1b28765
8 changed files with 82 additions and 20 deletions

View File

@ -74,6 +74,7 @@
#include <objbase.h>
#include <shlobj.h>
#else
#include <sys/statvfs.h>
#include <utime.h>
#endif
@ -801,25 +802,97 @@ QString NormalizePath(QString path)
}
}
static const QString BAD_PATH_CHARS = "\"?<>:;*|!\r\n";
static const QString BAD_FILENAME_CHARS = BAD_PATH_CHARS + "\\/";
QString removeDuplicates(QString a)
{
auto b = a.split("");
b.removeDuplicates();
return b.join("");
}
QString getFileSystemType(const QString& path)
{
QString fileSystemType;
#ifdef Q_OS_WIN
wchar_t volume[MAX_PATH + 1] = { 0 };
if (GetVolumeInformationW((LPCWSTR)path.utf16(), nullptr, 0, nullptr, nullptr, nullptr, volume, MAX_PATH)) {
fileSystemType = QString::fromWCharArray(volume);
}
#elif defined(Q_OS_UNIX)
struct statvfs buf;
if (statvfs(path.toUtf8().constData(), &buf) == 0) {
switch (buf.f_type) {
case 0x4d44: // "MSDOS"
fileSystemType = "FAT32";
break;
case 0x5346544e: // "NTFS"
fileSystemType = "NTFS";
break;
case 0x4244: // "HFS+" or "H+" on some systems
case 0x482b: // "HFS+" or "H+" on some systems
fileSystemType = "HFS+";
break;
case 0x41465342: // "APFS"
fileSystemType = "APFS";
break;
case 0x65735546: // "exFAT"
fileSystemType = "exFAT";
break;
default:
break;
}
}
#endif
return fileSystemType;
}
static const QString BAD_WIN_CHARS = "\"?<>:*|\r\n";
static const QString BAD_FAT32_CHARS = "<>:\"|?*+.,;=[]!";
static const QString BAD_NTFS_CHARS = "<>:\"|?*";
static const QString BAD_HFS_CHARS = ":";
static const QString BAD_EXFAT_CHARS = "<>:\"|?*";
static const QString BAD_FILENAME_CHARS =
removeDuplicates(BAD_WIN_CHARS + BAD_FAT32_CHARS + BAD_NTFS_CHARS + BAD_HFS_CHARS + BAD_EXFAT_CHARS) + "\\/";
QString RemoveInvalidFilenameChars(QString string, QChar replaceWith)
{
for (int i = 0; i < string.length(); i++)
if (string.at(i) < ' ' || BAD_FILENAME_CHARS.contains(string.at(i)))
string[i] = replaceWith;
return string;
}
QString RemoveInvalidPathChars(QString string, QChar replaceWith)
QString RemoveInvalidPathChars(QString path, QChar replaceWith)
{
for (int i = 0; i < string.length(); i++)
if (string.at(i) < ' ' || BAD_PATH_CHARS.contains(string.at(i)))
string[i] = replaceWith;
QString invalidChars;
#ifdef Q_OS_WIN
invalidChars = BAD_WIN_CHARS;
#endif
return string;
QString fileSystemType = getFileSystemType(QFileInfo(path).absolutePath());
if (fileSystemType == "FAT32") {
invalidChars += BAD_FAT32_CHARS;
} else if (fileSystemType == "NTFS") {
invalidChars += BAD_NTFS_CHARS;
} else if (fileSystemType == "HFS+" || fileSystemType == "APFS") {
invalidChars += BAD_HFS_CHARS;
} else if (fileSystemType == "exFAT") {
invalidChars += BAD_EXFAT_CHARS;
}
if (invalidChars.size() != 0) {
for (int i = 0; i < path.length(); i++) {
if (path.at(i) < ' ' || invalidChars.contains(path.at(i))) {
path[i] = replaceWith;
}
}
}
return path;
}
QString DirNameFromString(QString string, QString inDir)