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(__WXMSW__) || 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 convention that may be used with GetHumanReadableSize()
71 wxSIZE_CONV_TRADIONAL
, // 1024 bytes = 1 KB
72 wxSIZE_CONV_IEC
, // 1024 bytes = 1 KiB
73 wxSIZE_CONV_SI
// 1000 bytes = 1 KB
77 // the kind of normalization to do with the file name: these values can be
78 // or'd together to perform several operations at once
81 wxPATH_NORM_ENV_VARS
= 0x0001, // replace env vars with their values
82 wxPATH_NORM_DOTS
= 0x0002, // squeeze all .. and .
83 wxPATH_NORM_TILDE
= 0x0004, // Unix only: replace ~ and ~user
84 wxPATH_NORM_CASE
= 0x0008, // if case insensitive => tolower
85 wxPATH_NORM_ABSOLUTE
= 0x0010, // make the path absolute
86 wxPATH_NORM_LONG
= 0x0020, // make the path the long form
87 wxPATH_NORM_SHORTCUT
= 0x0040, // resolve the shortcut, if it is a shortcut
88 wxPATH_NORM_ALL
= 0x00ff & ~wxPATH_NORM_CASE
91 // what exactly should GetPath() return?
94 wxPATH_NO_SEPARATOR
= 0x0000, // for symmetry with wxPATH_GET_SEPARATOR
95 wxPATH_GET_VOLUME
= 0x0001, // include the volume if applicable
96 wxPATH_GET_SEPARATOR
= 0x0002 // terminate the path with the separator
102 wxPATH_MKDIR_FULL
= 0x0001 // create directories recursively
108 wxPATH_RMDIR_FULL
= 0x0001, // delete with subdirectories if empty
109 wxPATH_RMDIR_RECURSIVE
= 0x0002 // delete all recursively (dangerous!)
113 // error code of wxFileName::GetSize()
114 extern WXDLLIMPEXP_DATA_BASE(const wxULongLong
) wxInvalidSize
;
115 #endif // wxUSE_LONGLONG
119 // ----------------------------------------------------------------------------
120 // wxFileName: encapsulates a file path
121 // ----------------------------------------------------------------------------
123 class WXDLLIMPEXP_BASE wxFileName
126 // constructors and assignment
129 wxFileName() { Clear(); }
130 wxFileName(const wxFileName
& filepath
) { Assign(filepath
); }
132 // from a full filename: if it terminates with a '/', a directory path
133 // is contructed (the name will be empty), otherwise a file name and
134 // extension are extracted from it
135 wxFileName( const wxString
& fullpath
, wxPathFormat format
= wxPATH_NATIVE
)
136 { Assign( fullpath
, format
); }
138 // from a directory name and a file name
139 wxFileName(const wxString
& path
,
140 const wxString
& name
,
141 wxPathFormat format
= wxPATH_NATIVE
)
142 { Assign(path
, name
, format
); }
144 // from a volume, directory name, file base name and extension
145 wxFileName(const wxString
& volume
,
146 const wxString
& path
,
147 const wxString
& name
,
149 wxPathFormat format
= wxPATH_NATIVE
)
150 { Assign(volume
, path
, name
, ext
, format
); }
152 // from a directory name, file base name and extension
153 wxFileName(const wxString
& path
,
154 const wxString
& name
,
156 wxPathFormat format
= wxPATH_NATIVE
)
157 { Assign(path
, name
, ext
, format
); }
159 // the same for delayed initialization
161 void Assign(const wxFileName
& filepath
);
163 void Assign(const wxString
& fullpath
,
164 wxPathFormat format
= wxPATH_NATIVE
);
166 void Assign(const wxString
& volume
,
167 const wxString
& path
,
168 const wxString
& name
,
171 wxPathFormat format
= wxPATH_NATIVE
);
173 void Assign(const wxString
& volume
,
174 const wxString
& path
,
175 const wxString
& name
,
177 wxPathFormat format
= wxPATH_NATIVE
)
178 { Assign(volume
, path
, name
, ext
, !ext
.empty(), format
); }
180 void Assign(const wxString
& path
,
181 const wxString
& name
,
182 wxPathFormat format
= wxPATH_NATIVE
);
184 void Assign(const wxString
& path
,
185 const wxString
& name
,
187 wxPathFormat format
= wxPATH_NATIVE
);
189 void AssignDir(const wxString
& dir
, wxPathFormat format
= wxPATH_NATIVE
);
191 // assorted assignment operators
193 wxFileName
& operator=(const wxFileName
& filename
)
194 { if (this != &filename
) Assign(filename
); return *this; }
196 wxFileName
& operator=(const wxString
& filename
)
197 { Assign(filename
); return *this; }
199 // reset all components to default, uninitialized state
202 // static pseudo constructors
203 static wxFileName
FileName(const wxString
& file
,
204 wxPathFormat format
= wxPATH_NATIVE
);
205 static wxFileName
DirName(const wxString
& dir
,
206 wxPathFormat format
= wxPATH_NATIVE
);
210 // is the filename valid at all?
213 // we're fine if we have the path or the name or if we're a root dir
214 return m_dirs
.size() != 0 || !m_name
.empty() || !m_relative
||
215 !m_ext
.empty() || m_hasExt
;
218 // does the file with this name exists?
219 bool FileExists() const;
220 static bool FileExists( const wxString
&file
);
222 // does the directory with this name exists?
223 bool DirExists() const;
224 static bool DirExists( const wxString
&dir
);
226 // checks on most common flags for files/directories;
227 // more platform-specific features (like e.g. Unix permissions) are not
228 // available in wxFileName
230 bool IsDirWritable() const { return wxIsWritable(GetPath()); }
231 static bool IsDirWritable(const wxString
&path
) { return wxDirExists(path
) && wxIsWritable(path
); }
233 bool IsDirReadable() const { return wxIsReadable(GetPath()); }
234 static bool IsDirReadable(const wxString
&path
) { return wxDirExists(path
) && wxIsReadable(path
); }
236 // NOTE: IsDirExecutable() is not present because the meaning of "executable"
237 // directory is very platform-dependent and also not so useful
239 bool IsFileWritable() const { return wxIsWritable(GetFullPath()); }
240 static bool IsFileWritable(const wxString
&path
) { return wxFileExists(path
) && wxIsWritable(path
); }
242 bool IsFileReadable() const { return wxIsReadable(GetFullPath()); }
243 static bool IsFileReadable(const wxString
&path
) { return wxFileExists(path
) && wxIsReadable(path
); }
245 bool IsFileExecutable() const { return wxIsExecutable(GetFullPath()); }
246 static bool IsFileExecutable(const wxString
&path
) { return wxFileExists(path
) && wxIsExecutable(path
); }
251 // set the file last access/mod and creation times
252 // (any of the pointers may be NULL)
253 bool SetTimes(const wxDateTime
*dtAccess
,
254 const wxDateTime
*dtMod
,
255 const wxDateTime
*dtCreate
) const;
257 // set the access and modification times to the current moment
260 // return the last access, last modification and create times
261 // (any of the pointers may be NULL)
262 bool GetTimes(wxDateTime
*dtAccess
,
264 wxDateTime
*dtCreate
) const;
266 // convenience wrapper: get just the last mod time of the file
267 wxDateTime
GetModificationTime() const
270 (void)GetTimes(NULL
, &dtMod
, NULL
);
273 #endif // wxUSE_DATETIME
275 #if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
276 bool MacSetTypeAndCreator( wxUint32 type
, wxUint32 creator
) ;
277 bool MacGetTypeAndCreator( wxUint32
*type
, wxUint32
*creator
) const;
278 // gets the 'common' type and creator for a certain extension
279 static bool MacFindDefaultTypeAndCreator( const wxString
& ext
, wxUint32
*type
, wxUint32
*creator
) ;
280 // registers application defined extensions and their default type and creator
281 static void MacRegisterDefaultTypeAndCreator( const wxString
& ext
, wxUint32 type
, wxUint32 creator
) ;
282 // looks up the appropriate type and creator from the registration and then sets
283 bool MacSetDefaultTypeAndCreator() ;
286 // various file/dir operations
288 // retrieve the value of the current working directory
289 void AssignCwd(const wxString
& volume
= wxEmptyString
);
290 static wxString
GetCwd(const wxString
& volume
= wxEmptyString
);
292 // change the current working directory
294 static bool SetCwd( const wxString
&cwd
);
296 // get the value of user home (Unix only mainly)
297 void AssignHomeDir();
298 static wxString
GetHomeDir();
300 // get the system temporary directory
301 static wxString
GetTempDir();
303 #if wxUSE_FILE || wxUSE_FFILE
304 // get a temp file name starting with the specified prefix
305 void AssignTempFileName(const wxString
& prefix
);
306 static wxString
CreateTempFileName(const wxString
& prefix
);
310 // get a temp file name starting with the specified prefix and open the
311 // file passed to us using this name for writing (atomically if
313 void AssignTempFileName(const wxString
& prefix
, wxFile
*fileTemp
);
314 static wxString
CreateTempFileName(const wxString
& prefix
,
319 // get a temp file name starting with the specified prefix and open the
320 // file passed to us using this name for writing (atomically if
322 void AssignTempFileName(const wxString
& prefix
, wxFFile
*fileTemp
);
323 static wxString
CreateTempFileName(const wxString
& prefix
,
325 #endif // wxUSE_FFILE
327 // directory creation and removal.
328 bool Mkdir(int perm
= wxS_DIR_DEFAULT
, int flags
= 0) const;
329 static bool Mkdir(const wxString
&dir
, int perm
= wxS_DIR_DEFAULT
,
332 bool Rmdir(int flags
= 0) const;
333 static bool Rmdir(const wxString
&dir
, int flags
= 0);
335 // operations on the path
337 // normalize the path: with the default flags value, the path will be
338 // made absolute, without any ".." and "." and all environment
339 // variables will be expanded in it
341 // this may be done using another (than current) value of cwd
342 bool Normalize(int flags
= wxPATH_NORM_ALL
,
343 const wxString
& cwd
= wxEmptyString
,
344 wxPathFormat format
= wxPATH_NATIVE
);
346 // get a path path relative to the given base directory, i.e. opposite
349 // pass an empty string to get a path relative to the working directory
351 // returns true if the file name was modified, false if we failed to do
352 // anything with it (happens when the file is on a different volume,
354 bool MakeRelativeTo(const wxString
& pathBase
= wxEmptyString
,
355 wxPathFormat format
= wxPATH_NATIVE
);
357 // make the path absolute
359 // this may be done using another (than current) value of cwd
360 bool MakeAbsolute(const wxString
& cwd
= wxEmptyString
,
361 wxPathFormat format
= wxPATH_NATIVE
)
362 { return Normalize(wxPATH_NORM_DOTS
| wxPATH_NORM_ABSOLUTE
|
363 wxPATH_NORM_TILDE
, cwd
, format
); }
365 #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
366 // if the path is a shortcut, return the target and optionally,
368 bool GetShortcutTarget(const wxString
& shortcutPath
,
369 wxString
& targetFilename
,
370 wxString
* arguments
= NULL
) const;
374 // if the path contains the value of the environment variable named envname
375 // then this function replaces it with the string obtained from
376 // wxString::Format(replacementFmtString, value_of_envname_variable)
379 // wxFileName fn("/usr/openwin/lib/someFile");
380 // fn.ReplaceEnvVariable("OPENWINHOME");
381 // // now fn.GetFullPath() == "$OPENWINHOME/lib/someFile"
382 bool ReplaceEnvVariable(const wxString
& envname
,
383 const wxString
& replacementFmtString
= "$%s",
384 wxPathFormat format
= wxPATH_NATIVE
);
387 // replaces, if present in the path, the home directory for the given user
388 // (see wxGetHomeDir) with a tilde
389 bool ReplaceHomeDir(wxPathFormat format
= wxPATH_NATIVE
);
394 // compares with the rules of the given platforms format
395 bool SameAs(const wxFileName
& filepath
,
396 wxPathFormat format
= wxPATH_NATIVE
) const;
398 // compare with another filename object
399 bool operator==(const wxFileName
& filename
) const
400 { return SameAs(filename
); }
401 bool operator!=(const wxFileName
& filename
) const
402 { return !SameAs(filename
); }
404 // compare with a filename string interpreted as a native file name
405 bool operator==(const wxString
& filename
) const
406 { return SameAs(wxFileName(filename
)); }
407 bool operator!=(const wxString
& filename
) const
408 { return !SameAs(wxFileName(filename
)); }
410 // are the file names of this type cases sensitive?
411 static bool IsCaseSensitive( wxPathFormat format
= wxPATH_NATIVE
);
413 // is this filename absolute?
414 bool IsAbsolute(wxPathFormat format
= wxPATH_NATIVE
) const;
416 // is this filename relative?
417 bool IsRelative(wxPathFormat format
= wxPATH_NATIVE
) const
418 { return !IsAbsolute(format
); }
420 // Returns the characters that aren't allowed in filenames
421 // on the specified platform.
422 static wxString
GetForbiddenChars(wxPathFormat format
= wxPATH_NATIVE
);
424 // Information about path format
426 // get the string separating the volume from the path for this format,
427 // return an empty string if this format doesn't support the notion of
429 static wxString
GetVolumeSeparator(wxPathFormat format
= wxPATH_NATIVE
);
431 // get the string of path separators for this format
432 static wxString
GetPathSeparators(wxPathFormat format
= wxPATH_NATIVE
);
434 // get the string of path terminators, i.e. characters which terminate the
436 static wxString
GetPathTerminators(wxPathFormat format
= wxPATH_NATIVE
);
438 // get the canonical path separator for this format
439 static wxUniChar
GetPathSeparator(wxPathFormat format
= wxPATH_NATIVE
)
440 { return GetPathSeparators(format
)[0u]; }
442 // is the char a path separator for this format?
443 static bool IsPathSeparator(wxChar ch
, wxPathFormat format
= wxPATH_NATIVE
);
445 // is this is a DOS path which beings with a windows unique volume name
446 // ('\\?\Volume{guid}\')?
447 static bool IsMSWUniqueVolumeNamePath(const wxString
& path
,
448 wxPathFormat format
= wxPATH_NATIVE
);
451 size_t GetDirCount() const { return m_dirs
.size(); }
452 void AppendDir(const wxString
& dir
);
453 void PrependDir(const wxString
& dir
);
454 void InsertDir(size_t before
, const wxString
& dir
);
455 void RemoveDir(size_t pos
);
456 void RemoveLastDir() { RemoveDir(GetDirCount() - 1); }
459 void SetExt( const wxString
&ext
) { m_ext
= ext
; m_hasExt
= !m_ext
.empty(); }
460 void ClearExt() { m_ext
= wxEmptyString
; m_hasExt
= false; }
461 void SetEmptyExt() { m_ext
= wxT(""); m_hasExt
= true; }
462 wxString
GetExt() const { return m_ext
; }
463 bool HasExt() const { return m_hasExt
; }
465 void SetName( const wxString
&name
) { m_name
= name
; }
466 wxString
GetName() const { return m_name
; }
467 bool HasName() const { return !m_name
.empty(); }
469 void SetVolume( const wxString
&volume
) { m_volume
= volume
; }
470 wxString
GetVolume() const { return m_volume
; }
471 bool HasVolume() const { return !m_volume
.empty(); }
473 // full name is the file name + extension (but without the path)
474 void SetFullName(const wxString
& fullname
);
475 wxString
GetFullName() const;
477 const wxArrayString
& GetDirs() const { return m_dirs
; }
479 // flags are combination of wxPATH_GET_XXX flags
480 wxString
GetPath(int flags
= wxPATH_GET_VOLUME
,
481 wxPathFormat format
= wxPATH_NATIVE
) const;
483 // Replace current path with this one
484 void SetPath( const wxString
&path
, wxPathFormat format
= wxPATH_NATIVE
);
486 // Construct full path with name and ext
487 wxString
GetFullPath( wxPathFormat format
= wxPATH_NATIVE
) const;
489 // Return the short form of the path (returns identity on non-Windows platforms)
490 wxString
GetShortPath() const;
492 // Return the long form of the path (returns identity on non-Windows platforms)
493 wxString
GetLongPath() const;
495 // Is this a file or directory (not necessarily an existing one)
496 bool IsDir() const { return m_name
.empty() && m_ext
.empty(); }
500 // get the canonical path format for this platform
501 static wxPathFormat
GetFormat( wxPathFormat format
= wxPATH_NATIVE
);
503 // split a fullpath into the volume, path, (base) name and extension
504 // (all of the pointers can be NULL)
505 static void SplitPath(const wxString
& fullpath
,
511 wxPathFormat format
= wxPATH_NATIVE
);
513 static void SplitPath(const wxString
& fullpath
,
520 SplitPath(fullpath
, volume
, path
, name
, ext
, NULL
, format
);
523 // compatibility version: volume is part of path
524 static void SplitPath(const wxString
& fullpath
,
528 wxPathFormat format
= wxPATH_NATIVE
);
530 // split a path into volume and pure path part
531 static void SplitVolume(const wxString
& fullpathWithVolume
,
534 wxPathFormat format
= wxPATH_NATIVE
);
536 // strip the file extension: "foo.bar" => "foo" (but ".baz" => ".baz")
537 static wxString
StripExtension(const wxString
& fullpath
);
539 #ifdef wxHAS_FILESYSTEM_VOLUMES
540 // return the string representing a file system volume, or drive
541 static wxString
GetVolumeString(char drive
, int flags
= wxPATH_GET_SEPARATOR
);
542 #endif // wxHAS_FILESYSTEM_VOLUMES
547 // returns the size of the given filename
548 wxULongLong
GetSize() const;
549 static wxULongLong
GetSize(const wxString
&file
);
551 // returns the size in a human readable form
553 GetHumanReadableSize(const wxString
& nullsize
= _("Not available"),
555 wxSizeConvention conv
= wxSIZE_CONV_TRADIONAL
) const;
557 GetHumanReadableSize(const wxULongLong
& sz
,
558 const wxString
& nullsize
= _("Not available"),
560 wxSizeConvention conv
= wxSIZE_CONV_TRADIONAL
);
561 #endif // wxUSE_LONGLONG
564 // deprecated methods, don't use any more
565 // --------------------------------------
567 #ifndef __DIGITALMARS__
568 wxString
GetPath( bool withSep
, wxPathFormat format
= wxPATH_NATIVE
) const
569 { return GetPath(withSep
? wxPATH_GET_SEPARATOR
: 0, format
); }
571 wxString
GetPathWithSep(wxPathFormat format
= wxPATH_NATIVE
) const
572 { return GetPath(wxPATH_GET_VOLUME
| wxPATH_GET_SEPARATOR
, format
); }
575 // check whether this dir is valid for Append/Prepend/InsertDir()
576 static bool IsValidDirComponent(const wxString
& dir
);
578 // the drive/volume/device specification (always empty for Unix)
581 // the path components of the file
582 wxArrayString m_dirs
;
584 // the file name and extension (empty for directories)
588 // when m_dirs is empty it may mean either that we have no path at all or
589 // that our path is '/', i.e. the root directory
591 // we use m_relative to distinguish between these two cases, it will be
592 // true in the former and false in the latter
594 // NB: the path is not absolute just because m_relative is false, it still
595 // needs the drive (i.e. volume) in some formats (Windows)
598 // when m_ext is empty, it may be because we don't have any extension or
599 // because we have an empty extension
601 // the difference is important as file with name "foo" and without
602 // extension has full name "foo" while with empty extension it is "foo."
606 #endif // _WX_FILENAME_H_