-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.~pas
263 lines (240 loc) · 7.9 KB
/
functions.~pas
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
{******************************************************************************}
{* This unit contains all functions that required for both compression and *}
{* decompression for LZW, AC, and RLE+BWT. I intent to separate these *}
{* functions from the core, since it will be helpful in estimating the *}
{* complexity of algorithms *}
{* *}
{* @Author : Plipus Telaumbanua *}
{* @Email : philipstel@gmail.com *}
{* @Website : www.philipstel.web.id *}
{* *}
{* @Created : Oct 20 2010 *}
{* @Last Modified : Dec 31 2010 *}
{* *}
{* Computer Science Departement *}
{* University Of North Sumatera *}
{* Indonesia *}
{******************************************************************************}
unit functions;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
{**
* Framework for functions that will be used.
* Write only param and its returning value
*
* @Param of IsInDictionary : Dict is an array with infinite length theorically,
* but it should be finite. It is also called as a dynamic array. Only available
* for parameter not for a variable declaration. // <-- has been mooved
**}
function Get_File_Size(sFileToExamine: string; bInKBytes: Boolean): string;
function FormatByteSize(const bytes: Longint): string;
{ estimate the complexity of these funtions }
function DecimalToBiner( Dec : Integer) : string;
function BinerToDecimal(Biner: string) : integer;
function BitPadding(Str: string; LengthPad: byte; PadType: string) : string;
function SubStr(Str: string; Start: integer; Long: integer):string;
function Explode(D, S: String): TStringList;
function Ceil(X: Real): Integer;
function Floor(X: Real): Integer;
function Pow(X, Y: Integer): Integer;
const
{ 16-bits }
MAX = 16;
var
{**
* 1 - 16 bits respectively for biner to decimal
* 16 bits mean 2 power of 16 in which there are possible integer from 0 - 65535
* (the total of its number is 65535, since it begins from 0)
**}
Power : array[0..MAX] of Integer = (1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,
2048, 4096, 8192, 16384, 32768, 65536);
{**
* All functions that has been declared in the interface will be
* implemented here.
**}
implementation
{**
* This function get the file size in string format
* @Param1 : <string> sFileToExamine : the name of file
* @Param2 : <boolean> bInKBytes : default is true
* @Return : <string>
**}
function Get_File_Size(sFileToExamine: string; bInKBytes: Boolean): string;
var
SearchRec: TSearchRec;
sgPath: string;
inRetval, I1: Integer;
begin
sgPath := ExpandFileName(sFileToExamine);
try
inRetval := FindFirst(ExpandFileName(sFileToExamine), faAnyFile, SearchRec);
if inRetval = 0 then
I1 := SearchRec.Size
else
I1 := -1;
finally
SysUtils.FindClose(SearchRec);
end;
{ 'Result' is a reserve word }
{ we can use 'Result' instead of 'Get_File_Size' to return the value }
Result := IntToStr(I1);
end;
{ This function reformat the file size }
function FormatByteSize(const bytes: Longint): string;
const
B = 1; { byte }
KB = 1024 * B; { kilobyte }
MB = 1024 * KB; { megabyte }
GB = 1024 * MB; { gigabyte }
begin
if bytes > GB then begin
result := FormatFloat('#.## GB', bytes / GB)
end else if bytes > MB then begin
result := FormatFloat('#.## MB', bytes / MB)
end else if bytes > KB then begin
result := FormatFloat('#.## KB', bytes / KB)
end else begin
result := FormatFloat('#.## bytes', bytes) ;
end;
end;
{**
* Convertion decimal number to biner using 16-bits
* @Param 1 : <integer> Dec : ex : 0000h - FFFFh
* @Return : <string> Biner : ex : 010101000
**}
function DecimalToBiner(Dec : Integer) : string;
var
Biner : string;
Modules : integer;
begin
while( Dec > 0 ) do begin
Modules := Dec mod 2;
Dec := Dec div 2;
Biner := IntToStr(modules) + Biner;
end;
Result := Biner;
end;
{**
* Opposite of 'DecimalToBiner' convertion biner to decimal using 16-bits
* @Param 1 : <string> biner : ex 65535
* @Return : <integer> integer : ex FFFFh
**}
function BinerToDecimal(Biner: string) : integer;
var
I, Dec, Index : integer;
begin
Dec := 0;
Index := 0;
for I := Length(Biner) downto 1 do begin
Dec := Dec + (Power[Index] * StrToInt(Biner[i]) );
Inc(Index);
end;
Result := Dec;
end;
{**
* This function will adding '0' as much as 'LengthPad' to the most left of 'Str'
* @Param 1 : <string> Str : ex '101011'
* @Param 2 : <byte> LengthPad : How Length ?
* @Param 3 : <const> PadType : to LEFT or to RIGHT?
* @Return : <string> : Return the padding value.
**}
function BitPadding(Str: string; LengthPad: byte; PadType: string) : string;
var
I : Byte;
begin
for I := Length(Str) to LengthPad - 1 do begin
if PadType = 'LEFT' then begin
Str := '0' + Str;
end else begin
Str := Str + '0';
end;
end;
Result := Str;
end;
{**
* This function will take a substring from a string from 'start' as much as 'long' characters
* @Param 1 : <string> Str : String to be taken
* @Param 2 : <byte> Start : Start from index
* @Param 3 : <const> Long : string long
* @Return : <string> : a substring starting from 'start' as much as 'long'
**}
function SubStr(Str: string; Start: integer; Long: integer):string;
var
Output : string;
I : integer;
begin
Output := '';
for I := Start to ((Start + Long) - 1) do begin
Output := Output + Str[I];
end;
Result := Output;
end;
{**
* This function break or split string based on the delimeter inside the string
* to be substrings that identifed by index start from 0
* @Param 1 : <string> D : Indicator
* @Param 2 : <string> S : String to be fold
* @Return : <TStringList> : Return array of string
**}
function Explode(D : String; S : String): TStringList;
var
C: Integer;
begin
Result := TStringList.Create;
C := 0;
while S <> '' do begin
if Pos(D, S) > 0 then begin
Result.Add(Copy(S, 1, Pos(D, S) - 1));
Delete(S, 1, Length(Result[C]) + Length(D));
end else begin
Result.Add(S);
S := '';
end;
Inc(C);
end;
end;
function Ceil(X: Real): Integer;
var
Format: Integer;
begin
if X < 1 then begin
Result := 1;
end else begin
{ Round first, ceil if >= 5 othwerwise floor }
Format := StrToInt(FormatFloat('#', X));
{ means: ceiling occurss or not, so we need let as it }
if (Format > X) or (Format = X) then begin
Result := Format;
end else begin
Result := Format + 1;
end;
end;
end;
function Floor(X: Real): Integer;
var
Format: Integer;
begin
if X < 1 then begin
Result := 0;
end else begin
Format := StrToInt(FormatFloat('#', X));
if (Format > X) then begin
Result := Format - 1;
end else begin
Result := Format;
end;
end;
end;
function Pow(X, Y: Integer): Integer;
var
I, Res: Integer;
begin
Res := 1;
for I := 1 to Y do begin
Res := Res * X;
end;
Result := Res;
end;
end.