Diffstat (limited to 'noncore/apps/opie-reader/util.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/opie-reader/util.cpp | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/noncore/apps/opie-reader/util.cpp b/noncore/apps/opie-reader/util.cpp index a736f8b..2ca1cc8 100644 --- a/noncore/apps/opie-reader/util.cpp +++ b/noncore/apps/opie-reader/util.cpp | |||
@@ -1,36 +1,45 @@ | |||
1 | #include "util.h" | 1 | #include "util.h" |
2 | #include <limits.h> | ||
2 | 3 | ||
3 | QString filesize(unsigned long l) | 4 | QString filesize(unsigned long l) |
4 | { | 5 | { |
5 | char units[] = { 'K', 'M', 'G' }; | 6 | char units[] = { 'K', 'M', 'G' }; |
6 | int iu = 0; | 7 | int iu = 0; |
7 | while (l > 999 && iu < sizeof(units)) | 8 | while (l > 999 && iu < sizeof(units)) |
8 | { | 9 | { |
9 | l = (l+512)/1024; | 10 | l = (l+512)/1024; |
10 | iu++; | 11 | iu++; |
11 | } | 12 | } |
12 | QString ret = QString::number(l); | 13 | QString ret = QString::number(l); |
13 | if (iu != 0) | 14 | if (iu != 0) |
14 | { | 15 | { |
15 | ret += QChar(units[iu-1]); | 16 | ret += QChar(units[iu-1]); |
16 | } | 17 | } |
17 | return ret; | 18 | return ret; |
18 | } | 19 | } |
19 | 20 | ||
20 | QString percent(unsigned long pos, unsigned long len) | 21 | QString percent(unsigned long pos, unsigned long len) |
21 | { | 22 | { |
22 | unsigned long permil = 0; | 23 | unsigned long permil = 0; |
23 | if (len != 0) | 24 | if (len != 0) |
24 | { | 25 | { |
25 | permil = (1000*pos+len/2)/len; | 26 | if (pos > UINT_MAX/1000) |
27 | { | ||
28 | unsigned long l1 = (len+500)/1000; | ||
29 | permil = (pos+l1/2)/l1; | ||
30 | } | ||
31 | else | ||
32 | { | ||
33 | permil = (1000*pos+len/2)/len; | ||
34 | } | ||
26 | } | 35 | } |
27 | unsigned long percen = permil/10; | 36 | unsigned long percen = permil/10; |
28 | unsigned long frac = permil - 10*percen; | 37 | unsigned long frac = permil - 10*percen; |
29 | return QString::number(percen) + "." + QString::number(frac); | 38 | return QString::number(percen) + "." + QString::number(frac); |
30 | } | 39 | } |
31 | 40 | ||
32 | QString fmt(unsigned long pos, unsigned long len) | 41 | QString fmt(unsigned long pos, unsigned long len) |
33 | { | 42 | { |
34 | QString ret = filesize(pos) + "/" + filesize(len) + " : " + percent(pos,len) +"%"; | 43 | QString ret = filesize(pos) + "/" + filesize(len) + " : " + percent(pos,len) +"%"; |
35 | return ret; | 44 | return ret; |
36 | } | 45 | } |