📄 qdir.cpp
字号:
/****************************************************************************** $Id: qt/src/tools/qdir.cpp 2.2.3 edited 2000-12-08 $**** Implementation of QDir class**** Created : 950427**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of the tools module of the Qt GUI Toolkit.**** This file may be distributed under the terms of the Q Public License** as defined by Trolltech AS of Norway and appearing in the file** LICENSE.QPL included in the packaging of this file.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition** licenses may use this file in accordance with the Qt Commercial License** Agreement provided with the Software.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for** information about Qt Commercial License Agreements.** See http://www.trolltech.com/qpl/ for QPL licensing information.** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#include "qglobal.h"#include "qdir.h"#ifndef QT_NO_DIR#include "qfileinfo.h"#include "qfiledefs_p.h"#include "qregexp.h"#include "qstringlist.h"#include <stdlib.h>#include <ctype.h>// NOT REVISED/*! \class QDir qdir.h \brief Traverses directory structures and contents in a platform-independent way. \ingroup io 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: \code QDir d( "example" ); // "./example" if ( !d.exists() ) qWarning( "Cannot find the example directory" ); \endcode 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. Example: \code QDir d = QDir::root(); // "/" if ( !d.cd("tmp") ) { // "/tmp" qWarning( "Cannot find the \"/tmp\" directory" ); } else { QFile f( d.filePath("ex1.txt") ); // "/tmp/ex1.txt" if ( !f.open(IO_ReadWrite) ) qWarning( "Cannot create the file %s", f.name() ); } \endcode To read the contents of a directory you can use the entryList() and entryInfoList() functions. Example: \code #include <stdio.h> #include <qdir.h> // // This program scans the current directory and lists all files // that are not symbolic links, sorted by size with the smallest files // first. // int main( int argc, char **argv ) { QDir d; d.setFilter( QDir::Files | QDir::Hidden | QDir::NoSymLinks ); d.setSorting( QDir::Size | QDir::Reversed ); const QFileInfoList *list = d.entryInfoList(); QFileInfoListIterator it( *list ); // create list iterator QFileInfo *fi; // pointer for traversing printf( " BYTES FILENAME\n" ); // print header while ( (fi=it.current()) ) { // for each file... printf( "%10li %s\n", fi->size(), fi->fileName().data() ); ++it; // goto next list element } } \endcode*//*! Constructs a QDir pointing to the current directory. \sa currentDirPath()*/QDir::QDir(){ dPath = QString::fromLatin1("."); init();}/*! Constructs a QDir. \arg \e path is the directory. \arg \e nameFilter is the file name filter. \arg \e sortSpec is the sort specification, which describes how to sort the files in the directory. \arg \e filterSpec is the filter specification, which describes how to filter the files in the directory. Most of these arguments (except \e path) have optional values. Example: \code // lists all files in /tmp QDir d( "/tmp" ); for ( int i=0; i<d.count(); i++ ) printf( "%s\n", d[i] ); \endcode If \e path is "" or null, the directory is set to "." (the current directory). If \e nameFilter is "" or null, it is set to "*" (all files). No check is made to ensure that the directory exists. \sa exists(), setPath(), setNameFilter(), setFilter(), setSorting()*/QDir::QDir( const QString &path, const QString &nameFilter, int sortSpec, int filterSpec ){ init(); dPath = cleanDirPath( path ); if ( dPath.isEmpty() ) dPath = QString::fromLatin1("."); nameFilt = nameFilter; if ( nameFilt.isEmpty() ) nameFilt = QString::fromLatin1("*"); filtS = (FilterSpec)filterSpec; sortS = (SortSpec)sortSpec;}/*! Constructs a QDir that is a copy of the given directory. \sa operator=()*/QDir::QDir( const QDir &d ){ dPath = d.dPath; fList = 0; fiList = 0; nameFilt = d.nameFilt; dirty = TRUE; allDirs = d.allDirs; filtS = d.filtS; sortS = d.sortS;}void QDir::init(){ fList = 0; fiList = 0; nameFilt = QString::fromLatin1("*"); dirty = TRUE; allDirs = FALSE; filtS = All; sortS = SortSpec(Name | IgnoreCase);}/*! Destructs the QDir and cleans up.*/QDir::~QDir(){ if ( fList ) delete fList; if ( fiList ) delete fiList;}/*! 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. \sa path(), absPath(), exists(), cleanDirPath(), dirName(), absFilePath(), isRelative(), convertToAbs()*/void QDir::setPath( const QString &path ){ dPath = cleanDirPath( path ); if ( dPath.isEmpty() ) dPath = QString::fromLatin1("."); dirty = TRUE;}/*! \fn QString QDir::path() const 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()). \sa setPath(), absPath(), exists(), cleanDirPath(), dirName(), absFilePath(), convertSeparators()*//*! Returns the absolute (a path that starts with '/') path, which may contain symbolic links, but never contains redundant ".", ".." or multiple separators. \sa setPath(), canonicalPath(), exists(), cleanDirPath(), dirName(), absFilePath()*/QString QDir::absPath() const{ if ( QDir::isRelativePath(dPath) ) { QString tmp = currentDirPath(); if ( tmp.right(1) != QString::fromLatin1("/") ) tmp += '/'; tmp += dPath; return cleanDirPath( tmp ); } else { return cleanDirPath( dPath ); }}/*! 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. \sa path(), absPath(), absFilePath(), exists(), QString::isNull()*/QString QDir::dirName() const{ int pos = dPath.findRev( '/' ); if ( pos == -1 ) return dPath; return dPath.right( dPath.length() - pos - 1 );}/*! 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 \e fileName will not be removed (see cleanDirPath()). If \e acceptAbsPath is TRUE a \e fileName starting with a separator ('/') will be returned without change. If \e acceptAbsPath is FALSE an absolute path will be appended to the directory path. \sa absFilePath(), isRelative(), canonicalPath()*/QString QDir::filePath( const QString &fileName, bool acceptAbsPath ) const{ if ( acceptAbsPath && !isRelativePath(fileName) ) return QString(fileName); QString tmp = dPath; if ( tmp.isEmpty() || (tmp[(int)tmp.length()-1] != '/' && !!fileName && fileName[0] != '/') ) tmp += '/'; tmp += fileName; return tmp;}/*! 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 \e fileName will NOT be removed (see cleanDirPath()). If \e acceptAbsPath is TRUE a \e fileName starting with a separator ('/') will be returned without change. if \e acceptAbsPath is FALSE an absolute path will be appended to the directory path. \sa filePath()*/QString QDir::absFilePath( const QString &fileName, bool acceptAbsPath ) const{ if ( acceptAbsPath && !isRelativePath( fileName ) ) return fileName; QString tmp = absPath(); if ( tmp.isEmpty() || (tmp[(int)tmp.length()-1] != '/' && !!fileName && fileName[0] != '/') ) tmp += '/'; tmp += fileName; return tmp;}/*! Converts the '/' separators in \a 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.*/QString QDir::convertSeparators( const QString &pathName ){ QString n( pathName );#if defined(_OS_FATFS_) || defined(_OS_OS2EMX_) for ( int i=0; i<(int)n.length(); i++ ) { if ( n[i] == '/' ) n[i] = '\\'; }#endif return n;}/*! 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 \e acceptAbsPath is TRUE a path starting with a separator ('/') will cd to the absolute directory, if \e acceptAbsPath is FALSE any number of separators at the beginning of \e dirName will be removed. Example: \code QDir d = QDir::home(); // now points to home directory if ( !d.cd("c++") ) { // now points to "c++" under home directory if OK
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -