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

Annotation of /dao/DelphiScanner/Utils.pas

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20