Merge pull request #7282 from glassez/fsutils

Improve utils/fs.* and fix coding style
This commit is contained in:
sledgehammer999 2017-08-15 22:47:39 +03:00 committed by GitHub
commit cccb22f0e3
2 changed files with 42 additions and 45 deletions

View file

@ -72,26 +72,26 @@ QString Utils::Fs::fromNativePath(const QString &path)
QString Utils::Fs::fileExtension(const QString &filename)
{
QString ext = QString(filename).remove(QB_EXT);
const int point_index = ext.lastIndexOf(".");
return (point_index >= 0) ? ext.mid(point_index + 1) : QString();
const int pointIndex = ext.lastIndexOf(".");
return (pointIndex >= 0) ? ext.mid(pointIndex + 1) : QString();
}
QString Utils::Fs::fileName(const QString& file_path)
QString Utils::Fs::fileName(const QString &filePath)
{
QString path = fromNativePath(file_path);
const int slash_index = path.lastIndexOf("/");
if (slash_index == -1)
QString path = fromNativePath(filePath);
const int slashIndex = path.lastIndexOf("/");
if (slashIndex == -1)
return path;
return path.mid(slash_index + 1);
return path.mid(slashIndex + 1);
}
QString Utils::Fs::folderName(const QString& file_path)
QString Utils::Fs::folderName(const QString &filePath)
{
QString path = fromNativePath(file_path);
const int slash_index = path.lastIndexOf("/");
if (slash_index == -1)
QString path = fromNativePath(filePath);
const int slashIndex = path.lastIndexOf("/");
if (slashIndex == -1)
return path;
return path.left(slash_index);
return path.left(slashIndex);
}
/**
@ -102,7 +102,7 @@ QString Utils::Fs::folderName(const QString& file_path)
bool Utils::Fs::smartRemoveEmptyFolderTree(const QString &path)
{
if (path.isEmpty() || !QDir(path).exists())
return false;
return true;
static const QStringList deleteFilesList = {
// Windows
@ -119,7 +119,9 @@ bool Utils::Fs::smartRemoveEmptyFolderTree(const QString& path)
QDirIterator iter(path, (QDir::AllDirs | QDir::NoDotAndDotDot), QDirIterator::Subdirectories);
while (iter.hasNext())
dirList << iter.next() + "/";
std::sort(dirList.begin(), dirList.end(), [](const QString &l, const QString &r) { return l.count("/") > r.count("/"); }); // sort descending by directory depth
// sort descending by directory depth
std::sort(dirList.begin(), dirList.end()
, [](const QString &l, const QString &r) { return l.count("/") > r.count("/"); });
for (const QString &p : dirList) {
// remove unwanted files
@ -147,9 +149,9 @@ bool Utils::Fs::smartRemoveEmptyFolderTree(const QString& path)
*
* This function will try to fix the file permissions before removing it.
*/
bool Utils::Fs::forceRemove(const QString& file_path)
bool Utils::Fs::forceRemove(const QString &filePath)
{
QFile f(file_path);
QFile f(filePath);
if (!f.exists())
return true;
// Make sure we have read/write permissions
@ -236,9 +238,9 @@ qint64 Utils::Fs::freeDiskSpaceOnPath(const QString &path)
return QStorageInfo(path).bytesAvailable();
}
QString Utils::Fs::branchPath(const QString& file_path, QString* removed)
QString Utils::Fs::branchPath(const QString &filePath, QString *removed)
{
QString ret = fromNativePath(file_path);
QString ret = fromNativePath(filePath);
if (ret.endsWith("/"))
ret.chop(1);
const int slashIndex = ret.lastIndexOf("/");
@ -261,7 +263,7 @@ bool Utils::Fs::sameFileNames(const QString &first, const QString &second)
QString Utils::Fs::expandPath(const QString &path)
{
QString ret = fromNativePath(path.trimmed());
QString ret = path.trimmed();
if (ret.isEmpty())
return ret;
@ -270,12 +272,7 @@ QString Utils::Fs::expandPath(const QString &path)
QString Utils::Fs::expandPathAbs(const QString &path)
{
QString ret = expandPath(path);
if (!QDir::isAbsolutePath(ret))
ret = QDir(ret).absolutePath();
return ret;
return QDir(expandPath(path)).absolutePath();
}
QString Utils::Fs::tempPath()

View file

@ -44,21 +44,21 @@ namespace Utils
QString toNativePath(const QString &path);
QString fromNativePath(const QString &path);
QString fileExtension(const QString &filename);
QString fileName(const QString& file_path);
QString folderName(const QString& file_path);
QString fileName(const QString &filePath);
QString folderName(const QString &filePath);
qint64 computePathSize(const QString &path);
bool sameFiles(const QString &path1, const QString &path2);
QString toValidFileSystemName(const QString &name, bool allowSeparators = false
, const QString &pad = QLatin1String(" "));
bool isValidFileSystemName(const QString &name, bool allowSeparators = false);
qint64 freeDiskSpaceOnPath(const QString &path);
QString branchPath(const QString& file_path, QString* removed = 0);
QString branchPath(const QString &filePath, QString *removed = 0);
bool sameFileNames(const QString &first, const QString &second);
QString expandPath(const QString &path);
QString expandPathAbs(const QString &path);
bool smartRemoveEmptyFolderTree(const QString &path);
bool forceRemove(const QString& file_path);
bool forceRemove(const QString &filePath);
void removeDirRecursive(const QString &path);
QString tempPath();