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 // the kind of normalization to do with the file name: these values can be
69 // or'd together to perform several operations at once
72 wxPATH_NORM_ENV_VARS
= 0x0001, // replace env vars with their values
73 wxPATH_NORM_DOTS
= 0x0002, // squeeze all .. and . and prepend cwd
74 wxPATH_NORM_TILDE
= 0x0004, // Unix only: replace ~ and ~user
75 wxPATH_NORM_CASE
= 0x0008, // if case insensitive => tolower
76 wxPATH_NORM_ABSOLUTE
= 0x0010, // make the path absolute
77 wxPATH_NORM_LONG
= 0x0020, // make the path the long form
78 wxPATH_NORM_SHORTCUT
= 0x0040, // resolve the shortcut, if it is a shortcut
79 wxPATH_NORM_ALL
= 0x00ff & ~wxPATH_NORM_CASE
82 // what exactly should GetPath() return?
85 wxPATH_NO_SEPARATOR
= 0x0000, // for symmetry with wxPATH_GET_SEPARATOR
86 wxPATH_GET_VOLUME
= 0x0001, // include the volume if applicable
87 wxPATH_GET_SEPARATOR
= 0x0002 // terminate the path with the separator
93 wxPATH_MKDIR_FULL
= 0x0001 // create directories recursively
97 // error code of wxFileName::GetSize()
98 extern WXDLLIMPEXP_DATA_BASE(const wxULongLong
) wxInvalidSize
;
99 #endif // wxUSE_LONGLONG
103 // ----------------------------------------------------------------------------
104 // wxFileName: encapsulates a file path
105 // ----------------------------------------------------------------------------
107 class WXDLLIMPEXP_BASE wxFileName
110 // constructors and assignment
113 wxFileName() { Clear(); }
114 wxFileName(const wxFileName
& filepath
) { Assign(filepath
); }
116 // from a full filename: if it terminates with a '/', a directory path
117 // is contructed (the name will be empty), otherwise a file name and
118 // extension are extracted from it
119 wxFileName( const wxString
& fullpath
, wxPathFormat format
= wxPATH_NATIVE
)
120 { Assign( fullpath
, format
); }
122 // from a directory name and a file name
123 wxFileName(const wxString
& path
,
124 const wxString
& name
,
125 wxPathFormat format
= wxPATH_NATIVE
)
126 { Assign(path
, name
, format
); }
128 // from a volume, directory name, file base name and extension
129 wxFileName(const wxString
& volume
,
130 const wxString
& path
,
131 const wxString
& name
,
133 wxPathFormat format
= wxPATH_NATIVE
)
134 { Assign(volume
, path
, name
, ext
, format
); }
136 // from a directory name, file base name and extension
137 wxFileName(const wxString
& path
,
138 const wxString
& name
,
140 wxPathFormat format
= wxPATH_NATIVE
)
141 { Assign(path
, name
, ext
, format
); }
143 // the same for delayed initialization
145 void Assign(const wxFileName
& filepath
);
147 void Assign(const wxString
& fullpath
,
148 wxPathFormat format
= wxPATH_NATIVE
);
150 void Assign(const wxString
& volume
,
151 const wxString
& path
,
152 const wxString
& name
,
155 wxPathFormat format
= wxPATH_NATIVE
);
157 void Assign(const wxString
& volume
,
158 const wxString
& path
,
159 const wxString
& name
,
161 wxPathFormat format
= wxPATH_NATIVE
)
162 { Assign(volume
, path
, name
, ext
, !ext
.empty(), format
); }
164 void Assign(const wxString
& path
,
165 const wxString
& name
,
166 wxPathFormat format
= wxPATH_NATIVE
);
168 void Assign(const wxString
& path
,
169 const wxString
& name
,
171 wxPathFormat format
= wxPATH_NATIVE
);
173 void AssignDir(const wxString
& dir
, wxPathFormat format
= wxPATH_NATIVE
);
175 // assorted assignment operators
177 wxFileName
& operator=(const wxFileName
& filename
)
178 { if (this != &filename
) Assign(filename
); return *this; }
180 wxFileName
& operator=(const wxString
& filename
)
181 { Assign(filename
); return *this; }
183 // reset all components to default, uninitialized state
186 // static pseudo constructors
187 static wxFileName
FileName(const wxString
& file
,
188 wxPathFormat format
= wxPATH_NATIVE
);
189 static wxFileName
DirName(const wxString
& dir
,
190 wxPathFormat format
= wxPATH_NATIVE
);
194 // is the filename valid at all?
197 // we're fine if we have the path or the name or if we're a root dir
198 return m_dirs
.size() != 0 || !m_name
.empty() || !m_relative
||
199 !m_ext
.empty() || m_hasExt
;
202 // does the file with this name exists?
203 bool FileExists() const;
204 static bool FileExists( const wxString
&file
);
206 // does the directory with this name exists?
207 bool DirExists() const;
208 static bool DirExists( const wxString
&dir
);
210 // checks on most common flags for files/directories;
211 // more platform-specific features (like e.g. Unix permissions) are not
212 // available in wxFileName
214 bool IsDirWritable() const { return wxIsWritable(GetPath()); }
215 static bool IsDirWritable(const wxString
&path
) { return wxDirExists(path
) && wxIsWritable(path
); }
217 bool IsDirReadable() const { return wxIsReadable(GetPath()); }
218 static bool IsDirReadable(const wxString
&path
) { return wxDirExists(path
) && wxIsReadable(path
); }
220 // NOTE: IsDirExecutable() is not present because the meaning of "executable"
221 // directory is very platform-dependent and also not so useful
223 bool IsFileWritable() const { return wxIsWritable(GetFullPath()); }
224 static bool IsFileWritable(const wxString
&path
) { return wxFileExists(path
) && wxIsWritable(path
); }
226 bool IsFileReadable() const { return wxIsReadable(GetFullPath()); }
227 static bool IsFileReadable(const wxString
&path
) { return wxFileExists(path
) && wxIsReadable(path
); }
229 bool IsFileExecutable() const { return wxIsExecutable(GetFullPath()); }
230 static bool IsFileExecutable(const wxString
&path
) { return wxFileExists(path
) && wxIsExecutable(path
); }
235 // set the file last access/mod and creation times
236 // (any of the pointers may be NULL)
237 bool SetTimes(const wxDateTime
*dtAccess
,
238 const wxDateTime
*dtMod
,
239 const wxDateTime
*dtCreate
);
241 // set the access and modification times to the current moment
244 // return the last access, last modification and create times
245 // (any of the pointers may be NULL)
246 bool GetTimes(wxDateTime
*dtAccess
,
248 wxDateTime
*dtCreate
) const;
250 // convenience wrapper: get just the last mod time of the file
251 wxDateTime
GetModificationTime() const
254 (void)GetTimes(NULL
, &dtMod
, NULL
);
257 #endif // wxUSE_DATETIME
259 #if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
260 bool MacSetTypeAndCreator( wxUint32 type
, wxUint32 creator
) ;
261 bool MacGetTypeAndCreator( wxUint32
*type
, wxUint32
*creator
) ;
262 // gets the 'common' type and creator for a certain extension
263 static bool MacFindDefaultTypeAndCreator( const wxString
& ext
, wxUint32
*type
, wxUint32
*creator
) ;
264 // registers application defined extensions and their default type and creator
265 static void MacRegisterDefaultTypeAndCreator( const wxString
& ext
, wxUint32 type
, wxUint32 creator
) ;
266 // looks up the appropriate type and creator from the registration and then sets
267 bool MacSetDefaultTypeAndCreator() ;
270 // various file/dir operations
272 // retrieve the value of the current working directory
273 void AssignCwd(const wxString
& volume
= wxEmptyString
);
274 static wxString
GetCwd(const wxString
& volume
= wxEmptyString
);
276 // change the current working directory
278 static bool SetCwd( const wxString
&cwd
);
280 // get the value of user home (Unix only mainly)
281 void AssignHomeDir();
282 static wxString
GetHomeDir();
284 // get the system temporary directory
285 static wxString
GetTempDir();
287 #if wxUSE_FILE || wxUSE_FFILE
288 // get a temp file name starting with the specified prefix
289 void AssignTempFileName(const wxString
& prefix
);
290 static wxString
CreateTempFileName(const wxString
& prefix
);
294 // get a temp file name starting with the specified prefix and open the
295 // file passed to us using this name for writing (atomically if
297 void AssignTempFileName(const wxString
& prefix
, wxFile
*fileTemp
);
298 static wxString
CreateTempFileName(const wxString
& prefix
,
303 // get a temp file name starting with the specified prefix and open the
304 // file passed to us using this name for writing (atomically if
306 void AssignTempFileName(const wxString
& prefix
, wxFFile
*fileTemp
);
307 static wxString
CreateTempFileName(const wxString
& prefix
,
309 #endif // wxUSE_FFILE
311 // directory creation and removal.
312 bool Mkdir( int perm
= wxS_DIR_DEFAULT
, int flags
= 0);
313 static bool Mkdir( const wxString
&dir
, int perm
= wxS_DIR_DEFAULT
, int flags
= 0 );
316 static bool Rmdir( const wxString
&dir
);
318 // operations on the path
320 // normalize the path: with the default flags value, the path will be
321 // made absolute, without any ".." and "." and all environment
322 // variables will be expanded in it
324 // this may be done using another (than current) value of cwd
325 bool Normalize(int flags
= wxPATH_NORM_ALL
,
326 const wxString
& cwd
= wxEmptyString
,
327 wxPathFormat format
= wxPATH_NATIVE
);
329 // get a path path relative to the given base directory, i.e. opposite
332 // pass an empty string to get a path relative to the working directory
334 // returns true if the file name was modified, false if we failed to do
335 // anything with it (happens when the file is on a different volume,
337 bool MakeRelativeTo(const wxString
& pathBase
= wxEmptyString
,
338 wxPathFormat format
= wxPATH_NATIVE
);
340 // make the path absolute
342 // this may be done using another (than current) value of cwd
343 bool MakeAbsolute(const wxString
& cwd
= wxEmptyString
,
344 wxPathFormat format
= wxPATH_NATIVE
)
345 { return Normalize(wxPATH_NORM_DOTS
| wxPATH_NORM_ABSOLUTE
|
346 wxPATH_NORM_TILDE
, cwd
, format
); }
348 #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
349 // if the path is a shortcut, return the target and optionally,
351 bool GetShortcutTarget(const wxString
& shortcutPath
,
352 wxString
& targetFilename
,
353 wxString
* arguments
= NULL
);
358 // compares with the rules of the given platforms format
359 bool SameAs(const wxFileName
& filepath
,
360 wxPathFormat format
= wxPATH_NATIVE
) const;
362 // compare with another filename object
363 bool operator==(const wxFileName
& filename
) const
364 { return SameAs(filename
); }
365 bool operator!=(const wxFileName
& filename
) const
366 { return !SameAs(filename
); }
368 // compare with a filename string interpreted as a native file name
369 bool operator==(const wxString
& filename
) const
370 { return SameAs(wxFileName(filename
)); }
371 bool operator!=(const wxString
& filename
) const
372 { return !SameAs(wxFileName(filename
)); }
374 // are the file names of this type cases sensitive?
375 static bool IsCaseSensitive( wxPathFormat format
= wxPATH_NATIVE
);
377 // is this filename absolute?
378 bool IsAbsolute(wxPathFormat format
= wxPATH_NATIVE
) const;
380 // is this filename relative?
381 bool IsRelative(wxPathFormat format
= wxPATH_NATIVE
) const
382 { return !IsAbsolute(format
); }
384 // Returns the characters that aren't allowed in filenames
385 // on the specified platform.
386 static wxString
GetForbiddenChars(wxPathFormat format
= wxPATH_NATIVE
);
388 // Information about path format
390 // get the string separating the volume from the path for this format,
391 // return an empty string if this format doesn't support the notion of
393 static wxString
GetVolumeSeparator(wxPathFormat format
= wxPATH_NATIVE
);
395 // get the string of path separators for this format
396 static wxString
GetPathSeparators(wxPathFormat format
= wxPATH_NATIVE
);
398 // get the string of path terminators, i.e. characters which terminate the
400 static wxString
GetPathTerminators(wxPathFormat format
= wxPATH_NATIVE
);
402 // get the canonical path separator for this format
403 static wxUniChar
GetPathSeparator(wxPathFormat format
= wxPATH_NATIVE
)
404 { return GetPathSeparators(format
)[0u]; }
406 // is the char a path separator for this format?
407 static bool IsPathSeparator(wxChar ch
, wxPathFormat format
= wxPATH_NATIVE
);
410 size_t GetDirCount() const { return m_dirs
.size(); }
411 void AppendDir(const wxString
& dir
);
412 void PrependDir(const wxString
& dir
);
413 void InsertDir(size_t before
, const wxString
& dir
);
414 void RemoveDir(size_t pos
);
415 void RemoveLastDir() { RemoveDir(GetDirCount() - 1); }
418 void SetExt( const wxString
&ext
) { m_ext
= ext
; m_hasExt
= !m_ext
.empty(); }
419 void ClearExt() { m_ext
= wxEmptyString
; m_hasExt
= false; }
420 void SetEmptyExt() { m_ext
= wxT(""); m_hasExt
= true; }
421 wxString
GetExt() const { return m_ext
; }
422 bool HasExt() const { return m_hasExt
; }
424 void SetName( const wxString
&name
) { m_name
= name
; }
425 wxString
GetName() const { return m_name
; }
426 bool HasName() const { return !m_name
.empty(); }
428 void SetVolume( const wxString
&volume
) { m_volume
= volume
; }
429 wxString
GetVolume() const { return m_volume
; }
430 bool HasVolume() const { return !m_volume
.empty(); }
432 // full name is the file name + extension (but without the path)
433 void SetFullName(const wxString
& fullname
);
434 wxString
GetFullName() const;
436 const wxArrayString
& GetDirs() const { return m_dirs
; }
438 // flags are combination of wxPATH_GET_XXX flags
439 wxString
GetPath(int flags
= wxPATH_GET_VOLUME
,
440 wxPathFormat format
= wxPATH_NATIVE
) const;
442 // Replace current path with this one
443 void SetPath( const wxString
&path
, wxPathFormat format
= wxPATH_NATIVE
);
445 // Construct full path with name and ext
446 wxString
GetFullPath( wxPathFormat format
= wxPATH_NATIVE
) const;
448 // Return the short form of the path (returns identity on non-Windows platforms)
449 wxString
GetShortPath() const;
451 // Return the long form of the path (returns identity on non-Windows platforms)
452 wxString
GetLongPath() const;
454 // Is this a file or directory (not necessarily an existing one)
455 bool IsDir() const { return m_name
.empty() && m_ext
.empty(); }
459 // get the canonical path format for this platform
460 static wxPathFormat
GetFormat( wxPathFormat format
= wxPATH_NATIVE
);
462 // split a fullpath into the volume, path, (base) name and extension
463 // (all of the pointers can be NULL)
464 static void SplitPath(const wxString
& fullpath
,
470 wxPathFormat format
= wxPATH_NATIVE
);
472 static void SplitPath(const wxString
& fullpath
,
479 SplitPath(fullpath
, volume
, path
, name
, ext
, NULL
, format
);
482 // compatibility version: volume is part of path
483 static void SplitPath(const wxString
& fullpath
,
487 wxPathFormat format
= wxPATH_NATIVE
);
489 // split a path into volume and pure path part
490 static void SplitVolume(const wxString
& fullpathWithVolume
,
493 wxPathFormat format
= wxPATH_NATIVE
);
495 #ifdef wxHAS_FILESYSTEM_VOLUMES
496 // return the string representing a file system volume, or drive
497 static wxString
GetVolumeString(char drive
, int flags
= wxPATH_GET_SEPARATOR
);
498 #endif // wxHAS_FILESYSTEM_VOLUMES
503 // returns the size of the given filename
504 wxULongLong
GetSize() const;
505 static wxULongLong
GetSize(const wxString
&file
);
507 // returns the size in a human readable form
508 wxString
GetHumanReadableSize(const wxString
&nullsize
= wxGetTranslation(_T("Not available")),
509 int precision
= 1) const;
510 static wxString
GetHumanReadableSize(const wxULongLong
&sz
,
511 const wxString
&nullsize
= wxGetTranslation(_T("Not available")),
513 #endif // wxUSE_LONGLONG
516 // deprecated methods, don't use any more
517 // --------------------------------------
519 #ifndef __DIGITALMARS__
520 wxString
GetPath( bool withSep
, wxPathFormat format
= wxPATH_NATIVE
) const
521 { return GetPath(withSep
? wxPATH_GET_SEPARATOR
: 0, format
); }
523 wxString
GetPathWithSep(wxPathFormat format
= wxPATH_NATIVE
) const
524 { return GetPath(wxPATH_GET_VOLUME
| wxPATH_GET_SEPARATOR
, format
); }
527 // check whether this dir is valid for Append/Prepend/InsertDir()
528 static bool IsValidDirComponent(const wxString
& dir
);
530 // the drive/volume/device specification (always empty for Unix)
533 // the path components of the file
534 wxArrayString m_dirs
;
536 // the file name and extension (empty for directories)
540 // when m_dirs is empty it may mean either that we have no path at all or
541 // that our path is '/', i.e. the root directory
543 // we use m_relative to distinguish between these two cases, it will be
544 // true in the former and false in the latter
546 // NB: the path is not absolute just because m_relative is false, it still
547 // needs the drive (i.e. volume) in some formats (Windows)
550 // when m_ext is empty, it may be because we don't have any extension or
551 // because we have an empty extension
553 // the difference is important as file with name "foo" and without
554 // extension has full name "foo" while with empty extension it is "foo."
558 #endif // _WX_FILENAME_H_