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(_T(""), 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 static wxFileName
DirName(const wxString
& dir
);
175 // is the filename valid at all?
176 bool IsOk() const { return m_dirs
.size() != 0 || !m_name
.IsEmpty(); }
178 // does the file with this name exists?
179 bool FileExists() const;
180 static bool FileExists( const wxString
&file
);
182 // does the directory with this name exists?
183 bool DirExists() const;
184 static bool DirExists( const wxString
&dir
);
186 // VZ: also need: IsDirWritable(), IsFileExecutable() &c (TODO)
190 // set the file last access/mod and creation times
191 // (any of the pointers may be NULL)
192 bool SetTimes(const wxDateTime
*dtAccess
,
193 const wxDateTime
*dtMod
,
194 const wxDateTime
*dtCreate
);
196 // set the access and modification times to the current moment
199 // return the last access, last modification and create times
200 // (any of the pointers may be NULL)
201 bool GetTimes(wxDateTime
*dtAccess
,
203 wxDateTime
*dtCreate
) const;
205 // convenience wrapper: get just the last mod time of the file
206 wxDateTime
GetModificationTime() const
209 (void)GetTimes(NULL
, &dtMod
, NULL
);
212 #endif // wxUSE_DATETIME
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() ;
225 // various file/dir operations
227 // retrieve the value of the current working directory
228 void AssignCwd(const wxString
& volume
= wxEmptyString
);
229 static wxString
GetCwd(const wxString
& volume
= wxEmptyString
);
231 // change the current working directory
233 static bool SetCwd( const wxString
&cwd
);
235 // get the value of user home (Unix only mainly)
236 void AssignHomeDir();
237 static wxString
GetHomeDir();
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
242 void AssignTempFileName(const wxString
& prefix
, wxFile
*fileTemp
= NULL
);
243 static wxString
CreateTempFileName(const wxString
& prefix
,
244 wxFile
*fileTemp
= NULL
);
246 // directory creation and removal.
247 // if full is TRUE, will try to make each directory in the path.
248 bool Mkdir( int perm
= 0777, int flags
= 0);
249 static bool Mkdir( const wxString
&dir
, int perm
= 0777, int flags
= 0 );
252 static bool Rmdir( const wxString
&dir
);
254 // operations on the path
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
260 // this may be done using another (than current) value of cwd
261 bool Normalize(int flags
= wxPATH_NORM_ALL
,
262 const wxString
& cwd
= wxEmptyString
,
263 wxPathFormat format
= wxPATH_NATIVE
);
265 // get a path path relative to the given base directory, i.e. opposite
268 // pass an empty string to get a path relative to the working directory
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,
273 bool MakeRelativeTo(const wxString
& pathBase
= _T(""),
274 wxPathFormat format
= wxPATH_NATIVE
);
276 // make the path absolute
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
); }
286 // compares with the rules of the given platforms format
287 bool SameAs(const wxFileName
& filepath
,
288 wxPathFormat format
= wxPATH_NATIVE
) const;
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
); }
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
)); }
302 // are the file names of this type cases sensitive?
303 static bool IsCaseSensitive( wxPathFormat format
= wxPATH_NATIVE
);
305 // is this filename absolute?
306 bool IsAbsolute(wxPathFormat format
= wxPATH_NATIVE
) const;
308 // is this filename relative?
309 bool IsRelative(wxPathFormat format
= wxPATH_NATIVE
) const
310 { return !IsAbsolute(format
); }
312 // Information about path format
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
317 static wxString
GetVolumeSeparator(wxPathFormat format
= wxPATH_NATIVE
);
319 // get the string of path separators for this format
320 static wxString
GetPathSeparators(wxPathFormat format
= wxPATH_NATIVE
);
322 // get the canonical path separator for this format
323 static wxChar
GetPathSeparator(wxPathFormat format
= wxPATH_NATIVE
)
324 { return GetPathSeparators(format
)[0u]; }
326 // is the char a path separator for this format?
327 static bool IsPathSeparator(wxChar ch
, wxPathFormat format
= wxPATH_NATIVE
);
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
);
334 size_t GetDirCount() const { return m_dirs
.size(); }
337 void SetExt( const wxString
&ext
) { m_ext
= ext
; }
338 wxString
GetExt() const { return m_ext
; }
339 bool HasExt() const { return !m_ext
.empty(); }
341 void SetName( const wxString
&name
) { m_name
= name
; }
342 wxString
GetName() const { return m_name
; }
343 bool HasName() const { return !m_name
.empty(); }
345 void SetVolume( const wxString
&volume
) { m_volume
= volume
; }
346 wxString
GetVolume() const { return m_volume
; }
347 bool HasVolume() const { return !m_volume
.empty(); }
349 // full name is the file name + extension (but without the path)
350 void SetFullName(const wxString
& fullname
);
351 wxString
GetFullName() const;
353 const wxArrayString
& GetDirs() const { return m_dirs
; }
355 // flags are combination of wxPATH_GET_XXX flags
356 wxString
GetPath(int flags
= 0, wxPathFormat format
= wxPATH_NATIVE
) const;
358 // Replace current path with this one
359 void SetPath( const wxString
&path
, wxPathFormat format
= wxPATH_NATIVE
);
361 // Construct full path with name and ext
362 wxString
GetFullPath( wxPathFormat format
= wxPATH_NATIVE
) const;
364 // Return the short form of the path (returns identity on non-Windows platforms)
365 wxString
GetShortPath() const;
367 // Return the long form of the path (returns identity on non-Windows platforms)
368 wxString
GetLongPath() const;
370 // Is this a file or directory (not necessarily an existing one)
371 bool IsDir() const { return m_name
.empty() && m_ext
.empty(); }
375 // get the canonical path format for this platform
376 static wxPathFormat
GetFormat( wxPathFormat format
= wxPATH_NATIVE
);
378 // split a fullpath into the volume, path, (base) name and extension
379 // (all of the pointers can be NULL)
380 static void SplitPath(const wxString
& fullpath
,
385 wxPathFormat format
= wxPATH_NATIVE
);
387 // compatibility version
388 static void SplitPath(const wxString
& fullpath
,
392 wxPathFormat format
= wxPATH_NATIVE
);
395 // deprecated methods, don't use any more
396 // --------------------------------------
398 #ifndef __DIGITALMARS__
399 wxString
GetPath( bool withSep
, wxPathFormat format
= wxPATH_NATIVE
) const
400 { return GetPath(withSep
? wxPATH_GET_SEPARATOR
: 0, format
); }
402 wxString
GetPathWithSep(wxPathFormat format
= wxPATH_NATIVE
) const
403 { return GetPath(wxPATH_GET_SEPARATOR
, format
); }
406 // the drive/volume/device specification (always empty for Unix)
409 // the path components of the file
410 wxArrayString m_dirs
;
412 // the file name and extension (empty for directories)
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
419 // we use m_relative to distinguish between these two cases, it will be
420 // TRUE in the former and FALSE in the latter
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)
427 #endif // _WX_FILENAME_H_