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