wxfilenamewxfilename encapsulates a file name. this class serves two purposes: first, it provides the functions to split the file names into components and to recombine these components in the full file name which can then be passed to the os file functions (and wxwidgets functions wrapping them). second, it includes the functions for working with the files itself. note that to change the file data you should use wxfile class instead. wxfilename provides functions for working with the file attributes. when working with directory names (i.e. without filename and extension) make sure not to misuse the file name part of this class with the last directory. instead initialize the wxfilename instance like this:
wxfilename dirname( wxt("c:\mydir"), wxemptystring ); mymethod( dirname.getpath() );the same can be done using the static method wxfilename::dirname:
wxfilename dirname = wxfilename::dirname( wxt("c:\mydir") ); mymethod( dirname.getpath() );accordingly, methods dealing with directories or directory names like isdirreadable use getpath whereas methods dealing with file names like isfilereadable use getfullpath. if it is not known wether a string contains a directory name or a complete file name (such as when interpreting user input) you need to use the static function wxfilename::direxists (or its identical variants wxdir::exists and wxdirexists) and construct the wxfilename instance accordingly. this will only work if the directory actually exists, of course:
wxstring user_input; // get input from user wxfilename fname; if (wxdirexists(user_input)) fname.assigndir( user_input ); else fname.assign( user_input );derived from no base class include files <wx/filename.h> data structures many wxfilename methods accept the path format argument which is by wxpath_native by default meaning to use the path format native for the current platform. the path format affects the operation of wxfilename functions in several ways: first and foremost, it defines the path separator character to use, but it also affects other things such as whether the path has the drive part or not.
enum wxpathformat { wxpath_native = 0, // the path format for the current platform wxpath_unix, wxpath_beos = wxpath_unix, wxpath_mac, wxpath_dos, wxpath_win = wxpath_dos, wxpath_os2 = wxpath_dos, wxpath_vms, wxpath_max // not a valid value for specifying path format }function groups
file name format
file name formatwxfilename currently supports the file names in the unix, dos/windows, mac os and vms formats. although these formats are quite different, wxfilename tries to treat them all in the same generic way. it supposes that all file names consist of the following parts: the volume (also known as drive under windows or device under vms), the path which is a sequence of directory names separated by the path separators and the full filename itself which, in turn, is composed from the base file name and the extension. all of the individual components of the file name may be empty and, for example, the volume name is always empty under unix, but if they are all empty simultaneously, the filename object is considered to be in an invalid state and isok returns false for it. file names can be case-sensitive or not, the function iscasesensitive allows to determine this. the rules for determining whether the file name is absolute or relative also depend on the file name format and the only portable way to answer this question is to use isabsolute or isrelative method. note that on windows, "x:" refers to the current working directory on drive x. therefore, a wxfilename instance constructed from for example "x:dir/file.ext" treats the portion beyond drive separator as being relative to that directory. to ensure that the filename is absolute, you may use makeabsolute. there is also an inverse function makerelativeto which undoes what normalize(wxpath_norm_dots) does. other functions returning information about the file format provided by this class are getvolumeseparator, ispathseparator.
file name constructionyou can initialize a wxfilename instance using one of the following functions:
wxfilename constructors
file testsbefore doing other tests, you should use isok to verify that the filename is well defined. if it is, fileexists can be used to test whether a file with such name exists and direxists can be used to test for directory existence. file names should be compared using sameas method or operator ==. for testing basic access modes, you can use:
isdirwritable
file name componentsthese functions allow to examine and modify the individual directories of the path:
appenddir to change the components of the file name individually you can use the following functions:
getext
operationsthese methods allow to work with the file creation, access and modification times. note that not all filesystems under all platforms implement these times in the same way. for example, the access time under windows has a resolution of one day (so it is really the access date and not time). the access time may be updated when the file is executed or not depending on the platform.
getmodificationtime other file system operations functions are: members
wxfilename::wxfilenamewxfilename() default constructor. wxfilename(const wxfilename& filename) copy constructor. wxfilename(const wxstring& fullpath, wxpathformat format = wxpath_native) constructor taking a full filename. if it terminates with a '/', a directory path is constructed (the name will be empty), otherwise a file name and extension are extracted from it. wxfilename(const wxstring& path, const wxstring& name, wxpathformat format = wxpath_native) constructor from a directory name and a file name. wxfilename(const wxstring& path, const wxstring& name, const wxstring& ext, wxpathformat format = wxpath_native) constructor from a directory name, base file name and extension. wxfilename(const wxstring& volume, const wxstring& path, const wxstring& name, const wxstring& ext, wxpathformat format = wxpath_native) constructor from a volume name, a directory name, base file name and extension.
wxfilename::appenddirvoid appenddir(const wxstring& dir) appends a directory component to the path. this component should contain a single directory name level, i.e. not contain any path or volume separators nor should it be empty, otherwise the function does nothing (and generates an assert failure in debug build).
wxfilename::assignvoid assign(const wxfilename& filepath) void assign(const wxstring& fullpath, wxpathformat format = wxpath_native) void assign(const wxstring& volume, const wxstring& path, const wxstring& name, const wxstring& ext, bool hasext, wxpathformat format = wxpath_native) void assign(const wxstring& volume, const wxstring& path, const wxstring& name, const wxstring& ext, wxpathformat format = wxpath_native) void assign(const wxstring& path, const wxstring& name, wxpathformat format = wxpath_native) void assign(const wxstring& path, const wxstring& name, const wxstring& ext, wxpathformat format = wxpath_native) creates the file name from various combinations of data.
wxfilename::assigncwdstatic void assigncwd(const wxstring& volume = wxemptystring) makes this object refer to the current working directory on the specified volume (or current volume if volume is empty). see also
wxfilename::assigndirvoid assigndir(const wxstring& dir, wxpathformat format = wxpath_native) sets this file name object to the given directory name. the name and extension will be empty.
wxfilename::assignhomedirvoid assignhomedir() sets this file name object to the home directory.
wxfilename::assigntempfilenamevoid assigntempfilename(const wxstring& prefix, wxfile *filetemp = null) the function calls createtempfilename to create a temporary file and sets this object to the name of the file. if a temporary file couldn't be created, the object is put into the invalid state.
wxfilename::clearvoid clear() reset all components to default, uninitialized state.
wxfilename::clearextvoid setclearext() removes the extension from the file name resulting in a file name with no trailing dot. see also
wxfilename::createtempfilenamestatic wxstring createtempfilename(const wxstring& prefix, wxfile *filetemp = null) returns a temporary file name starting with the given prefix. if the prefix is an absolute path, the temporary file is created in this directory, otherwise it is created in the default system directory for the temporary files or in the current directory. if the function succeeds, the temporary file is actually created. if filetemp is not null, this file will be opened using the name of the temporary file. when possible, this is done in an atomic way ensuring that no race condition occurs between the temporary file name generation and opening it which could often lead to security compromise on the multiuser systems. if filetemp is null, the file is only created, but not opened. under unix, the temporary file will have read and write permissions for the owner only to minimize the security problems. parameters prefix
filetemp
return value the full temporary file name or an empty string on error.
wxfilename::direxistsbool direxists() const static bool direxists(const wxstring& dir) returns true if the directory with this name exists.
wxfilename::dirnamestatic wxfilename dirname(const wxstring& dir, wxpathformat format = wxpath_native) returns the object corresponding to the directory with the given name. the dir parameter may have trailing path separator or not.
wxfilename::fileexistsbool fileexists() const static bool fileexists(const wxstring& file) returns true if the file with this name exists. see also
wxfilename::filenamestatic wxfilename filename(const wxstring& file, wxpathformat format = wxpath_native) returns the file name object corresponding to the given file. this function exists mainly for symmetry with dirname.
wxfilename::getcwdstatic wxstring getcwd(const wxstring& volume = "") retrieves the value of the current working directory on the specified volume. if the volume is empty, the program's current working directory is returned for the current volume. return value the string containing the current working directory or an empty string on error. see also
wxfilename::getdircountsize_t getdircount() const returns the number of directories in the file name.
wxfilename::getdirsconst wxarraystring& getdirs() const returns the directories in string array form.
wxfilename::getextwxstring getext() const returns the file name extension.
wxfilename::getforbiddencharsstatic wxstring getforbiddenchars(wxpathformat format = wxpath_native) returns the characters that can't be used in filenames and directory names for the specified format.
wxfilename::getformatstatic wxpathformat getformat(wxpathformat format = wxpath_native) returns the canonical path format for this platform.
wxfilename::getfullnamewxstring getfullname() const returns the full name (including extension but excluding directories).
wxfilename::getfullpathwxstring getfullpath(wxpathformat format = wxpath_native) const returns the full path with name and extension.
wxfilename::gethomedirstatic wxstring gethomedir() returns the home directory.
wxfilename::getlongpathwxstring getlongpath() const return the long form of the path (returns identity on non-windows platforms)
wxfilename::getmodificationtimewxdatetime getmodificationtime() const returns the last time the file was last modified.
wxfilename::getnamewxstring getname() const returns the name part of the filename (without extension). see also
wxfilename::getpathwxstring getpath(int flags = wxpath_get_volume, wxpathformat format = wxpath_native) const returns the path part of the filename (without the name or extension). the possible flags values are:
wxfilename::getpathseparatorstatic wxchar getpathseparator(wxpathformat format = wxpath_native) returns the usually used path separator for this format. for all formats but wxpath_dos there is only one path separator anyhow, but for dos there are two of them and the native one, i.e. the backslash is returned by this method. see also
wxfilename::getpathseparatorsstatic wxstring getpathseparators(wxpathformat format = wxpath_native) returns the string containing all the path separators for this format. for all formats but wxpath_dos this string contains only one character but for dos and windows both '/' and '\' may be used as separators. see also
wxfilename::getpathterminatorsstatic wxstring getpathterminators(wxpathformat format = wxpath_native) returns the string of characters which may terminate the path part. this is the same as getpathseparators except for vms path format where ] is used at the end of the path part.
wxfilename::getpathwithsepwxstring getpathwithsep(wxpathformat format = wxpath_native) const returns the path with the trailing separator, useful for appending the name to the given path. this is the same as calling getpath (wxpath_get_volume | wxpath_get_separator, format).
wxfilename::getshortpathwxstring getshortpath() const return the short form of the path (returns identity on non-windows platforms).
wxfilename::getsizewxulonglong getsize() const static wxulonglong getsize(const wxstring& filename) returns the size of this file (first form) or the size of the given file (second form). if the file does not exist or its size could not be read (because e.g. the file is locked by another process) the returned value is wxinvalidsize.
wxfilename::gethumanreadablesizewxstring gethumanreadablesize(const wxstring& failmsg = "not available", int precision = 1) const static wxstring gethumanreadablesize(const wxulonglong& bytes, const wxstring& nullsize = "not available", int precision = 1) returns the size of this file (first form) or the given number of bytes (second form) in a human-readable form. if the size could not be retrieved the failmsg string is returned (first form). if bytes is wxinvalidsize or zero, then nullsize is returned (second form). in case of success, the returned string is a floating-point number with precision decimal digits followed by the size unit (b, kb, mb, gb, tb: respectively bytes, kilobytes, megabytes, gigabytes, terabytes).
wxfilename::gettempdirstatic wxstring gettempdir() returns the directory used for temporary files.
wxfilename::gettimesbool gettimes(wxdatetime* dtaccess, wxdatetime* dtmod, wxdatetime* dtcreate) const returns the last access, last modification and creation times. the last access time is updated whenever the file is read or written (or executed in the case of windows), last modification time is only changed when the file is written to. finally, the creation time is indeed the time when the file was created under windows and the inode change time under unix (as it is impossible to retrieve the real file creation time there anyhow) which can also be changed by many operations after the file creation. if no filename or extension is specified in this instance of wxfilename (and therefore isdir returns true) then this function will return the directory times of the path specified by getpath, otherwise the file times of the file specified by getfullpath. any of the pointers may be null if the corresponding time is not needed. return value true on success, false if we failed to retrieve the times.
wxfilename::getvolumewxstring getvolume() const returns the string containing the volume for this file name, empty if it doesn't have one or if the file system doesn't support volumes at all (for example, unix).
wxfilename::getvolumeseparatorstatic wxstring getvolumeseparator(wxpathformat format = wxpath_native) returns the string separating the volume from the path for this format.
wxfilename::hasextbool hasext() const returns true if an extension is present.
wxfilename::hasnamebool hasname() const returns true if a name is present.
wxfilename::hasvolumebool hasvolume() const returns true if a volume specifier is present.
wxfilename::insertdirvoid insertdir(size_t before, const wxstring& dir) inserts a directory component before the zero-based position in the directory list. please see appenddir for important notes.
wxfilename::isabsolutebool isabsolute(wxpathformat format = wxpath_native) returns true if this filename is absolute.
wxfilename::iscasesensitivestatic bool iscasesensitive(wxpathformat format = wxpath_native) returns true if the file names of this type are case-sensitive.
wxfilename::isdirreadablebool isdirreadable() const static bool isdirreadable(const wxstring& dir) returns true if the directory component of this instance (or given dir) is an existing directory and this process has read permissions on it. read permissions on a directory mean that you can list the directory contents but it doesn't imply that you have read permissions on the files contained.
wxfilename::isdirwritablebool isdirwritable() const static bool isdirwritable(const wxstring& dir) returns true if the directory component of this instance (or given dir) is an existing directory and this process has write permissions on it. write permissions on a directory mean that you can create new files in the directory.
wxfilename::isfileexecutablebool isfileexecutable() const static bool isfileexecutable(const wxstring& file) returns true if a file with this name exists and if this process has execute permissions on it.
wxfilename::isfilereadablebool isfilereadable() const static bool isfilereadable(const wxstring& file) returns true if a file with this name exists and if this process has read permissions on it.
wxfilename::isfilewritablebool isfilewritable() const static bool isfilewritable(const wxstring& file) returns true if a file with this name exists and if this process has write permissions on it.
wxfilename::isokbool isok() const returns true if the filename is valid, false if it is not initialized yet. the assignment functions and clear may reset the object to the uninitialized, invalid state (the former only do it on failure).
wxfilename::ispathseparatorstatic bool ispathseparator(wxchar ch, wxpathformat format = wxpath_native) returns true if the char is a path separator for this format.
wxfilename::isrelativebool isrelative(wxpathformat format = wxpath_native) returns true if this filename is not absolute.
wxfilename::isdirbool isdir() const returns true if this object represents a directory, false otherwise (i.e. if it is a file). note that this method doesn't test whether the directory or file really exists, you should use direxists or fileexists for this.
wxfilename::macfinddefaulttypeandcreatorstatic bool macfinddefaulttypeandcreator(const wxstring& ext, wxuint32* type, wxuint32* creator) on mac os, gets the common type and creator for the given extension.
wxfilename::macregisterdefaulttypeandcreatorstatic void macregisterdefaulttypeandcreator(const wxstring& ext, wxuint32 type, wxuint32 creator) on mac os, registers application defined extensions and their default type and creator.
wxfilename::macsetdefaulttypeandcreatorbool macsetdefaulttypeandcreator() on mac os, looks up the appropriate type and creator from the registration and then sets it.
wxfilename::makeabsolutebool makeabsolute(const wxstring& cwd = wxemptystring, wxpathformat format = wxpath_native) make the file name absolute. this is a shortcut for normalize(wxpath_norm_dots | wxpath_norm_absolute | wxpath_norm_tilde, cwd, format). see also makerelativeto, normalize, isabsolute
wxfilename::makerelativetobool makerelativeto(const wxstring& pathbase = wxemptystring, wxpathformat format = wxpath_native) this function tries to put this file name in a form relative to pathbase. in other words, it returns the file name which should be used to access this file if the current directory were pathbase. pathbase
format
return value true if the file name has been changed, false if we failed to do anything with it (currently this only happens if the file name is on a volume different from the volume specified by pathbase). see also
wxfilename::mkdirbool mkdir(int perm = 0777, int flags = 0) static bool mkdir(const wxstring& dir, int perm = 0777, int flags = 0) dir
parm
flags
return value returns true if the directory was successfully created, false otherwise.
wxfilename::normalizebool normalize(int flags = wxpath_norm_all, const wxstring& cwd = wxemptystring, wxpathformat format = wxpath_native) normalize the path. with the default flags value, the path will be made absolute, without any ".." and "." and all environment variables will be expanded in it. flags
format
return value true if normalization was successfully or false otherwise.
wxfilename::prependdirvoid prependdir(const wxstring& dir) prepends a directory to the file path. please see appenddir for important notes.
wxfilename::removedirvoid removedir(size_t pos) removes the specified directory component from the path. see also
wxfilename::removelastdirvoid removelastdir() removes last directory component from the path.
wxfilename::rmdirbool rmdir() static bool rmdir(const wxstring& dir) deletes the specified directory from the file system.
wxfilename::sameasbool sameas(const wxfilename& filepath, wxpathformat format = wxpath_native) const compares the filename using the rules of this platform.
wxfilename::setcwdbool setcwd() static bool setcwd(const wxstring& cwd) changes the current working directory.
wxfilename::setextvoid setext(const wxstring& ext) sets the extension of the file name. setting an empty string as the extension will remove the extension resulting in a file name without a trailing dot, unlike a call to setemptyext. see also
wxfilename::setemptyextvoid setemptyext() sets the extension of the file name to be an empty extension. this is different from having no extension at all as the file name will have a trailing dot after a call to this method. see also
wxfilename::setfullnamevoid setfullname(const wxstring& fullname) the full name is the file name and extension (but without the path).
wxfilename::setnamevoid setname(const wxstring& name) sets the name part (without extension). see also
wxfilename::settimesbool settimes(const wxdatetime* dtaccess, const wxdatetime* dtmod, const wxdatetime* dtcreate) sets the file creation and last access/modification times (any of the pointers may be null).
wxfilename::setvolumevoid setvolume(const wxstring& volume) sets the volume specifier.
wxfilename::splitpathstatic void splitpath(const wxstring& fullpath, wxstring* volume, wxstring* path, wxstring* name, wxstring* ext, bool *hasext = null, wxpathformat format = wxpath_native) static void splitpath(const wxstring& fullpath, wxstring* volume, wxstring* path, wxstring* name, wxstring* ext, wxpathformat format = wxpath_native) static void splitpath(const wxstring& fullpath, wxstring* path, wxstring* name, wxstring* ext, wxpathformat format = wxpath_native) this function splits a full file name into components: the volume (with the first version) path (including the volume in the second version), the base name and the extension. any of the output parameters (volume, path, name or ext) may be null if you are not interested in the value of a particular component. also, fullpath may be empty on entry. on return, path contains the file path (without the trailing separator), name contains the file name and ext contains the file extension without leading dot. all three of them may be empty if the corresponding component is. the old contents of the strings pointed to by these parameters will be overwritten in any case (if the pointers are not null). note that for a filename "foo.'' the extension is present, as indicated by the trailing dot, but empty. if you need to cope with such cases, you should use hasext instead of relying on testing whether ext is empty or not.
wxfilename::splitvolumestatic void splitvolume(const wxstring& fullpath, wxstring* volume, wxstring* path, wxpathformat format = wxpath_native) splits the given fullpath into the volume part (which may be empty) and the pure path part, not containing any volume. see also
wxfilename::touchbool touch() sets the access and modification times to the current moment.
wxfilename::operator=wxfilename& operator operator=(const wxfilename& filename) wxfilename& operator operator=(const wxstring& filename) assigns the new value to this filename object.
wxfilename::operator==bool operator operator==(const wxfilename& filename) const bool operator operator==(const wxstring& filename) const returns true if the filenames are equal. the string filenames is interpreted as a path in the native filename format.
wxfilename::operator!=bool operator operator!=(const wxfilename& filename) const bool operator operator!=(const wxstring& filename) const returns true if the filenames are different. the string filenames is interpreted as a path in the native filename format.
|