-
Notifications
You must be signed in to change notification settings - Fork 1
/
Func_Lib.pas
370 lines (328 loc) · 8.41 KB
/
Func_Lib.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
unit Func_Lib;
(******************************)
(* TIME FOR KILL *)
(* Created by XProger *)
(* begin: 29.10.2003 *)
(* end: --.--.---- *)
(* *)
(* site: www.XProger.narod.ru *)
(* e-mail: XProger@list.ru *)
(******************************)
interface
uses
Windows;
type
TFindData = record
Handle: DWORD;
Data: TWin32FindData;
end;
//Dlls
function LoadDll(var Dll: THandle; const FileName: string): boolean;
function LoadProc(Dll: THandle; var Proc: pointer; const ProcName: string): boolean;
procedure FreeDll(var Dll: THandle);
//Files
procedure FindClose(var FindData: TFindData);
function FindMatchingFile(var FindData: TFindData): integer;
function FindFirst(const Path: string; var FindData: TFindData): boolean;
function FindNext(var FindData: TFindData): boolean;
function ExtractFileDir(const FileName: string): string;
function ExtractFileName(const FileName: string): string;
function ExtractFileNameEx(const FileName: string): string;
function ExtractFileExt(const FileName: string): string;
function FileExists(const FileName: string): boolean;
function GetSystemDir: string;
procedure incs(var x: single; r: single);
procedure decs(var x: single; r: single);
//Strings
function IntToStr(i: integer): String;
function StrToInt(Str: String): integer;
// function StrToFloat(const S: string): Extended;
function UpperCase(const S: string): string;
function LowerCase(const S: string): string;
function Tag_Length(const s: string): integer;
function StrLCopy(Dest: PChar; const Source: PChar; MaxLen: Cardinal): PChar; assembler;
//Advanced string functions
function StrSpace(var Str: string): string;
procedure DelSpace(var Str: string);
//Float
function Conv(cs: double; numb: integer): double;
type
TBits = array [0..7] of boolean;
function BitsToByte(b: TBits): byte;
procedure ByteToBits(b: byte; var bits: TBits);
//var
//ModDir : string;
implementation
//Dlls
function LoadDll(var Dll: THandle; const FileName: string): boolean;
begin
Dll := LoadLibrary(PChar(FileName));
Result := Dll <> 0;
end;
function LoadProc(Dll: THandle; var Proc: pointer; const ProcName: string): boolean;
begin
Proc:=nil;
Proc:=GetProcAddress(Dll, PChar(ProcName));
Result:=Proc<>nil;
end;
procedure FreeDll(var Dll: THandle);
begin
FreeLibrary(Dll);
Dll := 0;
end;
//Files
procedure FindClose(var FindData: TFindData);
begin
if FindData.Handle <> INVALID_HANDLE_VALUE then
begin
Windows.FindClose(FindData.Handle);
FindData.Handle := INVALID_HANDLE_VALUE;
end;
end;
function FindMatchingFile(var FindData: TFindData): integer;
begin
while FindData.Data.dwFileAttributes <> 0 do
if not FindNextFile(FindData.Handle, FindData.Data) then
begin
Result := GetLastError;
Exit;
end
else
break;
Result := 0;
end;
function FindFirst(const Path: string; var FindData: TFindData): boolean;
begin
Result := false;
FindData.Handle := FindFirstFile(PChar(Path), FindData.Data);
if FindData.Handle <> INVALID_HANDLE_VALUE then
Result := true
else
FindClose(FindData);
end;
function FindNext(var FindData: TFindData): boolean;
begin
if FindNextFile(FindData.Handle, FindData.Data) then
Result := true
else
Result := false;
end;
function ExtractFileDir(const FileName: string): string;
var
i: integer;
begin
for i := Length(FileName) downto 1 do
if FileName[i] = '\' then break;
Result := Copy(FileName, 1, i-1);
end;
function ExtractFileName(const FileName: string): string;
var
i: integer;
begin
Result:='';
for i := Length(FileName) downto 1 do
if FileName[i] = '\' then
break
else
Result := FileName[i] + Result;
end;
function ExtractFileNameEx(const FileName: string): string;
var
k, l : integer;
begin
k := Length(FileName);
l := k;
while (k > 0) and (FileName[k] <> '\') do Dec(k);
while (l > 0) and (FileName[l] <> '.') do Dec(l);
if l = 0 then l := Length(FileName) + 1;
if (l > 0) then
Result := Copy(FileName, k + 1, l - k - 1)
else
Result := FileName;
end;
function ExtractFileExt(const FileName: string): string;
var
i: integer;
begin
Result:='';
for i:=Length(FileName) downto 1 do
if FileName[i] <> '.' then
Result := FileName[i] + Result
else
break;
end;
function FileExists(const FileName: string): boolean;
var
F: File;
begin
FileMode := 64;
AssignFile(F, FileName); //Îòêðûâàåì ôàéë
{$I-} //Îòìåíà IO îøèáîê
Reset(F); //Îòêðûâàåì ôàéë äëÿ ÷òåíèÿ
{$I+} //Âêëþ÷àåì ïðîâåðêó IO îøèáîê
Result := IOResult = 0;
if Result then
CloseFile(F);
end;
//Get System Directory
function GetSystemDir: string;
var
iLength: Cardinal;
begin
iLength := 255;
setLength(Result, iLength);
iLength := GetSystemDirectory(PChar(Result), iLength);
setLength(Result, iLength);
end;
//Strings
function IntToStr(i: integer): String;
begin
Str(i, Result);
end;
function StrToInt(Str: String): integer;
var
Er: integer;
begin
Val(Str, Result, Er);
if Er <> 0 then
Result := 0;
end;
function UpperCase(const S: string): string;
var
i: integer;
begin
Result:='';
for i:=1 to Length(S) do
Result:=Result+UpCase(s[i]);
end;
function LowerCase(const s: string): string;
var
i, l : integer;
Rc, Sc : PChar;
begin
// îòëè÷èè îò ñòàíäàðòíîé, ðàáîòàåò ñ êèðèëëèöåé :)
l := Length(s);
SetLength(Result, l);
Rc := Pointer(Result);
Sc := Pointer(s);
for i := 1 to l do
begin
if s[i] in ['A'..'Z', 'À'..'ß'] then
Rc^ := Char(Byte(Sc^) + 32)
else
Rc^ := Sc^;
inc(Rc);
inc(Sc);
end;
end;
// Äëèííà ñòðîêè áåç òåãîâ öâåòà
function Tag_Length(const s: string): integer;
var
i : integer;
begin
Result := Length(s);
for i := 1 to Length(s) do
if s[i] = '^' then
dec(Result, 2);
end;
function StrLCopy(Dest: PChar; const Source: PChar; MaxLen: Cardinal): PChar; assembler;
asm
PUSH EDI
PUSH ESI
PUSH EBX
MOV ESI,EAX
MOV EDI,EDX
MOV EBX,ECX
XOR AL,AL
TEST ECX,ECX
JZ @@1
REPNE SCASB
JNE @@1
INC ECX
@@1: SUB EBX,ECX
MOV EDI,ESI
MOV ESI,EDX
MOV EDX,EDI
MOV ECX,EBX
SHR ECX,2
REP MOVSD
MOV ECX,EBX
AND ECX,3
REP MOVSB
STOSB
MOV EAX,EDX
POP EBX
POP ESI
POP EDI
end;
//Advanced string functions
function StrSpace(var Str: string): string;
var
sp : integer;
begin
sp := pos(' ', Str);
if sp < 1 then
sp := Length(Str)
else
sp := sp - 1;
Result := Copy(Str, 1, sp);
Delete(Str, 1, sp + 1);
end;
procedure DelSpace(var Str: string);
var
i: integer;
begin
for i := 1 to Length(Str) do
if Str[i] <> ' ' then break;
Str := Copy(Str, i, Length(Str));
for i := Length(Str) downto 1 do
if Str[i] <> ' ' then break;
Str := Copy(Str, 1, i);
end;
////////////////////////////////////////////////////////////////
// ×èñëà ñ ïëàâàþùåé òî÷êîé //
////////////////////////////////////////////////////////////////
procedure incs(var x: single; r: single);
begin
x := x + r;
end;
procedure decs(var x: single; r: single);
begin
x := x - r;
end;
{--------------------------------------------------------------------------}
{Ôóíêöèÿ ïðåäñòàâëåíèÿ ÷èñåë ñ ïëàâàþùåé òî÷êîé è íóæíûì ÷èñëîì ðàçðÿäîâ. }
{Ïðèìåð: Conv(2.005,2) âîçâðàùàåò 2.01; Conv(2.5,0) âîçâðàùàåò 3 }
{--------------------------------------------------------------------------}
function Conv(cs: double; numb: integer): double;
var
db : double;
i : int64;
ii, ink, i1 : integer;
begin
ink := 1;
for ii := 1 to numb do
ink := ink*10;
db := cs*ink*100;
i := trunc(int(db) / 100);
i1 := trunc(db - i*100);
if i1 > 49 then
inc(i);
Result := i/ink;
end;
function BitsToByte(b: TBits): byte;
begin
Result:=Ord(b[0])+Ord(b[1]) shl 1+Ord(b[2])shl 2+Ord(b[3] )shl 3 + Ord(b[4] )shl 4 + Ord(b[5] )shl 5 + Ord(b[6] )shl 6 + Ord(b[7] )shl 7;
end;
procedure ByteToBits(b: byte; var bits: TBits);
begin
bits[0]:=b and 1>0;
bits[1]:=b and 2>0;
bits[2]:=b and 4>0;
bits[3]:=b and 8>0;
bits[4]:=b and 16>0;
bits[5]:=b and 32>0;
bits[6]:=b and 64>0;
bits[7]:=b and 128>0;
end;
end.