source: ObjectBaseTypes/Level 2/UFileSystem.pas

Last change on this file was 14, checked in by george, 15 years ago
  • Upraveno: Rozšíření sady tříd objektových typů.
File size: 5.3 KB
Line 
1unit UFileSystem;
2
3interface
4
5uses
6 Types, SysUtils;
7
8type
9 TDirectory = class;
10
11 TFilePath = class
12 private
13 FPath: string;
14 function GetDirectory: string;
15 function GetDrive: string;
16 function GetName: string;
17 procedure SetDirectory(const Value: string);
18 procedure SetDrive(const Value: string);
19 procedure SetExtension(const Value: string);
20 procedure SetName(const Value: string);
21 function GetExtension: string;
22 public
23 procedure Expand;
24 procedure ExtractRelativePath(const BaseName, DestName: string);
25 property Path: string read FPath write FPath;
26 property Drive: string read GetDrive write SetDrive;
27 property Directory: string read GetDirectory write SetDirectory;
28 property Name: string read GetName write SetName;
29 property Extension: string read GetExtension write SetExtension;
30 end;
31
32 TFile = class
33 private
34 Parent: TDirectory;
35 FFile: File;
36 function GetFileSize: Int64;
37 procedure Remove;
38 public
39 AccessTime: TDateTime;
40 CreateTime: TDateTime;
41 ModifyTime: TDateTime;
42 Name: string;
43 property Size: Int64 read GetFileSize;
44 function Exists: Boolean;
45 procedure Rename(NewName: string);
46 constructor Create;
47 end;
48
49 TFileSearch = class
50 SearchRec: TSearchRec;
51 function FindFirst: TFile;
52 function FindNext: TFile;
53 destructor Destroy; override;
54 end;
55
56 TFileSystem = class;
57
58 TDirectory = class
59 private
60 FileSystem: TFileSystem;
61 Parent: TDirectory;
62 FFullPath: string;
63 function GetFullPath: string;
64 public
65 Name: string;
66 function CreateFile(Name: string): TFile;
67 function CreateDirectory(Name: string): TDirectory;
68 procedure RemoveDirectory(Directory: TDirectory);
69 function FindFirstFile(Filter: string): TFileSearch;
70 property FullPath: string read GetFullPath;
71 function Exists: Boolean;
72 end;
73
74 TFileSystem = class
75 private
76 function GetSize: Int64;
77 function GetFreeSpace: Int64;
78 public
79 Drive: Byte;
80 function GetLetter: Char;
81 function GetRootDirectory: TDirectory;
82 function GetCurrentDirectory: TDirectory;
83 procedure SetCurrentDirectory(Directory: TDirectory);
84 property FreeSpace: Int64 read GetFreeSpace;
85 property Size: Int64 read GetSize;
86 end;
87
88implementation
89
90{ TFileSystem }
91
92function TFileSystem.GetCurrentDirectory: TDirectory;
93begin
94 Result := TDirectory.Create;
95 Result.FileSystem := Self;
96 Result.FFullPath := GetCurrentDir;
97end;
98
99function TFileSystem.GetFreeSpace: Int64;
100begin
101 Result := DiskFree(Drive);
102end;
103
104function TFileSystem.GetLetter: Char;
105begin
106 Result := Chr(Ord('A') + Drive - 1);
107end;
108
109function TFileSystem.GetRootDirectory: TDirectory;
110begin
111 Result := TDirectory.Create;
112 Result.FileSystem := Self;
113 Result.FFullPath := GetLetter + ':\';
114end;
115
116function TFileSystem.GetSize: Int64;
117begin
118 Result := DiskSize(Drive);
119end;
120
121procedure TFileSystem.SetCurrentDirectory(Directory: TDirectory);
122begin
123 SetCurrentDir(Directory.FFullPath);
124end;
125
126{ TDirectory }
127
128function TDirectory.CreateDirectory(Name: string): TDirectory;
129begin
130 Result := TDirectory.Create;
131 Result.Parent := Self;
132 Result.Name := Name;
133 SysUtils.CreateDir(Result.FFullPath);
134end;
135
136function TDirectory.CreateFile(Name: string): TFile;
137begin
138 Result := TFile.Create;
139 Result.Parent := Self;
140 Result.Name := Name;
141 Result.Create;
142end;
143
144function TDirectory.Exists: Boolean;
145begin
146 Result := DirectoryExists(FullPath);
147end;
148
149function TDirectory.FindFirstFile(Filter: string): TFileSearch;
150begin
151 Result := TFileSearch.Create;
152 SysUtils.FindFirst(FullPath + filter, $2f, Result.SearchRec);
153end;
154
155function TDirectory.GetFullPath: string;
156begin
157
158end;
159
160procedure TDirectory.RemoveDirectory(Directory: TDirectory);
161begin
162 SysUtils.RemoveDir(Directory.FFullPath);
163end;
164
165{ TFile }
166
167constructor TFile.Create;
168begin
169end;
170
171function TFile.Exists: Boolean;
172begin
173 Result := FileExists(Name);
174end;
175
176function TFile.GetFileSize: Int64;
177begin
178 Result := FileSize(FFile);
179end;
180
181procedure TFile.Remove;
182begin
183 Truncate(FFile);
184end;
185
186procedure TFile.Rename(NewName: string);
187begin
188 RenameFile(Name, NewName);
189end;
190
191{ TFileSearch }
192
193destructor TFileSearch.Destroy;
194begin
195 FindClose(SearchRec);
196 inherited;
197end;
198
199function TFileSearch.FindFirst: TFile;
200begin
201
202end;
203
204function TFileSearch.FindNext: TFile;
205begin
206 SysUtils.FindNext(SearchRec);
207end;
208
209{ TFilePath }
210
211procedure TFilePath.Expand;
212begin
213 FPath := ExpandFileName(FPath);
214end;
215
216procedure TFilePath.ExtractRelativePath(const BaseName, DestName: string);
217begin
218 Path := SysUtils.ExtractRelativePath(BaseName, DestName);
219end;
220
221function TFilePath.GetDirectory: string;
222begin
223 Result := ExtractFilePath(Path);
224end;
225
226function TFilePath.GetDrive: string;
227begin
228 Result := ExtractFileDrive(Path);
229end;
230
231function TFilePath.GetExtension: string;
232begin
233 Result := ExtractFileExt(Path);
234end;
235
236function TFilePath.GetName: string;
237begin
238 Result := ExtractFileName(Path);
239end;
240
241procedure TFilePath.SetDirectory(const Value: string);
242begin
243
244end;
245
246procedure TFilePath.SetDrive(const Value: string);
247begin
248
249end;
250
251procedure TFilePath.SetExtension(const Value: string);
252begin
253
254end;
255
256procedure TFilePath.SetName(const Value: string);
257begin
258
259end;
260
261end.
Note: See TracBrowser for help on using the repository browser.