aboutsummaryrefslogtreecommitdiff
path: root/src/ioutil.cpp
blob: 8e07367227c4f60214aae35e6358841da96e48bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "ioutil.hpp"

bool IoUtil::writeAll(QIODevice &dst, QByteArray const & src)
{
    qint64 offset = 0;

    while(offset < src.size())
    {
        qint64 len = dst.write(src.data() + offset, src.size() - offset);
        if(len == 0)
            return false;
        offset += len;
    }

    return true;
}

QString IoUtil::size_human(qint64 size)
{
    if(size < 1024)
        return QString(QObject::tr("%1 B")).arg(size);

    float num = size;
    QStringList list;
    list << QObject::tr("KB") << QObject::tr("MB") << QObject::tr("GB") << QObject::tr("TB");

    QStringListIterator i(list);
    QString unit(QObject::tr("B"));

    while(num >= 1024.0 && i.hasNext())
    {
        unit = i.next();
        num /= 1024.0;
    }
    return QString().setNum(num,'f',2)+" "+unit;
}

QUrl IoUtil::uniformUrl(QUrl url)
{
    // We have to manually strip the root path slash
    // since StripTrailingSlash doesn't strip it for some reason.
    if (url.path() == "/") url.setPath(QString { });

    // This will strip slashes from non-root paths
    return url.adjusted(QUrl::RemoveFragment | QUrl::StripTrailingSlash);
}

QString IoUtil::uniformUrlString(QUrl url)
{
    return IoUtil::uniformUrl(url).toString(QUrl::FullyEncoded);
}