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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
//Author: Max Howell <max.howell@methylblue.com>, (C) 2004
//Copyright: See COPYING file that comes with this distribution
#include "fileTree.h"
#include <tdeglobal.h>
#include <tdelocale.h>
#include <tqstring.h>
//static definitions
const FileSize File::DENOMINATOR[4] = { 1ull, 1ull<<10, 1ull<<20, 1ull<<30 };
const char File::PREFIX[5][2] = { "", "K", "M", "G", "T" };
TQString
File::fullPath( const Directory *root /*= 0*/ ) const
{
TQString path;
if( root == this ) root = 0; //prevent returning empty string when there is something we could return
const File *d;
for( d = this; d != root && d && d->parent() != 0; d = d->parent() )
{
if( !path.isEmpty() )
path = "/" + path;
path = d->name() + path;
}
if( d )
{
while( d->parent() )
d = d->parent();
if( d->directory().endsWith( "/" ) )
return d->directory() + path;
else
return d->directory() + "/" + path;
}
else
return path;
}
TQString
File::humanReadableSize( UnitPrefix key /*= mega*/ ) const //FIXME inline
{
return humanReadableSize( m_size, key );
}
TQString
File::humanReadableSize( FileSize size, UnitPrefix key /*= mega*/ ) //static
{
TQString s;
double prettySize = (double)size / (double)DENOMINATOR[key];
const TDELocale &locale = *TDEGlobal::locale();
if( prettySize >= 0.01 )
{
if( prettySize < 1 ) s = locale.formatNumber( prettySize, 2 );
else if( prettySize < 100 ) s = locale.formatNumber( prettySize, 1 );
else s = locale.formatNumber( prettySize, 0 );
s += ' ';
s += PREFIX[key];
s += 'B';
}
if( prettySize < 0.1 )
{
s += " (";
s += locale.formatNumber( size / DENOMINATOR[ key ? key - 1 : 0 ], 0 );
s += ' ';
s += PREFIX[key];
s += "B)";
}
return s;
}
|