Mention wxFILE_EXISTS_NO_FOLLOW in wxFILE_EXISTS_SYMLINK description.
[wxWidgets.git] / interface / wx / filename.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: filename.h
3 // Purpose: interface of wxFileName
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9
10 /**
11 The various values for the path format: this mainly affects the path
12 separator but also whether or not the path has the drive part
13 (as under Windows).
14
15 See wxFileName for more info.
16 */
17 enum wxPathFormat
18 {
19 wxPATH_NATIVE = 0, //!< the path format for the current platform.
20 wxPATH_UNIX,
21 wxPATH_BEOS = wxPATH_UNIX,
22 wxPATH_MAC,
23 wxPATH_DOS,
24 wxPATH_WIN = wxPATH_DOS,
25 wxPATH_OS2 = wxPATH_DOS,
26 wxPATH_VMS,
27
28 wxPATH_MAX //!< Not a valid value for specifying path format
29 };
30
31 /**
32 Different conventions for human readable sizes.
33
34 @see wxFileName::GetHumanReadableSize().
35
36 @since 2.9.1
37 */
38 enum wxSizeConvention
39 {
40 /// 1024 bytes = 1KB.
41 wxSIZE_CONV_TRADITIONAL,
42
43 /// 1024 bytes = 1KiB.
44 wxSIZE_CONV_IEC,
45
46 /// 1000 bytes = 1KB.
47 wxSIZE_CONV_SI
48 };
49
50
51 /**
52 The kind of normalization to do with the file name: these values can be
53 or'd together to perform several operations at once.
54 See wxFileName::Normalize() for more info.
55 */
56 enum wxPathNormalize
57 {
58 //! Replace environment variables with their values.
59 //! wxFileName understands both Unix and Windows (but only under Windows) environment
60 //! variables expansion: i.e. @c "$var", @c "$(var)" and @c "${var}" are always understood
61 //! and in addition under Windows @c "%var%" is also.
62 wxPATH_NORM_ENV_VARS = 0x0001,
63
64 wxPATH_NORM_DOTS = 0x0002, //!< Squeeze all @c ".." and @c ".".
65 wxPATH_NORM_TILDE = 0x0004, //!< Replace @c "~" and @c "~user" (Unix only).
66 wxPATH_NORM_CASE = 0x0008, //!< If the platform is case insensitive, make lowercase the path.
67 wxPATH_NORM_ABSOLUTE = 0x0010, //!< Make the path absolute.
68 wxPATH_NORM_LONG = 0x0020, //!< Expand the path to the "long" form (Windows only).
69 wxPATH_NORM_SHORTCUT = 0x0040, //!< Resolve the shortcut, if it is a shortcut (Windows only).
70
71 //! A value indicating all normalization flags except for @c wxPATH_NORM_CASE.
72 wxPATH_NORM_ALL = 0x00ff & ~wxPATH_NORM_CASE
73 };
74
75 /**
76 Flags for wxFileName::Rmdir().
77 */
78 enum
79 {
80 /// Delete the specified directory and its subdirectories if they are empty.
81 wxPATH_RMDIR_FULL = 1,
82
83 /**
84 Delete the specified directory and all the files and subdirectories in it
85 recursively.
86
87 This flag is obviously @b dangerous and should be used with care and
88 after asking the user for confirmation.
89 */
90 wxPATH_RMDIR_RECURSIVE = 2
91 };
92
93 /**
94 Flags for wxFileName::Exists().
95
96 @since 2.9.5
97 */
98 enum
99 {
100 wxFILE_EXISTS_REGULAR = 0x0001, //!< Check for existence of a regular file
101 wxFILE_EXISTS_DIR = 0x0002, //!< Check for existence of a directory
102 /**
103 Check for existence of a symlink.
104
105 Notice that this flag should be used either with
106 ::wxFILE_EXISTS_NO_FOLLOW or with a wxFileName object on which
107 wxFileName::DontFollowLink() had been called, otherwise it would never
108 be satisfied as wxFileName::Exists() would be checking for the
109 existence of the symlink target and not the symlink itself.
110 */
111 wxFILE_EXISTS_SYMLINK = 0x0004,
112 wxFILE_EXISTS_DEVICE = 0x0008, //!< Check for existence of a device
113 wxFILE_EXISTS_FIFO = 0x0016, //!< Check for existence of a FIFO
114 wxFILE_EXISTS_SOCKET = 0x0032, //!< Check for existence of a socket
115 wxFILE_EXISTS_ANY = 0x0FFF, //!< Check for existence of anything
116 wxFILE_EXISTS_NO_FOLLOW = 0x1000 //!< Don't dereference a contained symbolic link
117 };
118
119 /**
120 The return value of wxFileName::GetSize() in case of error.
121 */
122 wxULongLong wxInvalidSize;
123
124
125 /**
126 @class wxFileName
127
128 wxFileName encapsulates a file name.
129
130 This class serves two purposes: first, it provides the functions to split the
131 file names into components and to recombine these components in the full file
132 name which can then be passed to the OS file functions
133 (and @ref group_funcmacro_file "wxWidgets functions" wrapping them).
134 Second, it includes the functions for working with the files itself. Note that
135 to change the file data you should use wxFile class instead.
136 wxFileName provides functions for working with the file attributes.
137
138 When working with directory names (i.e. without filename and extension)
139 make sure not to misuse the file name part of this class with the last
140 directory. Instead initialize the wxFileName instance like this:
141
142 @code
143 wxFileName dirname( "C:\mydir", "" );
144 MyMethod( dirname.GetPath() );
145 @endcode
146
147 The same can be done using the static method wxFileName::DirName():
148
149 @code
150 wxFileName dirname = wxFileName::DirName( "C:\mydir" );
151 MyMethod( dirname.GetPath() );
152 @endcode
153
154 Accordingly, methods dealing with directories or directory names like
155 wxFileName::IsDirReadable() use wxFileName::GetPath() whereas methods dealing
156 with file names like wxFileName::IsFileReadable() use wxFileName::GetFullPath().
157
158 If it is not known whether a string contains a directory name or a complete
159 file name (such as when interpreting user input) you need to use the static
160 function wxFileName::DirExists() (or its identical variants wxDir::Exists() and
161 wxDirExists()) and construct the wxFileName instance accordingly.
162 This will only work if the directory actually exists, of course:
163
164 @code
165 wxString user_input;
166 // get input from user
167
168 wxFileName fname;
169 if (wxDirExists(user_input))
170 fname.AssignDir( user_input );
171 else
172 fname.Assign( user_input );
173 @endcode
174
175 Please note that many wxFileName methods accept the path format argument
176 which is by @c wxPATH_NATIVE by default meaning to use the path format
177 native for the current platform.
178 The path format affects the operation of wxFileName functions in several ways:
179 first and foremost, it defines the path separator character to use, but it
180 also affects other things such as whether the path has the drive part or not.
181 See wxPathFormat for more info.
182
183
184 @section filename_format File name format
185
186 wxFileName currently supports the file names in the Unix, DOS/Windows,
187 Mac OS and VMS formats. Although these formats are quite different,
188 wxFileName tries to treat them all in the same generic way.
189 It supposes that all file names consist of the following parts: the volume
190 (also known as drive under Windows or device under VMS), the path which is
191 a sequence of directory names separated by the path separators and the full
192 filename itself which, in turn, is composed from the base file name and the
193 extension. All of the individual components of the file name may be empty
194 and, for example, the volume name is always empty under Unix, but if they
195 are all empty simultaneously, the filename object is considered to be in an
196 invalid state and wxFileName::IsOk() returns false for it.
197
198 File names can be case-sensitive or not, the function wxFileName::IsCaseSensitive()
199 allows to determine this. The rules for determining whether the file name is
200 absolute or relative also depend on the file name format and the only portable way
201 to answer this question is to use wxFileName::IsAbsolute() or wxFileName::IsRelative()
202 method.
203
204 Note that on Windows,"X:" refers to the current working directory on drive X.
205 Therefore, a wxFileName instance constructed from for example "X:dir/file.ext"
206 treats the portion beyond drive separator as being relative to that directory.
207 To ensure that the filename is absolute, you may use wxFileName::MakeAbsolute().
208 There is also an inverse function wxFileName::MakeRelativeTo() which undoes
209 what wxFileName::Normalize(wxPATH_NORM_DOTS) does.
210 Other functions returning information about the file format provided by this
211 class are wxFileName::GetVolumeSeparator(), wxFileName::IsPathSeparator().
212
213
214 @section filename_construction File name construction
215
216 You can initialize a wxFileName instance using one of the following functions:
217
218 @li wxFileName::wxFileName()
219 @li wxFileName::Assign()
220 @li wxFileName::AssignCwd()
221 @li wxFileName::AssignDir()
222 @li wxFileName::AssignHomeDir()
223 @li wxFileName::AssignTempFileName()
224 @li wxFileName::DirName()
225 @li wxFileName::FileName()
226 @li wxFileName::operator=()
227
228
229 @section filename_tests File name tests
230
231 Before doing other tests, you should use wxFileName::IsOk() to verify that
232 the filename is well defined. If it is, FileExists() can be used to test whether
233 a file with such name exists and wxFileName::DirExists() can be used to test
234 for directory existence.
235 File names should be compared using the wxFileName::SameAs() method or
236 wxFileName::operator==(). For testing basic access modes, you can use:
237
238 @li wxFileName::IsDirWritable()
239 @li wxFileName::IsDirReadable()
240 @li wxFileName::IsFileWritable()
241 @li wxFileName::IsFileReadable()
242 @li wxFileName::IsFileExecutable()
243
244
245 @section filename_components File name components
246
247 These functions allow to examine and modify the individual directories
248 of the path:
249
250 @li wxFileName::AppendDir()
251 @li wxFileName::InsertDir()
252 @li wxFileName::GetDirCount()
253 @li wxFileName::PrependDir()
254 @li wxFileName::RemoveDir()
255 @li wxFileName::RemoveLastDir()
256
257 To change the components of the file name individually you can use the
258 following functions:
259
260 @li wxFileName::GetExt()
261 @li wxFileName::GetName()
262 @li wxFileName::GetVolume()
263 @li wxFileName::HasExt()
264 @li wxFileName::HasName()
265 @li wxFileName::HasVolume()
266 @li wxFileName::SetExt()
267 @li wxFileName::ClearExt()
268 @li wxFileName::SetEmptyExt()
269 @li wxFileName::SetName()
270 @li wxFileName::SetVolume()
271
272 You can initialize a wxFileName instance using one of the following functions:
273
274
275 @section filename_operations File name operations
276
277 These methods allow to work with the file creation, access and modification
278 times. Note that not all filesystems under all platforms implement these times
279 in the same way. For example, the access time under Windows has a resolution of
280 one day (so it is really the access date and not time). The access time may be
281 updated when the file is executed or not depending on the platform.
282
283 @li wxFileName::GetModificationTime()
284 @li wxFileName::GetTimes()
285 @li wxFileName::SetTimes()
286 @li wxFileName::Touch()
287
288 Other file system operations functions are:
289
290 @li wxFileName::Mkdir()
291 @li wxFileName::Rmdir()
292
293
294 @library{wxbase}
295 @category{file}
296 */
297 class wxFileName
298 {
299 public:
300 /**
301 Default constructor.
302 */
303 wxFileName();
304
305 /**
306 Copy constructor.
307 */
308 wxFileName(const wxFileName& filename);
309
310 /**
311 Constructor taking a full filename.
312
313 If it terminates with a '/', a directory path is constructed
314 (the name will be empty), otherwise a file name and extension
315 are extracted from it.
316 */
317 wxFileName(const wxString& fullpath,
318 wxPathFormat format = wxPATH_NATIVE);
319
320 /**
321 Constructor a directory name and file name.
322 */
323 wxFileName(const wxString& path, const wxString& name,
324 wxPathFormat format = wxPATH_NATIVE);
325
326 /**
327 Constructor from a directory name, base file name and extension.
328 */
329 wxFileName(const wxString& path, const wxString& name,
330 const wxString& ext,
331 wxPathFormat format = wxPATH_NATIVE);
332
333 /**
334 Constructor from a volume name, a directory name, base file name and extension.
335 */
336 wxFileName(const wxString& volume, const wxString& path,
337 const wxString& name,
338 const wxString& ext,
339 wxPathFormat format = wxPATH_NATIVE);
340
341 /**
342 Appends a directory component to the path. This component should contain a
343 single directory name level, i.e. not contain any path or volume separators nor
344 should it be empty, otherwise the function does nothing (and generates an
345 assert failure in debug build).
346 */
347 void AppendDir(const wxString& dir);
348
349 /**
350 Creates the file name from another filename object.
351 */
352 void Assign(const wxFileName& filepath);
353
354 /**
355 Creates the file name from a full file name with a path.
356 */
357 void Assign(const wxString& fullpath,
358 wxPathFormat format = wxPATH_NATIVE);
359
360 /**
361 Creates the file name from volume, path, name and extension.
362 */
363 void Assign(const wxString& volume, const wxString& path,
364 const wxString& name,
365 const wxString& ext,
366 bool hasExt,
367 wxPathFormat format = wxPATH_NATIVE);
368
369 /**
370 Creates the file name from volume, path, name and extension.
371 */
372 void Assign(const wxString& volume, const wxString& path,
373 const wxString& name,
374 const wxString& ext,
375 wxPathFormat format = wxPATH_NATIVE);
376
377 /**
378 Creates the file name from file path and file name.
379 */
380 void Assign(const wxString& path, const wxString& name,
381 wxPathFormat format = wxPATH_NATIVE);
382
383 /**
384 Creates the file name from path, name and extension.
385 */
386 void Assign(const wxString& path, const wxString& name,
387 const wxString& ext,
388 wxPathFormat format = wxPATH_NATIVE);
389
390 /**
391 Makes this object refer to the current working directory on the specified
392 volume (or current volume if @a volume is empty).
393
394 @see GetCwd()
395 */
396 void AssignCwd(const wxString& volume = wxEmptyString);
397
398 /**
399 Sets this file name object to the given directory name.
400 The name and extension will be empty.
401 */
402 void AssignDir(const wxString& dir,
403 wxPathFormat format = wxPATH_NATIVE);
404
405 /**
406 Sets this file name object to the home directory.
407 */
408 void AssignHomeDir();
409
410 /**
411 The function calls CreateTempFileName() to create a temporary file
412 and sets this object to the name of the file.
413
414 If a temporary file couldn't be created, the object is put into
415 an invalid state (see IsOk()).
416 */
417 void AssignTempFileName(const wxString& prefix);
418
419 /**
420 The function calls CreateTempFileName() to create a temporary
421 file name and open @a fileTemp with it.
422
423 If the file couldn't be opened, the object is put into
424 an invalid state (see IsOk()).
425 */
426 void AssignTempFileName(const wxString& prefix, wxFile* fileTemp);
427
428 /**
429 The function calls CreateTempFileName() to create a temporary
430 file name and open @a fileTemp with it.
431
432 If the file couldn't be opened, the object is put into
433 an invalid state (see IsOk()).
434 */
435 void AssignTempFileName(const wxString& prefix, wxFFile* fileTemp);
436
437 /**
438 Reset all components to default, uninitialized state.
439 */
440 void Clear();
441
442 /**
443 Removes the extension from the file name resulting in a
444 file name with no trailing dot.
445
446 @see SetExt(), SetEmptyExt()
447 */
448 void ClearExt();
449
450
451 /**
452 Returns a temporary file name starting with the given @e prefix.
453 If @a prefix is an absolute path and ends in a separator, the
454 temporary file is created in this directory; if it is an absolute
455 filepath or there is no separator, the temporary file is created in its
456 path, with the 'name' segment prepended to the temporary filename;
457 otherwise it is created in the default system directory for temporary
458 files or in the current directory.
459
460 If the function succeeds, the temporary file is actually created.
461 If @a fileTemp is not @NULL, this wxFile will be opened using the name of
462 the temporary file. Where possible this is done in an atomic way to ensure that
463 no race condition occurs between creating the temporary file name and opening
464 it, which might lead to a security compromise on multiuser systems.
465 If @a fileTemp is @NULL, the file is created but not opened.
466 Under Unix, the temporary file will have read and write permissions for the
467 owner only, to minimize security problems.
468
469 @param prefix
470 Location to use for the temporary file name construction. If @a prefix
471 is a directory it must have a terminal separator
472 @param fileTemp
473 The file to open, or @NULL just to get the name
474
475 @return The full temporary filepath, or an empty string on error.
476 */
477 static wxString CreateTempFileName(const wxString& prefix,
478 wxFile* fileTemp = NULL);
479
480 /**
481 This is the same as CreateTempFileName(const wxString &prefix, wxFile *fileTemp)
482 but takes a wxFFile parameter instead of wxFile.
483 */
484 static wxString CreateTempFileName(const wxString& prefix,
485 wxFFile* fileTemp = NULL);
486
487
488 /**
489 Returns @true if the directory with this name exists.
490
491 Notice that this function tests the directory part of this object,
492 i.e. the string returned by GetPath(), and not the full path returned
493 by GetFullPath().
494
495 @see FileExists(), Exists()
496 */
497 bool DirExists() const;
498
499 /**
500 Returns @true if the directory with name @a dir exists.
501
502 @see FileExists(), Exists()
503 */
504 static bool DirExists(const wxString& dir);
505
506 /**
507 Returns the object corresponding to the directory with the given name.
508 The @a dir parameter may have trailing path separator or not.
509 */
510 static wxFileName DirName(const wxString& dir,
511 wxPathFormat format = wxPATH_NATIVE);
512
513 /**
514 Turns off symlink dereferencing.
515
516 By default, all operations in this class work on the target of a
517 symbolic link (symlink) if the path of the file is actually a symlink.
518 Using this method allows to turn off this "symlink following" behaviour
519 and apply the operations to this path itself, even if it is a symlink.
520
521 The following methods are currently affected by this option:
522 - GetTimes() (but not SetTimes() as there is no portable way to
523 change the time of symlink itself).
524 - Existence checks: FileExists(), DirExists() and Exists() (notice
525 that static versions of these methods always follow symlinks).
526 - IsSameAs().
527
528 @see ShouldFollowLink()
529
530 @since 2.9.5
531 */
532 void DontFollowLink();
533
534 /**
535 Calls the static overload of this function with the full path of this
536 object.
537
538 @since 2.9.4 (@a flags is new since 2.9.5)
539 */
540 bool Exists(int flags = wxFILE_EXISTS_ANY) const;
541
542 /**
543 Returns @true if either a file or a directory or something else with
544 this name exists in the file system.
545
546 Don't dereference @a path if it is a symbolic link and @a flags
547 argument contains ::wxFILE_EXISTS_NO_FOLLOW.
548
549 This method is equivalent to @code FileExists() || DirExists() @endcode
550 under Windows, but under Unix it also returns true if the file
551 identifies a special file system object such as a device, a socket or a
552 FIFO.
553
554 Alternatively you may check for the existence of a file system entry of
555 a specific type by passing the appropriate @a flags (this parameter is
556 new since wxWidgets 2.9.5). E.g. to test for a symbolic link existence
557 you could use ::wxFILE_EXISTS_SYMLINK.
558
559 @since 2.9.4
560
561 @see FileExists(), DirExists()
562 */
563 static bool Exists(const wxString& path, int flags = wxFILE_EXISTS_ANY);
564
565 /**
566 Returns @true if the file with this name exists.
567
568 @see DirExists(), Exists()
569 */
570 bool FileExists() const;
571
572 /**
573 Returns @true if the file with name @a file exists.
574
575 @see DirExists(), Exists()
576 */
577 static bool FileExists(const wxString& file);
578
579 /**
580 Returns the file name object corresponding to the given @e file. This
581 function exists mainly for symmetry with DirName().
582 */
583 static wxFileName FileName(const wxString& file,
584 wxPathFormat format = wxPATH_NATIVE);
585
586 /**
587 Retrieves the value of the current working directory on the specified volume.
588 If the volume is empty, the program's current working directory is returned for
589 the current volume.
590
591 @return The string containing the current working directory or an empty
592 string on error.
593
594 @see AssignCwd()
595 */
596 static wxString GetCwd(const wxString& volume = wxEmptyString);
597
598 /**
599 Returns the number of directories in the file name.
600 */
601 size_t GetDirCount() const;
602
603 /**
604 Returns the directories in string array form.
605 */
606 const wxArrayString& GetDirs() const;
607
608 /**
609 Returns the file name extension.
610 */
611 wxString GetExt() const;
612
613 /**
614 Returns the characters that can't be used in filenames and directory names
615 for the specified format.
616 */
617 static wxString GetForbiddenChars(wxPathFormat format = wxPATH_NATIVE);
618
619 /**
620 Returns the canonical path format for this platform.
621 */
622 static wxPathFormat GetFormat(wxPathFormat format = wxPATH_NATIVE);
623
624 /**
625 Returns the full name (including extension but excluding directories).
626 */
627 wxString GetFullName() const;
628
629 /**
630 Returns the full path with name and extension.
631 */
632 wxString GetFullPath(wxPathFormat format = wxPATH_NATIVE) const;
633
634 /**
635 Returns the home directory.
636 */
637 static wxString GetHomeDir();
638
639 //@{
640 /**
641 Returns the representation of the file size in a human-readable form.
642
643 In the first version, the size of this file is used. In the second one,
644 the specified size @a bytes is used.
645
646 If the file size could not be retrieved or @a bytes is ::wxInvalidSize
647 or zero, the @c failmsg string is returned.
648
649 Otherwise the returned string is a floating-point number with @c
650 precision decimal digits followed by the abbreviation of the unit used.
651 By default the traditional, although incorrect, convention of using SI
652 units for multiples of 1024 is used, i.e. returned string will use
653 suffixes of B, KB, MB, GB, TB for bytes, kilobytes, megabytes,
654 gigabytes and terabytes respectively. With the IEC convention the names
655 of the units are changed to B, KiB, MiB, GiB and TiB for bytes,
656 kibibytes, mebibytes, gibibytes and tebibytes. Finally, with SI
657 convention the same B, KB, MB, GB and TB suffixes are used but in their
658 correct SI meaning, i.e. as multiples of 1000 and not 1024.
659
660 Support for the different size conventions is new in wxWidgets 2.9.1,
661 in previous versions only the traditional convention was implemented.
662 */
663 wxString
664 GetHumanReadableSize(const wxString& failmsg = _("Not available"),
665 int precision = 1,
666 wxSizeConvention conv = wxSIZE_CONV_TRADITIONAL) const;
667
668 static wxString
669 GetHumanReadableSize(const wxULongLong& bytes,
670 const wxString& nullsize = _("Not available"),
671 int precision = 1,
672 wxSizeConvention conv = wxSIZE_CONV_TRADITIONAL);
673 //@}
674
675 /**
676 Return the long form of the path (returns identity on non-Windows platforms).
677 */
678 wxString GetLongPath() const;
679
680 /**
681 Returns the last time the file was last modified.
682 */
683 wxDateTime GetModificationTime() const;
684
685 /**
686 Returns the name part of the filename (without extension).
687
688 @see GetFullName()
689 */
690 wxString GetName() const;
691
692 /**
693 Returns the path part of the filename (without the name or extension).
694
695 The possible flags values are:
696
697 - @b wxPATH_GET_VOLUME:
698 Return the path with the volume (does nothing for the filename formats
699 without volumes), otherwise the path without volume part is returned.
700
701 - @b wxPATH_GET_SEPARATOR:
702 Return the path with the trailing separator, if this flag is not given
703 there will be no separator at the end of the path.
704
705 - @b wxPATH_NO_SEPARATOR:
706 Don't include the trailing separator in the returned string. This is
707 the default (the value of this flag is 0) and exists only for symmetry
708 with wxPATH_GET_SEPARATOR.
709
710 @note If the path is a toplevel one (e.g. @c "/" on Unix or @c "C:\" on
711 Windows), then the returned path will contain trailing separator
712 even with @c wxPATH_NO_SEPARATOR.
713 */
714 wxString GetPath(int flags = wxPATH_GET_VOLUME,
715 wxPathFormat format = wxPATH_NATIVE) const;
716
717 /**
718 Returns the usually used path separator for this format.
719 For all formats but @c wxPATH_DOS there is only one path separator anyhow,
720 but for DOS there are two of them and the native one, i.e. the backslash
721 is returned by this method.
722
723 @see GetPathSeparators()
724 */
725 static wxUniChar GetPathSeparator(wxPathFormat format = wxPATH_NATIVE);
726
727 /**
728 Returns the string containing all the path separators for this format.
729 For all formats but @c wxPATH_DOS this string contains only one character
730 but for DOS and Windows both @c '/' and @c '\' may be used as separators.
731
732 @see GetPathSeparator()
733 */
734 static wxString GetPathSeparators(wxPathFormat format = wxPATH_NATIVE);
735
736 /**
737 Returns the string of characters which may terminate the path part.
738 This is the same as GetPathSeparators() except for VMS
739 path format where ] is used at the end of the path part.
740 */
741 static wxString GetPathTerminators(wxPathFormat format = wxPATH_NATIVE);
742
743 /**
744 Returns the path with the trailing separator, useful for appending the name
745 to the given path.
746
747 This is the same as calling
748 @code
749 GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR, format)
750 @endcode
751 */
752 wxString GetPathWithSep(wxPathFormat format = wxPATH_NATIVE) const;
753
754 /**
755 Return the short form of the path (returns identity on non-Windows platforms).
756 */
757 wxString GetShortPath() const;
758
759 /**
760 Returns the size of the file If the file does not exist or its size could
761 not be read (because e.g. the file is locked by another process) the returned
762 value is ::wxInvalidSize.
763 */
764 wxULongLong GetSize() const;
765
766 /**
767 Returns the size of the file If the file does not exist or its size could
768 not be read (because e.g. the file is locked by another process) the returned
769 value is ::wxInvalidSize.
770 */
771 static wxULongLong GetSize(const wxString& filename);
772
773 /**
774 Returns the directory used for temporary files.
775 */
776 static wxString GetTempDir();
777
778 /**
779 Returns the last access, last modification and creation times.
780 The last access time is updated whenever the file is read or written
781 (or executed in the case of Windows), last modification time is only
782 changed when the file is written to.
783 Finally, the creation time is indeed the time when the file was created
784 under Windows and the inode change time under Unix (as it is impossible to
785 retrieve the real file creation time there anyhow) which can also be changed
786 by many operations after the file creation.
787
788 If no filename or extension is specified in this instance of wxFileName
789 (and therefore IsDir() returns @true) then this function will return the
790 directory times of the path specified by GetPath(), otherwise the file
791 times of the file specified by GetFullPath().
792 Any of the pointers may be @NULL if the corresponding time is not needed.
793
794 @return @true on success, @false if we failed to retrieve the times.
795 */
796 bool GetTimes(wxDateTime* dtAccess, wxDateTime* dtMod,
797 wxDateTime* dtCreate) const;
798
799 /**
800 Returns the string containing the volume for this file name, empty if it
801 doesn't have one or if the file system doesn't support volumes at all
802 (for example, Unix).
803 */
804 wxString GetVolume() const;
805
806 /**
807 Returns the string separating the volume from the path for this format.
808 */
809 static wxString GetVolumeSeparator(wxPathFormat format = wxPATH_NATIVE);
810
811 /**
812 This function builds a volume path string, for example "C:\\".
813
814 Implemented for the platforms which use drive letters, i.e. DOS, MSW
815 and OS/2 only.
816
817 @since 2.9.0
818
819 @param drive
820 The drive letter, 'A' through 'Z' or 'a' through 'z'.
821
822 @param flags
823 @c wxPATH_NO_SEPARATOR or @c wxPATH_GET_SEPARATOR to omit or include
824 the trailing path separator, the default is to include it.
825
826 @return Volume path string.
827 */
828 static wxString GetVolumeString(char drive, int flags = wxPATH_GET_SEPARATOR);
829
830 /**
831 Returns @true if an extension is present.
832 */
833 bool HasExt() const;
834
835 /**
836 Returns @true if a name is present.
837 */
838 bool HasName() const;
839
840 /**
841 Returns @true if a volume specifier is present.
842 */
843 bool HasVolume() const;
844
845 /**
846 Inserts a directory component before the zero-based position in the directory
847 list. Please see AppendDir() for important notes.
848 */
849 void InsertDir(size_t before, const wxString& dir);
850
851 /**
852 Returns @true if this filename is absolute.
853 */
854 bool IsAbsolute(wxPathFormat format = wxPATH_NATIVE) const;
855
856 /**
857 Returns @true if the file names of this type are case-sensitive.
858 */
859 static bool IsCaseSensitive(wxPathFormat format = wxPATH_NATIVE);
860
861 /**
862 Returns @true if this object represents a directory, @false otherwise
863 (i.e. if it is a file).
864
865 Note that this method doesn't test whether the directory or file really
866 exists, you should use DirExists() or FileExists() for this.
867 */
868 bool IsDir() const;
869
870 /**
871 Returns @true if the directory component of this instance is an existing
872 directory and this process has read permissions on it. Read permissions
873 on a directory mean that you can list the directory contents but it
874 doesn't imply that you have read permissions on the files contained.
875 */
876 bool IsDirReadable() const;
877
878 /**
879 Returns @true if the given @e dir is an existing directory and this process
880 has read permissions on it. Read permissions on a directory mean that you
881 can list the directory contents but it doesn't imply that you have read
882 permissions on the files contained.
883 */
884 static bool IsDirReadable(const wxString& dir);
885
886 /**
887 Returns @true if the directory component of this instance
888 is an existing directory and this process has write permissions on it.
889 Write permissions on a directory mean that you can create new files in the
890 directory.
891 */
892 bool IsDirWritable() const;
893
894 /**
895 Returns @true if the given @a dir is an existing directory and this
896 process has write permissions on it.
897 Write permissions on a directory mean that you can create new files in the
898 directory.
899 */
900 static bool IsDirWritable(const wxString& dir);
901
902 /**
903 Returns @true if a file with this name exists and if this process has execute
904 permissions on it.
905 */
906 bool IsFileExecutable() const;
907
908 /**
909 Returns @true if a file with this name exists and if this process has execute
910 permissions on it.
911 */
912 static bool IsFileExecutable(const wxString& file);
913
914 /**
915 Returns @true if a file with this name exists and if this process has read
916 permissions on it.
917 */
918 bool IsFileReadable() const;
919
920 /**
921 Returns @true if a file with this name exists and if this process has read
922 permissions on it.
923 */
924 static bool IsFileReadable(const wxString& file);
925
926 /**
927 Returns @true if a file with this name exists and if this process has write
928 permissions on it.
929 */
930 bool IsFileWritable() const;
931
932 /**
933 Returns @true if a file with this name exists and if this process has write
934 permissions on it.
935 */
936 static bool IsFileWritable(const wxString& file);
937
938 /**
939 Returns @true if the filename is valid, @false if it is not initialized yet.
940 The assignment functions and Clear() may reset the object to the uninitialized,
941 invalid state (the former only do it on failure).
942 */
943 bool IsOk() const;
944
945 /**
946 Returns @true if the char is a path separator for this format.
947 */
948 static bool IsPathSeparator(wxChar ch,
949 wxPathFormat format = wxPATH_NATIVE);
950
951 /**
952 Returns @true if the volume part of the path is a unique volume name.
953
954 This function will always return @false if the path format is not
955 wxPATH_DOS.
956
957 Unique volume names are Windows volume identifiers which remain the same
958 regardless of where the volume is actually mounted. Example of a path
959 using a volume name could be
960 @code
961 \\?\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\Program Files\setup.exe
962 @endcode
963
964 @since 2.9.1
965 */
966 static bool IsMSWUniqueVolumeNamePath(const wxString& path,
967 wxPathFormat format = wxPATH_NATIVE);
968
969 /**
970 Returns @true if this filename is not absolute.
971 */
972 bool IsRelative(wxPathFormat format = wxPATH_NATIVE) const;
973
974 /**
975 On Mac OS, gets the common type and creator for the given extension.
976
977 @onlyfor{wxosx}
978 */
979 static bool MacFindDefaultTypeAndCreator(const wxString& ext,
980 wxUint32* type,
981 wxUint32* creator);
982
983 /**
984 On Mac OS, registers application defined extensions and their default type
985 and creator.
986
987 @onlyfor{wxosx}
988 */
989 static void MacRegisterDefaultTypeAndCreator(const wxString& ext,
990 wxUint32 type,
991 wxUint32 creator);
992
993 /**
994 On Mac OS, looks up the appropriate type and creator from the registration
995 and then sets it.
996
997 @onlyfor{wxosx}
998 */
999 bool MacSetDefaultTypeAndCreator();
1000
1001 /**
1002 Make the file name absolute.
1003 This is a shortcut for
1004 @code
1005 wxFileName::Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_ABSOLUTE |
1006 wxPATH_NORM_TILDE, cwd, format)
1007 @endcode
1008
1009 @see MakeRelativeTo(), Normalize(), IsAbsolute()
1010 */
1011 bool MakeAbsolute(const wxString& cwd = wxEmptyString,
1012 wxPathFormat format = wxPATH_NATIVE);
1013
1014 /**
1015 This function tries to put this file name in a form relative to
1016 @a pathBase.
1017 In other words, it returns the file name which should be used to access
1018 this file if the current directory were pathBase.
1019
1020 @param pathBase
1021 The directory to use as root, current directory is used by default
1022 @param format
1023 The file name format, native by default
1024
1025 @return @true if the file name has been changed, @false if we failed to do
1026 anything with it (currently this only happens if the file name
1027 is on a volume different from the volume specified by @a pathBase).
1028
1029 @see Normalize()
1030 */
1031 bool MakeRelativeTo(const wxString& pathBase = wxEmptyString,
1032 wxPathFormat format = wxPATH_NATIVE);
1033
1034 /**
1035 Creates a directory.
1036
1037 @param perm
1038 The permissions for the newly created directory.
1039 See the ::wxPosixPermissions enumeration for more info.
1040 @param flags
1041 If the flags contain @c wxPATH_MKDIR_FULL flag, try to create each
1042 directory in the path and also don't return an error if the target
1043 directory already exists.
1044
1045 @return Returns @true if the directory was successfully created, @false
1046 otherwise.
1047 */
1048 bool Mkdir(int perm = wxS_DIR_DEFAULT, int flags = 0) const;
1049
1050 /**
1051 Creates a directory.
1052
1053 @param dir
1054 The directory to create
1055 @param perm
1056 The permissions for the newly created directory.
1057 See the ::wxPosixPermissions enumeration for more info.
1058 @param flags
1059 If the flags contain @c wxPATH_MKDIR_FULL flag, try to create each
1060 directory in the path and also don't return an error if the target
1061 directory already exists.
1062
1063 @return Returns @true if the directory was successfully created, @false
1064 otherwise.
1065 */
1066 static bool Mkdir(const wxString& dir, int perm = wxS_DIR_DEFAULT,
1067 int flags = 0);
1068
1069 /**
1070 Normalize the path.
1071
1072 With the default flags value, the path will be made absolute, without
1073 any ".." and "." and all environment variables will be expanded in it.
1074
1075 Notice that in some rare cases normalizing a valid path may result in
1076 an invalid wxFileName object. E.g. normalizing "./" path using
1077 wxPATH_NORM_DOTS but not wxPATH_NORM_ABSOLUTE will result in a
1078 completely empty and thus invalid object. As long as there is a non
1079 empty file name the result of normalization will be valid however.
1080
1081 @param flags
1082 The kind of normalization to do with the file name. It can be
1083 any or-combination of the ::wxPathNormalize enumeration values.
1084 @param cwd
1085 If not empty, this directory will be used instead of current
1086 working directory in normalization (see @c wxPATH_NORM_ABSOLUTE).
1087 @param format
1088 The file name format to use when processing the paths, native by default.
1089
1090 @return @true if normalization was successfully or @false otherwise.
1091 */
1092 bool Normalize(int flags = wxPATH_NORM_ALL,
1093 const wxString& cwd = wxEmptyString,
1094 wxPathFormat format = wxPATH_NATIVE);
1095
1096 /**
1097 Prepends a directory to the file path.
1098 Please see AppendDir() for important notes.
1099 */
1100 void PrependDir(const wxString& dir);
1101
1102 /**
1103 Removes the specified directory component from the path.
1104
1105 @see GetDirCount()
1106 */
1107 void RemoveDir(size_t pos);
1108
1109 /**
1110 Removes last directory component from the path.
1111 */
1112 void RemoveLastDir();
1113
1114 /**
1115 If the path contains the value of the environment variable named @a envname
1116 then this function replaces it with the string obtained from
1117 wxString::Format(replacementFmtString, value_of_envname_variable).
1118
1119 This function is useful to make the path shorter or to make it dependent
1120 from a certain environment variable.
1121 Normalize() with @c wxPATH_NORM_ENV_VARS can perform the opposite of this
1122 function (depending on the value of @a replacementFmtString).
1123
1124 The name and extension of this filename are not modified.
1125
1126 Example:
1127 @code
1128 wxFileName fn("/usr/openwin/lib/someFile");
1129 fn.ReplaceEnvVariable("OPENWINHOME");
1130 // now fn.GetFullPath() == "$OPENWINHOME/lib/someFile"
1131 @endcode
1132
1133 @since 2.9.0
1134
1135 @return @true if the operation was successful (which doesn't mean
1136 that something was actually replaced, just that ::wxGetEnv
1137 didn't fail).
1138 */
1139 bool ReplaceEnvVariable(const wxString& envname,
1140 const wxString& replacementFmtString = "$%s",
1141 wxPathFormat format = wxPATH_NATIVE);
1142
1143 /**
1144 Replaces, if present in the path, the home directory for the given user
1145 (see ::wxGetHomeDir) with a tilde (~).
1146
1147 Normalize() with @c wxPATH_NORM_TILDE performs the opposite of this
1148 function.
1149
1150 The name and extension of this filename are not modified.
1151
1152 @since 2.9.0
1153
1154 @return @true if the operation was successful (which doesn't mean
1155 that something was actually replaced, just that ::wxGetHomeDir
1156 didn't fail).
1157 */
1158 bool ReplaceHomeDir(wxPathFormat format = wxPATH_NATIVE);
1159
1160
1161 /**
1162 Deletes the specified directory from the file system.
1163
1164 @param flags
1165 Can contain one of wxPATH_RMDIR_FULL or wxPATH_RMDIR_RECURSIVE. By
1166 default contains neither so the directory will not be removed
1167 unless it is empty.
1168
1169 @return Returns @true if the directory was successfully deleted, @false
1170 otherwise.
1171 */
1172 bool Rmdir(int flags = 0) const;
1173
1174 /**
1175 Deletes the specified directory from the file system.
1176
1177 @param dir
1178 The directory to delete
1179 @param flags
1180 Can contain one of wxPATH_RMDIR_FULL or wxPATH_RMDIR_RECURSIVE. By
1181 default contains neither so the directory will not be removed
1182 unless it is empty.
1183
1184 @return Returns @true if the directory was successfully deleted, @false
1185 otherwise.
1186 */
1187 static bool Rmdir(const wxString& dir, int flags = 0);
1188
1189 /**
1190 Compares the filename using the rules of this platform.
1191 */
1192 bool SameAs(const wxFileName& filepath,
1193 wxPathFormat format = wxPATH_NATIVE) const;
1194
1195 /**
1196 Changes the current working directory.
1197 */
1198 bool SetCwd() const;
1199
1200 /**
1201 Changes the current working directory.
1202 */
1203 static bool SetCwd(const wxString& cwd);
1204
1205 /**
1206 Sets the extension of the file name to be an empty extension.
1207 This is different from having no extension at all as the file
1208 name will have a trailing dot after a call to this method.
1209
1210 @see SetExt(), ClearExt()
1211 */
1212 void SetEmptyExt();
1213
1214 /**
1215 Sets the extension of the file name.
1216
1217 Setting an empty string as the extension will remove the extension
1218 resulting in a file name without a trailing dot, unlike a call to
1219 SetEmptyExt().
1220
1221 @see SetEmptyExt(), ClearExt()
1222 */
1223 void SetExt(const wxString& ext);
1224
1225 /**
1226 The full name is the file name and extension (but without the path).
1227 */
1228 void SetFullName(const wxString& fullname);
1229
1230 /**
1231 Sets the name part (without extension).
1232
1233 @see SetFullName()
1234 */
1235 void SetName(const wxString& name);
1236
1237 /**
1238 Sets the path.
1239
1240 The @a path argument includes both the path and the volume, if
1241 supported by @a format.
1242
1243 Calling this function doesn't affect the name and extension components,
1244 to change them as well you can use Assign() or just an assignment
1245 operator.
1246
1247 @see GetPath()
1248 */
1249 void SetPath(const wxString& path, wxPathFormat format = wxPATH_NATIVE);
1250
1251 /**
1252 Sets the file creation and last access/modification times (any of the pointers
1253 may be @NULL).
1254 */
1255 bool SetTimes(const wxDateTime* dtAccess,
1256 const wxDateTime* dtMod,
1257 const wxDateTime* dtCreate) const;
1258
1259 /**
1260 Sets the volume specifier.
1261 */
1262 void SetVolume(const wxString& volume);
1263
1264 /**
1265 Return whether some operations will follow symlink.
1266
1267 By default, file operations "follow symlink", i.e. operate on its
1268 target and not on the symlink itself. See DontFollowLink() for more
1269 information.
1270
1271 @since 2.9.5
1272 */
1273 bool ShouldFollowLink() const;
1274
1275 //@{
1276 /**
1277 This function splits a full file name into components: the volume (with the
1278 first version) path (including the volume in the second version), the base name
1279 and the extension.
1280
1281 Any of the output parameters (@e volume, @e path, @a name or @e ext) may
1282 be @NULL if you are not interested in the value of a particular component.
1283 Also, @a fullpath may be empty on entry.
1284 On return, @a path contains the file path (without the trailing separator),
1285 @a name contains the file name and @a ext contains the file extension
1286 without leading dot. All three of them may be empty if the corresponding
1287 component is. The old contents of the strings pointed to by these parameters
1288 will be overwritten in any case (if the pointers are not @NULL).
1289
1290 Note that for a filename "foo." the extension is present, as indicated by the
1291 trailing dot, but empty. If you need to cope with such cases, you should use
1292 @a hasExt instead of relying on testing whether @a ext is empty or not.
1293 */
1294 static void SplitPath(const wxString& fullpath,
1295 wxString* volume,
1296 wxString* path,
1297 wxString* name,
1298 wxString* ext,
1299 bool* hasExt = NULL,
1300 wxPathFormat format = wxPATH_NATIVE);
1301 static void SplitPath(const wxString& fullpath,
1302 wxString* volume,
1303 wxString* path,
1304 wxString* name,
1305 wxString* ext,
1306 wxPathFormat format);
1307 static void SplitPath(const wxString& fullpath,
1308 wxString* path,
1309 wxString* name,
1310 wxString* ext,
1311 wxPathFormat format = wxPATH_NATIVE);
1312 //@}
1313
1314 /**
1315 Splits the given @a fullpath into the volume part (which may be empty) and
1316 the pure path part, not containing any volume.
1317
1318 @see SplitPath()
1319 */
1320 static void SplitVolume(const wxString& fullpath,
1321 wxString* volume,
1322 wxString* path,
1323 wxPathFormat format = wxPATH_NATIVE);
1324
1325
1326 /**
1327 Strip the file extension.
1328
1329 This function does more than just removing everything after the last
1330 period from the string, for example it will return the string ".vimrc"
1331 unchanged because the part after the period is not an extension but the
1332 file name in this case. You can use wxString::BeforeLast() to really
1333 get just the part before the last period (but notice that that function
1334 returns empty string if period is not present at all unlike this
1335 function which returns the @a fullname unchanged in this case).
1336
1337 @param fullname
1338 File path including name and, optionally, extension.
1339
1340 @return
1341 File path without extension
1342
1343 @since 2.9.0
1344 */
1345 static wxString StripExtension(const wxString& fullname);
1346
1347 /**
1348 Sets the access and modification times to the current moment.
1349 */
1350 bool Touch() const;
1351
1352 /**
1353 Returns @true if the filenames are different. The string @e filenames
1354 is interpreted as a path in the native filename format.
1355 */
1356 bool operator!=(const wxFileName& filename) const;
1357
1358 /**
1359 Returns @true if the filenames are different. The string @e filenames
1360 is interpreted as a path in the native filename format.
1361 */
1362 bool operator!=(const wxString& filename) const;
1363
1364 /**
1365 Returns @true if the filenames are equal. The string @e filenames is
1366 interpreted as a path in the native filename format.
1367 */
1368 bool operator==(const wxFileName& filename) const;
1369
1370 /**
1371 Returns @true if the filenames are equal. The string @e filenames is
1372 interpreted as a path in the native filename format.
1373 */
1374 bool operator==(const wxString& filename) const;
1375
1376 /**
1377 Assigns the new value to this filename object.
1378 */
1379 wxFileName& operator=(const wxFileName& filename);
1380
1381 /**
1382 Assigns the new value to this filename object.
1383 */
1384 wxFileName& operator=(const wxString& filename);
1385 };