/[projects]/dao/DelphiScanner/Utils.pas
ViewVC logotype

Contents of /dao/DelphiScanner/Utils.pas

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20