Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

qfile.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** $Id: qfile.cpp,v 1.2 2003/07/10 02:40:11 llornkcor Exp $
00003 **
00004 ** Implementation of QFile class
00005 **
00006 ** Created : 930812
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 // POSIX Large File Support redefines open -> open64
00041 #if defined(open)
00042 # undef open
00043 #endif
00044 
00045 // POSIX Large File Support redefines truncate -> truncate64
00046 #if defined(truncate)
00047 # undef truncate
00048 #endif
00049 
00050 #include "qfile.h"
00051 
00052 
00053 extern bool qt_file_access( const QString& fn, int t );
00054 
00138 QFile::QFile()
00139 {
00140     init();
00141 }
00142 
00149 QFile::QFile( const QString &name )
00150     : fn(name)
00151 {
00152     init();
00153 }
00154 
00155 
00160 QFile::~QFile()
00161 {
00162     close();
00163 }
00164 
00165 
00171 void QFile::init()
00172 {
00173     setFlags( IO_Direct );
00174     setStatus( IO_Ok );
00175     fh     = 0;
00176     fd     = 0;
00177     length = 0;
00178     ioIndex = 0;
00179     ext_f  = FALSE;                             // not an external file handle
00180 }
00181 
00182 
00216 void QFile::setName( const QString &name )
00217 {
00218     if ( isOpen() ) {
00219 #if defined(QT_CHECK_STATE)
00220         qWarning( "QFile::setName: File is open" );
00221 #endif
00222         close();
00223     }
00224     fn = name;
00225 }
00226 
00235 bool QFile::exists() const
00236 {
00237     return qt_file_access( fn, F_OK );
00238 }
00239 
00245 bool QFile::exists( const QString &fileName )
00246 {
00247     return qt_file_access( fileName, F_OK );
00248 }
00249 
00250 
00258 bool QFile::remove()
00259 {
00260     close();
00261     return remove( fn );
00262 }
00263 
00264 #if defined(Q_OS_MAC) || defined(Q_OS_MSDOS) || defined(Q_OS_WIN32) || defined(Q_OS_OS2)
00265 # define HAS_TEXT_FILEMODE                      // has translate/text filemode
00266 #endif
00267 #if defined(O_NONBLOCK)
00268 # define HAS_ASYNC_FILEMODE
00269 # define OPEN_ASYNC O_NONBLOCK
00270 #elif defined(O_NDELAY)
00271 # define HAS_ASYNC_FILEMODE
00272 # define OPEN_ASYNC O_NDELAY
00273 #endif
00274 
00281 void QFile::flush()
00282 {
00283     if ( isOpen() && fh )                       // can only flush open/buffered
00284         fflush( fh );                           //   file
00285 }
00286 
00298 bool QFile::atEnd() const
00299 {
00300     if ( !isOpen() ) {
00301 #if defined(QT_CHECK_STATE)
00302         qWarning( "QFile::atEnd: File is not open" );
00303 #endif
00304         return FALSE;
00305     }
00306     if ( isDirectAccess() && !isTranslated() ) {
00307         if ( at() < length )
00308             return FALSE;
00309     }
00310     return QIODevice::atEnd();
00311 }
00312 
00328 Q_LONG QFile::readLine( char *p, Q_ULONG maxlen )
00329 {
00330     if ( maxlen == 0 )                          // application bug?
00331         return 0;
00332 #if defined(QT_CHECK_STATE)
00333     Q_CHECK_PTR( p );
00334     if ( !isOpen() ) {                          // file not open
00335         qWarning( "QFile::readLine: File not open" );
00336         return -1;
00337     }
00338     if ( !isReadable() ) {                      // reading not permitted
00339         qWarning( "QFile::readLine: Read operation not permitted" );
00340         return -1;
00341     }
00342 #endif
00343     Q_LONG nread;                               // number of bytes read
00344     if ( isRaw() ) {                            // raw file
00345         nread = QIODevice::readLine( p, maxlen );
00346     } else {                                    // buffered file
00347         p = fgets( p, maxlen, fh );
00348         if ( p ) {
00349             nread = qstrlen( p );
00350             if ( !isSequentialAccess() )
00351                 ioIndex += nread;
00352         } else {
00353             nread = -1;
00354             setStatus(IO_ReadError);
00355         }
00356     }
00357     return nread;
00358 }
00359 
00360 
00380 Q_LONG QFile::readLine( QString& s, Q_ULONG maxlen )
00381 {
00382     QByteArray ba(maxlen);
00383     Q_LONG l = readLine(ba.data(),maxlen);
00384     if ( l >= 0 ) {
00385         ba.truncate(l);
00386         s = QString(ba);
00387     }
00388     return l;
00389 }
00390 
00391 
00401 int QFile::getch()
00402 {
00403 #if defined(QT_CHECK_STATE)
00404     if ( !isOpen() ) {                          // file not open
00405         qWarning( "QFile::getch: File not open" );
00406         return EOF;
00407     }
00408     if ( !isReadable() ) {                      // reading not permitted
00409         qWarning( "QFile::getch: Read operation not permitted" );
00410         return EOF;
00411     }
00412 #endif
00413 
00414     int ch;
00415 
00416     if ( !ungetchBuffer.isEmpty() ) {
00417         int len = ungetchBuffer.length();
00418         ch = ungetchBuffer[ len-1 ];
00419         ungetchBuffer.truncate( len - 1 );
00420         return ch;
00421     }
00422 
00423     if ( isRaw() ) {                            // raw file (inefficient)
00424         char buf[1];
00425         ch = readBlock( buf, 1 ) == 1 ? buf[0] : EOF;
00426     } else {                                    // buffered file
00427         if ( (ch = getc( fh )) != EOF )
00428             if ( !isSequentialAccess() )
00429                 ioIndex++;
00430         else
00431             setStatus(IO_ReadError);
00432     }
00433     return ch;
00434 }
00435 
00444 int QFile::putch( int ch )
00445 {
00446 #if defined(QT_CHECK_STATE)
00447     if ( !isOpen() ) {                          // file not open
00448         qWarning( "QFile::putch: File not open" );
00449         return EOF;
00450     }
00451     if ( !isWritable() ) {                      // writing not permitted
00452         qWarning( "QFile::putch: Write operation not permitted" );
00453         return EOF;
00454     }
00455 #endif
00456     if ( isRaw() ) {                            // raw file (inefficient)
00457         char buf[1];
00458         buf[0] = ch;
00459         ch = writeBlock( buf, 1 ) == 1 ? ch : EOF;
00460     } else {                                    // buffered file
00461         if ( (ch = putc( ch, fh )) != EOF ) {
00462             if ( !isSequentialAccess() )
00463                 ioIndex++;
00464             if ( ioIndex > length )             // update file length
00465                 length = ioIndex;
00466         } else {
00467             setStatus(IO_WriteError);
00468         }
00469     }
00470     return ch;
00471 }
00472 
00484 int QFile::ungetch( int ch )
00485 {
00486 #if defined(QT_CHECK_STATE)
00487     if ( !isOpen() ) {                          // file not open
00488         qWarning( "QFile::ungetch: File not open" );
00489         return EOF;
00490     }
00491     if ( !isReadable() ) {                      // reading not permitted
00492         qWarning( "QFile::ungetch: Read operation not permitted" );
00493         return EOF;
00494     }
00495 #endif
00496     if ( ch == EOF )                            // cannot unget EOF
00497         return ch;
00498 
00499     if ( isSequentialAccess() && !fh) {
00500         // pipe or similar => we cannot ungetch, so do it manually
00501         ungetchBuffer +=ch;
00502         return ch;
00503     }
00504 
00505     if ( isRaw() ) {                            // raw file (very inefficient)
00506         char buf[1];
00507         at( ioIndex-1 );
00508         buf[0] = ch;
00509         if ( writeBlock(buf, 1) == 1 )
00510             at ( ioIndex-1 );
00511         else
00512             ch = EOF;
00513     } else {                                    // buffered file
00514         if ( (ch = ungetc(ch, fh)) != EOF )
00515             if ( !isSequentialAccess() )
00516                 ioIndex--;
00517         else
00518             setStatus( IO_ReadError );
00519     }
00520     return ch;
00521 }
00522 
00523 
00524 static QCString locale_encoder( const QString &fileName )
00525 {
00526     return fileName.local8Bit();
00527 }
00528 
00529 
00530 static QFile::EncoderFn encoder = locale_encoder;
00531 
00554 QCString QFile::encodeName( const QString &fileName )
00555 {
00556     return (*encoder)(fileName);
00557 }
00558 
00573 void QFile::setEncodingFunction( EncoderFn f )
00574 {
00575     encoder = f;
00576 }
00577 
00578 static
00579 QString locale_decoder( const QCString &localFileName )
00580 {
00581     return QString::fromLocal8Bit(localFileName);
00582 }
00583 
00584 static QFile::DecoderFn decoder = locale_decoder;
00585 
00591 QString QFile::decodeName( const QCString &localFileName )
00592 {
00593     return (*decoder)(localFileName);
00594 }
00595 
00611 void QFile::setDecodingFunction( DecoderFn f )
00612 {
00613     decoder = f;
00614 }
00615 

Generated on Sat Nov 5 16:18:30 2005 for OPIE by  doxygen 1.4.2