Ref T125, human readable file size

This commit is contained in:
Klaus Basan
2017-08-15 02:11:22 +02:00
committed by Mathew Sutcliffe
parent 8c032fb4d7
commit 53189eafd5
4 changed files with 36 additions and 5 deletions

View File

@@ -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 <QCoreApplication>
#include <QDateTime>
@@ -23,6 +25,7 @@
#include <algorithm>
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

View File

@@ -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

View File

@@ -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

View File

@@ -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);