unit iz_utils;

interface
uses
  windows,iz_types;

  function FloatToStr( f : float ) : string;
  function IntToStr( Num : int ) : string;
  function StrToInt(Value: string): Integer;
  function Qrtf (val : float) : float;

  function FileExists(const FileName: string): bool;      //Расширение файла

  function UpperCase(const S: string): string;            //Перевод в верхний регистр

  function SplitString(s : string; var str : string) : string;

  function zRound(Pos : izPosition_f) : izPosition_i;

implementation

function FloatToStr( f : float ) : string;
begin
  Str( f:3:7, Result );
end;

function IntToStr( Num : int ) : string;
begin
  Str( Num, Result );
end;

function StrToInt(Value: string): Integer;
var
  i: Integer;
begin
  Result:=0;
  if Value='' then exit;
  for i:=1 to Length(Value) do begin
    case Value[i] of
      '1': Result:=Result*10+1;
      '2': Result:=Result*10+2;
      '3': Result:=Result*10+3;
      '4': Result:=Result*10+4;
      '5': Result:=Result*10+5;
      '6': Result:=Result*10+6;
      '7': Result:=Result*10+7;
      '8': Result:=Result*10+8;
      '9': Result:=Result*10+9;
      '0': Result:=Result*10;
    end;
  end;
end;

function Qrtf (val : float) : float;
begin
  result := val*val;
end;

function FileExists( const FileName : string ) : bool;
var
  hFile : THandle;
begin
  hFile := CreateFile(PChar(FileName), 0, 0, nil, OPEN_EXISTING, 0, 0);
  Result := hFile <> INVALID_HANDLE_VALUE;
  if Result = true then CloseHandle(hFile);
end;

function UpperCase(const S: string): string;
var
  Ch: Char;
  L: integer;
  Source, Dest: PChar;
begin
  L := Length(S);
  SetLength(Result, L);
  Source := Pointer(S);
  Dest := Pointer(Result);
  while L <> 0 do
  begin
    Ch := Source^;
    if (Ch >= 'a') and (Ch <= 'z') then Dec(Ch, 32);
    Dest^ := Ch;
    Inc(Source);
    Inc(Dest);
    Dec(L);
  end;
end;

function SplitString(s : string; var str : string) : string;
var
  res : string;
  cnt : integer;
  sl : integer;
begin
  cnt := Pos(s, str);
  sl := Length(s);
  if cnt > 0 then
  begin
    if sl > 1 then
      res := '';
    res := copy(str, 1, cnt-sl);
    str := copy(str, cnt + sl, Length(str)-(cnt-sl-1));
  end
  else
  begin
    res := str;
    str := '';
  end;
  Result := res;
end;

function zRound(Pos : izPosition_f) : izPosition_i;
var
  t_pos : izPosition_i;
begin
  t_pos[0] := round(pos[0]);
  t_pos[1] := round(pos[1]);
  result := t_pos;
end;
  
end.
 