Ref T455, utility file functions

This commit is contained in:
Klaus Basan
2018-12-06 23:35:07 +01:00
parent 136d161bf0
commit ca3345bbe0
2 changed files with 49 additions and 0 deletions

View File

@@ -20,6 +20,7 @@
#include <QIODevice>
#include <QList>
#include <QLockFile>
#include <QRegularExpression>
#include <QTextStream>
#include <QtGlobal>
#include <QMap>
@@ -129,6 +130,42 @@ namespace BlackMisc
return path.left(path.lastIndexOf('/'));
}
QString CFileUtils::stripFirstSlashPart(const QString &path)
{
QString p = normalizeFilePathToQtStandard(path);
int i = p.indexOf('/');
if (i < 0) { return p; }
if ((i + 1) >= path.length()) { return QStringLiteral(""); }
return path.mid(i + 1);
}
QStringList CFileUtils::stripFirstSlashParts(const QStringList &paths)
{
QStringList stripped;
for (const QString &path : paths)
{
stripped.push_back(stripFileFromPath(path));
}
return stripped;
}
QString CFileUtils::stripLeadingSlashOrDriveLetter(const QString &path)
{
thread_local const QRegularExpression re("^\\/+|^[a-zA-Z]:\\/*");
QString p(path);
return p.replace(re, "");
}
QStringList CFileUtils::stripLeadingSlashOrDriveLetters(const QStringList &paths)
{
QStringList stripped;
for (const QString &path : paths)
{
stripped.push_back(stripLeadingSlashOrDriveLetter(path));
}
return stripped;
}
QString CFileUtils::lastPathSegment(const QString &path)
{
if (path.isEmpty()) { return QStringLiteral(""); }

View File

@@ -85,6 +85,18 @@ namespace BlackMisc
//! Strip file from path a/b/c.json a/b, return path
static QString stripFileFromPath(const QString &path);
//! Strip first slash part "/a/b" => "a/b", "h:/foo" => foo
static QString stripFirstSlashPart(const QString &path);
//! Strip first slash part "/a/b" => "a/b", "h:/foo" => foo
static QStringList stripFirstSlashParts(const QStringList &paths);
//! Strip leading slash or drive letter "/a/b" => "a/b" "H:/Foo" => "Foo"
static QString stripLeadingSlashOrDriveLetter(const QString &path);
//! Strip first slash part "/a/b" => "a/b", "h:/foo" => foo
static QStringList stripLeadingSlashOrDriveLetters(const QStringList &paths);
//! Last path segment a/b/c => c
static QString lastPathSegment(const QString &path);