unit Utils;
interface
uses
Controls,
Graphics, //TColor
IniFiles,
Windows //TFileTime
;
type
TStrArray = array of string;
TUtils = class
class function split(input: string; schar: Char; s: Integer): string;
class function Explode(var a: TStrArray; Border, S: string): Integer;
class procedure RoundCornerOf(Control: TWinControl);
class function BarCodeValid (ACode: string): boolean;
class function CheckSumModulo10(const data:string):string;
class function TColorToHex(Color : TColor) : string;
class function HexToTColor(sColor : string) : TColor;
class function AdobeReaderExists(): Boolean;
class function Sto_GetFmtFileVersion(const FileName: String = ''): String;
class function CheckUrl(url:string): boolean;
class function FileTime2DateTime(FileTime: TFileTime): TDateTime;
end;
implementation
uses StrUtils,
Types, //TRect ,
Messages,
SysUtils, //IntToStr etc
Registry,
wininet //CheckUrl
;
{
VERY fast split function
this function returns part of a string based on
constant defineable delimiters, such as ";". So
SPLIT('this is a test ',' ',3) = 'is' or
SPLIT('data;another;yet;again;more;',';',4) = 'yet'
Split function shifts index integer by two to
be compatible with commonly used PD split function
gpl 2004 / Juhani Suhonen
}
class function TUtils.split(input: string; schar: Char; s: Integer): string;
var
c: array of Integer;
b, t: Integer;
begin
Dec(s, 2); // for compatibility with very old & slow split function
t := 0; // variable T needs to be initialized...
setlength(c, Length(input));
for b := 0 to pred(High(c)) do
begin
c[b + 1] := posex(schar, input, succ(c[b]));
// BREAK LOOP if posex looped (position before previous)
// or wanted position reached..
if (c[b + 1] < c[b]) or (s < t) then break
else
Inc(t);
end;
Result := Copy(input, succ(c[s]), pred(c[s + 1] - c[s]));
end;
class function TUtils.Explode(var a: TStrArray; Border, S: string): Integer;
var
S2: string;
begin
Result := 0;
S2 := S + Border;
repeat
SetLength(A, Length(A) + 1);
a[Result] := Copy(S2, 0,Pos(Border, S2) - 1);
Delete(S2, 1,Length(a[Result] + Border));
Inc(Result);
until S2 = '';
end;
class procedure TUtils.RoundCornerOf(Control: TWinControl);
var
R: TRect;
Rgn: HRGN;
begin
with Control do
begin
R := ClientRect;
// rgn := CreateRoundRectRgn(R.Left, R.Top, R.Right, R.Bottom, 20, 20) ;
rgn := CreateRoundRectRgn(R.Left, R.Top, R.Right, R.Bottom, 25, 25) ;
// rgn := CreateRoundRectRgn(R.Left, R.Top, R.Right, R.Bottom, 30, 30) ;
Perform(EM_GETRECT, 0, lParam(@r)) ;
InflateRect(r, - 4, - 4) ;
Perform(EM_SETRECTNP, 0, lParam(@r)) ;
SetWindowRgn(Handle, rgn, True) ;
Invalidate;
end;
end;
class function TUtils.BarCodeValid (ACode: string): boolean;
var
I: integer;
SumOdd, SumEven: integer;
ADigit, AChecksumDigit: integer;
begin
SumOdd := 0;
SumEven := 0;
for I := 1 to (Length (ACode) - 1) do begin
ADigit := StrToIntDef (ACode [I], 0);
if (I MOD 2 = 0) then begin
SumEven := SumEven + ADigit;
end else begin
SumOdd := SumOdd + ADigit;
end; {if}
end; {for}
AChecksumDigit := StrToIntDef (ACode [Length (ACode)], 0);
Result := ((SumOdd*3 + SumEven + AChecksumDigit) MOD 10 = 0);
end; {--BarCodeValid--}
{ used for EAN 8/13 }
class function TUtils.CheckSumModulo10(const data:string):string;
var i,fak,sum : Integer;
begin
sum := 0;
fak := Length(data);
for i:=1 to Length(data) do
begin
if (fak mod 2) = 0 then
sum := sum + (StrToInt(data[i])*1)
else
sum := sum + (StrToInt(data[i])*3);
dec(fak);
end;
if (sum mod 10) = 0 then
result := data+'0'
else
result := data+IntToStr(10-(sum mod 10));
end;
class function TUtils.TColorToHex(Color : TColor) : string;
begin
Result :=
IntToHex(GetRValue(Color), 2) +
IntToHex(GetGValue(Color), 2) +
IntToHex(GetBValue(Color), 2) ;
end;
class function TUtils.HexToTColor(sColor : string) : TColor;
begin
Result :=
RGB(
StrToInt('$'+Copy(sColor, 1, 2)),
StrToInt('$'+Copy(sColor, 3, 2)),
StrToInt('$'+Copy(sColor, 5, 2))
) ;
end;
class function TUtils.AdobeReaderExists(): Boolean;
var
AReg: TRegistry;
begin
result:= false;
AReg := TRegistry.Create;
AReg.RootKey := HKEY_LOCAL_MACHINE;
if AReg.KeyExists('\SOFTWARE\Adobe\Acrobat Reader') then
result:= True;
AReg.Free;
end;
///
/// This function reads the file resource of "FileName" and returns
/// the version number as formatted text.
///
/// Sto_GetFmtFileVersion() = '4.13.128.0'
/// Sto_GetFmtFileVersion('', '%.2d-%.2d-%.2d') = '04-13-128'
///
/// If "Fmt" is invalid, the function may raise an
/// EConvertError exception.
/// Full path to exe or dll. If an empty
/// string is passed, the function uses the filename of the
/// running exe or dll.
/// Format string, you can use at most four integer
/// values.
/// Formatted version number of file, '' if no version
/// resource found.
class function TUtils.Sto_GetFmtFileVersion(const FileName: String = ''): String;
var
sFileName: String;
iBufferSize: DWORD;
iDummy: DWORD;
pBuffer: Pointer;
pFileInfo: Pointer;
iVer: array[1..4] of Integer;
begin
// set default value
Result := '';
// get filename of exe/dll if no filename is specified
sFileName := Trim(FileName);
if (sFileName = '') then
sFileName := GetModuleName(HInstance);
// get size of version info (0 if no version info exists)
iBufferSize := GetFileVersionInfoSize(PChar(sFileName), iDummy);
if (iBufferSize > 0) then
begin
GetMem(pBuffer, iBufferSize);
try
// get fixed file info (language independent)
GetFileVersionInfo(PChar(sFileName), 0, iBufferSize, pBuffer);
VerQueryValue(pBuffer, '\', pFileInfo, iDummy);
// read version blocks
iVer[1] := HiWord(PVSFixedFileInfo(pFileInfo)^.dwFileVersionMS);
iVer[2] := LoWord(PVSFixedFileInfo(pFileInfo)^.dwFileVersionMS);
iVer[3] := HiWord(PVSFixedFileInfo(pFileInfo)^.dwFileVersionLS);
iVer[4] := LoWord(PVSFixedFileInfo(pFileInfo)^.dwFileVersionLS);
finally
FreeMem(pBuffer);
end;
// format result string
Result := Format('%d.%d.%d.%d', [iVer[1], iVer[2], iVer[3], iVer[4]]);
end;
end;
class function TUtils.CheckUrl(url:string):boolean;
var
hSession, hfile: hInternet;
dwindex,dwcodelen :dword;
dwcode:array[1..20] of char;
res : pchar;
begin
if pos('http://',lowercase(url))=0 then
url := 'http://'+url;
Result := false;
hSession := InternetOpen('InetURL:/1.0',
INTERNET_OPEN_TYPE_PRECONFIG,
nil,
nil,
0);
if assigned(hsession) then
begin
hfile := InternetOpenUrl(hsession,
pchar(url),
nil,
0,
INTERNET_FLAG_RELOAD,
0);
dwIndex := 0;
dwCodeLen := 10;
HttpQueryInfo(hfile,
HTTP_QUERY_STATUS_CODE,
@dwcode,
dwcodeLen,
dwIndex);
res := pchar(@dwcode);
result:= (res ='200') or (res ='302');
if assigned(hfile) then
InternetCloseHandle(hfile);
InternetCloseHandle(hsession);
end;
end;
class function TUtils.FileTime2DateTime(FileTime: TFileTime): TDateTime;
var
LocalFileTime: TFileTime;
SystemTime: TSystemTime;
begin
FileTimeToLocalFileTime(FileTime, LocalFileTime) ;
FileTimeToSystemTime(LocalFileTime, SystemTime) ;
Result := SystemTimeToDateTime(SystemTime) ;
end;
end.