1 /****************************************************************************
4 ** Implementation of QFileInfo class
8 ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
10 ** This file is part of the tools module of the Qt GUI Toolkit.
12 ** This file may be distributed under the terms of the Q Public License
13 ** as defined by Trolltech AS of Norway and appearing in the file
14 ** LICENSE.QPL included in the packaging of this file.
16 ** This file may be distributed and/or modified under the terms of the
17 ** GNU General Public License version 2 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.GPL included in the
19 ** packaging of this file.
21 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22 ** licenses may use this file in accordance with the Qt Commercial License
23 ** Agreement provided with the Software.
25 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29 ** information about Qt Commercial License Agreements.
30 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
31 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
33 ** Contact info@trolltech.com if any conditions of this licensing are
36 **********************************************************************/
40 #include "qfileinfo.h"
41 #include "qfiledefs_p.h"
42 #include "qdatetime.h"
45 extern bool qt_file_access( const QString& fn, int t );
49 \class QFileInfo qfileinfo.h
50 \brief The QFileInfo class provides system-independent file information.
54 QFileInfo provides information about a file's name and position (path) in
55 the file system, its access rights and whether it is a directory or a
56 symbolic link. Its size and last modified/read times are also available.
58 To speed up performance QFileInfo caches information about the file. Since
59 files can be changed by other users or programs, or even by other parts of
60 the same program there is a function that refreshes the file information;
61 refresh(). If you would rather like a QFileInfo to access the file system
62 every time you request information from it, you can call the function
65 A QFileInfo can point to a file using either a relative or an absolute
66 file path. Absolute file paths begin with the directory separator
67 ('/') or a drive specification (not applicable to UNIX).
68 Relative file names begin with a directory name or a file name and specify
69 a path relative to the current directory. An example of
70 an absolute path is the string "/tmp/quartz". A relative path might look like
71 "src/fatlib". You can use the function isRelative() to check if a QFileInfo
72 is using a relative or an absolute file path. You can call the function
73 convertToAbs() to convert a relative QFileInfo to an absolute one.
75 If you need to read and traverse directories, see the QDir class.
80 Constructs a new empty QFileInfo.
83 QFileInfo::QFileInfo()
90 Constructs a new QFileInfo that gives information about the given file.
91 The string given can be an absolute or a relative file path.
93 \sa bool setFile(QString ), isRelative(), QDir::setCurrent(),
94 QDir::isRelativePath()
97 QFileInfo::QFileInfo( const QString &file )
106 Constructs a new QFileInfo that gives information about \e file.
108 If the file has a relative path, the QFileInfo will also have one.
113 QFileInfo::QFileInfo( const QFile &file )
122 Constructs a new QFileInfo that gives information about the file
123 named \e fileName in the directory \e d.
125 If the directory has a relative path, the QFileInfo will also have one.
130 QFileInfo::QFileInfo( const QDir &d, const QString &fileName )
132 fn = d.filePath( fileName );
139 Constructs a new QFileInfo that is a copy of \e fi.
142 QFileInfo::QFileInfo( const QFileInfo &fi )
146 fic = new QFileInfoCache;
155 Destructs the QFileInfo.
158 QFileInfo::~QFileInfo()
165 Makes a copy of \e fi and assigns it to this QFileInfo.
168 QFileInfo &QFileInfo::operator=( const QFileInfo &fi )
176 fic = new QFileInfoCache;
187 Sets the file to obtain information about.
189 The string given can be an absolute or a relative file path. Absolute file
190 paths begin with the directory separator (e.g. '/' under UNIX) or a drive
191 specification (not applicable to UNIX). Relative file names begin with a
192 directory name or a file name and specify a path relative to the current
197 #include <qfileinfo.h>
202 QString absolute = "/liver/aorta";
203 QString relative = "liver/aorta";
204 QFileInfo fi1( absolute );
205 QFileInfo fi2( relative );
207 QDir::setCurrent( QDir::rootDirPath() );
208 // fi1 and fi2 now point to the same file
210 QDir::setCurrent( "/tmp" );
211 // fi1 now points to "/liver/aorta",
212 // while fi2 points to "/tmp/liver/aorta"
216 \sa isRelative(), QDir::setCurrent(), QDir::isRelativePath()
219 void QFileInfo::setFile( const QString &file )
228 Sets the file to obtain information about.
230 If the file has a relative path, the QFileInfo will also have one.
235 void QFileInfo::setFile( const QFile &file )
244 Sets the file to obtains information about to \e fileName in the
247 If the directory has a relative path, the QFileInfo will also have one.
252 void QFileInfo::setFile( const QDir &d, const QString &fileName )
254 fn = d.filePath( fileName );
262 Returns TRUE if the file pointed to exists, otherwise FALSE.
265 bool QFileInfo::exists() const
267 return qt_file_access( fn, F_OK );
271 Refresh the information about the file, i.e. read in information from the
272 file system the next time a cached property is fetched.
277 void QFileInfo::refresh() const
279 QFileInfo *that = (QFileInfo*)this; // Mutable function
285 \fn bool QFileInfo::caching() const
286 Returns TRUE if caching is enabled.
287 \sa setCaching(), refresh()
291 Enables caching of file information if \e enable is TRUE, or disables it
292 if \e enable is FALSE.
294 When caching is enabled, QFileInfo reads the file information the first
297 Caching is enabled by default.
299 \sa refresh(), caching()
302 void QFileInfo::setCaching( bool enable )
304 if ( cache == enable )
315 Returns the name, i.e. the file name including the path (which can be
316 absolute or relative).
318 \sa isRelative(), absFilePath()
321 QString QFileInfo::filePath() const
327 Returns the base name of the file.
329 The base name consists of all characters in the file name up to (but not
330 including) the first '.' character. The path is not included.
334 QFileInfo fi( "/tmp/abdomen.lower" );
335 QString base = fi.baseName(); // base = "abdomen"
338 \sa fileName(), extension()
341 QString QFileInfo::baseName() const
343 QString tmp = fileName();
344 int pos = tmp.find( '.' );
348 return tmp.left( pos );
352 Returns the extension name of the file.
354 If \a complete is TRUE (the default), extension() returns the string
355 of all characters in the file name after (but not including) the
356 first '.' character. For a file named "archive.tar.gz" this
359 If \a complete is FALSE, extension() returns the string of all
360 characters in the file name after (but not including) the last '.'
361 character. For a file named "archive.tar.gz" this returns "gz".
365 QFileInfo fi( "lex.yy.c" );
366 QString ext = fi.extension(); // ext = "yy.c"
367 QString ext = fi.extension( FALSE ); // ext = "c"
370 \sa fileName(), baseName()
374 QString QFileInfo::extension( bool complete ) const
376 QString s = fileName();
377 int pos = complete ? s.find( '.' ) : s.findRev( '.' );
379 return QString::fromLatin1( "" );
381 return s.right( s.length() - pos - 1 );
385 Returns the directory path of the file.
387 If the QFileInfo is relative and \e absPath is FALSE, the QDir will be
388 relative, otherwise it will be absolute.
390 \sa dirPath(), filePath(), fileName(), isRelative()
393 QDir QFileInfo::dir( bool absPath ) const
395 return QDir( dirPath(absPath) );
401 Returns TRUE if the file is readable.
402 \sa isWritable(), isExecutable(), permission()
405 bool QFileInfo::isReadable() const
407 return qt_file_access( fn, R_OK );
411 Returns TRUE if the file is writable.
412 \sa isReadable(), isExecutable(), permission()
415 bool QFileInfo::isWritable() const
417 return qt_file_access( fn, W_OK );
421 Returns TRUE if the file is executable.
422 \sa isReadable(), isWritable(), permission()
425 bool QFileInfo::isExecutable() const
427 return qt_file_access( fn, X_OK );
432 Returns TRUE if the file path name is relative to the current directory,
433 FALSE if the path is absolute (e.g. under UNIX a path is relative if it
434 does not start with a '/').
436 According to Einstein this function should always return TRUE.
439 bool QFileInfo::isRelative() const
441 return QDir::isRelativePath( fn );
445 Converts the file path name to an absolute path.
447 If it is already absolute nothing is done.
449 \sa filePath(), isRelative()
452 bool QFileInfo::convertToAbs()
456 return QDir::isRelativePath( fn );