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_
18 1. support for drives under Windows
19 2. more file operations:
21 b) [acm]time() - get and set
23 3. SameFileAs() function to compare inodes under Unix
26 #include "wx/arrstr.h"
27 #include "wx/filefn.h"
28 #include "wx/datetime.h"
30 #include "wx/longlong.h"
34 class WXDLLIMPEXP_FWD_BASE wxFile
;
38 class WXDLLIMPEXP_FWD_BASE wxFFile
;
41 // this symbol is defined for the platforms where file systems use volumes in
43 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
44 #define wxHAS_FILESYSTEM_VOLUMES
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // the various values for the path format: this mainly affects the path
52 // separator but also whether or not the path has the drive part (as under
56 wxPATH_NATIVE
= 0, // the path format for the current platform
58 wxPATH_BEOS
= wxPATH_UNIX
,
61 wxPATH_WIN
= wxPATH_DOS
,
62 wxPATH_OS2
= wxPATH_DOS
,
65 wxPATH_MAX
// Not a valid value for specifying path format
68 // different conventions that may be used with GetHumanReadableSize()
71 wxSIZE_CONV_TRADITIONAL
, // 1024 bytes = 1 KB
72 wxSIZE_CONV_IEC
, // 1024 bytes = 1 KiB
73 wxSIZE_CONV_SI
// 1000 bytes = 1 KB
76 // the kind of normalization to do with the file name: these values can be
77 // or'd together to perform several operations at once
80 wxPATH_NORM_ENV_VARS
= 0x0001, // replace env vars with their values
81 wxPATH_NORM_DOTS
= 0x0002, // squeeze all .. and .
82 wxPATH_NORM_TILDE
= 0x0004, // Unix only: replace ~ and ~user
83 wxPATH_NORM_CASE
= 0x0008, // if case insensitive => tolower
84 wxPATH_NORM_ABSOLUTE
= 0x0010, // make the path absolute
85 wxPATH_NORM_LONG
= 0x0020, // make the path the long form
86 wxPATH_NORM_SHORTCUT
= 0x0040, // resolve the shortcut, if it is a shortcut
87 wxPATH_NORM_ALL
= 0x00ff & ~wxPATH_NORM_CASE
90 // what exactly should GetPath() return?
93 wxPATH_NO_SEPARATOR
= 0x0000, // for symmetry with wxPATH_GET_SEPARATOR
94 wxPATH_GET_VOLUME
= 0x0001, // include the volume if applicable
95 wxPATH_GET_SEPARATOR
= 0x0002 // terminate the path with the separator
101 wxPATH_MKDIR_FULL
= 0x0001 // create directories recursively
107 wxPATH_RMDIR_FULL
= 0x0001, // delete with subdirectories if empty
108 wxPATH_RMDIR_RECURSIVE
= 0x0002 // delete all recursively (dangerous!)
112 // error code of wxFileName::GetSize()
113 extern WXDLLIMPEXP_DATA_BASE(const wxULongLong
) wxInvalidSize
;
114 #endif // wxUSE_LONGLONG
118 // ----------------------------------------------------------------------------
119 // wxFileName: encapsulates a file path
120 // ----------------------------------------------------------------------------
122 class WXDLLIMPEXP_BASE wxFileName
125 // constructors and assignment
128 wxFileName() { Clear(); }
129 wxFileName(const wxFileName
& filepath
) { Assign(filepath
); }
131 // from a full filename: if it terminates with a '/', a directory path
132 // is contructed (the name will be empty), otherwise a file name and
133 // extension are extracted from it
134 wxFileName( const wxString
& fullpath
, wxPathFormat format
= wxPATH_NATIVE
)
135 { Assign( fullpath
, format
); m_dontFollowLinks
= false; }
137 // from a directory name and a file name
138 wxFileName(const wxString
& path
,
139 const wxString
& name
,
140 wxPathFormat format
= wxPATH_NATIVE
)
141 { Assign(path
, name
, format
); m_dontFollowLinks
= false; }
143 // from a volume, directory name, file base name and extension
144 wxFileName(const wxString
& volume
,
145 const wxString
& path
,
146 const wxString
& name
,
148 wxPathFormat format
= wxPATH_NATIVE
)
149 { Assign(volume
, path
, name
, ext
, format
); m_dontFollowLinks
= false; }
151 // from a directory name, file base name and extension
152 wxFileName(const wxString
& path
,
153 const wxString
& name
,
155 wxPathFormat format
= wxPATH_NATIVE
)
156 { Assign(path
, name
, ext
, format
); m_dontFollowLinks
= false; }
158 // the same for delayed initialization
160 void Assign(const wxFileName
& filepath
);
162 void Assign(const wxString
& fullpath
,
163 wxPathFormat format
= wxPATH_NATIVE
);
165 void Assign(const wxString
& volume
,
166 const wxString
& path
,
167 const wxString
& name
,
170 wxPathFormat format
= wxPATH_NATIVE
);
172 void Assign(const wxString
& volume
,
173 const wxString
& path
,
174 const wxString
& name
,
176 wxPathFormat format
= wxPATH_NATIVE
)
177 { Assign(volume
, path
, name
, ext
, !ext
.empty(), format
); }
179 void Assign(const wxString
& path
,
180 const wxString
& name
,
181 wxPathFormat format
= wxPATH_NATIVE
);
183 void Assign(const wxString
& path
,
184 const wxString
& name
,
186 wxPathFormat format
= wxPATH_NATIVE
);
188 void AssignDir(const wxString
& dir
, wxPathFormat format
= wxPATH_NATIVE
);
190 // assorted assignment operators
192 wxFileName
& operator=(const wxFileName
& filename
)
193 { if (this != &filename
) Assign(filename
); return *this; }
195 wxFileName
& operator=(const wxString
& filename
)
196 { Assign(filename
); return *this; }
198 // reset all components to default, uninitialized state
201 // static pseudo constructors
202 static wxFileName
FileName(const wxString
& file
,
203 wxPathFormat format
= wxPATH_NATIVE
);
204 static wxFileName
DirName(const wxString
& dir
,
205 wxPathFormat format
= wxPATH_NATIVE
);
209 // is the filename valid at all?
212 // we're fine if we have the path or the name or if we're a root dir
213 return m_dirs
.size() != 0 || !m_name
.empty() || !m_relative
||
214 !m_ext
.empty() || m_hasExt
;
217 // does the file with this name exist?
218 bool FileExists() const;
219 static bool FileExists( const wxString
&file
);
221 // does the directory with this name exist?
222 bool DirExists() const;
223 static bool DirExists( const wxString
&dir
);
225 // does anything at all with this name (i.e. file, directory or some
226 // other file system object such as a device, socket, ...) exist?
228 static bool Exists(const wxString
& path
);
231 // checks on most common flags for files/directories;
232 // more platform-specific features (like e.g. Unix permissions) are not
233 // available in wxFileName
235 bool IsDirWritable() const { return wxIsWritable(GetPath()); }
236 static bool IsDirWritable(const wxString
&path
) { return wxDirExists(path
) && wxIsWritable(path
); }
238 bool IsDirReadable() const { return wxIsReadable(GetPath()); }
239 static bool IsDirReadable(const wxString
&path
) { return wxDirExists(path
) && wxIsReadable(path
); }
241 // NOTE: IsDirExecutable() is not present because the meaning of "executable"
242 // directory is very platform-dependent and also not so useful
244 bool IsFileWritable() const { return wxIsWritable(GetFullPath()); }
245 static bool IsFileWritable(const wxString
&path
) { return wxFileExists(path
) && wxIsWritable(path
); }
247 bool IsFileReadable() const { return wxIsReadable(GetFullPath()); }
248 static bool IsFileReadable(const wxString
&path
) { return wxFileExists(path
) && wxIsReadable(path
); }
250 bool IsFileExecutable() const { return wxIsExecutable(GetFullPath()); }
251 static bool IsFileExecutable(const wxString
&path
) { return wxFileExists(path
) && wxIsExecutable(path
); }
256 // set the file last access/mod and creation times
257 // (any of the pointers may be NULL)
258 bool SetTimes(const wxDateTime
*dtAccess
,
259 const wxDateTime
*dtMod
,
260 const wxDateTime
*dtCreate
) const;
262 // set the access and modification times to the current moment
265 // return the last access, last modification and create times
266 // (any of the pointers may be NULL)
267 bool GetTimes(wxDateTime
*dtAccess
,
269 wxDateTime
*dtCreate
) const;
271 // convenience wrapper: get just the last mod time of the file
272 wxDateTime
GetModificationTime() const
275 (void)GetTimes(NULL
, &dtMod
, NULL
);
278 #endif // wxUSE_DATETIME
280 #if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
281 bool MacSetTypeAndCreator( wxUint32 type
, wxUint32 creator
) ;
282 bool MacGetTypeAndCreator( wxUint32
*type
, wxUint32
*creator
) const;
283 // gets the 'common' type and creator for a certain extension
284 static bool MacFindDefaultTypeAndCreator( const wxString
& ext
, wxUint32
*type
, wxUint32
*creator
) ;
285 // registers application defined extensions and their default type and creator
286 static void MacRegisterDefaultTypeAndCreator( const wxString
& ext
, wxUint32 type
, wxUint32 creator
) ;
287 // looks up the appropriate type and creator from the registration and then sets
288 bool MacSetDefaultTypeAndCreator() ;
291 // various file/dir operations
293 // retrieve the value of the current working directory
294 void AssignCwd(const wxString
& volume
= wxEmptyString
);
295 static wxString
GetCwd(const wxString
& volume
= wxEmptyString
);
297 // change the current working directory
299 static bool SetCwd( const wxString
&cwd
);
301 // get the value of user home (Unix only mainly)
302 void AssignHomeDir();
303 static wxString
GetHomeDir();
305 // get the system temporary directory
306 static wxString
GetTempDir();
308 #if wxUSE_FILE || wxUSE_FFILE
309 // get a temp file name starting with the specified prefix
310 void AssignTempFileName(const wxString
& prefix
);
311 static wxString
CreateTempFileName(const wxString
& prefix
);
315 // get a temp file name starting with the specified prefix and open the
316 // file passed to us using this name for writing (atomically if
318 void AssignTempFileName(const wxString
& prefix
, wxFile
*fileTemp
);
319 static wxString
CreateTempFileName(const wxString
& prefix
,
324 // get a temp file name starting with the specified prefix and open the
325 // file passed to us using this name for writing (atomically if
327 void AssignTempFileName(const wxString
& prefix
, wxFFile
*fileTemp
);
328 static wxString
CreateTempFileName(const wxString
& prefix
,
330 #endif // wxUSE_FFILE
332 // directory creation and removal.
333 bool Mkdir(int perm
= wxS_DIR_DEFAULT
, int flags
= 0) const;
334 static bool Mkdir(const wxString
&dir
, int perm
= wxS_DIR_DEFAULT
,
337 bool Rmdir(int flags
= 0) const;
338 static bool Rmdir(const wxString
&dir
, int flags
= 0);
340 // operations on the path
342 // normalize the path: with the default flags value, the path will be
343 // made absolute, without any ".." and "." and all environment
344 // variables will be expanded in it
346 // this may be done using another (than current) value of cwd
347 bool Normalize(int flags
= wxPATH_NORM_ALL
,
348 const wxString
& cwd
= wxEmptyString
,
349 wxPathFormat format
= wxPATH_NATIVE
);
351 // get a path path relative to the given base directory, i.e. opposite
354 // pass an empty string to get a path relative to the working directory
356 // returns true if the file name was modified, false if we failed to do
357 // anything with it (happens when the file is on a different volume,
359 bool MakeRelativeTo(const wxString
& pathBase
= wxEmptyString
,
360 wxPathFormat format
= wxPATH_NATIVE
);
362 // make the path absolute
364 // this may be done using another (than current) value of cwd
365 bool MakeAbsolute(const wxString
& cwd
= wxEmptyString
,
366 wxPathFormat format
= wxPATH_NATIVE
)
367 { return Normalize(wxPATH_NORM_DOTS
| wxPATH_NORM_ABSOLUTE
|
368 wxPATH_NORM_TILDE
, cwd
, format
); }
371 // If the path is a symbolic link (Unix-only), indicate that all
372 // filesystem operations on this path should be performed on the link
373 // itself and not on the file it points to, as is the case by default.
375 // No effect if this is not a symbolic link.
376 void DontFollowLink()
378 m_dontFollowLinks
= true;
381 // If the path is a symbolic link (Unix-only), returns whether various
382 // file operations should act on the link itself, or on its target.
384 // This does not test if the path is really a symlink or not.
385 bool ShouldFollowLink() const
387 return !m_dontFollowLinks
;
390 #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
391 // if the path is a shortcut, return the target and optionally,
393 bool GetShortcutTarget(const wxString
& shortcutPath
,
394 wxString
& targetFilename
,
395 wxString
* arguments
= NULL
) const;
399 // if the path contains the value of the environment variable named envname
400 // then this function replaces it with the string obtained from
401 // wxString::Format(replacementFmtString, value_of_envname_variable)
404 // wxFileName fn("/usr/openwin/lib/someFile");
405 // fn.ReplaceEnvVariable("OPENWINHOME");
406 // // now fn.GetFullPath() == "$OPENWINHOME/lib/someFile"
407 bool ReplaceEnvVariable(const wxString
& envname
,
408 const wxString
& replacementFmtString
= "$%s",
409 wxPathFormat format
= wxPATH_NATIVE
);
412 // replaces, if present in the path, the home directory for the given user
413 // (see wxGetHomeDir) with a tilde
414 bool ReplaceHomeDir(wxPathFormat format
= wxPATH_NATIVE
);
419 // compares with the rules of the given platforms format
420 bool SameAs(const wxFileName
& filepath
,
421 wxPathFormat format
= wxPATH_NATIVE
) const;
423 // compare with another filename object
424 bool operator==(const wxFileName
& filename
) const
425 { return SameAs(filename
); }
426 bool operator!=(const wxFileName
& filename
) const
427 { return !SameAs(filename
); }
429 // compare with a filename string interpreted as a native file name
430 bool operator==(const wxString
& filename
) const
431 { return SameAs(wxFileName(filename
)); }
432 bool operator!=(const wxString
& filename
) const
433 { return !SameAs(wxFileName(filename
)); }
435 // are the file names of this type cases sensitive?
436 static bool IsCaseSensitive( wxPathFormat format
= wxPATH_NATIVE
);
438 // is this filename absolute?
439 bool IsAbsolute(wxPathFormat format
= wxPATH_NATIVE
) const;
441 // is this filename relative?
442 bool IsRelative(wxPathFormat format
= wxPATH_NATIVE
) const
443 { return !IsAbsolute(format
); }
445 // Returns the characters that aren't allowed in filenames
446 // on the specified platform.
447 static wxString
GetForbiddenChars(wxPathFormat format
= wxPATH_NATIVE
);
449 // Information about path format
451 // get the string separating the volume from the path for this format,
452 // return an empty string if this format doesn't support the notion of
454 static wxString
GetVolumeSeparator(wxPathFormat format
= wxPATH_NATIVE
);
456 // get the string of path separators for this format
457 static wxString
GetPathSeparators(wxPathFormat format
= wxPATH_NATIVE
);
459 // get the string of path terminators, i.e. characters which terminate the
461 static wxString
GetPathTerminators(wxPathFormat format
= wxPATH_NATIVE
);
463 // get the canonical path separator for this format
464 static wxUniChar
GetPathSeparator(wxPathFormat format
= wxPATH_NATIVE
)
465 { return GetPathSeparators(format
)[0u]; }
467 // is the char a path separator for this format?
468 static bool IsPathSeparator(wxChar ch
, wxPathFormat format
= wxPATH_NATIVE
);
470 // is this is a DOS path which beings with a windows unique volume name
471 // ('\\?\Volume{guid}\')?
472 static bool IsMSWUniqueVolumeNamePath(const wxString
& path
,
473 wxPathFormat format
= wxPATH_NATIVE
);
476 size_t GetDirCount() const { return m_dirs
.size(); }
477 void AppendDir(const wxString
& dir
);
478 void PrependDir(const wxString
& dir
);
479 void InsertDir(size_t before
, const wxString
& dir
);
480 void RemoveDir(size_t pos
);
481 void RemoveLastDir() { RemoveDir(GetDirCount() - 1); }
484 void SetExt( const wxString
&ext
) { m_ext
= ext
; m_hasExt
= !m_ext
.empty(); }
485 void ClearExt() { m_ext
= wxEmptyString
; m_hasExt
= false; }
486 void SetEmptyExt() { m_ext
= wxT(""); m_hasExt
= true; }
487 wxString
GetExt() const { return m_ext
; }
488 bool HasExt() const { return m_hasExt
; }
490 void SetName( const wxString
&name
) { m_name
= name
; }
491 wxString
GetName() const { return m_name
; }
492 bool HasName() const { return !m_name
.empty(); }
494 void SetVolume( const wxString
&volume
) { m_volume
= volume
; }
495 wxString
GetVolume() const { return m_volume
; }
496 bool HasVolume() const { return !m_volume
.empty(); }
498 // full name is the file name + extension (but without the path)
499 void SetFullName(const wxString
& fullname
);
500 wxString
GetFullName() const;
502 const wxArrayString
& GetDirs() const { return m_dirs
; }
504 // flags are combination of wxPATH_GET_XXX flags
505 wxString
GetPath(int flags
= wxPATH_GET_VOLUME
,
506 wxPathFormat format
= wxPATH_NATIVE
) const;
508 // Replace current path with this one
509 void SetPath( const wxString
&path
, wxPathFormat format
= wxPATH_NATIVE
);
511 // Construct full path with name and ext
512 wxString
GetFullPath( wxPathFormat format
= wxPATH_NATIVE
) const;
514 // Return the short form of the path (returns identity on non-Windows platforms)
515 wxString
GetShortPath() const;
517 // Return the long form of the path (returns identity on non-Windows platforms)
518 wxString
GetLongPath() const;
520 // Is this a file or directory (not necessarily an existing one)
521 bool IsDir() const { return m_name
.empty() && m_ext
.empty(); }
525 // get the canonical path format for this platform
526 static wxPathFormat
GetFormat( wxPathFormat format
= wxPATH_NATIVE
);
528 // split a fullpath into the volume, path, (base) name and extension
529 // (all of the pointers can be NULL)
530 static void SplitPath(const wxString
& fullpath
,
536 wxPathFormat format
= wxPATH_NATIVE
);
538 static void SplitPath(const wxString
& fullpath
,
545 SplitPath(fullpath
, volume
, path
, name
, ext
, NULL
, format
);
548 // compatibility version: volume is part of path
549 static void SplitPath(const wxString
& fullpath
,
553 wxPathFormat format
= wxPATH_NATIVE
);
555 // split a path into volume and pure path part
556 static void SplitVolume(const wxString
& fullpathWithVolume
,
559 wxPathFormat format
= wxPATH_NATIVE
);
561 // strip the file extension: "foo.bar" => "foo" (but ".baz" => ".baz")
562 static wxString
StripExtension(const wxString
& fullpath
);
564 #ifdef wxHAS_FILESYSTEM_VOLUMES
565 // return the string representing a file system volume, or drive
566 static wxString
GetVolumeString(char drive
, int flags
= wxPATH_GET_SEPARATOR
);
567 #endif // wxHAS_FILESYSTEM_VOLUMES
572 // returns the size of the given filename
573 wxULongLong
GetSize() const;
574 static wxULongLong
GetSize(const wxString
&file
);
576 // returns the size in a human readable form
578 GetHumanReadableSize(const wxString
& nullsize
= _("Not available"),
580 wxSizeConvention conv
= wxSIZE_CONV_TRADITIONAL
) const;
582 GetHumanReadableSize(const wxULongLong
& sz
,
583 const wxString
& nullsize
= _("Not available"),
585 wxSizeConvention conv
= wxSIZE_CONV_TRADITIONAL
);
586 #endif // wxUSE_LONGLONG
589 // deprecated methods, don't use any more
590 // --------------------------------------
592 #ifndef __DIGITALMARS__
593 wxString
GetPath( bool withSep
, wxPathFormat format
= wxPATH_NATIVE
) const
594 { return GetPath(withSep
? wxPATH_GET_SEPARATOR
: 0, format
); }
596 wxString
GetPathWithSep(wxPathFormat format
= wxPATH_NATIVE
) const
597 { return GetPath(wxPATH_GET_VOLUME
| wxPATH_GET_SEPARATOR
, format
); }
600 // check whether this dir is valid for Append/Prepend/InsertDir()
601 static bool IsValidDirComponent(const wxString
& dir
);
603 // the drive/volume/device specification (always empty for Unix)
606 // the path components of the file
607 wxArrayString m_dirs
;
609 // the file name and extension (empty for directories)
613 // when m_dirs is empty it may mean either that we have no path at all or
614 // that our path is '/', i.e. the root directory
616 // we use m_relative to distinguish between these two cases, it will be
617 // true in the former and false in the latter
619 // NB: the path is not absolute just because m_relative is false, it still
620 // needs the drive (i.e. volume) in some formats (Windows)
623 // when m_ext is empty, it may be because we don't have any extension or
624 // because we have an empty extension
626 // the difference is important as file with name "foo" and without
627 // extension has full name "foo" while with empty extension it is "foo."
630 // by default, symlinks are dereferenced but this flag can be set with
631 // DontFollowLink() to change this and make different operations work on
632 // this file path itself instead of the target of the symlink
633 bool m_dontFollowLinks
;
636 #endif // _WX_FILENAME_H_