-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUnit1.pas
executable file
·189 lines (156 loc) · 5.12 KB
/
Unit1.pas
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Controls.Presentation,
FMX.StdCtrls, FMX.ScrollBox, FMX.Memo, FMX.Edit, FMX.Layouts,
FMX.ListBox;
type
TForm1 = class(TForm)
Log: TMemo;
Edit1: TEdit;
Layout1: TLayout;
btnGetInfo: TButton;
chkbxFormat: TCheckBox;
Layout2: TLayout;
Label1: TLabel;
lblFileSystemSize: TLabel;
Label2: TLabel;
lblFileSystemFreeSize: TLabel;
ComboBox1: TComboBox;
procedure btnGetInfoClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
private
List: TStringList;
procedure AddList(Name, Path: string);
{ Private declarations }
public
{ Public declarations }
end;
{ Class helper iOS and OSx }
TMacHelper = class
protected
class function GetFileSytemSizeByKey(sKey: string): Int64;
public
class function GetFileSystemSize: Int64;
class function GetFileSystemFreeSize: Int64;
end;
TByptesHelper = class
public
class function FormatBytesToStr(Bytes: Int64): string;
end;
var
Form1: TForm1;
implementation
uses
Macapi.ObjectiveC
{$IFDEF IOS}
, iOSapi.Foundation
{$ELSE}
, Macapi.Foundation
{$ENDIF IOS}
, System.Math
, System.IOUtils;
{$R *.fmx}
procedure TForm1.FormCreate(Sender: TObject);
begin
List := TStringList.Create;
ComboBox1.Items.Clear;
Self.AddList('/', '/');
Self.AddList('GetTempPath', TPath.GetTempPath);
Self.AddList('GetHomePath', TPath.GetHomePath);
Self.AddList('GetDocumentsPath', TPath.GetDocumentsPath);
Self.AddList('GetSharedDocumentsPath', TPath.GetSharedDocumentsPath);
Self.AddList('GetLibraryPath', TPath.GetLibraryPath);
Self.AddList('GetCachePath', TPath.GetCachePath);
Self.AddList('GetPublicPath', TPath.GetPublicPath);
Self.AddList('GetPicturesPath', TPath.GetPicturesPath);
Self.AddList('GetSharedPicturesPath', TPath.GetSharedPicturesPath);
Self.AddList('GetCameraPath', TPath.GetCameraPath);
Self.AddList('GetSharedCameraPath', TPath.GetSharedCameraPath);
Self.AddList('GetMusicPath', TPath.GetMusicPath);
Self.AddList('GetSharedMusicPath', TPath.GetSharedMusicPath);
Self.AddList('GetMoviesPath', TPath.GetMoviesPath);
Self.AddList('GetSharedMoviesPath', TPath.GetSharedMoviesPath);
Self.AddList('GetAlarmsPath', TPath.GetAlarmsPath);
Self.AddList('GetSharedAlarmsPath', TPath.GetSharedAlarmsPath);
Self.AddList('GetDownloadsPath', TPath.GetDownloadsPath);
Self.AddList('GetSharedDownloadsPath', TPath.GetSharedDownloadsPath);
Self.AddList('GetRingtonesPath', TPath.GetRingtonesPath);
Self.AddList('GetSharedRingtonesPath', TPath.GetSharedRingtonesPath);
ComboBox1.ItemIndex := 0;
end;
procedure TForm1.AddList(Name, Path: string);
begin
ComboBox1.Items.Add(Name);
List.Add(Path);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeAndNil(List);
end;
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
Edit1.Text := List[ComboBox1.ItemIndex];
end;
procedure TForm1.btnGetInfoClick(Sender: TObject);
var
iSize: Int64;
begin
Log.Lines.Clear;
iSize := TMacHelper.GetFileSystemSize;
if chkbxFormat.IsChecked then
lblFileSystemSize.Text := TByptesHelper.FormatBytesToStr(iSize)
else
lblFileSystemSize.Text := iSize.ToString;
iSize := TMacHelper.GetFileSystemFreeSize;
if chkbxFormat.IsChecked then
lblFileSystemFreeSize.Text := TByptesHelper.FormatBytesToStr(iSize)
else
lblFileSystemFreeSize.Text := iSize.ToString;
end;
{ TMacHelper }
class function TMacHelper.GetFileSystemFreeSize: Int64;
begin
Result := Self.GetFileSytemSizeByKey('NSFileSystemFreeSize')
end;
class function TMacHelper.GetFileSystemSize: Int64;
begin
Result := Self.GetFileSytemSizeByKey('NSFileSystemSize')
end;
class function TMacHelper.GetFileSytemSizeByKey(sKey: string): Int64;
var
Dict: NSDictionary;
P: Pointer;
FolderName: string;
const
_FolderName = '/';
_FoundationFwk: string = '/System/Library/Frameworks/Foundation.framework/Foundation'; //unit iOSapi.Foundation;
begin
Result := 0;
FolderName := Form1.Edit1.Text;
Form1.Log.Lines.Add(FolderName);
Dict := TNSFileManager.Wrap(TNSFileManager.OCClass.defaultManager).attributesOfFileSystemForPath(NSStr(FolderName), nil);
if Dict = nil then
Exit;
P := Dict.objectForKey((CocoaNSStringConst(_FoundationFwk, sKey) as ILocalObject).GetObjectID);
if Assigned(P) then
Result := TNSNumber.Wrap(P).unsignedLongLongValue;
end;
{ TByptesHelper }
class function TByptesHelper.FormatBytesToStr(Bytes: Int64): string;
const
Description: Array [0..8] of string = ('Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
Kilo = 1000;
var
i: Integer;
iValue: Extended;
begin
i := 0;
while Bytes > Power(Kilo, i + 1) do
Inc(i);
Result := FormatFloat('###0.##', Bytes / IntPower(Kilo, i)) + ' ' + Description[i];
end;
end.