1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileName - encapsulates a file path
4 // Author: Robert Roebling, Vadim Zeitlin
8 // Copyright: (c) 2000 Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_FILENAME_H_
13 #define _WX_FILENAME_H_
15 #if defined(__GNUG__) && !defined(__APPLE__)
16 #pragma interface "filename.h"
20 #include "wx/string.h"
21 #include "wx/arrstr.h"
27 1. support for drives under Windows
28 2. more file operations:
30 b) [acm]time() - get and set
32 d) file permissions with readable accessors for most common bits
33 such as IsReadable() &c
35 3. SameFileAs() function to compare inodes under Unix
38 // ridiculously enough, this will replace DirExists with wxDirExists etc
39 #include "wx/filefn.h"
40 #include "wx/datetime.h"
42 class WXDLLIMPEXP_BASE wxFile
;
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
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
53 wxPATH_NATIVE
= 0, // the path format for the current platform
59 wxPATH_BEOS
= wxPATH_UNIX
,
60 wxPATH_WIN
= wxPATH_DOS
,
61 wxPATH_OS2
= wxPATH_DOS
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
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
77 // what exactly should GetPath() return?
80 wxPATH_GET_VOLUME
= 0x0001, // include the volume if applicable
81 wxPATH_GET_SEPARATOR
= 0x0002 // terminate the path with the separator
87 wxPATH_MKDIR_FULL
= 0x0001 // create directories recursively
90 // ----------------------------------------------------------------------------
91 // wxFileName: encapsulates a file path
92 // ----------------------------------------------------------------------------
94 class WXDLLIMPEXP_BASE wxFileName
97 // constructors and assignment
100 wxFileName() { Clear(); }
101 wxFileName(const wxFileName
& filepath
) { Assign(filepath
); }
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
); }
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
); }
115 // from a volume, directory name, file base name and extension
116 wxFileName(const wxString
& volume
,
117 const wxString
& path
,
118 const wxString
& name
,
120 wxPathFormat format
= wxPATH_NATIVE
)
121 { Assign(volume
, path
, name
, ext
, format
); }
123 // from a directory name, file base name and extension
124 wxFileName(const wxString
& path
,
125 const wxString
& name
,
127 wxPathFormat format
= wxPATH_NATIVE
)
128 { Assign(path
, name
, ext
, format
); }
130 // the same for delayed initialization
132 void Assign(const wxFileName
& filepath
);
134 void Assign(const wxString
& fullpath
,
135 wxPathFormat format
= wxPATH_NATIVE
);
137 void Assign(const wxString
& volume
,
138 const wxString
& path
,
139 const wxString
& name
,
141 wxPathFormat format
= wxPATH_NATIVE
);
143 void Assign(const wxString
& path
,
144 const wxString
& name
,
145 wxPathFormat format
= wxPATH_NATIVE
);
147 void Assign(const wxString
& path
,
148 const wxString
& name
,
150 wxPathFormat format
= wxPATH_NATIVE
)
153 Assign(wxEmptyString
, path
, name
, ext
, format
);
156 void AssignDir(const wxString
& dir
, wxPathFormat format
= wxPATH_NATIVE
);
158 // assorted assignment operators
160 wxFileName
& operator=(const wxFileName
& filename
)
161 { Assign(filename
); return *this; }
163 wxFileName
& operator=(const wxString
& filename
)
164 { Assign(filename
); return *this; }
166 // reset all components to default, uninitialized state
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
);
177 // is the filename valid at all?
178 bool IsOk() const { return m_dirs
.size() != 0 || !m_name
.IsEmpty(); }
180 // does the file with this name exists?
181 bool FileExists() const;
182 static bool FileExists( const wxString
&file
);
184 // does the directory with this name exists?
185 bool DirExists() const;
186 static bool DirExists( const wxString
&dir
);
188 // VZ: also need: IsDirWritable(), IsFileExecutable() &c (TODO)
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
);
198 // set the access and modification times to the current moment
201 // return the last access, last modification and create times
202 // (any of the pointers may be NULL)
203 bool GetTimes(wxDateTime
*dtAccess
,
205 wxDateTime
*dtCreate
) const;
207 // convenience wrapper: get just the last mod time of the file
208 wxDateTime
GetModificationTime() const
211 (void)GetTimes(NULL
, &dtMod
, NULL
);
214 #endif // wxUSE_DATETIME
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() ;
227 // various file/dir operations
229 // retrieve the value of the current working directory
230 void AssignCwd(const wxString
& volume
= wxEmptyString
);
231 static wxString
GetCwd(const wxString
& volume
= wxEmptyString
);
233 // change the current working directory
235 static bool SetCwd( const wxString
&cwd
);
237 // get the value of user home (Unix only mainly)
238 void AssignHomeDir();
239 static wxString
GetHomeDir();
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
244 void AssignTempFileName(const wxString
& prefix
, wxFile
*fileTemp
= NULL
);
245 static wxString
CreateTempFileName(const wxString
& prefix
,
246 wxFile
*fileTemp
= NULL
);
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 );
254 static bool Rmdir( const wxString
&dir
);
256 // operations on the path
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
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
);
267 // get a path path relative to the given base directory, i.e. opposite
270 // pass an empty string to get a path relative to the working directory
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,
275 bool MakeRelativeTo(const wxString
& pathBase
= wxEmptyString
,
276 wxPathFormat format
= wxPATH_NATIVE
);
278 // make the path absolute
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
); }
288 // compares with the rules of the given platforms format
289 bool SameAs(const wxFileName
& filepath
,
290 wxPathFormat format
= wxPATH_NATIVE
) const;
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
); }
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
)); }
304 // are the file names of this type cases sensitive?
305 static bool IsCaseSensitive( wxPathFormat format
= wxPATH_NATIVE
);
307 // is this filename absolute?
308 bool IsAbsolute(wxPathFormat format
= wxPATH_NATIVE
) const;
310 // is this filename relative?
311 bool IsRelative(wxPathFormat format
= wxPATH_NATIVE
) const
312 { return !IsAbsolute(format
); }
314 // Information about path format
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
319 static wxString
GetVolumeSeparator(wxPathFormat format
= wxPATH_NATIVE
);
321 // get the string of path separators for this format
322 static wxString
GetPathSeparators(wxPathFormat format
= wxPATH_NATIVE
);
324 // get the canonical path separator for this format
325 static wxChar
GetPathSeparator(wxPathFormat format
= wxPATH_NATIVE
)
326 { return GetPathSeparators(format
)[0u]; }
328 // is the char a path separator for this format?
329 static bool IsPathSeparator(wxChar ch
, wxPathFormat format
= wxPATH_NATIVE
);
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(); }
339 void SetExt( const wxString
&ext
) { m_ext
= ext
; }
340 wxString
GetExt() const { return m_ext
; }
341 bool HasExt() const { return !m_ext
.empty(); }
343 void SetName( const wxString
&name
) { m_name
= name
; }
344 wxString
GetName() const { return m_name
; }
345 bool HasName() const { return !m_name
.empty(); }
347 void SetVolume( const wxString
&volume
) { m_volume
= volume
; }
348 wxString
GetVolume() const { return m_volume
; }
349 bool HasVolume() const { return !m_volume
.empty(); }
351 // full name is the file name + extension (but without the path)
352 void SetFullName(const wxString
& fullname
);
353 wxString
GetFullName() const;
355 const wxArrayString
& GetDirs() const { return m_dirs
; }
357 // flags are combination of wxPATH_GET_XXX flags
358 wxString
GetPath(int flags
= 0, wxPathFormat format
= wxPATH_NATIVE
) const;
360 // Replace current path with this one
361 void SetPath( const wxString
&path
, wxPathFormat format
= wxPATH_NATIVE
);
363 // Construct full path with name and ext
364 wxString
GetFullPath( wxPathFormat format
= wxPATH_NATIVE
) const;
366 // Return the short form of the path (returns identity on non-Windows platforms)
367 wxString
GetShortPath() const;
369 // Return the long form of the path (returns identity on non-Windows platforms)
370 wxString
GetLongPath() const;
372 // Is this a file or directory (not necessarily an existing one)
373 bool IsDir() const { return m_name
.empty() && m_ext
.empty(); }
377 // get the canonical path format for this platform
378 static wxPathFormat
GetFormat( wxPathFormat format
= wxPATH_NATIVE
);
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
,
387 wxPathFormat format
= wxPATH_NATIVE
);
389 // compatibility version
390 static void SplitPath(const wxString
& fullpath
,
394 wxPathFormat format
= wxPATH_NATIVE
);
397 // deprecated methods, don't use any more
398 // --------------------------------------
400 #ifndef __DIGITALMARS__
401 wxString
GetPath( bool withSep
, wxPathFormat format
= wxPATH_NATIVE
) const
402 { return GetPath(withSep
? wxPATH_GET_SEPARATOR
: 0, format
); }
404 wxString
GetPathWithSep(wxPathFormat format
= wxPATH_NATIVE
) const
405 { return GetPath(wxPATH_GET_SEPARATOR
, format
); }
408 // the drive/volume/device specification (always empty for Unix)
411 // the path components of the file
412 wxArrayString m_dirs
;
414 // the file name and extension (empty for directories)
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
421 // we use m_relative to distinguish between these two cases, it will be
422 // TRUE in the former and FALSE in the latter
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)
429 #endif // _WX_FILENAME_H_