]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/filename.h | |
3 | // Purpose: wxFileName - encapsulates a file path | |
4 | // Author: Robert Roebling, Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 28.12.00 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2000 Robert Roebling | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_FILENAME_H_ | |
13 | #define _WX_FILENAME_H_ | |
14 | ||
15 | #ifdef __GNUG__ | |
16 | #pragma interface "filename.h" | |
17 | #endif | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/string.h" | |
21 | #endif | |
22 | ||
23 | /* | |
24 | TODO: | |
25 | ||
26 | 1. support for drives under Windows | |
27 | 2. more file operations: | |
28 | a) chmod() | |
29 | b) [acm]time() - get and set | |
30 | c) file size | |
31 | d) file permissions with readable accessors for most common bits | |
32 | such as IsReadable() &c | |
33 | e) rename()? | |
34 | 3. SameFileAs() function to compare inodes under Unix | |
35 | */ | |
36 | ||
37 | // ridiculously enough, this will replace DirExists with wxDirExists etc | |
38 | #include "wx/filefn.h" | |
39 | #include "wx/datetime.h" | |
40 | ||
41 | // ---------------------------------------------------------------------------- | |
42 | // constants | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
45 | // the various values for the path format: this mainly affects the path | |
46 | // separator but also whether or not the path has the drive part (as under | |
47 | // Windows) | |
48 | enum wxPathFormat | |
49 | { | |
50 | wxPATH_NATIVE = 0, // the path format for the current platform | |
51 | wxPATH_UNIX, | |
52 | wxPATH_MAC, | |
53 | wxPATH_DOS, | |
54 | wxPATH_VMS, | |
55 | ||
56 | wxPATH_BEOS = wxPATH_UNIX, | |
57 | wxPATH_WIN = wxPATH_DOS, | |
58 | wxPATH_OS2 = wxPATH_DOS | |
59 | }; | |
60 | ||
61 | // the kind of normalization to do with the file name: these values can be | |
62 | // or'd together to perform several operations at once | |
63 | enum wxPathNormalize | |
64 | { | |
65 | wxPATH_NORM_ENV_VARS = 0x0001, // replace env vars with their values | |
66 | wxPATH_NORM_DOTS = 0x0002, // squeeze all .. and . and prepend cwd | |
67 | wxPATH_NORM_TILDE = 0x0004, // Unix only: replace ~ and ~user | |
68 | wxPATH_NORM_CASE = 0x0008, // if case insensitive => tolower | |
69 | wxPATH_NORM_ABSOLUTE = 0x0010, // make the path absolute | |
70 | wxPATH_NORM_LONG = 0x0020, // make the path the long form | |
71 | wxPATH_NORM_ALL = 0x003f | |
72 | }; | |
73 | ||
74 | // ---------------------------------------------------------------------------- | |
75 | // wxFileName: encapsulates a file path | |
76 | // ---------------------------------------------------------------------------- | |
77 | ||
78 | class WXDLLEXPORT wxFileName | |
79 | { | |
80 | public: | |
81 | // constructors and assignment | |
82 | ||
83 | // the usual stuff | |
84 | wxFileName() { } | |
85 | wxFileName( const wxFileName &filepath ) { Assign(filepath); } | |
86 | ||
87 | // from a full filename: if it terminates with a '/', a directory path | |
88 | // is contructed (the name will be empty), otherwise a file name and | |
89 | // extension are extracted from it | |
90 | wxFileName( const wxString& fullpath, wxPathFormat format = wxPATH_NATIVE ) | |
91 | { Assign( fullpath, format ); } | |
92 | ||
93 | // from a directory name and a file name | |
94 | wxFileName(const wxString& path, | |
95 | const wxString& name, | |
96 | wxPathFormat format = wxPATH_NATIVE) | |
97 | { Assign(path, name, format); } | |
98 | ||
99 | // from a directory name, file base name and extension | |
100 | wxFileName(const wxString& path, | |
101 | const wxString& name, | |
102 | const wxString& ext, | |
103 | wxPathFormat format = wxPATH_NATIVE) | |
104 | { Assign(path, name, ext, format); } | |
105 | ||
106 | // the same for delayed initialization | |
107 | ||
108 | void Assign(const wxFileName& filepath); | |
109 | ||
110 | void Assign(const wxString& fullpath, | |
111 | wxPathFormat format = wxPATH_NATIVE); | |
112 | ||
113 | void Assign(const wxString& volume, | |
114 | const wxString& path, | |
115 | const wxString& name, | |
116 | const wxString& ext, | |
117 | wxPathFormat format = wxPATH_NATIVE); | |
118 | ||
119 | void Assign(const wxString& path, | |
120 | const wxString& name, | |
121 | wxPathFormat format = wxPATH_NATIVE); | |
122 | ||
123 | void Assign(const wxString& path, | |
124 | const wxString& name, | |
125 | const wxString& ext, | |
126 | wxPathFormat format = wxPATH_NATIVE) | |
127 | { | |
128 | // empty volume | |
129 | Assign(_T(""), path, name, ext, format); | |
130 | } | |
131 | ||
132 | void AssignDir(const wxString& dir, wxPathFormat format = wxPATH_NATIVE); | |
133 | ||
134 | // assorted assignment operators | |
135 | ||
136 | wxFileName& operator=(const wxFileName& filename) | |
137 | { Assign(filename); return *this; } | |
138 | ||
139 | wxFileName& operator=(const wxString& filename) | |
140 | { Assign(filename); return *this; } | |
141 | ||
142 | // reset all components to default, uninitialized state | |
143 | void Clear(); | |
144 | ||
145 | // static pseudo constructors | |
146 | static wxFileName FileName(const wxString& file); | |
147 | static wxFileName DirName(const wxString& dir); | |
148 | ||
149 | // file tests | |
150 | ||
151 | // is the filename valid at all? | |
152 | bool IsOk() const { return !m_dirs.IsEmpty() || !m_name.IsEmpty(); } | |
153 | ||
154 | // does the file with this name exists? | |
155 | bool FileExists(); | |
156 | static bool FileExists( const wxString &file ); | |
157 | ||
158 | // does the directory with this name exists? | |
159 | bool DirExists(); | |
160 | static bool DirExists( const wxString &dir ); | |
161 | ||
162 | // VZ: also need: IsDirWritable(), IsFileExecutable() &c (TODO) | |
163 | ||
164 | // time functions | |
165 | ||
166 | // set the file creation and last access/mod times | |
167 | // (any of the pointers may be NULL) | |
168 | bool SetTimes(const wxDateTime *dtCreate, | |
169 | const wxDateTime *dtAccess, | |
170 | const wxDateTime *dtMod); | |
171 | ||
172 | // set the access and modification times to the current moment | |
173 | bool Touch(); | |
174 | ||
175 | // return the last access, last modification and last change times | |
176 | // (any of the pointers may be NULL) | |
177 | bool GetTimes(wxDateTime *dtAccess, | |
178 | wxDateTime *dtMod, | |
179 | wxDateTime *dtChange) const; | |
180 | ||
181 | // convenience wrapper: get just the last mod time of the file | |
182 | wxDateTime GetModificationTime() const | |
183 | { | |
184 | wxDateTime dtMod; | |
185 | (void)GetTimes(NULL, &dtMod, NULL); | |
186 | return dtMod; | |
187 | } | |
188 | ||
189 | // various file/dir operations | |
190 | ||
191 | // retrieve the value of the current working directory | |
192 | void AssignCwd(const wxString& volume = wxEmptyString); | |
193 | static wxString GetCwd(const wxString& volume = wxEmptyString); | |
194 | ||
195 | // change the current working directory | |
196 | bool SetCwd(); | |
197 | static bool SetCwd( const wxString &cwd ); | |
198 | ||
199 | // get the value of user home (Unix only mainly) | |
200 | void AssignHomeDir(); | |
201 | static wxString GetHomeDir(); | |
202 | ||
203 | // get a temp file name starting with thespecified prefix | |
204 | void AssignTempFileName( const wxString &prefix ); | |
205 | ||
206 | // directory creation and removal. | |
207 | // if full is TRUE, will try to make each directory in the path. | |
208 | bool Mkdir( int perm = 0777, bool full = FALSE); | |
209 | static bool Mkdir( const wxString &dir, int perm = 0777, bool full = FALSE ); | |
210 | ||
211 | bool Rmdir(); | |
212 | static bool Rmdir( const wxString &dir ); | |
213 | ||
214 | // operations on the path | |
215 | ||
216 | // normalize the path: with the default flags value, the path will be | |
217 | // made absolute, without any ".." and "." and all environment | |
218 | // variables will be expanded in it | |
219 | // | |
220 | // this may be done using another (than current) value of cwd | |
221 | bool Normalize(wxPathNormalize flags = wxPATH_NORM_ALL, | |
222 | const wxString& cwd = wxEmptyString, | |
223 | wxPathFormat format = wxPATH_NATIVE); | |
224 | ||
225 | // Comparison | |
226 | ||
227 | // compares with the rules of this platform | |
228 | bool SameAs(const wxFileName &filepath, | |
229 | wxPathFormat format = wxPATH_NATIVE); | |
230 | ||
231 | // uses the current platform settings | |
232 | bool operator==(const wxFileName& filename) { return SameAs(filename); } | |
233 | bool operator==(const wxString& filename) | |
234 | { return *this == wxFileName(filename); } | |
235 | ||
236 | // Tests | |
237 | ||
238 | // are the file names of this type cases sensitive? | |
239 | static bool IsCaseSensitive( wxPathFormat format = wxPATH_NATIVE ); | |
240 | ||
241 | // is this filename absolute? | |
242 | bool IsAbsolute( wxPathFormat format = wxPATH_NATIVE ); | |
243 | ||
244 | // is this filename relative? | |
245 | bool IsRelative( wxPathFormat format = wxPATH_NATIVE ) | |
246 | { return !IsAbsolute(format); } | |
247 | ||
248 | // Information about path format | |
249 | ||
250 | // get the string separating the volume from the path for this format | |
251 | static wxString GetVolumeSeparator(wxPathFormat format = wxPATH_NATIVE); | |
252 | ||
253 | // get the string of path separators for this format | |
254 | static wxString GetPathSeparators(wxPathFormat format = wxPATH_NATIVE); | |
255 | ||
256 | // is the char a path separator for this format? | |
257 | static bool IsPathSeparator(wxChar ch, wxPathFormat format = wxPATH_NATIVE); | |
258 | ||
259 | // FIXME: what exactly does this do? | |
260 | bool IsWild( wxPathFormat format = wxPATH_NATIVE ); | |
261 | ||
262 | // Dir accessors | |
263 | void AppendDir( const wxString &dir ); | |
264 | void PrependDir( const wxString &dir ); | |
265 | void InsertDir( int before, const wxString &dir ); | |
266 | void RemoveDir( int pos ); | |
267 | size_t GetDirCount() const { return m_dirs.GetCount(); } | |
268 | ||
269 | // Other accessors | |
270 | void SetExt( const wxString &ext ) { m_ext = ext; } | |
271 | wxString GetExt() const { return m_ext; } | |
272 | bool HasExt() const { return !m_ext.empty(); } | |
273 | ||
274 | void SetName( const wxString &name ) { m_name = name; } | |
275 | wxString GetName() const { return m_name; } | |
276 | bool HasName() const { return !m_name.empty(); } | |
277 | ||
278 | void SetVolume( const wxString &volume ) { m_volume = volume; } | |
279 | wxString GetVolume() const { return m_volume; } | |
280 | bool HasVolume() const { return !m_volume.empty(); } | |
281 | ||
282 | // full name is the file name + extension (but without the path) | |
283 | void SetFullName(const wxString& fullname); | |
284 | wxString GetFullName() const; | |
285 | ||
286 | const wxArrayString& GetDirs() const { return m_dirs; } | |
287 | ||
288 | // Construct path only - possibly with the trailing separator | |
289 | wxString GetPath( bool add_separator = FALSE, | |
290 | wxPathFormat format = wxPATH_NATIVE ) const; | |
291 | ||
292 | // more readable synonym | |
293 | wxString GetPathWithSep(wxPathFormat format = wxPATH_NATIVE ) const | |
294 | { return GetPath(TRUE /* add separator */, format); } | |
295 | ||
296 | // Construct full path with name and ext | |
297 | wxString GetFullPath( wxPathFormat format = wxPATH_NATIVE ) const; | |
298 | ||
299 | // Return the short form of the path (returns identity on non-Windows platforms) | |
300 | wxString GetShortPath() const; | |
301 | ||
302 | // Return the long form of the path (returns identity on non-Windows platforms) | |
303 | wxString GetLongPath() const; | |
304 | ||
305 | // various helpers | |
306 | ||
307 | // get the canonical path format for this platform | |
308 | static wxPathFormat GetFormat( wxPathFormat format = wxPATH_NATIVE ); | |
309 | ||
310 | // split a fullpath into the volume, path, (base) name and extension | |
311 | // (all of the pointers can be NULL) | |
312 | static void SplitPath(const wxString& fullpath, | |
313 | wxString *volume, | |
314 | wxString *path, | |
315 | wxString *name, | |
316 | wxString *ext, | |
317 | wxPathFormat format = wxPATH_NATIVE); | |
318 | ||
319 | // compatibility version | |
320 | static void SplitPath(const wxString& fullpath, | |
321 | wxString *path, | |
322 | wxString *name, | |
323 | wxString *ext, | |
324 | wxPathFormat format = wxPATH_NATIVE); | |
325 | ||
326 | private: | |
327 | // the drive/volume/device specification (always empty for Unix) | |
328 | wxString m_volume; | |
329 | ||
330 | // the path components of the file | |
331 | wxArrayString m_dirs; | |
332 | ||
333 | // the file name and extension (empty for directories) | |
334 | wxString m_name, | |
335 | m_ext; | |
336 | }; | |
337 | ||
338 | #endif // _WX_FILENAME_H_ | |
339 |