Blame qtools/qfileinfo.cpp

Packit 1c1d7e
/****************************************************************************
Packit 1c1d7e
** 
Packit 1c1d7e
**
Packit 1c1d7e
** Implementation of QFileInfo class
Packit 1c1d7e
**
Packit 1c1d7e
** Created : 950628
Packit 1c1d7e
**
Packit 1c1d7e
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
Packit 1c1d7e
**
Packit 1c1d7e
** This file is part of the tools module of the Qt GUI Toolkit.
Packit 1c1d7e
**
Packit 1c1d7e
** This file may be distributed under the terms of the Q Public License
Packit 1c1d7e
** as defined by Trolltech AS of Norway and appearing in the file
Packit 1c1d7e
** LICENSE.QPL included in the packaging of this file.
Packit 1c1d7e
**
Packit 1c1d7e
** This file may be distributed and/or modified under the terms of the
Packit 1c1d7e
** GNU General Public License version 2 as published by the Free Software
Packit 1c1d7e
** Foundation and appearing in the file LICENSE.GPL included in the
Packit 1c1d7e
** packaging of this file.
Packit 1c1d7e
**
Packit 1c1d7e
** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
Packit 1c1d7e
** licenses may use this file in accordance with the Qt Commercial License
Packit 1c1d7e
** Agreement provided with the Software.
Packit 1c1d7e
**
Packit 1c1d7e
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
Packit 1c1d7e
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Packit 1c1d7e
**
Packit 1c1d7e
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
Packit 1c1d7e
**   information about Qt Commercial License Agreements.
Packit 1c1d7e
** See http://www.trolltech.com/qpl/ for QPL licensing information.
Packit 1c1d7e
** See http://www.trolltech.com/gpl/ for GPL licensing information.
Packit 1c1d7e
**
Packit 1c1d7e
** Contact info@trolltech.com if any conditions of this licensing are
Packit 1c1d7e
** not clear to you.
Packit 1c1d7e
**
Packit 1c1d7e
**********************************************************************/
Packit 1c1d7e
Packit 1c1d7e
#include "qglobal.h"
Packit 1c1d7e
Packit 1c1d7e
#include "qfileinfo.h"
Packit 1c1d7e
#include "qfiledefs_p.h"
Packit 1c1d7e
#include "qdatetime.h"
Packit 1c1d7e
#include "qdir.h"
Packit 1c1d7e
Packit 1c1d7e
extern bool qt_file_access( const QString& fn, int t );
Packit 1c1d7e
Packit 1c1d7e
// NOT REVISED
Packit 1c1d7e
/*!
Packit 1c1d7e
  \class QFileInfo qfileinfo.h
Packit 1c1d7e
  \brief The QFileInfo class provides system-independent file information.
Packit 1c1d7e
Packit 1c1d7e
  \ingroup io
Packit 1c1d7e
Packit 1c1d7e
  QFileInfo provides information about a file's name and position (path) in
Packit 1c1d7e
  the file system, its access rights and whether it is a directory or a
Packit 1c1d7e
  symbolic link.  Its size and last modified/read times are also available.
Packit 1c1d7e
Packit 1c1d7e
  To speed up performance QFileInfo caches information about the file. Since
Packit 1c1d7e
  files can be changed by other users or programs, or even by other parts of
Packit 1c1d7e
  the same program there is a function that refreshes the file information;
Packit 1c1d7e
  refresh(). If you would rather like a QFileInfo to access the file system
Packit 1c1d7e
  every time you request information from it, you can call the function
Packit 1c1d7e
  setCaching( FALSE ).
Packit 1c1d7e
Packit 1c1d7e
  A QFileInfo can point to a file using either a relative or an absolute
Packit 1c1d7e
  file path. Absolute file paths begin with the directory separator
Packit 1c1d7e
  ('/') or a drive specification (not applicable to UNIX).
Packit 1c1d7e
  Relative file names begin with a directory name or a file name and specify
Packit 1c1d7e
  a path relative to the current directory. An example of
Packit 1c1d7e
  an absolute path is the string "/tmp/quartz". A relative path might look like
Packit 1c1d7e
  "src/fatlib". You can use the function isRelative() to check if a QFileInfo
Packit 1c1d7e
  is using a relative or an absolute file path. You can call the function
Packit 1c1d7e
  convertToAbs() to convert a relative QFileInfo to an absolute one.
Packit 1c1d7e
Packit 1c1d7e
  If you need to read and traverse directories, see the QDir class.
Packit 1c1d7e
*/
Packit 1c1d7e
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Constructs a new empty QFileInfo.
Packit 1c1d7e
*/
Packit 1c1d7e
Packit 1c1d7e
QFileInfo::QFileInfo()
Packit 1c1d7e
{
Packit 1c1d7e
    fic	  = 0;
Packit 1c1d7e
    cache = TRUE;
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Constructs a new QFileInfo that gives information about the given file.
Packit 1c1d7e
  The string given can be an absolute or a relative file path.
Packit 1c1d7e
Packit 1c1d7e
  \sa bool setFile(QString ), isRelative(), QDir::setCurrent(),
Packit 1c1d7e
  QDir::isRelativePath()
Packit 1c1d7e
*/
Packit 1c1d7e
Packit 1c1d7e
QFileInfo::QFileInfo( const QString &file )
Packit 1c1d7e
{
Packit 1c1d7e
    fn	  = file;
Packit 1c1d7e
    slashify( fn );
Packit 1c1d7e
    fic	  = 0;
Packit 1c1d7e
    cache = TRUE;
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Constructs a new QFileInfo that gives information about \e file.
Packit 1c1d7e
Packit 1c1d7e
  If the file has a relative path, the QFileInfo will also have one.
Packit 1c1d7e
Packit 1c1d7e
  \sa isRelative()
Packit 1c1d7e
*/
Packit 1c1d7e
Packit 1c1d7e
QFileInfo::QFileInfo( const QFile &file )
Packit 1c1d7e
{
Packit 1c1d7e
    fn	  = file.name();
Packit 1c1d7e
    slashify( fn );
Packit 1c1d7e
    fic	  = 0;
Packit 1c1d7e
    cache = TRUE;
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Constructs a new QFileInfo that gives information about the file
Packit 1c1d7e
  named \e fileName in the directory \e d.
Packit 1c1d7e
Packit 1c1d7e
  If the directory has a relative path, the QFileInfo will also have one.
Packit 1c1d7e
Packit 1c1d7e
  \sa isRelative()
Packit 1c1d7e
*/
Packit 1c1d7e
#ifndef QT_NO_DIR
Packit 1c1d7e
QFileInfo::QFileInfo( const QDir &d, const QString &fileName )
Packit 1c1d7e
{
Packit 1c1d7e
    fn	  = d.filePath( fileName );
Packit 1c1d7e
    slashify( fn );
Packit 1c1d7e
    fic	  = 0;
Packit 1c1d7e
    cache = TRUE;
Packit 1c1d7e
}
Packit 1c1d7e
#endif
Packit 1c1d7e
/*!
Packit 1c1d7e
  Constructs a new QFileInfo that is a copy of \e fi.
Packit 1c1d7e
*/
Packit 1c1d7e
Packit 1c1d7e
QFileInfo::QFileInfo( const QFileInfo &fi )
Packit 1c1d7e
{
Packit 1c1d7e
    fn = fi.fn;
Packit 1c1d7e
    if ( fi.fic ) {
Packit 1c1d7e
	fic = new QFileInfoCache;
Packit 1c1d7e
	*fic = *fi.fic;
Packit 1c1d7e
    } else {
Packit 1c1d7e
	fic = 0;
Packit 1c1d7e
    }
Packit 1c1d7e
    cache = fi.cache;
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Destructs the QFileInfo.
Packit 1c1d7e
*/
Packit 1c1d7e
Packit 1c1d7e
QFileInfo::~QFileInfo()
Packit 1c1d7e
{
Packit 1c1d7e
    delete fic;
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Makes a copy of \e fi and assigns it to this QFileInfo.
Packit 1c1d7e
*/
Packit 1c1d7e
Packit 1c1d7e
QFileInfo &QFileInfo::operator=( const QFileInfo &fi )
Packit 1c1d7e
{
Packit 1c1d7e
    fn = fi.fn;
Packit 1c1d7e
    if ( !fi.fic ) {
Packit 1c1d7e
	delete fic;
Packit 1c1d7e
	fic = 0;
Packit 1c1d7e
    } else {
Packit 1c1d7e
	if ( !fic ) {
Packit 1c1d7e
	    fic = new QFileInfoCache;
Packit 1c1d7e
	    CHECK_PTR( fic );
Packit 1c1d7e
	}
Packit 1c1d7e
	*fic = *fi.fic;
Packit 1c1d7e
    }
Packit 1c1d7e
    cache = fi.cache;
Packit 1c1d7e
    return *this;
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Sets the file to obtain information about.
Packit 1c1d7e
Packit 1c1d7e
  The string given can be an absolute or a relative file path. Absolute file
Packit 1c1d7e
  paths begin with the directory separator (e.g. '/' under UNIX) or a drive
Packit 1c1d7e
  specification (not applicable to UNIX). Relative file names begin with a
Packit 1c1d7e
  directory name or a file name and specify a path relative to the current
Packit 1c1d7e
  directory.
Packit 1c1d7e
Packit 1c1d7e
  Example:
Packit 1c1d7e
  \code
Packit 1c1d7e
    #include <qfileinfo.h>
Packit 1c1d7e
    #include <qdir.h>
Packit 1c1d7e
Packit 1c1d7e
    void test()
Packit 1c1d7e
    {
Packit 1c1d7e
	QString absolute = "/liver/aorta";
Packit 1c1d7e
	QString relative = "liver/aorta";
Packit 1c1d7e
	QFileInfo fi1( absolute );
Packit 1c1d7e
	QFileInfo fi2( relative );
Packit 1c1d7e
Packit 1c1d7e
	QDir::setCurrent( QDir::rootDirPath() );
Packit 1c1d7e
				// fi1 and fi2 now point to the same file
Packit 1c1d7e
Packit 1c1d7e
	QDir::setCurrent( "/tmp" );
Packit 1c1d7e
				// fi1 now points to "/liver/aorta",
Packit 1c1d7e
				// while fi2 points to "/tmp/liver/aorta"
Packit 1c1d7e
    }
Packit 1c1d7e
  \endcode
Packit 1c1d7e
Packit 1c1d7e
  \sa isRelative(), QDir::setCurrent(), QDir::isRelativePath()
Packit 1c1d7e
*/
Packit 1c1d7e
Packit 1c1d7e
void QFileInfo::setFile( const QString &file )
Packit 1c1d7e
{
Packit 1c1d7e
    fn = file;
Packit 1c1d7e
    slashify( fn );
Packit 1c1d7e
    delete fic;
Packit 1c1d7e
    fic = 0;
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Sets the file to obtain information about.
Packit 1c1d7e
Packit 1c1d7e
  If the file has a relative path, the QFileInfo will also have one.
Packit 1c1d7e
Packit 1c1d7e
  \sa isRelative()
Packit 1c1d7e
*/
Packit 1c1d7e
Packit 1c1d7e
void QFileInfo::setFile( const QFile &file )
Packit 1c1d7e
{
Packit 1c1d7e
    fn	= file.name();
Packit 1c1d7e
    slashify( fn );
Packit 1c1d7e
    delete fic;
Packit 1c1d7e
    fic = 0;
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Sets the file to obtains information about to \e fileName in the
Packit 1c1d7e
  directory \e d.
Packit 1c1d7e
Packit 1c1d7e
  If the directory has a relative path, the QFileInfo will also have one.
Packit 1c1d7e
Packit 1c1d7e
  \sa isRelative()
Packit 1c1d7e
*/
Packit 1c1d7e
#ifndef QT_NO_DIR
Packit 1c1d7e
void QFileInfo::setFile( const QDir &d, const QString &fileName )
Packit 1c1d7e
{
Packit 1c1d7e
    fn	= d.filePath( fileName );
Packit 1c1d7e
    slashify( fn );
Packit 1c1d7e
    delete fic;
Packit 1c1d7e
    fic = 0;
Packit 1c1d7e
}
Packit 1c1d7e
#endif
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Returns TRUE if the file pointed to exists, otherwise FALSE.
Packit 1c1d7e
*/
Packit 1c1d7e
Packit 1c1d7e
bool QFileInfo::exists() const
Packit 1c1d7e
{
Packit 1c1d7e
    return qt_file_access( fn, F_OK );
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Refresh the information about the file, i.e. read in information from the
Packit 1c1d7e
  file system the next time a cached property is fetched.
Packit 1c1d7e
Packit 1c1d7e
  \sa setCaching()
Packit 1c1d7e
*/
Packit 1c1d7e
Packit 1c1d7e
void QFileInfo::refresh() const
Packit 1c1d7e
{
Packit 1c1d7e
    QFileInfo *that = (QFileInfo*)this;		// Mutable function
Packit 1c1d7e
    delete that->fic;
Packit 1c1d7e
    that->fic = 0;
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  \fn bool QFileInfo::caching() const
Packit 1c1d7e
  Returns TRUE if caching is enabled.
Packit 1c1d7e
  \sa setCaching(), refresh()
Packit 1c1d7e
*/
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Enables caching of file information if \e enable is TRUE, or disables it
Packit 1c1d7e
  if \e enable is FALSE.
Packit 1c1d7e
Packit 1c1d7e
  When caching is enabled, QFileInfo reads the file information the first
Packit 1c1d7e
  time
Packit 1c1d7e
Packit 1c1d7e
  Caching is enabled by default.
Packit 1c1d7e
Packit 1c1d7e
  \sa refresh(), caching()
Packit 1c1d7e
*/
Packit 1c1d7e
Packit 1c1d7e
void QFileInfo::setCaching( bool enable )
Packit 1c1d7e
{
Packit 1c1d7e
    if ( cache == enable )
Packit 1c1d7e
	return;
Packit 1c1d7e
    cache = enable;
Packit 1c1d7e
    if ( cache ) {
Packit 1c1d7e
	delete fic;
Packit 1c1d7e
	fic = 0;
Packit 1c1d7e
    }
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Returns the name, i.e. the file name including the path (which can be
Packit 1c1d7e
  absolute or relative).
Packit 1c1d7e
Packit 1c1d7e
  \sa isRelative(), absFilePath()
Packit 1c1d7e
*/
Packit 1c1d7e
Packit 1c1d7e
QString QFileInfo::filePath() const
Packit 1c1d7e
{
Packit 1c1d7e
    return fn;
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Returns the base name of the file.
Packit 1c1d7e
Packit 1c1d7e
  The base name consists of all characters in the file name up to (but not
Packit 1c1d7e
  including) the first '.' character.  The path is not included.
Packit 1c1d7e
Packit 1c1d7e
  Example:
Packit 1c1d7e
  \code
Packit 1c1d7e
     QFileInfo fi( "/tmp/abdomen.lower" );
Packit 1c1d7e
     QString base = fi.baseName();		// base = "abdomen"
Packit 1c1d7e
  \endcode
Packit 1c1d7e
Packit 1c1d7e
  \sa fileName(), extension()
Packit 1c1d7e
*/
Packit 1c1d7e
Packit 1c1d7e
QString QFileInfo::baseName() const
Packit 1c1d7e
{
Packit 1c1d7e
    QString tmp = fileName();
Packit 1c1d7e
    int pos = tmp.find( '.' );
Packit 1c1d7e
    if ( pos == -1 )
Packit 1c1d7e
	return tmp;
Packit 1c1d7e
    else
Packit 1c1d7e
	return tmp.left( pos );
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Returns the extension name of the file.
Packit 1c1d7e
Packit 1c1d7e
  If \a complete is TRUE (the default), extension() returns the string
Packit 1c1d7e
  of all characters in the file name after (but not including) the
Packit 1c1d7e
  first '.'  character.  For a file named "archive.tar.gz" this
Packit 1c1d7e
  returns "tar.gz".
Packit 1c1d7e
Packit 1c1d7e
  If \a complete is FALSE, extension() returns the string of all
Packit 1c1d7e
  characters in the file name after (but not including) the last '.'
Packit 1c1d7e
  character.  For a file named "archive.tar.gz" this returns "gz".
Packit 1c1d7e
Packit 1c1d7e
  Example:
Packit 1c1d7e
  \code
Packit 1c1d7e
     QFileInfo fi( "lex.yy.c" );
Packit 1c1d7e
     QString ext = fi.extension();		// ext = "yy.c"
Packit 1c1d7e
     QString ext = fi.extension( FALSE );	// ext = "c"
Packit 1c1d7e
  \endcode
Packit 1c1d7e
Packit 1c1d7e
  \sa fileName(), baseName()
Packit 1c1d7e
Packit 1c1d7e
*/
Packit 1c1d7e
Packit 1c1d7e
QString QFileInfo::extension( bool complete ) const
Packit 1c1d7e
{
Packit 1c1d7e
    QString s = fileName();
Packit 1c1d7e
    int pos = complete ? s.find( '.' ) : s.findRev( '.' );
Packit 1c1d7e
    if ( pos < 0 )
Packit 1c1d7e
	return QString::fromLatin1( "" );
Packit 1c1d7e
    else
Packit 1c1d7e
	return s.right( s.length() - pos - 1 );
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Returns the directory path of the file.
Packit 1c1d7e
Packit 1c1d7e
  If the QFileInfo is relative and \e absPath is FALSE, the QDir will be
Packit 1c1d7e
  relative, otherwise it will be absolute.
Packit 1c1d7e
Packit 1c1d7e
  \sa dirPath(), filePath(), fileName(), isRelative()
Packit 1c1d7e
*/
Packit 1c1d7e
#ifndef QT_NO_DIR
Packit 1c1d7e
QDir QFileInfo::dir( bool absPath ) const
Packit 1c1d7e
{
Packit 1c1d7e
    return QDir( dirPath(absPath) );
Packit 1c1d7e
}
Packit 1c1d7e
#endif
Packit 1c1d7e
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Returns TRUE if the file is readable.
Packit 1c1d7e
  \sa isWritable(), isExecutable(), permission()
Packit 1c1d7e
*/
Packit 1c1d7e
Packit 1c1d7e
bool QFileInfo::isReadable() const
Packit 1c1d7e
{
Packit 1c1d7e
    return qt_file_access( fn, R_OK );
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Returns TRUE if the file is writable.
Packit 1c1d7e
  \sa isReadable(), isExecutable(), permission()
Packit 1c1d7e
*/
Packit 1c1d7e
Packit 1c1d7e
bool QFileInfo::isWritable() const
Packit 1c1d7e
{
Packit 1c1d7e
    return qt_file_access( fn, W_OK );
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Returns TRUE if the file is executable.
Packit 1c1d7e
  \sa isReadable(), isWritable(), permission()
Packit 1c1d7e
*/
Packit 1c1d7e
Packit 1c1d7e
bool QFileInfo::isExecutable() const
Packit 1c1d7e
{
Packit 1c1d7e
    return qt_file_access( fn, X_OK );
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Returns TRUE if the file path name is relative to the current directory,
Packit 1c1d7e
  FALSE if the path is absolute (e.g. under UNIX a path is relative if it
Packit 1c1d7e
  does not start with a '/').
Packit 1c1d7e
Packit 1c1d7e
  According to Einstein this function should always return TRUE.
Packit 1c1d7e
*/
Packit 1c1d7e
#ifndef QT_NO_DIR
Packit 1c1d7e
bool QFileInfo::isRelative() const
Packit 1c1d7e
{
Packit 1c1d7e
    return QDir::isRelativePath( fn );
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
/*!
Packit 1c1d7e
  Converts the file path name to an absolute path.
Packit 1c1d7e
Packit 1c1d7e
  If it is already absolute nothing is done.
Packit 1c1d7e
Packit 1c1d7e
  \sa filePath(), isRelative()
Packit 1c1d7e
*/
Packit 1c1d7e
Packit 1c1d7e
bool QFileInfo::convertToAbs()
Packit 1c1d7e
{
Packit 1c1d7e
    if ( isRelative() )
Packit 1c1d7e
	fn = absFilePath();
Packit 1c1d7e
    return QDir::isRelativePath( fn );
Packit 1c1d7e
}
Packit 1c1d7e
#endif