added format parameter to File/DirName()
[wxWidgets.git] / include / wx / filename.h
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 licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_FILENAME_H_
13 #define _WX_FILENAME_H_
14
15 #if defined(__GNUG__) && !defined(__APPLE__)
16 #pragma interface "filename.h"
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/string.h"
21 #include "wx/arrstr.h"
22 #endif
23
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
38 // ridiculously enough, this will replace DirExists with wxDirExists etc
39 #include "wx/filefn.h"
40 #include "wx/datetime.h"
41
42 class WXDLLIMPEXP_BASE wxFile;
43
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)
51 enum wxPathFormat
52 {
53 wxPATH_NATIVE = 0, // the path format for the current platform
54 wxPATH_UNIX,
55 wxPATH_MAC,
56 wxPATH_DOS,
57 wxPATH_VMS,
58
59 wxPATH_BEOS = wxPATH_UNIX,
60 wxPATH_WIN = wxPATH_DOS,
61 wxPATH_OS2 = wxPATH_DOS
62 };
63
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
69 wxPATH_NORM_DOTS = 0x0002, // squeeze all .. and . and prepend cwd
70 wxPATH_NORM_TILDE = 0x0004, // Unix only: replace ~ and ~user
71 wxPATH_NORM_CASE = 0x0008, // if case insensitive => tolower
72 wxPATH_NORM_ABSOLUTE = 0x0010, // make the path absolute
73 wxPATH_NORM_LONG = 0x0020, // make the path the long form
74 wxPATH_NORM_ALL = 0x003f
75 };
76
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
84 // MkDir flags
85 enum
86 {
87 wxPATH_MKDIR_FULL = 0x0001 // create directories recursively
88 };
89
90 // ----------------------------------------------------------------------------
91 // wxFileName: encapsulates a file path
92 // ----------------------------------------------------------------------------
93
94 class WXDLLIMPEXP_BASE wxFileName
95 {
96 public:
97 // constructors and assignment
98
99 // the usual stuff
100 wxFileName() { Clear(); }
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
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
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
130 // the same for delayed initialization
131
132 void Assign(const wxFileName& filepath);
133
134 void Assign(const wxString& fullpath,
135 wxPathFormat format = wxPATH_NATIVE);
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
143 void Assign(const wxString& path,
144 const wxString& name,
145 wxPathFormat format = wxPATH_NATIVE);
146
147 void Assign(const wxString& path,
148 const wxString& name,
149 const wxString& ext,
150 wxPathFormat format = wxPATH_NATIVE)
151 {
152 // empty volume
153 Assign(wxEmptyString, path, name, ext, format);
154 }
155
156 void AssignDir(const wxString& dir, wxPathFormat format = wxPATH_NATIVE);
157
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
166 // reset all components to default, uninitialized state
167 void Clear();
168
169 // static pseudo constructors
170 static wxFileName FileName(const wxString& file,
171 wxPathFormat format = wxPATH_NATIVE);
172 static wxFileName DirName(const wxString& dir,
173 wxPathFormat format = wxPATH_NATIVE);
174
175 // file tests
176
177 // is the filename valid at all?
178 bool IsOk() const { return m_dirs.size() != 0 || !m_name.IsEmpty(); }
179
180 // does the file with this name exists?
181 bool FileExists() const;
182 static bool FileExists( const wxString &file );
183
184 // does the directory with this name exists?
185 bool DirExists() const;
186 static bool DirExists( const wxString &dir );
187
188 // VZ: also need: IsDirWritable(), IsFileExecutable() &c (TODO)
189
190 // time functions
191 #if wxUSE_DATETIME
192 // set the file last access/mod and creation times
193 // (any of the pointers may be NULL)
194 bool SetTimes(const wxDateTime *dtAccess,
195 const wxDateTime *dtMod,
196 const wxDateTime *dtCreate);
197
198 // set the access and modification times to the current moment
199 bool Touch();
200
201 // return the last access, last modification and create times
202 // (any of the pointers may be NULL)
203 bool GetTimes(wxDateTime *dtAccess,
204 wxDateTime *dtMod,
205 wxDateTime *dtCreate) const;
206
207 // convenience wrapper: get just the last mod time of the file
208 wxDateTime GetModificationTime() const
209 {
210 wxDateTime dtMod;
211 (void)GetTimes(NULL, &dtMod, NULL);
212 return dtMod;
213 }
214 #endif // wxUSE_DATETIME
215
216 #ifdef __WXMAC__
217 bool MacSetTypeAndCreator( wxUint32 type , wxUint32 creator ) ;
218 bool MacGetTypeAndCreator( wxUint32 *type , wxUint32 *creator ) ;
219 // gets the 'common' type and creator for a certain extension
220 static bool MacFindDefaultTypeAndCreator( const wxString& ext , wxUint32 *type , wxUint32 *creator ) ;
221 // registers application defined extensions and their default type and creator
222 static void MacRegisterDefaultTypeAndCreator( const wxString& ext , wxUint32 type , wxUint32 creator ) ;
223 // looks up the appropriate type and creator from the registration and then sets
224 bool MacSetDefaultTypeAndCreator() ;
225 #endif
226
227 // various file/dir operations
228
229 // retrieve the value of the current working directory
230 void AssignCwd(const wxString& volume = wxEmptyString);
231 static wxString GetCwd(const wxString& volume = wxEmptyString);
232
233 // change the current working directory
234 bool SetCwd();
235 static bool SetCwd( const wxString &cwd );
236
237 // get the value of user home (Unix only mainly)
238 void AssignHomeDir();
239 static wxString GetHomeDir();
240
241 // get a temp file name starting with the specified prefix and open the
242 // file passed to us using this name for writing (atomically if
243 // possible)
244 void AssignTempFileName(const wxString& prefix, wxFile *fileTemp = NULL);
245 static wxString CreateTempFileName(const wxString& prefix,
246 wxFile *fileTemp = NULL);
247
248 // directory creation and removal.
249 // if full is TRUE, will try to make each directory in the path.
250 bool Mkdir( int perm = 0777, int flags = 0);
251 static bool Mkdir( const wxString &dir, int perm = 0777, int flags = 0 );
252
253 bool Rmdir();
254 static bool Rmdir( const wxString &dir );
255
256 // operations on the path
257
258 // normalize the path: with the default flags value, the path will be
259 // made absolute, without any ".." and "." and all environment
260 // variables will be expanded in it
261 //
262 // this may be done using another (than current) value of cwd
263 bool Normalize(int flags = wxPATH_NORM_ALL,
264 const wxString& cwd = wxEmptyString,
265 wxPathFormat format = wxPATH_NATIVE);
266
267 // get a path path relative to the given base directory, i.e. opposite
268 // of Normalize
269 //
270 // pass an empty string to get a path relative to the working directory
271 //
272 // returns TRUE if the file name was modified, FALSE if we failed to do
273 // anything with it (happens when the file is on a different volume,
274 // for example)
275 bool MakeRelativeTo(const wxString& pathBase = wxEmptyString,
276 wxPathFormat format = wxPATH_NATIVE);
277
278 // make the path absolute
279 //
280 // this may be done using another (than current) value of cwd
281 bool MakeAbsolute(const wxString& cwd = wxEmptyString,
282 wxPathFormat format = wxPATH_NATIVE)
283 { return Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_ABSOLUTE |
284 wxPATH_NORM_TILDE, cwd, format); }
285
286 // Comparison
287
288 // compares with the rules of the given platforms format
289 bool SameAs(const wxFileName& filepath,
290 wxPathFormat format = wxPATH_NATIVE) const;
291
292 // compare with another filename object
293 bool operator==(const wxFileName& filename) const
294 { return SameAs(filename); }
295 bool operator!=(const wxFileName& filename) const
296 { return !SameAs(filename); }
297
298 // compare with a filename string interpreted as a native file name
299 bool operator==(const wxString& filename) const
300 { return SameAs(wxFileName(filename)); }
301 bool operator!=(const wxString& filename) const
302 { return !SameAs(wxFileName(filename)); }
303
304 // are the file names of this type cases sensitive?
305 static bool IsCaseSensitive( wxPathFormat format = wxPATH_NATIVE );
306
307 // is this filename absolute?
308 bool IsAbsolute(wxPathFormat format = wxPATH_NATIVE) const;
309
310 // is this filename relative?
311 bool IsRelative(wxPathFormat format = wxPATH_NATIVE) const
312 { return !IsAbsolute(format); }
313
314 // Information about path format
315
316 // get the string separating the volume from the path for this format,
317 // return an empty string if this format doesn't support the notion of
318 // volumes at all
319 static wxString GetVolumeSeparator(wxPathFormat format = wxPATH_NATIVE);
320
321 // get the string of path separators for this format
322 static wxString GetPathSeparators(wxPathFormat format = wxPATH_NATIVE);
323
324 // get the canonical path separator for this format
325 static wxChar GetPathSeparator(wxPathFormat format = wxPATH_NATIVE)
326 { return GetPathSeparators(format)[0u]; }
327
328 // is the char a path separator for this format?
329 static bool IsPathSeparator(wxChar ch, wxPathFormat format = wxPATH_NATIVE);
330
331 // Dir accessors
332 void AppendDir( const wxString &dir );
333 void PrependDir( const wxString &dir );
334 void InsertDir( int before, const wxString &dir );
335 void RemoveDir( int pos );
336 size_t GetDirCount() const { return m_dirs.size(); }
337
338 // Other accessors
339 void SetExt( const wxString &ext ) { m_ext = ext; }
340 wxString GetExt() const { return m_ext; }
341 bool HasExt() const { return !m_ext.empty(); }
342
343 void SetName( const wxString &name ) { m_name = name; }
344 wxString GetName() const { return m_name; }
345 bool HasName() const { return !m_name.empty(); }
346
347 void SetVolume( const wxString &volume ) { m_volume = volume; }
348 wxString GetVolume() const { return m_volume; }
349 bool HasVolume() const { return !m_volume.empty(); }
350
351 // full name is the file name + extension (but without the path)
352 void SetFullName(const wxString& fullname);
353 wxString GetFullName() const;
354
355 const wxArrayString& GetDirs() const { return m_dirs; }
356
357 // flags are combination of wxPATH_GET_XXX flags
358 wxString GetPath(int flags = 0, wxPathFormat format = wxPATH_NATIVE) const;
359
360 // Replace current path with this one
361 void SetPath( const wxString &path, wxPathFormat format = wxPATH_NATIVE );
362
363 // Construct full path with name and ext
364 wxString GetFullPath( wxPathFormat format = wxPATH_NATIVE ) const;
365
366 // Return the short form of the path (returns identity on non-Windows platforms)
367 wxString GetShortPath() const;
368
369 // Return the long form of the path (returns identity on non-Windows platforms)
370 wxString GetLongPath() const;
371
372 // Is this a file or directory (not necessarily an existing one)
373 bool IsDir() const { return m_name.empty() && m_ext.empty(); }
374
375 // various helpers
376
377 // get the canonical path format for this platform
378 static wxPathFormat GetFormat( wxPathFormat format = wxPATH_NATIVE );
379
380 // split a fullpath into the volume, path, (base) name and extension
381 // (all of the pointers can be NULL)
382 static void SplitPath(const wxString& fullpath,
383 wxString *volume,
384 wxString *path,
385 wxString *name,
386 wxString *ext,
387 wxPathFormat format = wxPATH_NATIVE);
388
389 // compatibility version
390 static void SplitPath(const wxString& fullpath,
391 wxString *path,
392 wxString *name,
393 wxString *ext,
394 wxPathFormat format = wxPATH_NATIVE);
395
396
397 // deprecated methods, don't use any more
398 // --------------------------------------
399
400 #ifndef __DIGITALMARS__
401 wxString GetPath( bool withSep, wxPathFormat format = wxPATH_NATIVE ) const
402 { return GetPath(withSep ? wxPATH_GET_SEPARATOR : 0, format); }
403 #endif
404 wxString GetPathWithSep(wxPathFormat format = wxPATH_NATIVE ) const
405 { return GetPath(wxPATH_GET_SEPARATOR, format); }
406
407 private:
408 // the drive/volume/device specification (always empty for Unix)
409 wxString m_volume;
410
411 // the path components of the file
412 wxArrayString m_dirs;
413
414 // the file name and extension (empty for directories)
415 wxString m_name,
416 m_ext;
417
418 // when m_dirs is empty it may mean either that we have no path at all or
419 // that our path is '/', i.e. the root directory
420 //
421 // we use m_relative to distinguish between these two cases, it will be
422 // TRUE in the former and FALSE in the latter
423 //
424 // NB: the path is not absolute just because m_relative is FALSE, it still
425 // needs the drive (i.e. volume) in some formats (Windows)
426 bool m_relative;
427 };
428
429 #endif // _WX_FILENAME_H_
430