1 |
unit Utils;
|
2 |
|
3 |
interface
|
4 |
uses
|
5 |
Controls,
|
6 |
Graphics, //TColor
|
7 |
IniFiles,
|
8 |
Windows //TFileTime
|
9 |
;
|
10 |
|
11 |
type
|
12 |
TStrArray = array of string;
|
13 |
|
14 |
TUtils = class
|
15 |
class function split(input: string; schar: Char; s: Integer): string;
|
16 |
class function Explode(var a: TStrArray; Border, S: string): Integer;
|
17 |
|
18 |
class procedure RoundCornerOf(Control: TWinControl);
|
19 |
class function BarCodeValid (ACode: string): boolean;
|
20 |
class function CheckSumModulo10(const data:string):string;
|
21 |
|
22 |
class function TColorToHex(Color : TColor) : string;
|
23 |
class function HexToTColor(sColor : string) : TColor;
|
24 |
|
25 |
class function AdobeReaderExists(): Boolean;
|
26 |
|
27 |
class function Sto_GetFmtFileVersion(const FileName: String = ''): String;
|
28 |
|
29 |
class function CheckUrl(url:string): boolean;
|
30 |
|
31 |
class function FileTime2DateTime(FileTime: TFileTime): TDateTime;
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
end;
|
37 |
|
38 |
implementation
|
39 |
|
40 |
uses StrUtils,
|
41 |
Types, //TRect ,
|
42 |
Messages,
|
43 |
SysUtils, //IntToStr etc
|
44 |
Registry,
|
45 |
wininet //CheckUrl
|
46 |
|
47 |
;
|
48 |
|
49 |
{
|
50 |
VERY fast split function
|
51 |
this function returns part of a string based on
|
52 |
constant defineable delimiters, such as ";". So
|
53 |
SPLIT('this is a test ',' ',3) = 'is' or
|
54 |
SPLIT('data;another;yet;again;more;',';',4) = 'yet'
|
55 |
|
56 |
Split function shifts index integer by two to
|
57 |
be compatible with commonly used PD split function
|
58 |
gpl 2004 / Juhani Suhonen
|
59 |
}
|
60 |
class function TUtils.split(input: string; schar: Char; s: Integer): string;
|
61 |
var
|
62 |
c: array of Integer;
|
63 |
b, t: Integer;
|
64 |
begin
|
65 |
Dec(s, 2); // for compatibility with very old & slow split function
|
66 |
t := 0; // variable T needs to be initialized...
|
67 |
setlength(c, Length(input));
|
68 |
for b := 0 to pred(High(c)) do
|
69 |
begin
|
70 |
c[b + 1] := posex(schar, input, succ(c[b]));
|
71 |
// BREAK LOOP if posex looped (position before previous)
|
72 |
// or wanted position reached..
|
73 |
if (c[b + 1] < c[b]) or (s < t) then break
|
74 |
else
|
75 |
Inc(t);
|
76 |
end;
|
77 |
Result := Copy(input, succ(c[s]), pred(c[s + 1] - c[s]));
|
78 |
end;
|
79 |
|
80 |
class function TUtils.Explode(var a: TStrArray; Border, S: string): Integer;
|
81 |
var
|
82 |
S2: string;
|
83 |
begin
|
84 |
Result := 0;
|
85 |
S2 := S + Border;
|
86 |
repeat
|
87 |
SetLength(A, Length(A) + 1);
|
88 |
a[Result] := Copy(S2, 0,Pos(Border, S2) - 1);
|
89 |
Delete(S2, 1,Length(a[Result] + Border));
|
90 |
Inc(Result);
|
91 |
until S2 = '';
|
92 |
end;
|
93 |
|
94 |
|
95 |
|
96 |
class procedure TUtils.RoundCornerOf(Control: TWinControl);
|
97 |
var
|
98 |
R: TRect;
|
99 |
Rgn: HRGN;
|
100 |
begin
|
101 |
with Control do
|
102 |
begin
|
103 |
R := ClientRect;
|
104 |
// rgn := CreateRoundRectRgn(R.Left, R.Top, R.Right, R.Bottom, 20, 20) ;
|
105 |
rgn := CreateRoundRectRgn(R.Left, R.Top, R.Right, R.Bottom, 25, 25) ;
|
106 |
// rgn := CreateRoundRectRgn(R.Left, R.Top, R.Right, R.Bottom, 30, 30) ;
|
107 |
Perform(EM_GETRECT, 0, lParam(@r)) ;
|
108 |
InflateRect(r, - 4, - 4) ;
|
109 |
Perform(EM_SETRECTNP, 0, lParam(@r)) ;
|
110 |
SetWindowRgn(Handle, rgn, True) ;
|
111 |
Invalidate;
|
112 |
end;
|
113 |
end;
|
114 |
|
115 |
|
116 |
class function TUtils.BarCodeValid (ACode: string): boolean;
|
117 |
var
|
118 |
I: integer;
|
119 |
SumOdd, SumEven: integer;
|
120 |
ADigit, AChecksumDigit: integer;
|
121 |
begin
|
122 |
SumOdd := 0;
|
123 |
SumEven := 0;
|
124 |
for I := 1 to (Length (ACode) - 1) do begin
|
125 |
ADigit := StrToIntDef (ACode [I], 0);
|
126 |
if (I MOD 2 = 0) then begin
|
127 |
SumEven := SumEven + ADigit;
|
128 |
end else begin
|
129 |
SumOdd := SumOdd + ADigit;
|
130 |
end; {if}
|
131 |
end; {for}
|
132 |
AChecksumDigit := StrToIntDef (ACode [Length (ACode)], 0);
|
133 |
Result := ((SumOdd*3 + SumEven + AChecksumDigit) MOD 10 = 0);
|
134 |
end; {--BarCodeValid--}
|
135 |
|
136 |
{ used for EAN 8/13 }
|
137 |
class function TUtils.CheckSumModulo10(const data:string):string;
|
138 |
var i,fak,sum : Integer;
|
139 |
begin
|
140 |
sum := 0;
|
141 |
fak := Length(data);
|
142 |
for i:=1 to Length(data) do
|
143 |
begin
|
144 |
if (fak mod 2) = 0 then
|
145 |
sum := sum + (StrToInt(data[i])*1)
|
146 |
else
|
147 |
sum := sum + (StrToInt(data[i])*3);
|
148 |
dec(fak);
|
149 |
end;
|
150 |
if (sum mod 10) = 0 then
|
151 |
result := data+'0'
|
152 |
else
|
153 |
result := data+IntToStr(10-(sum mod 10));
|
154 |
end;
|
155 |
|
156 |
|
157 |
class function TUtils.TColorToHex(Color : TColor) : string;
|
158 |
begin
|
159 |
Result :=
|
160 |
IntToHex(GetRValue(Color), 2) +
|
161 |
IntToHex(GetGValue(Color), 2) +
|
162 |
IntToHex(GetBValue(Color), 2) ;
|
163 |
end;
|
164 |
|
165 |
class function TUtils.HexToTColor(sColor : string) : TColor;
|
166 |
begin
|
167 |
Result :=
|
168 |
RGB(
|
169 |
StrToInt('$'+Copy(sColor, 1, 2)),
|
170 |
StrToInt('$'+Copy(sColor, 3, 2)),
|
171 |
StrToInt('$'+Copy(sColor, 5, 2))
|
172 |
) ;
|
173 |
end;
|
174 |
|
175 |
|
176 |
class function TUtils.AdobeReaderExists(): Boolean;
|
177 |
var
|
178 |
AReg: TRegistry;
|
179 |
begin
|
180 |
result:= false;
|
181 |
AReg := TRegistry.Create;
|
182 |
AReg.RootKey := HKEY_LOCAL_MACHINE;
|
183 |
if AReg.KeyExists('\SOFTWARE\Adobe\Acrobat Reader') then
|
184 |
result:= True;
|
185 |
AReg.Free;
|
186 |
end;
|
187 |
|
188 |
|
189 |
/// <summary>
|
190 |
/// This function reads the file resource of "FileName" and returns
|
191 |
/// the version number as formatted text.</summary>
|
192 |
/// <example>
|
193 |
/// Sto_GetFmtFileVersion() = '4.13.128.0'
|
194 |
/// Sto_GetFmtFileVersion('', '%.2d-%.2d-%.2d') = '04-13-128'
|
195 |
/// </example>
|
196 |
/// <remarks>If "Fmt" is invalid, the function may raise an
|
197 |
/// EConvertError exception.</remarks>
|
198 |
/// <param name="FileName">Full path to exe or dll. If an empty
|
199 |
/// string is passed, the function uses the filename of the
|
200 |
/// running exe or dll.</param>
|
201 |
/// <param name="Fmt">Format string, you can use at most four integer
|
202 |
/// values.</param>
|
203 |
/// <returns>Formatted version number of file, '' if no version
|
204 |
/// resource found.</returns>
|
205 |
class function TUtils.Sto_GetFmtFileVersion(const FileName: String = ''): String;
|
206 |
var
|
207 |
sFileName: String;
|
208 |
iBufferSize: DWORD;
|
209 |
iDummy: DWORD;
|
210 |
pBuffer: Pointer;
|
211 |
pFileInfo: Pointer;
|
212 |
iVer: array[1..4] of Integer;
|
213 |
begin
|
214 |
// set default value
|
215 |
Result := '';
|
216 |
// get filename of exe/dll if no filename is specified
|
217 |
sFileName := Trim(FileName);
|
218 |
if (sFileName = '') then
|
219 |
sFileName := GetModuleName(HInstance);
|
220 |
// get size of version info (0 if no version info exists)
|
221 |
iBufferSize := GetFileVersionInfoSize(PChar(sFileName), iDummy);
|
222 |
if (iBufferSize > 0) then
|
223 |
begin
|
224 |
GetMem(pBuffer, iBufferSize);
|
225 |
try
|
226 |
// get fixed file info (language independent)
|
227 |
GetFileVersionInfo(PChar(sFileName), 0, iBufferSize, pBuffer);
|
228 |
VerQueryValue(pBuffer, '\', pFileInfo, iDummy);
|
229 |
// read version blocks
|
230 |
iVer[1] := HiWord(PVSFixedFileInfo(pFileInfo)^.dwFileVersionMS);
|
231 |
iVer[2] := LoWord(PVSFixedFileInfo(pFileInfo)^.dwFileVersionMS);
|
232 |
iVer[3] := HiWord(PVSFixedFileInfo(pFileInfo)^.dwFileVersionLS);
|
233 |
iVer[4] := LoWord(PVSFixedFileInfo(pFileInfo)^.dwFileVersionLS);
|
234 |
finally
|
235 |
FreeMem(pBuffer);
|
236 |
end;
|
237 |
|
238 |
// format result string
|
239 |
Result := Format('%d.%d.%d.%d', [iVer[1], iVer[2], iVer[3], iVer[4]]);
|
240 |
|
241 |
end;
|
242 |
end;
|
243 |
|
244 |
|
245 |
class function TUtils.CheckUrl(url:string):boolean;
|
246 |
var
|
247 |
hSession, hfile: hInternet;
|
248 |
dwindex,dwcodelen :dword;
|
249 |
dwcode:array[1..20] of char;
|
250 |
res : pchar;
|
251 |
begin
|
252 |
if pos('http://',lowercase(url))=0 then
|
253 |
url := 'http://'+url;
|
254 |
Result := false;
|
255 |
hSession := InternetOpen('InetURL:/1.0',
|
256 |
INTERNET_OPEN_TYPE_PRECONFIG,
|
257 |
nil,
|
258 |
nil,
|
259 |
0);
|
260 |
if assigned(hsession) then
|
261 |
begin
|
262 |
hfile := InternetOpenUrl(hsession,
|
263 |
pchar(url),
|
264 |
nil,
|
265 |
0,
|
266 |
INTERNET_FLAG_RELOAD,
|
267 |
0);
|
268 |
dwIndex := 0;
|
269 |
dwCodeLen := 10;
|
270 |
HttpQueryInfo(hfile,
|
271 |
HTTP_QUERY_STATUS_CODE,
|
272 |
@dwcode,
|
273 |
dwcodeLen,
|
274 |
dwIndex);
|
275 |
res := pchar(@dwcode);
|
276 |
result:= (res ='200') or (res ='302');
|
277 |
if assigned(hfile) then
|
278 |
InternetCloseHandle(hfile);
|
279 |
InternetCloseHandle(hsession);
|
280 |
end;
|
281 |
|
282 |
end;
|
283 |
|
284 |
|
285 |
class function TUtils.FileTime2DateTime(FileTime: TFileTime): TDateTime;
|
286 |
var
|
287 |
LocalFileTime: TFileTime;
|
288 |
SystemTime: TSystemTime;
|
289 |
begin
|
290 |
FileTimeToLocalFileTime(FileTime, LocalFileTime) ;
|
291 |
FileTimeToSystemTime(LocalFileTime, SystemTime) ;
|
292 |
Result := SystemTimeToDateTime(SystemTime) ;
|
293 |
end;
|
294 |
|
295 |
|
296 |
end.
|