1 {==============================================================================|
2 | Project : Delphree - Synapse | 002.003.000 |
3 |==============================================================================|
4 | Content: support procedures and functions |
5 |==============================================================================|
6 | The contents of this file are subject to the Mozilla Public License Ver. 1.1 |
7 | (the "License"); you may not use this file except in compliance with the |
8 | License. You may obtain a Copy of the License at http://www.mozilla.org/MPL/ |
10 | Software distributed under the License is distributed on an "AS IS" basis, |
11 | WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for |
12 | the specific language governing rights and limitations under the License. |
13 |==============================================================================|
14 | The Original Code is Synapse Delphi Library. |
15 |==============================================================================|
16 | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
17 | Portions created by Lukas Gebauer are Copyright (c) 1999,2000,2001. |
18 | Portions created by Hernan Sanchez are Copyright (c) 2000. |
19 | All Rights Reserved. |
20 |==============================================================================|
22 | Hernan Sanchez (hernan.sanchez@iname.com) |
23 |==============================================================================|
24 | History: see HISTORY.HTM from distribution package |
25 | (Found at URL: http://www.ararat.cz/synapse/) |
26 |==============================================================================}
42 function Timezone
: string;
43 function Rfc822DateTime(t
: TDateTime
): string;
44 function CDateTime(t
: TDateTime
): string;
45 function CodeInt(Value
: Word): string;
46 function DecodeInt(const Value
: string; Index
: Integer): Word;
47 function IsIP(const Value
: string): Boolean;
48 function ReverseIP(Value
: string): string;
49 function IPToID(Host
: string): string;
50 procedure Dump(const Buffer
, DumpFile
: string);
51 function SeparateLeft(const Value
, Delimiter
: string): string;
52 function SeparateRight(const Value
, Delimiter
: string): string;
53 function GetParameter(const Value
, Parameter
: string): string;
54 function GetEmailAddr(const Value
: string): string;
55 function GetEmailDesc(Value
: string): string;
56 function StrToHex(const Value
: string): string;
57 function IntToBin(Value
: Integer; Digits
: Byte): string;
58 function BinToInt(const Value
: string): Integer;
59 function ParseURL(URL
: string; var Prot
, User
, Pass
, Host
, Port
, Path
,
60 Para
: string): string;
61 function StringReplace(Value
, Search
, Replace
: string): string;
62 function RPos(const Sub
, Value
: String): Integer;
63 function StrToIntDef(const Value
: string; Default
: Integer) : Integer;
67 function Timezone
: string;
77 bias
:= ut
.__tm_gmtoff
div 60;
84 zoneinfo
: TTimeZoneInformation
;
88 case GetTimeZoneInformation(Zoneinfo
) of
90 bias
:= zoneinfo
.Bias
+ zoneinfo
.DaylightBias
;
92 bias
:= zoneinfo
.Bias
+ zoneinfo
.StandardBias
;
94 bias
:= zoneinfo
.Bias
;
104 Result
:= Result
+ Format('%.2d%.2d', [h
, m
]);
107 {==============================================================================}
109 function Rfc822DateTime(t
: TDateTime
): string;
111 Result
:= Date2StrFmt('ddd, d MMMM yyyy ',t
) + Time2StrFmt('hh:mm:ss', t
);
112 Result
:= Result
+ ' ' + Timezone
;
115 {==============================================================================}
117 function CDateTime(t
: TDateTime
): string;
119 Result
:= Date2StrFmt('mmm dd ',t
) + Time2StrFmt('hh:mm:ss', t
);
120 if Result
[5] = '0' then
124 {==============================================================================}
126 function CodeInt(Value
: Word): string;
128 Result
:= Chr(Hi(Value
)) + Chr(Lo(Value
))
131 {==============================================================================}
133 function DecodeInt(const Value
: string; Index
: Integer): Word;
137 if Length(Value
) > Index
then
138 x
:= Ord(Value
[Index
])
141 if Length(Value
) >= (Index
+ 1) then
142 y
:= Ord(Value
[Index
+ 1])
145 Result
:= x
* 256 + y
;
148 {==============================================================================}
150 function IsIP(const Value
: string): Boolean;
156 for n
:= 1 to Length(Value
) do
157 if not (Value
[n
] in ['0'..'9', '.']) then
164 if Value
[n
] = '.' then
171 {==============================================================================}
173 function ReverseIP(Value
: string): string;
179 x
:= DelimiterLast(Value
,'.');
180 Result
:= Result
+ '.' + Copy(Value
, x
+ 1, Length(Value
) - x
);
181 Delete(Value
, x
, Length(Value
) - x
+ 1);
183 if Length(Result
) > 0 then
184 if Result
[1] = '.' then
185 Delete(Result
, 1, 1);
188 {==============================================================================}
190 function IPToID(Host
: string): string;
199 s
:= StrScan(PChar(Host
), '.');
200 t
:= Copy(Host
, 1, (Length(Host
) - Length(s
)));
201 Delete(Host
, 1, (Length(Host
) - Length(s
) + 1));
202 i
:= StrToIntDef(t
, 0);
203 Result
:= Result
+ Chr(i
);
205 i
:= StrToIntDef(Host
, 0);
206 Result
:= Result
+ Chr(i
);
209 {==============================================================================}
211 procedure Dump(const Buffer
, DumpFile
: string);
218 for n
:= 1 to Length(Buffer
) do
219 s
:= s
+ ' +#$' + Int2Hex(Ord(Buffer
[n
]), 2);
220 AssignFile(f
, DumpFile
);
221 if FileExists(DumpFile
) then
222 DeleteFile(PChar(DumpFile
));
231 {==============================================================================}
233 function SeparateLeft(const Value
, Delimiter
: string): string;
237 x
:= Pos(Delimiter
, Value
);
239 Result
:= Trim(Value
)
241 Result
:= Trim(Copy(Value
, 1, x
- 1));
244 {==============================================================================}
246 function SeparateRight(const Value
, Delimiter
: string): string;
250 x
:= Pos(Delimiter
, Value
);
252 x
:= x
+ Length(Delimiter
) - 1;
253 Result
:= Trim(Copy(Value
, x
+ 1, Length(Value
) - x
));
256 {==============================================================================}
258 function GetParameter(const Value
, Parameter
: string): string;
263 x
:= Pos(UpperCase(Parameter
), UpperCase(Value
));
267 s
:= Copy(Value
, x
+ Length(Parameter
), Length(Value
)
268 - (x
+ Length(Parameter
)) + 1);
271 if Length(s
) > 1 then
275 s
:= Copy(s
, 2, Length(s
) - 1);
287 Result
:= Copy(s
, 1, x1
);
291 {==============================================================================}
293 function GetEmailAddr(const Value
: string): string;
297 s
:= SeparateRight(Value
, '<');
298 s
:= SeparateLeft(s
, '>');
302 {==============================================================================}
304 function GetEmailDesc(Value
: string): string;
308 Value
:= Trim(Value
);
309 s
:= SeparateRight(Value
, '"');
311 s
:= SeparateLeft(s
, '"')
314 s
:= SeparateRight(Value
, '(');
316 s
:= SeparateLeft(s
, ')')
319 s
:= SeparateLeft(Value
, '<');
327 {==============================================================================}
329 function StrToHex(const Value
: string): string;
334 for n
:= 1 to Length(Value
) do
335 Result
:= Result
+ Int2Hex(Byte(Value
[n
]), 2);
336 Result
:= LowerCase(Result
);
339 {==============================================================================}
341 function IntToBin(Value
: Integer; Digits
: Byte): string;
351 Result
:= '1' + Result
353 Result
:= '0' + Result
;
356 for n
:= x
to Digits
- 1 do
357 Result
:= '0' + Result
;
360 {==============================================================================}
362 function BinToInt(const Value
: string): Integer;
367 for n
:= 1 to Length(Value
) do
369 if Value
[n
] = '0' then
372 if Value
[n
] = '1' then
373 Result
:= Result
* 2 + 1
379 {==============================================================================}
381 function ParseURL(URL
: string; var Prot
, User
, Pass
, Host
, Port
, Path
,
382 Para
: string): string;
395 x
:= Pos('://', URL
);
398 Prot
:= SeparateLeft(URL
, '://');
399 sURL
:= SeparateRight(URL
, '://');
406 s
:= SeparateLeft(sURL
, '@');
407 sURL
:= SeparateRight(sURL
, '@');
411 User
:= SeparateLeft(s
, ':');
412 Pass
:= SeparateRight(s
, ':');
420 s1
:= SeparateLeft(sURL
, '/');
421 s2
:= SeparateRight(sURL
, '/');
431 Host
:= SeparateLeft(s1
, ':');
432 Port
:= SeparateRight(s1
, ':');
440 Path
:= '/' + SeparateLeft(s2
, '?');
441 Para
:= SeparateRight(s2
, '?');
449 {==============================================================================}
451 function StringReplace(Value
, Search
, Replace
: string): string;
453 x
, l
, ls
, lr
: Integer;
455 if (Value
= '') or (Search
= '') then
460 ls
:= Length(Search
);
461 lr
:= Length(Replace
);
463 x
:= Pos(Search
, Value
);
467 SetLength(Result
, l
+ x
- 1);
468 Move(Pointer(Value
)^, Pointer(@Result
[l
+ 1])^, x
- 1);
469 // Result:=Result+Copy(Value,1,x-1);
471 SetLength(Result
, l
+ lr
);
472 Move(Pointer(Replace
)^, Pointer(@Result
[l
+ 1])^, lr
);
473 // Result:=Result+Replace;
474 Delete(Value
, 1, x
- 1 + ls
);
475 x
:= Pos(Search
, Value
);
477 Result
:= Result
+ Value
;
480 {==============================================================================}
482 function RPos(const Sub
, Value
: String): Integer;
489 for n
:= Length(Value
) - l
+ 1 downto 1 do
491 if Copy(Value
, n
, l
) = Sub
then
500 function StrToIntDef(const Value
: string; Default
: Integer) : Integer;
502 if Value
[1]= '$' then Result
:= Hex2Int(Value
)
504 Result
:= Str2Int(Value
);
505 if (Result
= 0) and ((Value
<> '0') and (Value
<> '$0')) then Result
:= Default
;