00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "qplatformdefs.h"
00039
00040
00041 #if defined(open)
00042 # undef open
00043 #endif
00044
00045
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;
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 )
00284 fflush( fh );
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 )
00331 return 0;
00332 #if defined(QT_CHECK_STATE)
00333 Q_CHECK_PTR( p );
00334 if ( !isOpen() ) {
00335 qWarning( "QFile::readLine: File not open" );
00336 return -1;
00337 }
00338 if ( !isReadable() ) {
00339 qWarning( "QFile::readLine: Read operation not permitted" );
00340 return -1;
00341 }
00342 #endif
00343 Q_LONG nread;
00344 if ( isRaw() ) {
00345 nread = QIODevice::readLine( p, maxlen );
00346 } else {
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() ) {
00405 qWarning( "QFile::getch: File not open" );
00406 return EOF;
00407 }
00408 if ( !isReadable() ) {
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() ) {
00424 char buf[1];
00425 ch = readBlock( buf, 1 ) == 1 ? buf[0] : EOF;
00426 } else {
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() ) {
00448 qWarning( "QFile::putch: File not open" );
00449 return EOF;
00450 }
00451 if ( !isWritable() ) {
00452 qWarning( "QFile::putch: Write operation not permitted" );
00453 return EOF;
00454 }
00455 #endif
00456 if ( isRaw() ) {
00457 char buf[1];
00458 buf[0] = ch;
00459 ch = writeBlock( buf, 1 ) == 1 ? ch : EOF;
00460 } else {
00461 if ( (ch = putc( ch, fh )) != EOF ) {
00462 if ( !isSequentialAccess() )
00463 ioIndex++;
00464 if ( ioIndex > 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() ) {
00488 qWarning( "QFile::ungetch: File not open" );
00489 return EOF;
00490 }
00491 if ( !isReadable() ) {
00492 qWarning( "QFile::ungetch: Read operation not permitted" );
00493 return EOF;
00494 }
00495 #endif
00496 if ( ch == EOF )
00497 return ch;
00498
00499 if ( isSequentialAccess() && !fh) {
00500
00501 ungetchBuffer +=ch;
00502 return ch;
00503 }
00504
00505 if ( isRaw() ) {
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 {
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