00001 /**************************************************************************** 00002 ** $Id: qfileinfo.cpp,v 1.2 2003/07/10 02:40:11 llornkcor Exp $ 00003 ** 00004 ** Implementation of QFileInfo class 00005 ** 00006 ** Created : 950628 00007 ** 00008 ** Copyright (C) 1992-2002 Trolltech AS. All rights reserved. 00009 ** 00010 ** This file is part of the tools module of the Qt GUI Toolkit. 00011 ** 00012 ** This file may be distributed under the terms of the Q Public License 00013 ** as defined by Trolltech AS of Norway and appearing in the file 00014 ** LICENSE.QPL included in the packaging of this file. 00015 ** 00016 ** This file may be distributed and/or modified under the terms of the 00017 ** GNU General Public License version 2 as published by the Free Software 00018 ** Foundation and appearing in the file LICENSE.GPL included in the 00019 ** packaging of this file. 00020 ** 00021 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition 00022 ** licenses may use this file in accordance with the Qt Commercial License 00023 ** Agreement provided with the Software. 00024 ** 00025 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00026 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00027 ** 00028 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for 00029 ** information about Qt Commercial License Agreements. 00030 ** See http://www.trolltech.com/qpl/ for QPL licensing information. 00031 ** See http://www.trolltech.com/gpl/ for GPL licensing information. 00032 ** 00033 ** Contact info@trolltech.com if any conditions of this licensing are 00034 ** not clear to you. 00035 ** 00036 **********************************************************************/ 00037 00038 #include "qplatformdefs.h" 00039 00040 #include "qfileinfo.h" 00041 #include "qdatetime.h" 00042 #include "qdir.h" 00043 #include "qfiledefs_p.h" 00044 #if defined(QT_LARGEFILE_SUPPORT) && !defined(QT_ABI_QT4) 00045 #include <limits.h> 00046 #endif 00047 00048 00049 extern bool qt_file_access( const QString& fn, int t ); 00050 00126 QFileInfo::QFileInfo() 00127 { 00128 fic = 0; 00129 cache = TRUE; 00130 #if defined(Q_OS_UNIX) 00131 symLink = FALSE; 00132 #endif 00133 } 00134 00142 QFileInfo::QFileInfo( const QString &file ) 00143 { 00144 fn = file; 00145 slashify( fn ); 00146 fic = 0; 00147 cache = TRUE; 00148 #if defined(Q_OS_UNIX) 00149 symLink = FALSE; 00150 #endif 00151 } 00152 00163 QFileInfo::QFileInfo( const QFile &file ) 00164 { 00165 fn = file.name(); 00166 slashify( fn ); 00167 fic = 0; 00168 cache = TRUE; 00169 #if defined(Q_OS_UNIX) 00170 symLink = FALSE; 00171 #endif 00172 } 00173 00183 #ifndef QT_NO_DIR 00184 QFileInfo::QFileInfo( const QDir &d, const QString &fileName ) 00185 { 00186 fn = d.filePath( fileName ); 00187 slashify( fn ); 00188 fic = 0; 00189 cache = TRUE; 00190 #if defined(Q_OS_UNIX) 00191 symLink = FALSE; 00192 #endif 00193 } 00194 #endif 00195 00199 QFileInfo::QFileInfo( const QFileInfo &fi ) 00200 { 00201 fn = fi.fn; 00202 if ( fi.fic ) { 00203 fic = new QFileInfoCache; 00204 *fic = *fi.fic; 00205 } else { 00206 fic = 0; 00207 } 00208 cache = fi.cache; 00209 #if defined(Q_OS_UNIX) 00210 symLink = fi.symLink; 00211 #endif 00212 } 00213 00218 QFileInfo::~QFileInfo() 00219 { 00220 delete fic; 00221 } 00222 00223 00228 QFileInfo &QFileInfo::operator=( const QFileInfo &fi ) 00229 { 00230 fn = fi.fn; 00231 if ( !fi.fic ) { 00232 delete fic; 00233 fic = 0; 00234 } else { 00235 if ( !fic ) { 00236 fic = new QFileInfoCache; 00237 Q_CHECK_PTR( fic ); 00238 } 00239 *fic = *fi.fic; 00240 } 00241 cache = fi.cache; 00242 #if defined(Q_OS_UNIX) 00243 symLink = fi.symLink; 00244 #endif 00245 return *this; 00246 } 00247 00248 00277 void QFileInfo::setFile( const QString &file ) 00278 { 00279 fn = file; 00280 slashify( fn ); 00281 delete fic; 00282 fic = 0; 00283 } 00284 00297 void QFileInfo::setFile( const QFile &file ) 00298 { 00299 fn = file.name(); 00300 slashify( fn ); 00301 delete fic; 00302 fic = 0; 00303 } 00304 00316 #ifndef QT_NO_DIR 00317 void QFileInfo::setFile( const QDir &d, const QString &fileName ) 00318 { 00319 fn = d.filePath( fileName ); 00320 slashify( fn ); 00321 delete fic; 00322 fic = 0; 00323 } 00324 #endif 00325 00330 bool QFileInfo::exists() const 00331 { 00332 return qt_file_access( fn, F_OK ); 00333 } 00334 00342 void QFileInfo::refresh() const 00343 { 00344 QFileInfo *that = (QFileInfo*)this; // Mutable function 00345 delete that->fic; 00346 that->fic = 0; 00347 } 00348 00370 void QFileInfo::setCaching( bool enable ) 00371 { 00372 if ( cache == enable ) 00373 return; 00374 cache = enable; 00375 if ( cache ) { 00376 delete fic; 00377 fic = 0; 00378 } 00379 } 00380 00381 00389 QString QFileInfo::filePath() const 00390 { 00391 return fn; 00392 } 00393 00416 QString QFileInfo::baseName( bool complete ) const 00417 { 00418 QString tmp = fileName(); 00419 int pos = complete ? tmp.findRev( '.' ) : tmp.find( '.' ); 00420 if ( pos == -1 ) 00421 return tmp; 00422 else 00423 return tmp.left( pos ); 00424 } 00425 00447 QString QFileInfo::extension( bool complete ) const 00448 { 00449 QString s = fileName(); 00450 int pos = complete ? s.find( '.' ) : s.findRev( '.' ); 00451 if ( pos < 0 ) 00452 return QString::fromLatin1( "" ); 00453 else 00454 return s.right( s.length() - pos - 1 ); 00455 } 00456 00465 #ifndef QT_NO_DIR 00466 QDir QFileInfo::dir( bool absPath ) const 00467 { 00468 return QDir( dirPath(absPath) ); 00469 } 00470 #endif 00471 00472 00479 bool QFileInfo::isReadable() const 00480 { 00481 return qt_file_access( fn, R_OK ) && permission( ReadUser ); 00482 } 00483 00490 bool QFileInfo::isWritable() const 00491 { 00492 return qt_file_access( fn, W_OK ) && permission( WriteUser ); 00493 } 00494 00501 bool QFileInfo::isExecutable() const 00502 { 00503 return qt_file_access( fn, X_OK ) && permission( ExeUser ); 00504 } 00505 00506 #ifndef Q_WS_WIN 00507 bool QFileInfo::isHidden() const 00508 { 00509 return fileName()[ 0 ] == QChar( '.' ); 00510 } 00511 #endif 00512 00518 #ifndef QT_NO_DIR 00519 bool QFileInfo::isRelative() const 00520 { 00521 return QDir::isRelativePath( fn ); 00522 } 00523 00532 bool QFileInfo::convertToAbs() 00533 { 00534 if ( isRelative() ) 00535 fn = absFilePath(); 00536 return QDir::isRelativePath( fn ); 00537 } 00538 #endif 00539 00544 #if defined(QT_ABI_QT4) 00545 QIODevice::Offset QFileInfo::size() const 00546 #else 00547 uint QFileInfo::size() const 00548 #endif 00549 { 00550 if ( !fic || !cache ) 00551 doStat(); 00552 if ( fic ) 00553 #if defined(QT_ABI_QT4) 00554 return (QIODevice::Offset)fic->st.st_size; 00555 #elif defined(QT_LARGEFILE_SUPPORT) 00556 return (uint)fic->st.st_size > UINT_MAX ? UINT_MAX : (uint)fic->st.st_size; 00557 #else 00558 return (uint)fic->st.st_size; 00559 #endif 00560 else 00561 return 0; 00562 } 00563 00573 QDateTime QFileInfo::created() const 00574 { 00575 QDateTime dt; 00576 if ( !fic || !cache ) 00577 doStat(); 00578 if ( fic && fic->st.st_ctime != 0 ) { 00579 dt.setTime_t( fic->st.st_ctime ); 00580 return dt; 00581 } else { 00582 return lastModified(); 00583 } 00584 } 00585 00592 QDateTime QFileInfo::lastModified() const 00593 { 00594 QDateTime dt; 00595 if ( !fic || !cache ) 00596 doStat(); 00597 if ( fic ) 00598 dt.setTime_t( fic->st.st_mtime ); 00599 return dt; 00600 } 00601 00611 QDateTime QFileInfo::lastRead() const 00612 { 00613 QDateTime dt; 00614 if ( !fic || !cache ) 00615 doStat(); 00616 if ( fic && fic->st.st_atime != 0 ) { 00617 dt.setTime_t( fic->st.st_atime ); 00618 return dt; 00619 } else { 00620 return lastModified(); 00621 } 00622 } 00623 00624 #ifndef QT_NO_DIR 00625 00645 QString QFileInfo::absFilePath() const 00646 { 00647 QString tmp; 00648 if ( QDir::isRelativePath(fn) 00649 #if defined(Q_OS_WIN32) 00650 && fn[1] != ':' 00651 #endif 00652 ) { 00653 tmp = QDir::currentDirPath(); 00654 tmp += '/'; 00655 } 00656 tmp += fn; 00657 makeAbs( tmp ); 00658 return QDir::cleanDirPath( tmp ); 00659 } 00660 00661 #endif
1.4.2