Traverses directory structures and contents in a platform-independent way. More...
#include <qdir.h>
Public Types | |
enum | FilterSpec { Dirs = 0x001, Files = 0x002, Drives = 0x004, NoSymLinks = 0x008, All = 0x007, TypeMask = 0x00F, Readable = 0x010, Writable = 0x020, Executable = 0x040, RWEMask = 0x070, Modified = 0x080, Hidden = 0x100, System = 0x200, AccessMask = 0x3F0, DefaultFilter = -1 } |
enum | SortSpec { Name = 0x00, Time = 0x01, Size = 0x02, Unsorted = 0x03, SortByMask = 0x03, DirsFirst = 0x04, Reversed = 0x08, IgnoreCase = 0x10, DefaultSort = -1 } |
Public Member Functions | |
QDir () | |
QDir (const QString &path, const QString &nameFilter=QString::null, int sortSpec=Name|IgnoreCase, int filterSpec=All) | |
QDir (const QDir &) | |
virtual | ~QDir () |
QDir & | operator= (const QDir &) |
QDir & | operator= (const QString &path) |
virtual void | setPath (const QString &path) |
virtual QString | path () const |
virtual QString | absPath () const |
virtual QString | canonicalPath () const |
virtual QString | dirName () const |
virtual QString | filePath (const QString &fileName, bool acceptAbsPath=TRUE) const |
virtual QString | absFilePath (const QString &fileName, bool acceptAbsPath=TRUE) const |
virtual bool | cd (const QString &dirName, bool acceptAbsPath=TRUE) |
virtual bool | cdUp () |
QString | nameFilter () const |
virtual void | setNameFilter (const QString &nameFilter) |
FilterSpec | filter () const |
virtual void | setFilter (int filterSpec) |
SortSpec | sorting () const |
virtual void | setSorting (int sortSpec) |
bool | matchAllDirs () const |
virtual void | setMatchAllDirs (bool) |
uint | count () const |
QString | operator[] (int) const |
virtual QStrList | encodedEntryList (int filterSpec=DefaultFilter, int sortSpec=DefaultSort) const |
virtual QStrList | encodedEntryList (const QString &nameFilter, int filterSpec=DefaultFilter, int sortSpec=DefaultSort) const |
virtual QStringList | entryList (int filterSpec=DefaultFilter, int sortSpec=DefaultSort) const |
virtual QStringList | entryList (const QString &nameFilter, int filterSpec=DefaultFilter, int sortSpec=DefaultSort) const |
virtual const QFileInfoList * | entryInfoList (int filterSpec=DefaultFilter, int sortSpec=DefaultSort) const |
virtual const QFileInfoList * | entryInfoList (const QString &nameFilter, int filterSpec=DefaultFilter, int sortSpec=DefaultSort) const |
virtual bool | mkdir (const QString &dirName, bool acceptAbsPath=TRUE) const |
virtual bool | rmdir (const QString &dirName, bool acceptAbsPath=TRUE) const |
virtual bool | isReadable () const |
virtual bool | exists () const |
virtual bool | isRoot () const |
virtual bool | isRelative () const |
virtual void | convertToAbs () |
virtual bool | operator== (const QDir &) const |
virtual bool | operator!= (const QDir &) const |
virtual bool | remove (const QString &fileName, bool acceptAbsPath=TRUE) |
virtual bool | rename (const QString &name, const QString &newName, bool acceptAbsPaths=TRUE) |
virtual bool | exists (const QString &name, bool acceptAbsPath=TRUE) |
Static Public Member Functions | |
static QString | convertSeparators (const QString &pathName) |
static const QFileInfoList * | drives () |
static char | separator () |
static bool | setCurrent (const QString &path) |
static QDir | current () |
static QDir | home () |
static QDir | root () |
static QString | currentDirPath () |
static QString | homeDirPath () |
static QString | rootDirPath () |
static bool | match (const QStringList &filters, const QString &fileName) |
static bool | match (const QString &filter, const QString &fileName) |
static QString | cleanDirPath (const QString &dirPath) |
static bool | isRelativePath (const QString &path) |
Private Member Functions | |
void | init () |
virtual bool | readDirEntries (const QString &nameFilter, int FilterSpec, int SortSpec) |
Static Private Member Functions | |
static void | slashify (QString &) |
Private Attributes | |
QString | dPath |
QStringList * | fList |
QFileInfoList * | fiList |
QString | nameFilt |
FilterSpec | filtS |
SortSpec | sortS |
uint | dirty: 1 |
uint | allDirs: 1 |
Traverses directory structures and contents in a platform-independent way.
A QDir can point to a file using either a relative or an absolute file path. Absolute file paths begin with the directory separator ('/') or a drive specification (not applicable to UNIX). Relative file names begin with a directory name or a file name and specify a path relative to the current directory.
An example of an absolute path is the string "/tmp/quartz", a relative path might look like "src/fatlib". You can use the function isRelative() to check if a QDir is using a relative or an absolute file path. You can call the function convertToAbs() to convert a relative QDir to an absolute one.
The directory "example" under the current directory is checked for existence in the example below:
If you always use '/' as a directory separator, Qt will translate your paths to conform to the underlying operating system.
cd() and cdUp() can be used to navigate the directory tree. Note that the logical cd and cdUp operations are not performed if the new directory does not exist.
To read the contents of a directory you can use the entryList() and entryInfoList() functions.
enum QDir::FilterSpec |
This enum describes how QDir is to select what entries in a directory to return. The filter value is specified by or-ing together values from the following list:
Dirs
- List directories only Files
- List files only
Drives
- List disk drives (does nothing under unix) NoSymLinks
- Do not list symbolic links (where they exist) Readable
- List files for which the application has read access. Writable
- List files for which the application has write access. Executable
- List files for which the application has execute access Modified
- Only list files that have been modified (does nothing under unix) Hidden
- List hidden files (on unix, files starting with a .) System
- List system files (does nothing under unix) If you do not set any of Readable
, Writable
or Executable
, QDir will set all three of them. This makes the default easy to write and at the same time useful.
Examples: Readable|Writable
means list all files for which the application has read access, write access or both. Dirs|Drives
means list drives, directories, all files that the application can read, write or execute, and also symlinks to such files/directories.
Enumerator | |
---|---|
Dirs | |
Files | |
Drives | |
NoSymLinks | |
All | |
TypeMask | |
Readable | |
Writable | |
Executable | |
RWEMask | |
Modified | |
Hidden | |
System | |
AccessMask | |
DefaultFilter |
Definition at line 55 of file qdir.h.
enum QDir::SortSpec |
This enum describes how QDir is to sort entries in a directory when it returns a list of them. The sort value is specified by or-ing together values from the following list:
Name
- sort by name Time
- sort by time (modification time) Size
- sort by file size Unsorted
- do not sort
DirsFirst
- put all directories first in the list Reversed
- reverse the sort order IgnoreCase
- sort case-insensitively
You can only specify one of the first four. If you specify both DirsFirst
and Reversed
, directories are still put first but the list is otherwise reversed.
Enumerator | |
---|---|
Name | |
Time | |
Size | |
Unsorted | |
SortByMask | |
DirsFirst | |
Reversed | |
IgnoreCase | |
DefaultSort |
QDir::QDir | ( | ) |
Constructs a QDir pointing to the current directory.
Definition at line 137 of file qdir.cpp.
QDir::QDir | ( | const QString & | path, |
const QString & | nameFilter = QString::null , |
||
int | sortSpec = Name | IgnoreCase , |
||
int | filterSpec = All |
||
) |
Constructs a QDir.
Most of these arguments (except path) have optional values.
If path is "" or null, the directory is set to "." (the current directory). If nameFilter is "" or null, it is set to "*" (all files).
No check is made to ensure that the directory exists.
Definition at line 173 of file qdir.cpp.
QDir::QDir | ( | const QDir & | d | ) |
Constructs a QDir that is a copy of the given directory.
Definition at line 192 of file qdir.cpp.
Returns the absolute path name of a file in the directory. Does NOT check if the file actually exists in the directory. Redundant multiple separators or "." and ".." directories in fileName will NOT be removed (see cleanDirPath()).
If acceptAbsPath is TRUE a fileName starting with a separator ('/') will be returned without change. if acceptAbsPath is FALSE an absolute path will be appended to the directory path.
Definition at line 351 of file qdir.cpp.
|
virtual |
Returns the absolute (a path that starts with '/') path, which may contain symbolic links, but never contains redundant ".", ".." or multiple separators.
Definition at line 276 of file qdir.cpp.
|
virtual |
Definition at line 79 of file qdir_unix.cpp.
Changes directory by descending into the given directory. Returns TRUE if the new directory exists and is readable. Note that the logical cd operation is NOT performed if the new directory does not exist.
If acceptAbsPath is TRUE a path starting with a separator ('/') will cd to the absolute directory, if acceptAbsPath is FALSE any number of separators at the beginning of dirName will be removed.
Calling cd( ".." ) is equivalent to calling cdUp().
Definition at line 429 of file qdir.cpp.
|
virtual |
Changes directory by moving one directory up the path followed to arrive at the current directory.
Returns TRUE if the new directory exists and is readable. Note that the logical cdUp() operation is not performed if the new directory does not exist.
Definition at line 464 of file qdir.cpp.
Removes all multiple directory separators ('/') and resolves any "." or ".." found in the path.
Symbolic links are kept. This function does not return the canonical path, but rather the most simplified version of the input. "../stuff" becomes "stuff", "stuff/../nonsense" becomes "nonsense" and "\\stuff\\more\\..\\nonsense" becomes "\\stuff\\nonsense".
Definition at line 1077 of file qdir.cpp.
Converts the '/' separators in pathName to system native separators. Returns the translated string.
On Windows, convertSeparators("c:/winnt/system32") returns "c:\winnt\system32".
No conversion is done on UNIX.
Definition at line 376 of file qdir.cpp.
|
virtual |
Converts the directory path to an absolute path. If it is already absolute nothing is done.
Definition at line 848 of file qdir.cpp.
uint QDir::count | ( | ) | const |
Returns the number of files that was found. Equivalent to entryList().count().
Definition at line 641 of file qdir.cpp.
|
static |
Returns the current directory.
Definition at line 978 of file qdir.cpp.
|
static |
Definition at line 141 of file qdir_unix.cpp.
|
virtual |
Returns the name of the directory, this is NOT the same as the path, e.g. a directory with the name "mail", might have the path "/var/spool/mail". If the directory has no name (e.g. the root directory) a null string is returned.
No check is made to ensure that a directory with this name actually exists.
Definition at line 300 of file qdir.cpp.
|
static |
Definition at line 276 of file qdir_unix.cpp.
|
virtual |
This function is included to easy porting from Qt 1.x to Qt 2.0, it is the same as entryList(), but encodes the filenames as 8-bit strings using QFile::encodedName().
It is more efficient to use entryList().
Definition at line 672 of file qdir.cpp.
|
virtual |
This function is included to easy porting from Qt 1.x to Qt 2.0, it is the same as entryList(), but encodes the filenames as 8-bit strings using QFile::encodedName().
It is more efficient to use entryList().
Definition at line 689 of file qdir.cpp.
|
virtual |
Returns a list of QFileInfo objects for all files and directories in the directory pointed to using the setSorting(), setFilter() and setNameFilter() specifications.
The the filter and sorting specifications can be overridden using the filterSpec and sortSpec arguments.
Returns 0 if the directory is unreadable or does not exist.
The returned pointer is a const pointer to a QFileInfoList. The list is owned by the QDir object and will be reused on the next call to entryInfoList() for the same QDir instance. If you want to keep the entries of the list after a subsequent call to this function you will need to copy them.
Definition at line 772 of file qdir.cpp.
|
virtual |
Returns a list of QFileInfo objects for all files and directories in the directory pointed to using the setSorting(), setFilter() and setNameFilter() specifications.
The the filter and sorting specifications can be overridden using the nameFilter, filterSpec and sortSpec arguments.
Returns 0 if the directory is unreadable or does not exist.
The returned pointer is a const pointer to a QFileInfoList. The list is owned by the QDir object and will be reused on the next call to entryInfoList() for the same QDir instance. If you want to keep the entries of the list after a subsequent call to this function you will need to copy them.
Definition at line 799 of file qdir.cpp.
|
virtual |
Returns a list of the names of all files and directories in the directory indicated by the setSorting(), setFilter() and setNameFilter() specifications.
The the filter and sorting specifications can be overridden using the filterSpec and sortSpec arguments.
Returns an empty list if the directory is unreadable or does not exist.
Definition at line 717 of file qdir.cpp.
|
virtual |
Returns a list of the names of all files and directories in the directory indicated by the setSorting(), setFilter() and setNameFilter() specifications.
The the filter and sorting specifications can be overridden using the nameFilter, filterSpec and sortSpec arguments.
Returns and empty list if the directory is unreadable or does not exist.
Definition at line 739 of file qdir.cpp.
|
virtual |
Returns TRUE if the directory exists. (If a file with the same name is found this function will of course return FALSE).
Definition at line 820 of file qdir.cpp.
Checks for existence of a file.
If acceptAbsPaths is TRUE a path starting with a separator ('/') will check the file with the absolute path, if acceptAbsPath is FALSE any number of separators at the beginning of name will be removed.
Returns TRUE if the file exists, otherwise FALSE.
Definition at line 939 of file qdir.cpp.
Returns the path name of a file in the directory. Does NOT check if the file actually exists in the directory. If the QDir is relative the returned path name will also be relative. Redundant multiple separators or "." and ".." directories in fileName will not be removed (see cleanDirPath()).
If acceptAbsPath is TRUE a fileName starting with a separator ('/') will be returned without change. If acceptAbsPath is FALSE an absolute path will be appended to the directory path.
Definition at line 323 of file qdir.cpp.
|
inline |
Returns the value set by setFilter()
|
static |
|
static |
Returns the absolute path for the user's home directory,
Definition at line 69 of file qdir_unix.cpp.
|
virtual |
Definition at line 109 of file qdir_unix.cpp.
|
virtual |
Returns TRUE if the directory path is relative to the current directory, FALSE if the path is absolute (e.g. under UNIX a path is relative if it does not start with a '/').
According to Einstein this function should always return TRUE.
Definition at line 836 of file qdir.cpp.
Definition at line 169 of file qdir_unix.cpp.
|
virtual |
Definition at line 114 of file qdir_unix.cpp.
|
static |
Returns TRUE if the fileName matches one of the wildcards in the list filters.
Definition at line 1036 of file qdir.cpp.
Returns TRUE if the fileName matches the wildcard filter. Filter may also contain multiple wildcards separated by spaces or semicolons.
Definition at line 1058 of file qdir.cpp.
|
inline |
Returns the value set by setMatchAllDirs()
Definition at line 98 of file qdir_unix.cpp.
|
inline |
Returns the string set by setNameFilter()
Returns TRUE if the d and this dir have different path or different sort/filter settings, otherwise FALSE.
QString QDir::operator[] | ( | int | index | ) | const |
Returns the file name at position index in the list of found file names. Equivalent to entryList().at(index).
Returns null if the index is out of range or if the entryList() function failed.
Definition at line 657 of file qdir.cpp.
|
inlinevirtual |
Returns the path, this may contain symbolic links, but never contains redundant ".", ".." or multiple separators.
The returned path can be either absolute or relative (see setPath()).
|
privatevirtual |
Definition at line 177 of file qdir_unix.cpp.
Removes a file.
If acceptAbsPath is TRUE a path starting with a separator ('/') will remove the file with the absolute path, if acceptAbsPath is FALSE any number of separators at the beginning of fileName will be removed.
Returns TRUE if successful, otherwise FALSE.
Definition at line 915 of file qdir.cpp.
|
virtual |
Definition at line 119 of file qdir_unix.cpp.
Definition at line 104 of file qdir_unix.cpp.
|
static |
|
static |
Definition at line 163 of file qdir_unix.cpp.
|
static |
Returns the native directory separator; '/' under UNIX and '\' under MS-DOS, Windows NT and OS/2.
You do not need to use this function to build file paths. If you always use '/', Qt will translate your paths to conform to the underlying operating system.
Definition at line 134 of file qdir_unix.cpp.
|
virtual |
Sets the filter used by entryList() and entryInfoList(). The filter is used to specify the kind of files that should be returned by entryList() and entryInfoList().
Definition at line 538 of file qdir.cpp.
|
virtual |
If enable is TRUE, all directories will be listed (even if they do not match the filter or the name filter), otherwise only matched directories will be listed.
Definition at line 626 of file qdir.cpp.
|
virtual |
Sets the name filter used by entryList() and entryInfoList().
The name filter is a wildcarding filter that understands "*" and "?" wildcards, You may specify several filter entries separated by a " " or a ";". If you want entryList() and entryInfoList() to list all files ending with ".cpp" and all files ending with ".h", you simply call dir.setNameFilter("*.cpp *.h") or dir.setNameFilter("*.cpp;*.h")
Definition at line 486 of file qdir.cpp.
|
virtual |
Sets the path of the directory. The path is cleaned of redundant ".", ".." and multiple separators. No check is made to ensure that a directory with this path exists.
The path can be either absolute or relative. Absolute paths begin with the directory separator ('/') or a drive specification (not applicable to UNIX). Relative file names begin with a directory name or a file name and specify a path relative to the current directory. An example of an absolute path is the string "/tmp/quartz", a relative path might look like "src/fatlib". You can use the function isRelative() to check if a QDir is using a relative or an absolute file path. You can call the function convertToAbs() to convert a relative QDir to an absolute one.
Definition at line 248 of file qdir.cpp.
|
virtual |
Sets the sorting order used by entryList() and entryInfoList().
The sortSpec is specified by or-ing values from the enum SortSpec. The different values are:
One of these:
Use the operating system order (UNIX does NOT sort alphabetically).
ORed with zero or more of these:
Definition at line 600 of file qdir.cpp.
|
staticprivate |
Definition at line 65 of file qdir_unix.cpp.
|
inline |
Returns the value set by setSorting()
|
private |
|
private |
|
private |