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"
33 class WXDLLIMPEXP_FWD_BASE wxFile
;
37 class WXDLLIMPEXP_FWD_BASE wxFFile
;
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // the various values for the path format: this mainly affects the path
45 // separator but also whether or not the path has the drive part (as under
49 wxPATH_NATIVE
= 0, // the path format for the current platform
51 wxPATH_BEOS
= wxPATH_UNIX
,
54 wxPATH_WIN
= wxPATH_DOS
,
55 wxPATH_OS2
= wxPATH_DOS
,
58 wxPATH_MAX
// Not a valid value for specifying path format
61 // the kind of normalization to do with the file name: these values can be
62 // or'd together to perform several operations at once
65 wxPATH_NORM_ENV_VARS
= 0x0001, // replace env vars with their values
66 wxPATH_NORM_DOTS
= 0x0002, // squeeze all .. and . and prepend cwd
67 wxPATH_NORM_TILDE
= 0x0004, // Unix only: replace ~ and ~user
68 wxPATH_NORM_CASE
= 0x0008, // if case insensitive => tolower
69 wxPATH_NORM_ABSOLUTE
= 0x0010, // make the path absolute
70 wxPATH_NORM_LONG
= 0x0020, // make the path the long form
71 wxPATH_NORM_SHORTCUT
= 0x0040, // resolve the shortcut, if it is a shortcut
72 wxPATH_NORM_ALL
= 0x00ff & ~wxPATH_NORM_CASE
75 // what exactly should GetPath() return?
78 wxPATH_GET_VOLUME
= 0x0001, // include the volume if applicable
79 wxPATH_GET_SEPARATOR
= 0x0002 // terminate the path with the separator
85 wxPATH_MKDIR_FULL
= 0x0001 // create directories recursively
89 // error code of wxFileName::GetSize()
90 extern WXDLLIMPEXP_DATA_BASE(const wxULongLong
) wxInvalidSize
;
91 #endif // wxUSE_LONGLONG
95 // ----------------------------------------------------------------------------
96 // wxFileName: encapsulates a file path
97 // ----------------------------------------------------------------------------
99 class WXDLLIMPEXP_BASE wxFileName
102 // constructors and assignment
105 wxFileName() { Clear(); }
106 wxFileName(const wxFileName
& filepath
) { Assign(filepath
); }
108 // from a full filename: if it terminates with a '/', a directory path
109 // is contructed (the name will be empty), otherwise a file name and
110 // extension are extracted from it
111 wxFileName( const wxString
& fullpath
, wxPathFormat format
= wxPATH_NATIVE
)
112 { Assign( fullpath
, format
); }
114 // from a directory name and a file name
115 wxFileName(const wxString
& path
,
116 const wxString
& name
,
117 wxPathFormat format
= wxPATH_NATIVE
)
118 { Assign(path
, name
, format
); }
120 // from a volume, directory name, file base name and extension
121 wxFileName(const wxString
& volume
,
122 const wxString
& path
,
123 const wxString
& name
,
125 wxPathFormat format
= wxPATH_NATIVE
)
126 { Assign(volume
, path
, name
, ext
, format
); }
128 // from a directory name, file base name and extension
129 wxFileName(const wxString
& path
,
130 const wxString
& name
,
132 wxPathFormat format
= wxPATH_NATIVE
)
133 { Assign(path
, name
, ext
, format
); }
135 // the same for delayed initialization
137 void Assign(const wxFileName
& filepath
);
139 void Assign(const wxString
& fullpath
,
140 wxPathFormat format
= wxPATH_NATIVE
);
142 void Assign(const wxString
& volume
,
143 const wxString
& path
,
144 const wxString
& name
,
147 wxPathFormat format
= wxPATH_NATIVE
);
149 void Assign(const wxString
& volume
,
150 const wxString
& path
,
151 const wxString
& name
,
153 wxPathFormat format
= wxPATH_NATIVE
)
154 { Assign(volume
, path
, name
, ext
, !ext
.empty(), format
); }
156 void Assign(const wxString
& path
,
157 const wxString
& name
,
158 wxPathFormat format
= wxPATH_NATIVE
);
160 void Assign(const wxString
& path
,
161 const wxString
& name
,
163 wxPathFormat format
= wxPATH_NATIVE
);
165 void AssignDir(const wxString
& dir
, wxPathFormat format
= wxPATH_NATIVE
);
167 // assorted assignment operators
169 wxFileName
& operator=(const wxFileName
& filename
)
170 { Assign(filename
); return *this; }
172 wxFileName
& operator=(const wxString
& filename
)
173 { Assign(filename
); return *this; }
175 // reset all components to default, uninitialized state
178 // static pseudo constructors
179 static wxFileName
FileName(const wxString
& file
,
180 wxPathFormat format
= wxPATH_NATIVE
);
181 static wxFileName
DirName(const wxString
& dir
,
182 wxPathFormat format
= wxPATH_NATIVE
);
186 // is the filename valid at all?
189 // we're fine if we have the path or the name or if we're a root dir
190 return m_dirs
.size() != 0 || !m_name
.empty() || !m_relative
||
191 !m_ext
.empty() || m_hasExt
;
194 // does the file with this name exists?
195 bool FileExists() const;
196 static bool FileExists( const wxString
&file
);
198 // does the directory with this name exists?
199 bool DirExists() const;
200 static bool DirExists( const wxString
&dir
);
202 // checks on most common flags for files/directories;
203 // more platform-specific features (like e.g. Unix permissions) are not
204 // available in wxFileName
206 bool IsDirWritable() const { return wxIsWritable(GetPath()); }
207 static bool IsDirWritable(const wxString
&path
) { return wxDirExists(path
) && wxIsWritable(path
); }
209 bool IsDirReadable() const { return wxIsReadable(GetPath()); }
210 static bool IsDirReadable(const wxString
&path
) { return wxDirExists(path
) && wxIsReadable(path
); }
212 // NOTE: IsDirExecutable() is not present because the meaning of "executable"
213 // directory is very platform-dependent and also not so useful
215 bool IsFileWritable() const { return wxIsWritable(GetFullPath()); }
216 static bool IsFileWritable(const wxString
&path
) { return wxFileExists(path
) && wxIsWritable(path
); }
218 bool IsFileReadable() const { return wxIsReadable(GetFullPath()); }
219 static bool IsFileReadable(const wxString
&path
) { return wxFileExists(path
) && wxIsReadable(path
); }
221 bool IsFileExecutable() const { return wxIsExecutable(GetFullPath()); }
222 static bool IsFileExecutable(const wxString
&path
) { return wxFileExists(path
) && wxIsExecutable(path
); }
227 // set the file last access/mod and creation times
228 // (any of the pointers may be NULL)
229 bool SetTimes(const wxDateTime
*dtAccess
,
230 const wxDateTime
*dtMod
,
231 const wxDateTime
*dtCreate
);
233 // set the access and modification times to the current moment
236 // return the last access, last modification and create times
237 // (any of the pointers may be NULL)
238 bool GetTimes(wxDateTime
*dtAccess
,
240 wxDateTime
*dtCreate
) const;
242 // convenience wrapper: get just the last mod time of the file
243 wxDateTime
GetModificationTime() const
246 (void)GetTimes(NULL
, &dtMod
, NULL
);
249 #endif // wxUSE_DATETIME
252 bool MacSetTypeAndCreator( wxUint32 type
, wxUint32 creator
) ;
253 bool MacGetTypeAndCreator( wxUint32
*type
, wxUint32
*creator
) ;
254 // gets the 'common' type and creator for a certain extension
255 static bool MacFindDefaultTypeAndCreator( const wxString
& ext
, wxUint32
*type
, wxUint32
*creator
) ;
256 // registers application defined extensions and their default type and creator
257 static void MacRegisterDefaultTypeAndCreator( const wxString
& ext
, wxUint32 type
, wxUint32 creator
) ;
258 // looks up the appropriate type and creator from the registration and then sets
259 bool MacSetDefaultTypeAndCreator() ;
262 // various file/dir operations
264 // retrieve the value of the current working directory
265 void AssignCwd(const wxString
& volume
= wxEmptyString
);
266 static wxString
GetCwd(const wxString
& volume
= wxEmptyString
);
268 // change the current working directory
270 static bool SetCwd( const wxString
&cwd
);
272 // get the value of user home (Unix only mainly)
273 void AssignHomeDir();
274 static wxString
GetHomeDir();
276 // get the system temporary directory
277 static wxString
GetTempDir();
279 #if wxUSE_FILE || wxUSE_FFILE
280 // get a temp file name starting with the specified prefix
281 void AssignTempFileName(const wxString
& prefix
);
282 static wxString
CreateTempFileName(const wxString
& prefix
);
286 // get a temp file name starting with the specified prefix and open the
287 // file passed to us using this name for writing (atomically if
289 void AssignTempFileName(const wxString
& prefix
, wxFile
*fileTemp
);
290 static wxString
CreateTempFileName(const wxString
& prefix
,
295 // get a temp file name starting with the specified prefix and open the
296 // file passed to us using this name for writing (atomically if
298 void AssignTempFileName(const wxString
& prefix
, wxFFile
*fileTemp
);
299 static wxString
CreateTempFileName(const wxString
& prefix
,
301 #endif // wxUSE_FFILE
303 // directory creation and removal.
304 bool Mkdir( int perm
= 0777, int flags
= 0);
305 static bool Mkdir( const wxString
&dir
, int perm
= 0777, int flags
= 0 );
308 static bool Rmdir( const wxString
&dir
);
310 // operations on the path
312 // normalize the path: with the default flags value, the path will be
313 // made absolute, without any ".." and "." and all environment
314 // variables will be expanded in it
316 // this may be done using another (than current) value of cwd
317 bool Normalize(int flags
= wxPATH_NORM_ALL
,
318 const wxString
& cwd
= wxEmptyString
,
319 wxPathFormat format
= wxPATH_NATIVE
);
321 // get a path path relative to the given base directory, i.e. opposite
324 // pass an empty string to get a path relative to the working directory
326 // returns true if the file name was modified, false if we failed to do
327 // anything with it (happens when the file is on a different volume,
329 bool MakeRelativeTo(const wxString
& pathBase
= wxEmptyString
,
330 wxPathFormat format
= wxPATH_NATIVE
);
332 // make the path absolute
334 // this may be done using another (than current) value of cwd
335 bool MakeAbsolute(const wxString
& cwd
= wxEmptyString
,
336 wxPathFormat format
= wxPATH_NATIVE
)
337 { return Normalize(wxPATH_NORM_DOTS
| wxPATH_NORM_ABSOLUTE
|
338 wxPATH_NORM_TILDE
, cwd
, format
); }
340 #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
341 // if the path is a shortcut, return the target and optionally,
343 bool GetShortcutTarget(const wxString
& shortcutPath
,
344 wxString
& targetFilename
,
345 wxString
* arguments
= NULL
);
350 // compares with the rules of the given platforms format
351 bool SameAs(const wxFileName
& filepath
,
352 wxPathFormat format
= wxPATH_NATIVE
) const;
354 // compare with another filename object
355 bool operator==(const wxFileName
& filename
) const
356 { return SameAs(filename
); }
357 bool operator!=(const wxFileName
& filename
) const
358 { return !SameAs(filename
); }
360 // compare with a filename string interpreted as a native file name
361 bool operator==(const wxString
& filename
) const
362 { return SameAs(wxFileName(filename
)); }
363 bool operator!=(const wxString
& filename
) const
364 { return !SameAs(wxFileName(filename
)); }
366 // are the file names of this type cases sensitive?
367 static bool IsCaseSensitive( wxPathFormat format
= wxPATH_NATIVE
);
369 // is this filename absolute?
370 bool IsAbsolute(wxPathFormat format
= wxPATH_NATIVE
) const;
372 // is this filename relative?
373 bool IsRelative(wxPathFormat format
= wxPATH_NATIVE
) const
374 { return !IsAbsolute(format
); }
376 // Returns the characters that aren't allowed in filenames
377 // on the specified platform.
378 static wxString
GetForbiddenChars(wxPathFormat format
= wxPATH_NATIVE
);
380 // Information about path format
382 // get the string separating the volume from the path for this format,
383 // return an empty string if this format doesn't support the notion of
385 static wxString
GetVolumeSeparator(wxPathFormat format
= wxPATH_NATIVE
);
387 // get the string of path separators for this format
388 static wxString
GetPathSeparators(wxPathFormat format
= wxPATH_NATIVE
);
390 // get the string of path terminators, i.e. characters which terminate the
392 static wxString
GetPathTerminators(wxPathFormat format
= wxPATH_NATIVE
);
394 // get the canonical path separator for this format
395 static wxUniChar
GetPathSeparator(wxPathFormat format
= wxPATH_NATIVE
)
396 { return GetPathSeparators(format
)[0u]; }
398 // is the char a path separator for this format?
399 static bool IsPathSeparator(wxChar ch
, wxPathFormat format
= wxPATH_NATIVE
);
402 size_t GetDirCount() const { return m_dirs
.size(); }
403 void AppendDir(const wxString
& dir
);
404 void PrependDir(const wxString
& dir
);
405 void InsertDir(size_t before
, const wxString
& dir
);
406 void RemoveDir(size_t pos
);
407 void RemoveLastDir() { RemoveDir(GetDirCount() - 1); }
410 void SetExt( const wxString
&ext
) { m_ext
= ext
; m_hasExt
= !m_ext
.empty(); }
411 void ClearExt() { m_ext
= wxEmptyString
; m_hasExt
= false; }
412 void SetEmptyExt() { m_ext
= wxT(""); m_hasExt
= true; }
413 wxString
GetExt() const { return m_ext
; }
414 bool HasExt() const { return m_hasExt
; }
416 void SetName( const wxString
&name
) { m_name
= name
; }
417 wxString
GetName() const { return m_name
; }
418 bool HasName() const { return !m_name
.empty(); }
420 void SetVolume( const wxString
&volume
) { m_volume
= volume
; }
421 wxString
GetVolume() const { return m_volume
; }
422 bool HasVolume() const { return !m_volume
.empty(); }
424 // full name is the file name + extension (but without the path)
425 void SetFullName(const wxString
& fullname
);
426 wxString
GetFullName() const;
428 const wxArrayString
& GetDirs() const { return m_dirs
; }
430 // flags are combination of wxPATH_GET_XXX flags
431 wxString
GetPath(int flags
= wxPATH_GET_VOLUME
,
432 wxPathFormat format
= wxPATH_NATIVE
) const;
434 // Replace current path with this one
435 void SetPath( const wxString
&path
, wxPathFormat format
= wxPATH_NATIVE
);
437 // Construct full path with name and ext
438 wxString
GetFullPath( wxPathFormat format
= wxPATH_NATIVE
) const;
440 // Return the short form of the path (returns identity on non-Windows platforms)
441 wxString
GetShortPath() const;
443 // Return the long form of the path (returns identity on non-Windows platforms)
444 wxString
GetLongPath() const;
446 // Is this a file or directory (not necessarily an existing one)
447 bool IsDir() const { return m_name
.empty() && m_ext
.empty(); }
451 // get the canonical path format for this platform
452 static wxPathFormat
GetFormat( wxPathFormat format
= wxPATH_NATIVE
);
454 // split a fullpath into the volume, path, (base) name and extension
455 // (all of the pointers can be NULL)
456 static void SplitPath(const wxString
& fullpath
,
462 wxPathFormat format
= wxPATH_NATIVE
);
464 static void SplitPath(const wxString
& fullpath
,
471 SplitPath(fullpath
, volume
, path
, name
, ext
, NULL
, format
);
474 // compatibility version: volume is part of path
475 static void SplitPath(const wxString
& fullpath
,
479 wxPathFormat format
= wxPATH_NATIVE
);
481 // split a path into volume and pure path part
482 static void SplitVolume(const wxString
& fullpathWithVolume
,
485 wxPathFormat format
= wxPATH_NATIVE
);
490 // returns the size of the given filename
491 wxULongLong
GetSize() const;
492 static wxULongLong
GetSize(const wxString
&file
);
494 // returns the size in a human readable form
495 wxString
GetHumanReadableSize(const wxString
&nullsize
= wxGetTranslation(_T("Not available")),
496 int precision
= 1) const;
497 static wxString
GetHumanReadableSize(const wxULongLong
&sz
,
498 const wxString
&nullsize
= wxGetTranslation(_T("Not available")),
500 #endif // wxUSE_LONGLONG
503 // deprecated methods, don't use any more
504 // --------------------------------------
506 #ifndef __DIGITALMARS__
507 wxString
GetPath( bool withSep
, wxPathFormat format
= wxPATH_NATIVE
) const
508 { return GetPath(withSep
? wxPATH_GET_SEPARATOR
: 0, format
); }
510 wxString
GetPathWithSep(wxPathFormat format
= wxPATH_NATIVE
) const
511 { return GetPath(wxPATH_GET_VOLUME
| wxPATH_GET_SEPARATOR
, format
); }
514 // check whether this dir is valid for Append/Prepend/InsertDir()
515 static bool IsValidDirComponent(const wxString
& dir
);
517 // the drive/volume/device specification (always empty for Unix)
520 // the path components of the file
521 wxArrayString m_dirs
;
523 // the file name and extension (empty for directories)
527 // when m_dirs is empty it may mean either that we have no path at all or
528 // that our path is '/', i.e. the root directory
530 // we use m_relative to distinguish between these two cases, it will be
531 // true in the former and false in the latter
533 // NB: the path is not absolute just because m_relative is false, it still
534 // needs the drive (i.e. volume) in some formats (Windows)
537 // when m_ext is empty, it may be because we don't have any extension or
538 // because we have an empty extension
540 // the difference is important as file with name "foo" and without
541 // extension has full name "foo" while with empty extension it is "foo."
545 #endif // _WX_FILENAME_H_