📄 qfileinfo.cpp
字号:
/************************************************************************ Copyright (C) 2000-2005 Trolltech AS. All rights reserved.**** This file is part of the Qtopia Environment.** ** This program is free software; you can redistribute it and/or modify it** under the terms of the GNU General Public License as published by the** Free Software Foundation; either version 2 of the License, or (at your** option) any later version.** ** A copy of the GNU GPL license version 2 is included in this package as ** LICENSE.GPL.**** This program is distributed in the hope that it will be useful, but** WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ** See the GNU General Public License for more details.**** In addition, as a special exception Trolltech gives permission to link** the code of this program with Qtopia applications copyrighted, developed** and distributed by Trolltech under the terms of the Qtopia Personal Use** License Agreement. You must comply with the GNU General Public License** in all respects for all of the code used other than the applications** licensed under the Qtopia Personal Use License Agreement. If you modify** this file, you may extend this exception to your version of the file,** but you are not obligated to do so. If you do not wish to do so, delete** this exception statement from your version.** ** 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 "qplatformdefs.h"#include "qfileinfo.h"#include "qdatetime.h"#include "qdir.h"#include "qfiledefs_p.h"#include "qdeepcopy.h"#if defined(QT_LARGEFILE_SUPPORT) && !defined(QT_ABI_QT4)#include <limits.h>#endifextern bool qt_file_access( const QString& fn, int t );/*! \class QFileInfo \reentrant \brief The QFileInfo class provides system-independent file information. \ingroup io QFileInfo provides information about a file's name and position (path) in the file system, its access rights and whether it is a directory or symbolic link, etc. The file's size and last modified/read times are also available. A QFileInfo can point to a file with either a relative or an absolute file path. Absolute file paths begin with the directory separator "/" (or with a drive specification on Windows). Relative file names begin with a directory name or a file name and specify a path relative to the current working 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 whether a QFileInfo is using a relative or an absolute file path. You can call the function convertToAbs() to convert a relative QFileInfo's path to an absolute path. The file that the QFileInfo works on is set in the constructor or later with setFile(). Use exists() to see if the file exists and size() to get its size. To speed up performance, QFileInfo caches information about the file. Because files can be changed by other users or programs, or even by other parts of the same program, there is a function that refreshes the file information: refresh(). If you want to switch off a QFileInfo's caching and force it to access the file system every time you request information from it call setCaching(FALSE). The file's type is obtained with isFile(), isDir() and isSymLink(). The readLink() function provides the name of the file the symlink points to. Elements of the file's name can be extracted with dirPath() and fileName(). The fileName()'s parts can be extracted with baseName() and extension(). The file's dates are returned by created(), lastModified() and lastRead(). Information about the file's access permissions is obtained with isReadable(), isWritable() and isExecutable(). The file's ownership is available from owner(), ownerId(), group() and groupId(). You can examine a file's permissions and ownership in a single statement using the permission() function. If you need to read and traverse directories, see the QDir class.*//*! \enum QFileInfo::PermissionSpec This enum is used by the permission() function to report the permissions and ownership of a file. The values may be OR-ed together to test multiple permissions and ownership values. \value ReadOwner The file is readable by the owner of the file. \value WriteOwner The file is writable by the owner of the file. \value ExeOwner The file is executable by the owner of the file. \value ReadUser The file is readable by the user. \value WriteUser The file is writable by the user. \value ExeUser The file is executable by the user. \value ReadGroup The file is readable by the group. \value WriteGroup The file is writable by the group. \value ExeGroup The file is executable by the group. \value ReadOther The file is readable by anyone. \value WriteOther The file is writable by anyone. \value ExeOther The file is executable by anyone. \warning The semantics of \c ReadUser, \c WriteUser and \c ExeUser are unfortunately not platform independent: on Unix, the rights of the owner of the file are returned and on Windows the rights of the current user are returned. This behavior might change in a future Qt version. If you want to find the rights of the owner of the file, you should use the flags \c ReadOwner, \c WriteOwner and \c ExeOwner. If you want to find out the rights of the current user, you should use isReadable(), isWritable() and isExecutable().*//*! Constructs a new empty QFileInfo.*/QFileInfo::QFileInfo(){ fic = 0; cache = TRUE;#if defined(Q_OS_UNIX) symLink = FALSE;#endif}/*! Constructs a new QFileInfo that gives information about the given file. The \a file can also include an absolute or relative path. \sa setFile(), isRelative(), QDir::setCurrent(), QDir::isRelativePath()*/QFileInfo::QFileInfo( const QString &file ){ fn = file; slashify( fn ); fic = 0; cache = TRUE;#if defined(Q_OS_UNIX) symLink = FALSE;#endif}/*! Constructs a new QFileInfo that gives information about file \a file. If the \a file has a relative path, the QFileInfo will also have a relative path. \sa isRelative()*/QFileInfo::QFileInfo( const QFile &file ){ fn = file.name(); slashify( fn ); fic = 0; cache = TRUE;#if defined(Q_OS_UNIX) symLink = FALSE;#endif}/*! Constructs a new QFileInfo that gives information about the file called \a fileName in the directory \a d. If \a d has a relative path, the QFileInfo will also have a relative path. \sa isRelative()*/#ifndef QT_NO_DIRQFileInfo::QFileInfo( const QDir &d, const QString &fileName ){ fn = d.filePath( fileName ); slashify( fn ); fic = 0; cache = TRUE;#if defined(Q_OS_UNIX) symLink = FALSE;#endif}#endif/*! Constructs a new QFileInfo that is a copy of \a fi.*/QFileInfo::QFileInfo( const QFileInfo &fi ){ fn = fi.fn; if ( fi.fic ) { fic = new QFileInfoCache; *fic = *fi.fic; } else { fic = 0; } cache = fi.cache;#if defined(Q_OS_UNIX) symLink = fi.symLink;#endif}/*! Destroys the QFileInfo and frees its resources.*/QFileInfo::~QFileInfo(){ delete fic;}/*! Makes a copy of \a fi and assigns it to this QFileInfo.*/QFileInfo &QFileInfo::operator=( const QFileInfo &fi ){ fn = fi.fn; if ( !fi.fic ) { delete fic; fic = 0; } else { if ( !fic ) { fic = new QFileInfoCache; Q_CHECK_PTR( fic ); } *fic = *fi.fic; } cache = fi.cache;#if defined(Q_OS_UNIX) symLink = fi.symLink;#endif return *this;}/*! Sets the file that the QFileInfo provides information about to \a file. The \a file can also include an absolute or relative file path. Absolute paths begin with the directory separator (e.g. "/" under Unix) or a drive specification (under Windows). Relative file names begin with a directory name or a file name and specify a path relative to the current directory. Example: \code QString absolute = "/local/bin"; QString relative = "local/bin"; QFileInfo absFile( absolute ); QFileInfo relFile( relative ); QDir::setCurrent( QDir::rootDirPath() ); // absFile and relFile now point to the same file QDir::setCurrent( "/tmp" ); // absFile now points to "/local/bin", // while relFile points to "/tmp/local/bin" \endcode \sa isRelative(), QDir::setCurrent(), QDir::isRelativePath()*/void QFileInfo::setFile( const QString &file ){ fn = file; slashify( fn ); delete fic; fic = 0;}/*! \overload Sets the file that the QFileInfo provides information about to \a file. If \a file includes a relative path, the QFileInfo will also have a relative path. \sa isRelative()*/void QFileInfo::setFile( const QFile &file ){ fn = file.name(); slashify( fn ); delete fic; fic = 0;}/*! \overload Sets the file that the QFileInfo provides information about to \a fileName in directory \a d. If \a fileName includes a relative path, the QFileInfo will also have a relative path. \sa isRelative()*/#ifndef QT_NO_DIRvoid QFileInfo::setFile( const QDir &d, const QString &fileName ){ fn = d.filePath( fileName ); slashify( fn ); delete fic; fic = 0;}#endif/*! Returns TRUE if the file exists; otherwise returns FALSE.*/bool QFileInfo::exists() const{ return qt_file_access( fn, F_OK );}/*! Refreshes the information about the file, i.e. reads in information from the file system the next time a cached property is fetched. \sa setCaching()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -