root/trunk/Components/unit_MHLHelpers.pas

225464
1
(* *****************************************************************************
2
  *
3
  * MyHomeLib
4
  *
5
  * Copyright (C) 2008-2010 Aleksey Penkov
6
  *
7
  * Authors Aleksey Penkov   alex.penkov@gmail.com
8
  *         Nick Rymanov     nrymanov@gmail.com
9
  *
10
  ****************************************************************************** *)
11
1
unit unit_MHLHelpers;
12
unit unit_MHLHelpers;
2
13
3
interface
14
interface
...
...
14
//
25
//
15
// ZIP helpers
26
// ZIP helpers
16
//
27
//
17
function GetFileNameZip(Zip: TZipForge; No: integer): string;
28
function GetFileNameZip(Zip: TZipForge; No: Integer): string;
18
29
30
function GetFormattedSize(sizeInBytes: Integer; showBytes: Boolean = False): string;
31
19
implementation
32
implementation
20
33
21
function DecodeBase64(const CinLine: ansistring): ansistring;
34
function DecodeBase64(const CinLine: AnsiString): AnsiString;
22
const
35
const
23
  RESULT_ERROR = -2;
36
  RESULT_ERROR = -2;
24
var
37
var
...
...
26
  c: AnsiChar;
39
  c: AnsiChar;
27
  x: SmallInt;
40
  x: SmallInt;
28
  c4: Word;
41
  c4: Word;
29
  StoredC4: array[0..3] of SmallInt;
42
  StoredC4: array [0 .. 3] of SmallInt;
30
  InLineLength: Integer;
43
  InLineLength: Integer;
31
begin
44
begin
32
  Result := '';
45
  Result := '';
...
...
40
    begin
53
    begin
41
      c := CinLine[inLineIndex];
54
      c := CinLine[inLineIndex];
42
      case c of
55
      case c of
43
        '+': x := 62;
56
        '+':        x := 62;
44
        '/': x := 63;
57
        '/':        x := 63;
45
        '0'..'9': x := Ord(c) - (Ord('0') - 52);
58
        '0' .. '9': x := Ord(c) - (Ord('0') - 52);
46
        '=': x := -1;
59
        '=':        x := -1;
47
        'A'..'Z': x := Ord(c) - Ord('A');
60
        'A' .. 'Z': x := Ord(c) - Ord('A');
48
        'a'..'z': x := Ord(c) - (Ord('a') - 26);
61
        'a' .. 'z': x := Ord(c) - (Ord('a') - 26);
49
      else
62
      else
50
        x := RESULT_ERROR;
63
        x := RESULT_ERROR;
51
      end;
64
      end;
...
...
71
  end;
84
  end;
72
end;
85
end;
73
86
74
function GetFileNameZip(Zip: TZipForge; No: integer): string;
87
function GetFileNameZip(Zip: TZipForge; No: Integer): string;
75
var
88
var
76
  i: integer;
89
  i: Integer;
77
  ArchItem: TZFArchiveItem;
90
  ArchItem: TZFArchiveItem;
78
begin
91
begin
79
  i := 0;
92
  i := 0;
80
  if (Zip.FindFirst('*.*', ArchItem, faAnyFile - faDirectory)) then
93
  if (Zip.FindFirst('*.*', ArchItem, faAnyFile - faDirectory)) then
81
  while i <> No do
94
    while i <> No do
82
  begin
95
    begin
83
    Zip.FindNext(ArchItem);
96
      Zip.FindNext(ArchItem);   { TODO : стоит проверять результат этого вызова }
84
    Inc(i);
97
      Inc(i);
85
  end;
98
    end;
86
  Result := ArchItem.FileName;
99
  Result := ArchItem.FileName;
87
end;
100
end;
88
101
102
function GetFormattedSize(sizeInBytes: Integer; showBytes: Boolean = False): string;
103
const
104
  KILOBYTE = 1024;
105
  MEGABYTE = KILOBYTE * KILOBYTE;
106
  GIGABYTE = MEGABYTE * KILOBYTE;
89
107
108
  strSizes: array [0 .. 3] of string = ('GB', 'MB', 'KB', 'Bytes');
109
var
110
  c1: Integer;
111
  c2: Integer;
112
  nIndex: Integer;
113
  strSz: string;
114
begin
115
  c1 := 0;
116
  c2 := 0;
117
  nIndex := -1;
90
118
119
  if (sizeInBytes div GIGABYTE) <> 0 then
120
  begin
121
    c1 := sizeInBytes div GIGABYTE;
122
    c2 := (sizeInBytes mod GIGABYTE * 10) div GIGABYTE;
123
    nIndex := 0;
124
  end
125
  else if (sizeInBytes div MEGABYTE) <> 0 then
126
  begin
127
    c1 := sizeInBytes div MEGABYTE;
128
    c2 := (sizeInBytes mod MEGABYTE * 10) div MEGABYTE;
129
    nIndex := 1;
130
  end
131
  else if (sizeInBytes div KILOBYTE) <> 0 then
132
  begin
133
    c1 := sizeInBytes div KILOBYTE;
134
    c2 := (sizeInBytes mod KILOBYTE * 10) div KILOBYTE;
135
    nIndex := 2;
136
  end
137
  else
138
  begin
139
    c1 := sizeInBytes;
140
    c2 := 0;
141
    nIndex := 3;
142
  end;
143
144
  // ASSERT(-1 != nIndex && (size_t)nIndex < NR_ELEMENTS(strSizes));
145
146
  if c2 = 0 then
147
    strSz := Format('%u', [c1])
148
  else
149
    strSz := Format('%u.%u', [c1, c2]);
150
151
  if (nIndex < 3) and showBytes then
152
    Result := Format('%s %s (%u Bytes)', [strSz, strSizes[nIndex], sizeInBytes])  { TODO : форматировать байты с разделением на группы }
  else
153
    Result := Format('%s %s', [strSz, strSizes[nIndex]]);
154
end;
155
91
end.
156
end.