]>
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 | |
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 | |
9e8d8607 | 20 | #include "wx/string.h" |
df5ddbca RR |
21 | #endif |
22 | ||
9e8d8607 VZ |
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 | ||
df5ddbca | 37 | // ridiculously enough, this will replace DirExists with wxDirExists etc |
ee66f092 | 38 | #include "wx/filefn.h" |
ce16e5d7 | 39 | #include "wx/datetime.h" |
df5ddbca | 40 | |
df22f860 VZ |
41 | class WXDLLEXPORT wxFile; |
42 | ||
097ead30 VZ |
43 | // ---------------------------------------------------------------------------- |
44 | // constants | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
47 | // the various values for the path format: this mainly affects the path | |
48 | // separator but also whether or not the path has the drive part (as under | |
49 | // Windows) | |
df5ddbca RR |
50 | enum wxPathFormat |
51 | { | |
844f90fb | 52 | wxPATH_NATIVE = 0, // the path format for the current platform |
097ead30 VZ |
53 | wxPATH_UNIX, |
54 | wxPATH_MAC, | |
55 | wxPATH_DOS, | |
3c621059 | 56 | wxPATH_VMS, |
ee66f092 | 57 | |
097ead30 VZ |
58 | wxPATH_BEOS = wxPATH_UNIX, |
59 | wxPATH_WIN = wxPATH_DOS, | |
60 | wxPATH_OS2 = wxPATH_DOS | |
61 | }; | |
ee66f092 | 62 | |
097ead30 VZ |
63 | // the kind of normalization to do with the file name: these values can be |
64 | // or'd together to perform several operations at once | |
65 | enum wxPathNormalize | |
66 | { | |
67 | wxPATH_NORM_ENV_VARS = 0x0001, // replace env vars with their values | |
844f90fb | 68 | wxPATH_NORM_DOTS = 0x0002, // squeeze all .. and . and prepend cwd |
097ead30 | 69 | wxPATH_NORM_TILDE = 0x0004, // Unix only: replace ~ and ~user |
844f90fb VZ |
70 | wxPATH_NORM_CASE = 0x0008, // if case insensitive => tolower |
71 | wxPATH_NORM_ABSOLUTE = 0x0010, // make the path absolute | |
9e9b65c1 JS |
72 | wxPATH_NORM_LONG = 0x0020, // make the path the long form |
73 | wxPATH_NORM_ALL = 0x003f | |
df5ddbca RR |
74 | }; |
75 | ||
097ead30 VZ |
76 | // ---------------------------------------------------------------------------- |
77 | // wxFileName: encapsulates a file path | |
78 | // ---------------------------------------------------------------------------- | |
79 | ||
df5ddbca RR |
80 | class WXDLLEXPORT wxFileName |
81 | { | |
82 | public: | |
83 | // constructors and assignment | |
844f90fb VZ |
84 | |
85 | // the usual stuff | |
86 | wxFileName() { } | |
87 | wxFileName( const wxFileName &filepath ) { Assign(filepath); } | |
88 | ||
89 | // from a full filename: if it terminates with a '/', a directory path | |
90 | // is contructed (the name will be empty), otherwise a file name and | |
91 | // extension are extracted from it | |
92 | wxFileName( const wxString& fullpath, wxPathFormat format = wxPATH_NATIVE ) | |
93 | { Assign( fullpath, format ); } | |
94 | ||
95 | // from a directory name and a file name | |
96 | wxFileName(const wxString& path, | |
97 | const wxString& name, | |
98 | wxPathFormat format = wxPATH_NATIVE) | |
99 | { Assign(path, name, format); } | |
100 | ||
81f25632 VZ |
101 | // from a volume, directory name, file base name and extension |
102 | wxFileName(const wxString& volume, | |
103 | const wxString& path, | |
104 | const wxString& name, | |
105 | const wxString& ext, | |
106 | wxPathFormat format = wxPATH_NATIVE) | |
107 | { Assign(volume, path, name, ext, format); } | |
108 | ||
844f90fb VZ |
109 | // from a directory name, file base name and extension |
110 | wxFileName(const wxString& path, | |
111 | const wxString& name, | |
112 | const wxString& ext, | |
113 | wxPathFormat format = wxPATH_NATIVE) | |
114 | { Assign(path, name, ext, format); } | |
115 | ||
844f90fb VZ |
116 | // the same for delayed initialization |
117 | ||
844f90fb | 118 | void Assign(const wxFileName& filepath); |
04c943b1 | 119 | |
844f90fb VZ |
120 | void Assign(const wxString& fullpath, |
121 | wxPathFormat format = wxPATH_NATIVE); | |
04c943b1 VZ |
122 | |
123 | void Assign(const wxString& volume, | |
124 | const wxString& path, | |
125 | const wxString& name, | |
126 | const wxString& ext, | |
127 | wxPathFormat format = wxPATH_NATIVE); | |
128 | ||
844f90fb VZ |
129 | void Assign(const wxString& path, |
130 | const wxString& name, | |
131 | wxPathFormat format = wxPATH_NATIVE); | |
04c943b1 | 132 | |
844f90fb VZ |
133 | void Assign(const wxString& path, |
134 | const wxString& name, | |
135 | const wxString& ext, | |
04c943b1 VZ |
136 | wxPathFormat format = wxPATH_NATIVE) |
137 | { | |
138 | // empty volume | |
139 | Assign(_T(""), path, name, ext, format); | |
140 | } | |
141 | ||
52dbd056 | 142 | void AssignDir(const wxString& dir, wxPathFormat format = wxPATH_NATIVE); |
844f90fb | 143 | |
788722ac JS |
144 | // assorted assignment operators |
145 | ||
146 | wxFileName& operator=(const wxFileName& filename) | |
147 | { Assign(filename); return *this; } | |
148 | ||
149 | wxFileName& operator=(const wxString& filename) | |
150 | { Assign(filename); return *this; } | |
151 | ||
844f90fb VZ |
152 | // reset all components to default, uninitialized state |
153 | void Clear(); | |
154 | ||
155 | // static pseudo constructors | |
156 | static wxFileName FileName(const wxString& file); | |
157 | static wxFileName DirName(const wxString& dir); | |
158 | ||
951cd180 | 159 | // file tests |
844f90fb VZ |
160 | |
161 | // is the filename valid at all? | |
162 | bool IsOk() const { return !m_dirs.IsEmpty() || !m_name.IsEmpty(); } | |
163 | ||
164 | // does the file with this name exists? | |
df5ddbca | 165 | bool FileExists(); |
a35b27b1 | 166 | static bool FileExists( const wxString &file ); |
097ead30 | 167 | |
844f90fb | 168 | // does the directory with this name exists? |
df5ddbca | 169 | bool DirExists(); |
a35b27b1 | 170 | static bool DirExists( const wxString &dir ); |
097ead30 | 171 | |
844f90fb VZ |
172 | // VZ: also need: IsDirWritable(), IsFileExecutable() &c (TODO) |
173 | ||
951cd180 VZ |
174 | // time functions |
175 | ||
176 | // set the file creation and last access/mod times | |
177 | // (any of the pointers may be NULL) | |
178 | bool SetTimes(const wxDateTime *dtCreate, | |
179 | const wxDateTime *dtAccess, | |
180 | const wxDateTime *dtMod); | |
181 | ||
182 | // set the access and modification times to the current moment | |
183 | bool Touch(); | |
184 | ||
185 | // return the last access, last modification and last change times | |
186 | // (any of the pointers may be NULL) | |
187 | bool GetTimes(wxDateTime *dtAccess, | |
188 | wxDateTime *dtMod, | |
189 | wxDateTime *dtChange) const; | |
190 | ||
191 | // convenience wrapper: get just the last mod time of the file | |
192 | wxDateTime GetModificationTime() const | |
193 | { | |
194 | wxDateTime dtMod; | |
195 | (void)GetTimes(NULL, &dtMod, NULL); | |
196 | return dtMod; | |
197 | } | |
198 | ||
844f90fb VZ |
199 | // various file/dir operations |
200 | ||
201 | // retrieve the value of the current working directory | |
6f91bc33 VZ |
202 | void AssignCwd(const wxString& volume = wxEmptyString); |
203 | static wxString GetCwd(const wxString& volume = wxEmptyString); | |
097ead30 | 204 | |
844f90fb | 205 | // change the current working directory |
a35b27b1 RR |
206 | bool SetCwd(); |
207 | static bool SetCwd( const wxString &cwd ); | |
097ead30 | 208 | |
844f90fb | 209 | // get the value of user home (Unix only mainly) |
a35b27b1 RR |
210 | void AssignHomeDir(); |
211 | static wxString GetHomeDir(); | |
097ead30 | 212 | |
df22f860 VZ |
213 | // get a temp file name starting with the specified prefix and open the |
214 | // file passed to us using this name for writing (atomically if | |
215 | // possible) | |
216 | void AssignTempFileName(const wxString& prefix, wxFile *fileTemp = NULL); | |
217 | static wxString CreateTempFileName(const wxString& prefix, | |
218 | wxFile *fileTemp = NULL); | |
097ead30 | 219 | |
f0ce3409 JS |
220 | // directory creation and removal. |
221 | // if full is TRUE, will try to make each directory in the path. | |
222 | bool Mkdir( int perm = 0777, bool full = FALSE); | |
223 | static bool Mkdir( const wxString &dir, int perm = 0777, bool full = FALSE ); | |
097ead30 | 224 | |
a35b27b1 RR |
225 | bool Rmdir(); |
226 | static bool Rmdir( const wxString &dir ); | |
097ead30 | 227 | |
844f90fb VZ |
228 | // operations on the path |
229 | ||
230 | // normalize the path: with the default flags value, the path will be | |
231 | // made absolute, without any ".." and "." and all environment | |
232 | // variables will be expanded in it | |
233 | // | |
234 | // this may be done using another (than current) value of cwd | |
235 | bool Normalize(wxPathNormalize flags = wxPATH_NORM_ALL, | |
236 | const wxString& cwd = wxEmptyString, | |
237 | wxPathFormat format = wxPATH_NATIVE); | |
097ead30 | 238 | |
f7d886af VZ |
239 | // get a path path relative to the given base directory, i.e. opposite |
240 | // of Normalize | |
241 | // | |
242 | // pass an empty string to get a path relative to the working directory | |
243 | // | |
244 | // returns TRUE if the file name was modified, FALSE if we failed to do | |
245 | // anything with it (happens when the file is on a different volume, | |
246 | // for example) | |
247 | bool MakeRelativeTo(const wxString& pathBase = _T(""), | |
248 | wxPathFormat format = wxPATH_NATIVE); | |
249 | ||
250 | ||
df5ddbca | 251 | // Comparison |
844f90fb | 252 | |
788722ac JS |
253 | // compares with the rules of this platform |
254 | bool SameAs(const wxFileName &filepath, | |
255 | wxPathFormat format = wxPATH_NATIVE); | |
256 | ||
844f90fb VZ |
257 | // uses the current platform settings |
258 | bool operator==(const wxFileName& filename) { return SameAs(filename); } | |
259 | bool operator==(const wxString& filename) | |
260 | { return *this == wxFileName(filename); } | |
261 | ||
df5ddbca | 262 | // Tests |
04c943b1 VZ |
263 | |
264 | // are the file names of this type cases sensitive? | |
844f90fb | 265 | static bool IsCaseSensitive( wxPathFormat format = wxPATH_NATIVE ); |
04c943b1 VZ |
266 | |
267 | // is this filename absolute? | |
353f41cb RR |
268 | bool IsAbsolute() const |
269 | { return !m_relative; } | |
844f90fb | 270 | |
04c943b1 | 271 | // is this filename relative? |
353f41cb RR |
272 | bool IsRelative() const |
273 | { return m_relative; } | |
04c943b1 VZ |
274 | |
275 | // Information about path format | |
276 | ||
277 | // get the string separating the volume from the path for this format | |
278 | static wxString GetVolumeSeparator(wxPathFormat format = wxPATH_NATIVE); | |
279 | ||
844f90fb VZ |
280 | // get the string of path separators for this format |
281 | static wxString GetPathSeparators(wxPathFormat format = wxPATH_NATIVE); | |
282 | ||
283 | // is the char a path separator for this format? | |
284 | static bool IsPathSeparator(wxChar ch, wxPathFormat format = wxPATH_NATIVE); | |
285 | ||
286 | // FIXME: what exactly does this do? | |
df5ddbca | 287 | bool IsWild( wxPathFormat format = wxPATH_NATIVE ); |
097ead30 | 288 | |
df5ddbca RR |
289 | // Dir accessors |
290 | void AppendDir( const wxString &dir ); | |
291 | void PrependDir( const wxString &dir ); | |
292 | void InsertDir( int before, const wxString &dir ); | |
293 | void RemoveDir( int pos ); | |
097ead30 VZ |
294 | size_t GetDirCount() const { return m_dirs.GetCount(); } |
295 | ||
df5ddbca RR |
296 | // Other accessors |
297 | void SetExt( const wxString &ext ) { m_ext = ext; } | |
298 | wxString GetExt() const { return m_ext; } | |
04c943b1 | 299 | bool HasExt() const { return !m_ext.empty(); } |
097ead30 | 300 | |
df5ddbca RR |
301 | void SetName( const wxString &name ) { m_name = name; } |
302 | wxString GetName() const { return m_name; } | |
04c943b1 VZ |
303 | bool HasName() const { return !m_name.empty(); } |
304 | ||
305 | void SetVolume( const wxString &volume ) { m_volume = volume; } | |
306 | wxString GetVolume() const { return m_volume; } | |
307 | bool HasVolume() const { return !m_volume.empty(); } | |
097ead30 | 308 | |
7124df9b VZ |
309 | // full name is the file name + extension (but without the path) |
310 | void SetFullName(const wxString& fullname); | |
844f90fb | 311 | wxString GetFullName() const; |
097ead30 | 312 | |
04c943b1 | 313 | const wxArrayString& GetDirs() const { return m_dirs; } |
097ead30 | 314 | |
844f90fb | 315 | // Construct path only - possibly with the trailing separator |
7124df9b VZ |
316 | wxString GetPath( bool add_separator = FALSE, |
317 | wxPathFormat format = wxPATH_NATIVE ) const; | |
097ead30 | 318 | |
844f90fb VZ |
319 | // more readable synonym |
320 | wxString GetPathWithSep(wxPathFormat format = wxPATH_NATIVE ) const | |
321 | { return GetPath(TRUE /* add separator */, format); } | |
322 | ||
df5ddbca RR |
323 | // Construct full path with name and ext |
324 | wxString GetFullPath( wxPathFormat format = wxPATH_NATIVE ) const; | |
097ead30 | 325 | |
9e9b65c1 JS |
326 | // Return the short form of the path (returns identity on non-Windows platforms) |
327 | wxString GetShortPath() const; | |
328 | ||
329 | // Return the long form of the path (returns identity on non-Windows platforms) | |
330 | wxString GetLongPath() const; | |
331 | ||
9e8d8607 VZ |
332 | // various helpers |
333 | ||
334 | // get the canonical path format for this platform | |
df5ddbca | 335 | static wxPathFormat GetFormat( wxPathFormat format = wxPATH_NATIVE ); |
097ead30 | 336 | |
04c943b1 VZ |
337 | // split a fullpath into the volume, path, (base) name and extension |
338 | // (all of the pointers can be NULL) | |
9e8d8607 | 339 | static void SplitPath(const wxString& fullpath, |
04c943b1 | 340 | wxString *volume, |
9e8d8607 VZ |
341 | wxString *path, |
342 | wxString *name, | |
343 | wxString *ext, | |
344 | wxPathFormat format = wxPATH_NATIVE); | |
345 | ||
04c943b1 VZ |
346 | // compatibility version |
347 | static void SplitPath(const wxString& fullpath, | |
348 | wxString *path, | |
349 | wxString *name, | |
350 | wxString *ext, | |
6f91bc33 | 351 | wxPathFormat format = wxPATH_NATIVE); |
04c943b1 | 352 | |
df5ddbca | 353 | private: |
04c943b1 VZ |
354 | // the drive/volume/device specification (always empty for Unix) |
355 | wxString m_volume; | |
356 | ||
844f90fb | 357 | // the path components of the file |
df5ddbca | 358 | wxArrayString m_dirs; |
844f90fb VZ |
359 | |
360 | // the file name and extension (empty for directories) | |
361 | wxString m_name, | |
362 | m_ext; | |
353f41cb RR |
363 | |
364 | // is the path relative | |
365 | bool m_relative; | |
df5ddbca RR |
366 | }; |
367 | ||
097ead30 | 368 | #endif // _WX_FILENAME_H_ |
df5ddbca | 369 |