summaryrefslogtreecommitdiff
path: root/fpcdocs/gtk4ex/futils.pp
blob: 5b22b966b49092e79fc394cbd4311fd6ec253ccf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
unit futils;

{$mode objfpc}
{$h+}

Interface

Const
{$ifdef win32}
  PathSeparator='\';
{$else}
  PathSeparator='/';
{$endif}


Function StripTrailingSeparator(Const Dir : String) : String;
Function AddTrailingSeparator(Const Dir : String) : String;
Function FileSizeToString(Size: Int64) : String;
Function FileAttrsToString(FileAttrs : Integer) : String;

Implementation

Uses sysutils;

Function  StripTrailingSeparator(Const Dir : String) : String;

Var
  L : Integer;

begin
  Result:=Dir;
  L:=Length(result);
  If (L>1) and (Result[l]=PathSeparator) then
    SetLength(Result,L-1);
end;

Function  AddTraiLingSeparator(Const Dir : String) : String;

Var
  L : Integer;

begin
  Result:=Dir;
  L:=Length(Result);
  If (L>0) and (Result[l]<>PathSeparator) then
    Result:=Result+PathSeparator;
end;

Function  FileSizeToString(Size: Int64) : String;

Const
  Sizes : Array [0..4] of String =
     ('Bytes','Kb','Mb','Gb','Tb');
Var
    F : Double;
    I : longint;

begin
  If Size>1024 Then
    begin
    F:=Size;
    I:=0;
    While (F>1024) and (I<4) do
      begin
      F:=F / 1024;
      Inc(i);
      end;
    Result:=Format('%4.2f %s',[F,Sizes[i]]);
    end
  else
    Result:=Format('%d %s',[Size,Sizes[0]]);
end;

Function FileAttrsToString(FileAttrs : Integer) : String;

Const
  Attrs : Array[1..4] of integer =
          (faArchive,faReadOnly,faHidden,faSysfile);
  AttrChars : Array[1..4] of char =
          ('A','R','H','S');

Var
  i : longint;

begin
  Result:='';
  For I:=1 to 4 do
    If (Attrs[i] and FileAttrs)=Attrs[i] then
      Result:=Result+AttrChars[i];
end;

end.