From 53189eafd59a9210572678197bbf5ffe855e7739 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Tue, 15 Aug 2017 02:11:22 +0200 Subject: [PATCH] Ref T125, human readable file size --- src/blackmisc/fileutils.cpp | 30 +++++++++++++++++++++++++----- src/blackmisc/fileutils.h | 3 +++ src/blackmisc/math/mathutils.cpp | 5 +++++ src/blackmisc/math/mathutils.h | 3 +++ 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/blackmisc/fileutils.cpp b/src/blackmisc/fileutils.cpp index ecacfb8c7..d274384e6 100644 --- a/src/blackmisc/fileutils.cpp +++ b/src/blackmisc/fileutils.cpp @@ -7,9 +7,11 @@ * contained in the LICENSE file. */ -#include "blackconfig/buildconfig.h" -#include "blackmisc/fileutils.h" #include "blackmisc/worker.h" +#include "blackmisc/fileutils.h" +#include "blackmisc/math/mathutils.h" + +#include "blackconfig/buildconfig.h" #include #include @@ -23,6 +25,7 @@ #include using namespace BlackConfig; +using namespace BlackMisc::Math; namespace BlackMisc { @@ -124,8 +127,8 @@ namespace BlackMisc return false; } - QDir originDir(sourceFileInfo.absoluteFilePath()); - auto fileNames = originDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System); + const QDir originDir(sourceFileInfo.absoluteFilePath()); + const auto fileNames = originDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System); for (const QString &fileName : fileNames) { if (!copyRecursively(originDir.absoluteFilePath(fileName), targetDir.absoluteFilePath(fileName))) @@ -141,7 +144,6 @@ namespace BlackMisc return false; } } - return true; } @@ -354,4 +356,22 @@ namespace BlackMisc static const QString f("/%1"); return f.arg(filePath); } + + QString CFileUtils::humanReadableFileSize(qint64 size) + { + // from https://stackoverflow.com/a/30958189/356726 + // fell free to replace it by something better + static const QStringList units({ "KB", "MB", "GB", "TB" }); + if (size <= 1024) { return QString::number(size); } + + QStringListIterator i(units); + double currentSize = size; + QString unit; + while (currentSize >= 1024.0 && i.hasNext()) + { + unit = i.next(); + currentSize /= 1024.0; + } + return QString("%1 %2").arg(CMathUtils::roundAsString(currentSize, 2), unit); + } } // ns diff --git a/src/blackmisc/fileutils.h b/src/blackmisc/fileutils.h index 021e1e01c..e9790a181 100644 --- a/src/blackmisc/fileutils.h +++ b/src/blackmisc/fileutils.h @@ -130,6 +130,9 @@ namespace BlackMisc //! UNC file paths on Qt start with "/", but UNC file paths only work when they start with "//" //! \remark On Windows starting with "/" means an UNC path, on UNIX it varies, see http://unix.stackexchange.com/a/12291/19428 static QString fixWindowsUncPath(const QString &filePath); + + //! Human readable (GB, MB, ..) file size + static QString humanReadableFileSize(qint64 size); }; } // ns diff --git a/src/blackmisc/math/mathutils.cpp b/src/blackmisc/math/mathutils.cpp index 859222f24..5b4a6d856 100644 --- a/src/blackmisc/math/mathutils.cpp +++ b/src/blackmisc/math/mathutils.cpp @@ -51,6 +51,11 @@ namespace BlackMisc return rv; } + QString CMathUtils::roundAsString(double value, int digits) + { + return QString::number(round(value, digits)); + } + double CMathUtils::roundEpsilon(double value, double epsilon) { if (epsilon == 0) { return value; } // avoid division by 0 diff --git a/src/blackmisc/math/mathutils.h b/src/blackmisc/math/mathutils.h index 83ebb3d2d..666669a46 100644 --- a/src/blackmisc/math/mathutils.h +++ b/src/blackmisc/math/mathutils.h @@ -50,6 +50,9 @@ namespace BlackMisc //! Utility round method static double round(double value, int digits); + //! Utility round method, returning as string + static QString roundAsString(double value, int digits); + //! Round by given epsilon static double roundEpsilon(double value, double epsilon);