-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsize.cpp
More file actions
47 lines (42 loc) · 1.23 KB
/
size.cpp
File metadata and controls
47 lines (42 loc) · 1.23 KB
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
#include "size.h"
#include <QStringList>
Size::Size()
{
}
QString Size::getWsize(const quint64 size, const int steps) {
//this function convert a bytes size into an human readble size
double isize = size;
QStringList keys = Size::getUnits();
int n;
for (n = 0 ; isize >= steps ; n++) isize /= steps;
if (n >= keys.count()) n = keys.count() -1;
return QString::number(isize,10,2) + keys.at(n);
}
quint64 Size::getRsize(const QString wsize) {
//this method convert an humain size to a bytes size
QStringList keys;
QString number;
int len;
int sizeEndPos;
double size;
int p;
keys = Size::getUnits();
for (p = keys.count() -1 ; p ; p--) {
len = keys.at(p).length();
sizeEndPos = wsize.length() - len;
//prevent empty sizes like "b" (withous any numbers)
if (!sizeEndPos) return 0;
if (wsize.right(len) == keys.at(p)) {
number = wsize.mid(0,sizeEndPos);
size = number.toDouble();
while (p--) size *= 1024;
return size;
}
}
return 0;
}
QStringList Size::getUnits() {
QStringList keys;
keys << "b" << "Kb" << "Mb" << "Gb" << "Tb" << "Pb" << "Eb" << "Zb" << "Yb";
return keys;
}