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

Contents of /dao/DelphiScanner/Utils.pas

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20