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"
32 class WXDLLIMPEXP_BASE wxFile
;
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // the various values for the path format: this mainly affects the path
40 // separator but also whether or not the path has the drive part (as under
44 wxPATH_NATIVE
= 0, // the path format for the current platform
46 wxPATH_BEOS
= wxPATH_UNIX
,
49 wxPATH_WIN
= wxPATH_DOS
,
50 wxPATH_OS2
= wxPATH_DOS
,
53 wxPATH_MAX
// Not a valid value for specifying path format
56 // the kind of normalization to do with the file name: these values can be
57 // or'd together to perform several operations at once
60 wxPATH_NORM_ENV_VARS
= 0x0001, // replace env vars with their values
61 wxPATH_NORM_DOTS
= 0x0002, // squeeze all .. and . and prepend cwd
62 wxPATH_NORM_TILDE
= 0x0004, // Unix only: replace ~ and ~user
63 wxPATH_NORM_CASE
= 0x0008, // if case insensitive => tolower
64 wxPATH_NORM_ABSOLUTE
= 0x0010, // make the path absolute
65 wxPATH_NORM_LONG
= 0x0020, // make the path the long form
66 wxPATH_NORM_SHORTCUT
= 0x0040, // resolve the shortcut, if it is a shortcut
67 wxPATH_NORM_ALL
= 0x00ff & ~wxPATH_NORM_CASE
70 // what exactly should GetPath() return?
73 wxPATH_GET_VOLUME
= 0x0001, // include the volume if applicable
74 wxPATH_GET_SEPARATOR
= 0x0002 // terminate the path with the separator
80 wxPATH_MKDIR_FULL
= 0x0001 // create directories recursively
83 // error code of wxFileName::GetSize()
84 extern wxULongLong wxInvalidSize
;
88 // ----------------------------------------------------------------------------
89 // wxFileName: encapsulates a file path
90 // ----------------------------------------------------------------------------
92 class WXDLLIMPEXP_BASE wxFileName
95 // constructors and assignment
98 wxFileName() { Clear(); }
99 wxFileName(const wxFileName
& filepath
) { Assign(filepath
); }
101 // from a full filename: if it terminates with a '/', a directory path
102 // is contructed (the name will be empty), otherwise a file name and
103 // extension are extracted from it
104 wxFileName( const wxString
& fullpath
, wxPathFormat format
= wxPATH_NATIVE
)
105 { Assign( fullpath
, format
); }
107 // from a directory name and a file name
108 wxFileName(const wxString
& path
,
109 const wxString
& name
,
110 wxPathFormat format
= wxPATH_NATIVE
)
111 { Assign(path
, name
, format
); }
113 // from a volume, directory name, file base name and extension
114 wxFileName(const wxString
& volume
,
115 const wxString
& path
,
116 const wxString
& name
,
118 wxPathFormat format
= wxPATH_NATIVE
)
119 { Assign(volume
, path
, name
, ext
, format
); }
121 // from a directory name, file base name and extension
122 wxFileName(const wxString
& path
,
123 const wxString
& name
,
125 wxPathFormat format
= wxPATH_NATIVE
)
126 { Assign(path
, name
, ext
, format
); }
128 // the same for delayed initialization
130 void Assign(const wxFileName
& filepath
);
132 void Assign(const wxString
& fullpath
,
133 wxPathFormat format
= wxPATH_NATIVE
);
135 void Assign(const wxString
& volume
,
136 const wxString
& path
,
137 const wxString
& name
,
140 wxPathFormat format
= wxPATH_NATIVE
);
142 void Assign(const wxString
& volume
,
143 const wxString
& path
,
144 const wxString
& name
,
146 wxPathFormat format
= wxPATH_NATIVE
)
147 { Assign(volume
, path
, name
, ext
, !ext
.empty(), format
); }
149 void Assign(const wxString
& path
,
150 const wxString
& name
,
151 wxPathFormat format
= wxPATH_NATIVE
);
153 void Assign(const wxString
& path
,
154 const wxString
& name
,
156 wxPathFormat format
= wxPATH_NATIVE
);
158 void AssignDir(const wxString
& dir
, wxPathFormat format
= wxPATH_NATIVE
);
160 // assorted assignment operators
162 wxFileName
& operator=(const wxFileName
& filename
)
163 { Assign(filename
); return *this; }
165 wxFileName
& operator=(const wxString
& filename
)
166 { Assign(filename
); return *this; }
168 // reset all components to default, uninitialized state
171 // static pseudo constructors
172 static wxFileName
FileName(const wxString
& file
,
173 wxPathFormat format
= wxPATH_NATIVE
);
174 static wxFileName
DirName(const wxString
& dir
,
175 wxPathFormat format
= wxPATH_NATIVE
);
179 // is the filename valid at all?
182 // we're fine if we have the path or the name or if we're a root dir
183 return m_dirs
.size() != 0 || !m_name
.empty() || !m_relative
||
184 !m_ext
.empty() || m_hasExt
;
187 // does the file with this name exists?
188 bool FileExists() const;
189 static bool FileExists( const wxString
&file
);
191 // does the directory with this name exists?
192 bool DirExists() const;
193 static bool DirExists( const wxString
&dir
);
195 // checks on most common flags for files/directories;
196 // more platform-specific features (like e.g. Unix permissions) are not
197 // available in wxFileName
199 bool IsDirWritable() const { return wxIsWritable(GetPath()); }
200 static bool IsDirWritable(const wxString
&path
) { return wxDirExists(path
) && wxIsWritable(path
); }
202 bool IsDirReadable() const { return wxIsReadable(GetPath()); }
203 static bool IsDirReadable(const wxString
&path
) { return wxDirExists(path
) && wxIsReadable(path
); }
205 // NOTE: IsDirExecutable() is not present because the meaning of "executable"
206 // directory is very platform-dependent and also not so useful
208 bool IsFileWritable() const { return wxIsWritable(GetFullPath()); }
209 static bool IsFileWritable(const wxString
&path
) { return wxFileExists(path
) && wxIsWritable(path
); }
211 bool IsFileReadable() const { return wxIsReadable(GetFullPath()); }
212 static bool IsFileReadable(const wxString
&path
) { return wxFileExists(path
) && wxIsReadable(path
); }
214 bool IsFileExecutable() const { return wxIsExecutable(GetFullPath()); }
215 static bool IsFileExecutable(const wxString
&path
) { return wxFileExists(path
) && wxIsExecutable(path
); }
220 // set the file last access/mod and creation times
221 // (any of the pointers may be NULL)
222 bool SetTimes(const wxDateTime
*dtAccess
,
223 const wxDateTime
*dtMod
,
224 const wxDateTime
*dtCreate
);
226 // set the access and modification times to the current moment
229 // return the last access, last modification and create times
230 // (any of the pointers may be NULL)
231 bool GetTimes(wxDateTime
*dtAccess
,
233 wxDateTime
*dtCreate
) const;
235 // convenience wrapper: get just the last mod time of the file
236 wxDateTime
GetModificationTime() const
239 (void)GetTimes(NULL
, &dtMod
, NULL
);
242 #endif // wxUSE_DATETIME
245 bool MacSetTypeAndCreator( wxUint32 type
, wxUint32 creator
) ;
246 bool MacGetTypeAndCreator( wxUint32
*type
, wxUint32
*creator
) ;
247 // gets the 'common' type and creator for a certain extension
248 static bool MacFindDefaultTypeAndCreator( const wxString
& ext
, wxUint32
*type
, wxUint32
*creator
) ;
249 // registers application defined extensions and their default type and creator
250 static void MacRegisterDefaultTypeAndCreator( const wxString
& ext
, wxUint32 type
, wxUint32 creator
) ;
251 // looks up the appropriate type and creator from the registration and then sets
252 bool MacSetDefaultTypeAndCreator() ;
255 // various file/dir operations
257 // retrieve the value of the current working directory
258 void AssignCwd(const wxString
& volume
= wxEmptyString
);
259 static wxString
GetCwd(const wxString
& volume
= wxEmptyString
);
261 // change the current working directory
263 static bool SetCwd( const wxString
&cwd
);
265 // get the value of user home (Unix only mainly)
266 void AssignHomeDir();
267 static wxString
GetHomeDir();
270 // get a temp file name starting with the specified prefix and open the
271 // file passed to us using this name for writing (atomically if
273 void AssignTempFileName(const wxString
& prefix
, wxFile
*fileTemp
= NULL
);
274 static wxString
CreateTempFileName(const wxString
& prefix
,
275 wxFile
*fileTemp
= NULL
);
278 // directory creation and removal.
279 bool Mkdir( int perm
= 0777, int flags
= 0);
280 static bool Mkdir( const wxString
&dir
, int perm
= 0777, int flags
= 0 );
283 static bool Rmdir( const wxString
&dir
);
285 // operations on the path
287 // normalize the path: with the default flags value, the path will be
288 // made absolute, without any ".." and "." and all environment
289 // variables will be expanded in it
291 // this may be done using another (than current) value of cwd
292 bool Normalize(int flags
= wxPATH_NORM_ALL
,
293 const wxString
& cwd
= wxEmptyString
,
294 wxPathFormat format
= wxPATH_NATIVE
);
296 // get a path path relative to the given base directory, i.e. opposite
299 // pass an empty string to get a path relative to the working directory
301 // returns true if the file name was modified, false if we failed to do
302 // anything with it (happens when the file is on a different volume,
304 bool MakeRelativeTo(const wxString
& pathBase
= wxEmptyString
,
305 wxPathFormat format
= wxPATH_NATIVE
);
307 // make the path absolute
309 // this may be done using another (than current) value of cwd
310 bool MakeAbsolute(const wxString
& cwd
= wxEmptyString
,
311 wxPathFormat format
= wxPATH_NATIVE
)
312 { return Normalize(wxPATH_NORM_DOTS
| wxPATH_NORM_ABSOLUTE
|
313 wxPATH_NORM_TILDE
, cwd
, format
); }
315 #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
316 // if the path is a shortcut, return the target and optionally,
318 bool GetShortcutTarget(const wxString
& shortcutPath
,
319 wxString
& targetFilename
,
320 wxString
* arguments
= NULL
);
325 // compares with the rules of the given platforms format
326 bool SameAs(const wxFileName
& filepath
,
327 wxPathFormat format
= wxPATH_NATIVE
) const;
329 // compare with another filename object
330 bool operator==(const wxFileName
& filename
) const
331 { return SameAs(filename
); }
332 bool operator!=(const wxFileName
& filename
) const
333 { return !SameAs(filename
); }
335 // compare with a filename string interpreted as a native file name
336 bool operator==(const wxString
& filename
) const
337 { return SameAs(wxFileName(filename
)); }
338 bool operator!=(const wxString
& filename
) const
339 { return !SameAs(wxFileName(filename
)); }
341 // are the file names of this type cases sensitive?
342 static bool IsCaseSensitive( wxPathFormat format
= wxPATH_NATIVE
);
344 // is this filename absolute?
345 bool IsAbsolute(wxPathFormat format
= wxPATH_NATIVE
) const;
347 // is this filename relative?
348 bool IsRelative(wxPathFormat format
= wxPATH_NATIVE
) const
349 { return !IsAbsolute(format
); }
351 // Returns the characters that aren't allowed in filenames
352 // on the specified platform.
353 static wxString
GetForbiddenChars(wxPathFormat format
= wxPATH_NATIVE
);
355 // Information about path format
357 // get the string separating the volume from the path for this format,
358 // return an empty string if this format doesn't support the notion of
360 static wxString
GetVolumeSeparator(wxPathFormat format
= wxPATH_NATIVE
);
362 // get the string of path separators for this format
363 static wxString
GetPathSeparators(wxPathFormat format
= wxPATH_NATIVE
);
365 // get the string of path terminators, i.e. characters which terminate the
367 static wxString
GetPathTerminators(wxPathFormat format
= wxPATH_NATIVE
);
369 // get the canonical path separator for this format
370 static wxChar
GetPathSeparator(wxPathFormat format
= wxPATH_NATIVE
)
371 { return GetPathSeparators(format
)[0u]; }
373 // is the char a path separator for this format?
374 static bool IsPathSeparator(wxChar ch
, wxPathFormat format
= wxPATH_NATIVE
);
377 size_t GetDirCount() const { return m_dirs
.size(); }
378 void AppendDir(const wxString
& dir
);
379 void PrependDir(const wxString
& dir
);
380 void InsertDir(size_t before
, const wxString
& dir
);
381 void RemoveDir(size_t pos
);
382 void RemoveLastDir() { RemoveDir(GetDirCount() - 1); }
385 void SetExt( const wxString
&ext
) { m_ext
= ext
; m_hasExt
= !m_ext
.empty(); }
386 void ClearExt() { m_ext
= wxEmptyString
; m_hasExt
= false; }
387 void SetEmptyExt() { m_ext
= wxT(""); m_hasExt
= true; }
388 wxString
GetExt() const { return m_ext
; }
389 bool HasExt() const { return m_hasExt
; }
391 void SetName( const wxString
&name
) { m_name
= name
; }
392 wxString
GetName() const { return m_name
; }
393 bool HasName() const { return !m_name
.empty(); }
395 void SetVolume( const wxString
&volume
) { m_volume
= volume
; }
396 wxString
GetVolume() const { return m_volume
; }
397 bool HasVolume() const { return !m_volume
.empty(); }
399 // full name is the file name + extension (but without the path)
400 void SetFullName(const wxString
& fullname
);
401 wxString
GetFullName() const;
403 const wxArrayString
& GetDirs() const { return m_dirs
; }
405 // flags are combination of wxPATH_GET_XXX flags
406 wxString
GetPath(int flags
= wxPATH_GET_VOLUME
,
407 wxPathFormat format
= wxPATH_NATIVE
) const;
409 // Replace current path with this one
410 void SetPath( const wxString
&path
, wxPathFormat format
= wxPATH_NATIVE
);
412 // Construct full path with name and ext
413 wxString
GetFullPath( wxPathFormat format
= wxPATH_NATIVE
) const;
415 // Return the short form of the path (returns identity on non-Windows platforms)
416 wxString
GetShortPath() const;
418 // Return the long form of the path (returns identity on non-Windows platforms)
419 wxString
GetLongPath() const;
421 // Is this a file or directory (not necessarily an existing one)
422 bool IsDir() const { return m_name
.empty() && m_ext
.empty(); }
426 // get the canonical path format for this platform
427 static wxPathFormat
GetFormat( wxPathFormat format
= wxPATH_NATIVE
);
429 // split a fullpath into the volume, path, (base) name and extension
430 // (all of the pointers can be NULL)
431 static void SplitPath(const wxString
& fullpath
,
437 wxPathFormat format
= wxPATH_NATIVE
);
439 static void SplitPath(const wxString
& fullpath
,
446 SplitPath(fullpath
, volume
, path
, name
, ext
, NULL
, format
);
449 // compatibility version: volume is part of path
450 static void SplitPath(const wxString
& fullpath
,
454 wxPathFormat format
= wxPATH_NATIVE
);
456 // split a path into volume and pure path part
457 static void SplitVolume(const wxString
& fullpathWithVolume
,
460 wxPathFormat format
= wxPATH_NATIVE
);
464 // returns the size of the given filename
465 wxULongLong
GetSize() const;
466 static wxULongLong
GetSize(const wxString
&file
);
468 // returns the size in a human readable form
469 wxString
GetHumanReadableSize(const wxString
&nullsize
= wxGetTranslation(_T("Not available")),
470 int precision
= 1) const;
471 static wxString
GetHumanReadableSize(const wxULongLong
&sz
,
472 const wxString
&nullsize
= wxGetTranslation(_T("Not available")),
476 // deprecated methods, don't use any more
477 // --------------------------------------
479 #ifndef __DIGITALMARS__
480 wxString
GetPath( bool withSep
, wxPathFormat format
= wxPATH_NATIVE
) const
481 { return GetPath(withSep
? wxPATH_GET_SEPARATOR
: 0, format
); }
483 wxString
GetPathWithSep(wxPathFormat format
= wxPATH_NATIVE
) const
484 { return GetPath(wxPATH_GET_VOLUME
| wxPATH_GET_SEPARATOR
, format
); }
487 // check whether this dir is valid for Append/Prepend/InsertDir()
488 static bool IsValidDirComponent(const wxString
& dir
);
490 // the drive/volume/device specification (always empty for Unix)
493 // the path components of the file
494 wxArrayString m_dirs
;
496 // the file name and extension (empty for directories)
500 // when m_dirs is empty it may mean either that we have no path at all or
501 // that our path is '/', i.e. the root directory
503 // we use m_relative to distinguish between these two cases, it will be
504 // true in the former and false in the latter
506 // NB: the path is not absolute just because m_relative is false, it still
507 // needs the drive (i.e. volume) in some formats (Windows)
510 // when m_ext is empty, it may be because we don't have any extension or
511 // because we have an empty extension
513 // the difference is important as file with name "foo" and without
514 // extension has full name "foo" while with empty extension it is "foo."
518 #endif // _WX_FILENAME_H_