diff options
Diffstat (limited to 'libutil')
-rw-r--r-- | libutil/include/pathutils.h | 4 | ||||
-rw-r--r-- | libutil/src/pathutils.cpp | 30 |
2 files changed, 34 insertions, 0 deletions
diff --git a/libutil/include/pathutils.h b/libutil/include/pathutils.h index be6c8917..c04330a9 100644 --- a/libutil/include/pathutils.h +++ b/libutil/include/pathutils.h @@ -25,4 +25,8 @@ LIBUTIL_EXPORT QString PathCombine(QString path1, QString path2, QString path3); LIBUTIL_EXPORT QString AbsolutePath(QString path); +LIBUTIL_EXPORT QString RemoveInvalidFilenameChars(QString string, QChar replaceWith = '-'); + +LIBUTIL_EXPORT QString DirNameFromString(QString string, QString inDir = "."); + #endif // PATHUTILS_H diff --git a/libutil/src/pathutils.cpp b/libutil/src/pathutils.cpp index 8e91bf31..7ba7397c 100644 --- a/libutil/src/pathutils.cpp +++ b/libutil/src/pathutils.cpp @@ -35,3 +35,33 @@ QString AbsolutePath(QString path) { return QFileInfo(path).absolutePath(); } + +QString badFilenameChars = "\"\\/?<>:*|!"; + +QString RemoveInvalidFilenameChars(QString string, QChar replaceWith) +{ + for (int i = 0; i < string.length(); i++) + { + if (badFilenameChars.contains(string[i])) + { + string[i] = replaceWith; + } + } + return string; +} + +QString DirNameFromString(QString string, QString inDir) +{ + int num = 0; + QString dirName = RemoveInvalidFilenameChars(string, '-'); + while (QFileInfo(PathCombine(inDir, dirName)).exists()) + { + num++; + dirName = RemoveInvalidFilenameChars(dirName, '-') + QString::number(num); + + // If it's over 9000 + if (num > 9000) + return ""; + } + return dirName; +} |