]>
Commit | Line | Data |
---|---|---|
df5ddbca | 1 | ///////////////////////////////////////////////////////////////////////////// |
097ead30 VZ |
2 | // Name: wx/filename.h |
3 | // Purpose: wxFileName - encapsulates a file path | |
844f90fb | 4 | // Author: Robert Roebling, Vadim Zeitlin |
df5ddbca RR |
5 | // Modified by: |
6 | // Created: 28.12.00 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2000 Robert Roebling | |
371a5b4e | 9 | // Licence: wxWindows licence |
df5ddbca RR |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifndef _WX_FILENAME_H_ | |
13 | #define _WX_FILENAME_H_ | |
14 | ||
af49c4b8 | 15 | #if defined(__GNUG__) && !defined(__APPLE__) |
df5ddbca RR |
16 | #pragma interface "filename.h" |
17 | #endif | |
18 | ||
19 | #ifndef WX_PRECOMP | |
9e8d8607 | 20 | #include "wx/string.h" |
df5168c4 | 21 | #include "wx/arrstr.h" |
df5ddbca RR |
22 | #endif |
23 | ||
9e8d8607 VZ |
24 | /* |
25 | TODO: | |
26 | ||
27 | 1. support for drives under Windows | |
28 | 2. more file operations: | |
29 | a) chmod() | |
30 | b) [acm]time() - get and set | |
31 | c) file size | |
32 | d) file permissions with readable accessors for most common bits | |
33 | such as IsReadable() &c | |
34 | e) rename()? | |
35 | 3. SameFileAs() function to compare inodes under Unix | |
36 | */ | |
37 | ||
df5ddbca | 38 | // ridiculously enough, this will replace DirExists with wxDirExists etc |
ee66f092 | 39 | #include "wx/filefn.h" |
ce16e5d7 | 40 | #include "wx/datetime.h" |
df5ddbca | 41 | |
bddd7a8d | 42 | class WXDLLIMPEXP_BASE wxFile; |
df22f860 | 43 | |
097ead30 VZ |
44 | // ---------------------------------------------------------------------------- |
45 | // constants | |
46 | // ---------------------------------------------------------------------------- | |
47 | ||
48 | // the various values for the path format: this mainly affects the path | |
49 | // separator but also whether or not the path has the drive part (as under | |
50 | // Windows) | |
df5ddbca RR |
51 | enum wxPathFormat |
52 | { | |
844f90fb | 53 | wxPATH_NATIVE = 0, // the path format for the current platform |
097ead30 VZ |
54 | wxPATH_UNIX, |
55 | wxPATH_MAC, | |
56 | wxPATH_DOS, | |
3c621059 | 57 | wxPATH_VMS, |
ee66f092 | 58 | |
097ead30 VZ |
59 | wxPATH_BEOS = wxPATH_UNIX, |
60 | wxPATH_WIN = wxPATH_DOS, | |
61 | wxPATH_OS2 = wxPATH_DOS | |
62 | }; | |
ee66f092 | 63 | |
097ead30 VZ |
64 | // the kind of normalization to do with the file name: these values can be |
65 | // or'd together to perform several operations at once | |
66 | enum wxPathNormalize | |
67 | { | |
68 | wxPATH_NORM_ENV_VARS = 0x0001, // replace env vars with their values | |
844f90fb | 69 | wxPATH_NORM_DOTS = 0x0002, // squeeze all .. and . and prepend cwd |
097ead30 | 70 | wxPATH_NORM_TILDE = 0x0004, // Unix only: replace ~ and ~user |
844f90fb VZ |
71 | wxPATH_NORM_CASE = 0x0008, // if case insensitive => tolower |
72 | wxPATH_NORM_ABSOLUTE = 0x0010, // make the path absolute | |
9e9b65c1 JS |
73 | wxPATH_NORM_LONG = 0x0020, // make the path the long form |
74 | wxPATH_NORM_ALL = 0x003f | |
df5ddbca RR |
75 | }; |
76 | ||
33b97389 VZ |
77 | // what exactly should GetPath() return? |
78 | enum | |
79 | { | |
80 | wxPATH_GET_VOLUME = 0x0001, // include the volume if applicable | |
81 | wxPATH_GET_SEPARATOR = 0x0002 // terminate the path with the separator | |
82 | }; | |
83 | ||
1527281e VZ |
84 | // MkDir flags |
85 | enum | |
86 | { | |
87 | wxPATH_MKDIR_FULL = 0x0001 // create directories recursively | |
88 | }; | |
89 | ||
097ead30 VZ |
90 | // ---------------------------------------------------------------------------- |
91 | // wxFileName: encapsulates a file path | |
92 | // ---------------------------------------------------------------------------- | |
93 | ||
bddd7a8d | 94 | class WXDLLIMPEXP_BASE wxFileName |
df5ddbca RR |
95 | { |
96 | public: | |
97 | // constructors and assignment | |
844f90fb VZ |
98 | |
99 | // the usual stuff | |
fb969475 | 100 | wxFileName() { Clear(); } |
844f90fb VZ |
101 | wxFileName( const wxFileName &filepath ) { Assign(filepath); } |
102 | ||
103 | // from a full filename: if it terminates with a '/', a directory path | |
104 | // is contructed (the name will be empty), otherwise a file name and | |
105 | // extension are extracted from it | |
106 | wxFileName( const wxString& fullpath, wxPathFormat format = wxPATH_NATIVE ) | |
107 | { Assign( fullpath, format ); } | |
108 | ||
109 | // from a directory name and a file name | |
110 | wxFileName(const wxString& path, | |
111 | const wxString& name, | |
112 | wxPathFormat format = wxPATH_NATIVE) | |
113 | { Assign(path, name, format); } | |
114 | ||
81f25632 VZ |
115 | // from a volume, directory name, file base name and extension |
116 | wxFileName(const wxString& volume, | |
117 | const wxString& path, | |
118 | const wxString& name, | |
119 | const wxString& ext, | |
120 | wxPathFormat format = wxPATH_NATIVE) | |
121 | { Assign(volume, path, name, ext, format); } | |
122 | ||
844f90fb VZ |
123 | // from a directory name, file base name and extension |
124 | wxFileName(const wxString& path, | |
125 | const wxString& name, | |
126 | const wxString& ext, | |
127 | wxPathFormat format = wxPATH_NATIVE) | |
128 | { Assign(path, name, ext, format); } | |
129 | ||
844f90fb VZ |
130 | // the same for delayed initialization |
131 | ||
844f90fb | 132 | void Assign(const wxFileName& filepath); |
04c943b1 | 133 | |
844f90fb VZ |
134 | void Assign(const wxString& fullpath, |
135 | wxPathFormat format = wxPATH_NATIVE); | |
04c943b1 VZ |
136 | |
137 | void Assign(const wxString& volume, | |
138 | const wxString& path, | |
139 | const wxString& name, | |
140 | const wxString& ext, | |
141 | wxPathFormat format = wxPATH_NATIVE); | |
142 | ||
844f90fb VZ |
143 | void Assign(const wxString& path, |
144 | const wxString& name, | |
145 | wxPathFormat format = wxPATH_NATIVE); | |
04c943b1 | 146 | |
844f90fb VZ |
147 | void Assign(const wxString& path, |
148 | const wxString& name, | |
149 | const wxString& ext, | |
04c943b1 VZ |
150 | wxPathFormat format = wxPATH_NATIVE) |
151 | { | |
152 | // empty volume | |
fda7962d | 153 | Assign(wxEmptyString, path, name, ext, format); |
04c943b1 VZ |
154 | } |
155 | ||
52dbd056 | 156 | void AssignDir(const wxString& dir, wxPathFormat format = wxPATH_NATIVE); |
844f90fb | 157 | |
788722ac JS |
158 | // assorted assignment operators |
159 | ||
160 | wxFileName& operator=(const wxFileName& filename) | |
161 | { Assign(filename); return *this; } | |
162 | ||
163 | wxFileName& operator=(const wxString& filename) | |
164 | { Assign(filename); return *this; } | |
165 | ||
844f90fb VZ |
166 | // reset all components to default, uninitialized state |
167 | void Clear(); | |
168 | ||
169 | // static pseudo constructors | |
170 | static wxFileName FileName(const wxString& file); | |
171 | static wxFileName DirName(const wxString& dir); | |
172 | ||
951cd180 | 173 | // file tests |
844f90fb VZ |
174 | |
175 | // is the filename valid at all? | |
df5168c4 | 176 | bool IsOk() const { return m_dirs.size() != 0 || !m_name.IsEmpty(); } |
844f90fb VZ |
177 | |
178 | // does the file with this name exists? | |
8e41796c | 179 | bool FileExists() const; |
a35b27b1 | 180 | static bool FileExists( const wxString &file ); |
097ead30 | 181 | |
844f90fb | 182 | // does the directory with this name exists? |
8e41796c | 183 | bool DirExists() const; |
a35b27b1 | 184 | static bool DirExists( const wxString &dir ); |
097ead30 | 185 | |
844f90fb VZ |
186 | // VZ: also need: IsDirWritable(), IsFileExecutable() &c (TODO) |
187 | ||
951cd180 | 188 | // time functions |
e2b87f38 | 189 | #if wxUSE_DATETIME |
6dbb903b | 190 | // set the file last access/mod and creation times |
951cd180 | 191 | // (any of the pointers may be NULL) |
6dbb903b VZ |
192 | bool SetTimes(const wxDateTime *dtAccess, |
193 | const wxDateTime *dtMod, | |
194 | const wxDateTime *dtCreate); | |
951cd180 VZ |
195 | |
196 | // set the access and modification times to the current moment | |
197 | bool Touch(); | |
198 | ||
6dbb903b | 199 | // return the last access, last modification and create times |
951cd180 VZ |
200 | // (any of the pointers may be NULL) |
201 | bool GetTimes(wxDateTime *dtAccess, | |
202 | wxDateTime *dtMod, | |
6dbb903b | 203 | wxDateTime *dtCreate) const; |
951cd180 VZ |
204 | |
205 | // convenience wrapper: get just the last mod time of the file | |
206 | wxDateTime GetModificationTime() const | |
207 | { | |
208 | wxDateTime dtMod; | |
209 | (void)GetTimes(NULL, &dtMod, NULL); | |
210 | return dtMod; | |
211 | } | |
e2b87f38 | 212 | #endif // wxUSE_DATETIME |
951cd180 | 213 | |
4dfbcdd5 SC |
214 | #ifdef __WXMAC__ |
215 | bool MacSetTypeAndCreator( wxUint32 type , wxUint32 creator ) ; | |
216 | bool MacGetTypeAndCreator( wxUint32 *type , wxUint32 *creator ) ; | |
217 | // gets the 'common' type and creator for a certain extension | |
218 | static bool MacFindDefaultTypeAndCreator( const wxString& ext , wxUint32 *type , wxUint32 *creator ) ; | |
219 | // registers application defined extensions and their default type and creator | |
220 | static void MacRegisterDefaultTypeAndCreator( const wxString& ext , wxUint32 type , wxUint32 creator ) ; | |
221 | // looks up the appropriate type and creator from the registration and then sets | |
222 | bool MacSetDefaultTypeAndCreator() ; | |
223 | #endif | |
fb969475 | 224 | |
844f90fb VZ |
225 | // various file/dir operations |
226 | ||
227 | // retrieve the value of the current working directory | |
6f91bc33 VZ |
228 | void AssignCwd(const wxString& volume = wxEmptyString); |
229 | static wxString GetCwd(const wxString& volume = wxEmptyString); | |
097ead30 | 230 | |
844f90fb | 231 | // change the current working directory |
a35b27b1 RR |
232 | bool SetCwd(); |
233 | static bool SetCwd( const wxString &cwd ); | |
097ead30 | 234 | |
844f90fb | 235 | // get the value of user home (Unix only mainly) |
a35b27b1 RR |
236 | void AssignHomeDir(); |
237 | static wxString GetHomeDir(); | |
097ead30 | 238 | |
df22f860 VZ |
239 | // get a temp file name starting with the specified prefix and open the |
240 | // file passed to us using this name for writing (atomically if | |
241 | // possible) | |
242 | void AssignTempFileName(const wxString& prefix, wxFile *fileTemp = NULL); | |
243 | static wxString CreateTempFileName(const wxString& prefix, | |
244 | wxFile *fileTemp = NULL); | |
097ead30 | 245 | |
f0ce3409 JS |
246 | // directory creation and removal. |
247 | // if full is TRUE, will try to make each directory in the path. | |
1527281e VZ |
248 | bool Mkdir( int perm = 0777, int flags = 0); |
249 | static bool Mkdir( const wxString &dir, int perm = 0777, int flags = 0 ); | |
097ead30 | 250 | |
a35b27b1 RR |
251 | bool Rmdir(); |
252 | static bool Rmdir( const wxString &dir ); | |
097ead30 | 253 | |
844f90fb VZ |
254 | // operations on the path |
255 | ||
256 | // normalize the path: with the default flags value, the path will be | |
257 | // made absolute, without any ".." and "." and all environment | |
258 | // variables will be expanded in it | |
259 | // | |
260 | // this may be done using another (than current) value of cwd | |
32a0d013 | 261 | bool Normalize(int flags = wxPATH_NORM_ALL, |
844f90fb VZ |
262 | const wxString& cwd = wxEmptyString, |
263 | wxPathFormat format = wxPATH_NATIVE); | |
097ead30 | 264 | |
f7d886af VZ |
265 | // get a path path relative to the given base directory, i.e. opposite |
266 | // of Normalize | |
267 | // | |
268 | // pass an empty string to get a path relative to the working directory | |
269 | // | |
270 | // returns TRUE if the file name was modified, FALSE if we failed to do | |
271 | // anything with it (happens when the file is on a different volume, | |
272 | // for example) | |
fda7962d | 273 | bool MakeRelativeTo(const wxString& pathBase = wxEmptyString, |
f7d886af VZ |
274 | wxPathFormat format = wxPATH_NATIVE); |
275 | ||
0894707e VS |
276 | // make the path absolute |
277 | // | |
278 | // this may be done using another (than current) value of cwd | |
279 | bool MakeAbsolute(const wxString& cwd = wxEmptyString, | |
280 | wxPathFormat format = wxPATH_NATIVE) | |
281 | { return Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_ABSOLUTE | | |
282 | wxPATH_NORM_TILDE, cwd, format); } | |
f7d886af | 283 | |
df5ddbca | 284 | // Comparison |
844f90fb | 285 | |
2b5f62a0 VZ |
286 | // compares with the rules of the given platforms format |
287 | bool SameAs(const wxFileName& filepath, | |
288 | wxPathFormat format = wxPATH_NATIVE) const; | |
289 | ||
290 | // compare with another filename object | |
291 | bool operator==(const wxFileName& filename) const | |
292 | { return SameAs(filename); } | |
293 | bool operator!=(const wxFileName& filename) const | |
294 | { return !SameAs(filename); } | |
295 | ||
296 | // compare with a filename string interpreted as a native file name | |
297 | bool operator==(const wxString& filename) const | |
298 | { return SameAs(wxFileName(filename)); } | |
299 | bool operator!=(const wxString& filename) const | |
300 | { return !SameAs(wxFileName(filename)); } | |
844f90fb | 301 | |
04c943b1 | 302 | // are the file names of this type cases sensitive? |
844f90fb | 303 | static bool IsCaseSensitive( wxPathFormat format = wxPATH_NATIVE ); |
04c943b1 VZ |
304 | |
305 | // is this filename absolute? | |
6d3d1c2e | 306 | bool IsAbsolute(wxPathFormat format = wxPATH_NATIVE) const; |
844f90fb | 307 | |
04c943b1 | 308 | // is this filename relative? |
6d3d1c2e VZ |
309 | bool IsRelative(wxPathFormat format = wxPATH_NATIVE) const |
310 | { return !IsAbsolute(format); } | |
04c943b1 VZ |
311 | |
312 | // Information about path format | |
313 | ||
6d3d1c2e VZ |
314 | // get the string separating the volume from the path for this format, |
315 | // return an empty string if this format doesn't support the notion of | |
316 | // volumes at all | |
04c943b1 VZ |
317 | static wxString GetVolumeSeparator(wxPathFormat format = wxPATH_NATIVE); |
318 | ||
844f90fb VZ |
319 | // get the string of path separators for this format |
320 | static wxString GetPathSeparators(wxPathFormat format = wxPATH_NATIVE); | |
321 | ||
6d3d1c2e VZ |
322 | // get the canonical path separator for this format |
323 | static wxChar GetPathSeparator(wxPathFormat format = wxPATH_NATIVE) | |
324 | { return GetPathSeparators(format)[0u]; } | |
325 | ||
844f90fb VZ |
326 | // is the char a path separator for this format? |
327 | static bool IsPathSeparator(wxChar ch, wxPathFormat format = wxPATH_NATIVE); | |
328 | ||
df5ddbca RR |
329 | // Dir accessors |
330 | void AppendDir( const wxString &dir ); | |
331 | void PrependDir( const wxString &dir ); | |
332 | void InsertDir( int before, const wxString &dir ); | |
333 | void RemoveDir( int pos ); | |
df5168c4 | 334 | size_t GetDirCount() const { return m_dirs.size(); } |
097ead30 | 335 | |
df5ddbca RR |
336 | // Other accessors |
337 | void SetExt( const wxString &ext ) { m_ext = ext; } | |
338 | wxString GetExt() const { return m_ext; } | |
04c943b1 | 339 | bool HasExt() const { return !m_ext.empty(); } |
097ead30 | 340 | |
df5ddbca RR |
341 | void SetName( const wxString &name ) { m_name = name; } |
342 | wxString GetName() const { return m_name; } | |
04c943b1 VZ |
343 | bool HasName() const { return !m_name.empty(); } |
344 | ||
345 | void SetVolume( const wxString &volume ) { m_volume = volume; } | |
346 | wxString GetVolume() const { return m_volume; } | |
347 | bool HasVolume() const { return !m_volume.empty(); } | |
097ead30 | 348 | |
7124df9b VZ |
349 | // full name is the file name + extension (but without the path) |
350 | void SetFullName(const wxString& fullname); | |
844f90fb | 351 | wxString GetFullName() const; |
097ead30 | 352 | |
04c943b1 | 353 | const wxArrayString& GetDirs() const { return m_dirs; } |
097ead30 | 354 | |
33b97389 VZ |
355 | // flags are combination of wxPATH_GET_XXX flags |
356 | wxString GetPath(int flags = 0, wxPathFormat format = wxPATH_NATIVE) const; | |
357 | ||
a7b51bc8 RR |
358 | // Replace current path with this one |
359 | void SetPath( const wxString &path, wxPathFormat format = wxPATH_NATIVE ); | |
097ead30 | 360 | |
df5ddbca RR |
361 | // Construct full path with name and ext |
362 | wxString GetFullPath( wxPathFormat format = wxPATH_NATIVE ) const; | |
097ead30 | 363 | |
9e9b65c1 JS |
364 | // Return the short form of the path (returns identity on non-Windows platforms) |
365 | wxString GetShortPath() const; | |
366 | ||
367 | // Return the long form of the path (returns identity on non-Windows platforms) | |
368 | wxString GetLongPath() const; | |
369 | ||
2db991f4 VZ |
370 | // Is this a file or directory (not necessarily an existing one) |
371 | bool IsDir() const { return m_name.empty() && m_ext.empty(); } | |
372 | ||
9e8d8607 VZ |
373 | // various helpers |
374 | ||
375 | // get the canonical path format for this platform | |
df5ddbca | 376 | static wxPathFormat GetFormat( wxPathFormat format = wxPATH_NATIVE ); |
097ead30 | 377 | |
04c943b1 VZ |
378 | // split a fullpath into the volume, path, (base) name and extension |
379 | // (all of the pointers can be NULL) | |
9e8d8607 | 380 | static void SplitPath(const wxString& fullpath, |
04c943b1 | 381 | wxString *volume, |
9e8d8607 VZ |
382 | wxString *path, |
383 | wxString *name, | |
384 | wxString *ext, | |
385 | wxPathFormat format = wxPATH_NATIVE); | |
386 | ||
04c943b1 VZ |
387 | // compatibility version |
388 | static void SplitPath(const wxString& fullpath, | |
389 | wxString *path, | |
390 | wxString *name, | |
391 | wxString *ext, | |
6f91bc33 | 392 | wxPathFormat format = wxPATH_NATIVE); |
04c943b1 | 393 | |
33b97389 VZ |
394 | |
395 | // deprecated methods, don't use any more | |
396 | // -------------------------------------- | |
397 | ||
4ce1efe1 | 398 | #ifndef __DIGITALMARS__ |
33b97389 VZ |
399 | wxString GetPath( bool withSep, wxPathFormat format = wxPATH_NATIVE ) const |
400 | { return GetPath(withSep ? wxPATH_GET_SEPARATOR : 0, format); } | |
4ce1efe1 | 401 | #endif |
33b97389 VZ |
402 | wxString GetPathWithSep(wxPathFormat format = wxPATH_NATIVE ) const |
403 | { return GetPath(wxPATH_GET_SEPARATOR, format); } | |
404 | ||
df5ddbca | 405 | private: |
04c943b1 VZ |
406 | // the drive/volume/device specification (always empty for Unix) |
407 | wxString m_volume; | |
408 | ||
844f90fb | 409 | // the path components of the file |
df5ddbca | 410 | wxArrayString m_dirs; |
844f90fb VZ |
411 | |
412 | // the file name and extension (empty for directories) | |
413 | wxString m_name, | |
414 | m_ext; | |
6d3d1c2e VZ |
415 | |
416 | // when m_dirs is empty it may mean either that we have no path at all or | |
417 | // that our path is '/', i.e. the root directory | |
418 | // | |
419 | // we use m_relative to distinguish between these two cases, it will be | |
420 | // TRUE in the former and FALSE in the latter | |
421 | // | |
422 | // NB: the path is not absolute just because m_relative is FALSE, it still | |
423 | // needs the drive (i.e. volume) in some formats (Windows) | |
353f41cb | 424 | bool m_relative; |
df5ddbca RR |
425 | }; |
426 | ||
097ead30 | 427 | #endif // _WX_FILENAME_H_ |
df5ddbca | 428 |