Public Types | Public Member Functions | Static Public Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
QFile Class Reference

The QFile class is an I/O device that operates on files. More...

#include <qfile.h>

Inheritance diagram for QFile:
QIODevice

Public Types

typedef QCString(* EncoderFn) (const QString &fileName)
 
typedef QString(* DecoderFn) (const QCString &localfileName)
 

Public Member Functions

 QFile ()
 
 QFile (const QString &name)
 
virtual ~QFile ()
 
QString name () const
 
void setName (const QString &name)
 
bool exists () const
 
bool remove ()
 
bool open (int)
 
bool open (int, FILE *)
 
bool open (int, int)
 
void close ()
 
void flush ()
 
uint size () const
 
int at () const
 
bool at (int)
 
bool atEnd () const
 
int readBlock (char *data, uint len)
 
int writeBlock (const char *data, uint len)
 
int writeBlock (const QByteArray &data)
 
int readLine (char *data, uint maxlen)
 
int readLine (QString &, uint maxlen)
 
int getch ()
 
int putch (int)
 
int ungetch (int)
 
int handle () const
 
int64 pos () const
 
int64 toEnd ()
 
bool seek (int64 pos)
 
- Public Member Functions inherited from QIODevice
 QIODevice ()
 
virtual ~QIODevice ()
 
int flags () const
 
int mode () const
 
int state () const
 
bool isDirectAccess () const
 
bool isSequentialAccess () const
 
bool isCombinedAccess () const
 
bool isBuffered () const
 
bool isRaw () const
 
bool isSynchronous () const
 
bool isAsynchronous () const
 
bool isTranslated () const
 
bool isReadable () const
 
bool isWritable () const
 
bool isReadWrite () const
 
bool isInactive () const
 
bool isOpen () const
 
int status () const
 
void resetStatus ()
 
bool reset ()
 
int writeBlock (const QByteArray &data)
 
QByteArray readAll ()
 

Static Public Member Functions

static QCString encodeName (const QString &fileName)
 
static QString decodeName (const QCString &localFileName)
 
static void setEncodingFunction (EncoderFn)
 
static void setDecodingFunction (DecoderFn)
 
static bool exists (const QString &fileName)
 
static bool remove (const QString &fileName)
 

Protected Attributes

QString fn
 
FILE * fh
 
int fd
 
int length
 
bool ext_f
 
void * d
 
- Protected Attributes inherited from QIODevice
int ioIndex
 

Private Member Functions

void init ()
 
 QFile (const QFile &)
 
QFileoperator= (const QFile &)
 

Private Attributes

QCString ungetchBuffer
 

Additional Inherited Members

- Protected Member Functions inherited from QIODevice
void setFlags (int f)
 
void setType (int)
 
void setMode (int)
 
void setState (int)
 
void setStatus (int)
 

Detailed Description

The QFile class is an I/O device that operates on files.

QFile is an I/O device for reading and writing binary and text files. A QFile may be used by itself (readBlock and writeBlock) or by more conveniently using QDataStream or QTextStream.

Here is a code fragment that uses QTextStream to read a text file line by line. It prints each line with a line number.

QFile f("file.txt");
if ( f.open(IO_ReadOnly) ) { // file opened successfully
QTextStream t( &f ); // use a text stream
int n = 1;
while ( !t.eof() ) { // until end of file...
s = t.readLine(); // line of text excluding '\n'
printf( "%3d: %s\n", n++, (const char *)s );
}
f.close();
}

The QFileInfo class holds detailed information about a file, such as access permissions, file dates and file types.

The QDir class manages directories and lists of file names.

See also
QDataStream, QTextStream

Definition at line 50 of file qfile.h.

Member Typedef Documentation

typedef QString(* QFile::DecoderFn) (const QCString &localfileName)

Definition at line 61 of file qfile.h.

typedef QCString(* QFile::EncoderFn) (const QString &fileName)

Definition at line 60 of file qfile.h.

Constructor & Destructor Documentation

QFile::QFile ( )

Constructs a QFile with no name.

Definition at line 92 of file qfile.cpp.

93 {
94  init();
95 }
void init()
Definition: qfile.cpp:124
QFile::QFile ( const QString name)

Constructs a QFile with a file name name.

See also
setName()

Definition at line 102 of file qfile.cpp.

103  : fn(name)
104 {
105  init();
106 }
void init()
Definition: qfile.cpp:124
QString fn
Definition: qfile.h:102
QFile::~QFile ( )
virtual

Destructs a QFile. Calls close().

Definition at line 113 of file qfile.cpp.

114 {
115  close();
116 }
void close()
Definition: qfile_unix.cpp:614
QFile::QFile ( const QFile )
private

Member Function Documentation

int QFile::at ( ) const
inlinevirtual

Returns the file index.

See also
size()

Reimplemented from QIODevice.

Definition at line 124 of file qfile.h.

125 { return ioIndex; }
int ioIndex
Definition: qiodevice.h:141
bool QFile::at ( int  pos)
virtual

Sets the file index to pos. Returns TRUE if successful, otherwise FALSE.

Example:

QFile f( "data.bin" );
f.open( IO_ReadOnly ); // index set to 0
f.at( 100 ); // set index to 100
f.at( f.at()+50 ); // set index to 150
f.at( f.size()-80 ); // set index to 80 before EOF
f.close();
Warning
The result is undefined if the file was opened using the IO_Append specifier.
See also
size(), open()

Reimplemented from QIODevice.

Definition at line 434 of file qfile_unix.cpp.

435 {
436  if ( !isOpen() ) {
437 #if defined(CHECK_STATE)
438  qWarning( "QFile::at: File is not open" );
439 #endif
440  return FALSE;
441  }
442  bool ok;
443  if ( isRaw() ) { // raw file
444  pos = (int)LSEEK(fd, pos, SEEK_SET);
445  ok = pos != -1;
446  } else { // buffered file
447  ok = fseek(fh, pos, SEEK_SET) == 0;
448  }
449  if ( ok )
450  ioIndex = pos;
451 #if defined(CHECK_RANGE)
452  else
453  qWarning( "QFile::at: Cannot set file position %d", pos );
454 #endif
455  return ok;
456 }
bool isRaw() const
Definition: qiodevice.h:102
int64 pos() const
Definition: qfile_unix.cpp:637
const bool FALSE
Definition: qglobal.h:370
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
int fd
Definition: qfile.h:104
#define LSEEK
Definition: qfiledefs_p.h:213
FILE * fh
Definition: qfile.h:103
bool isOpen() const
Definition: qiodevice.h:110
int ioIndex
Definition: qiodevice.h:141
bool QFile::atEnd ( ) const
virtual

Returns TRUE if the end of file has been reached, otherwise FALSE.

See also
size()

Reimplemented from QIODevice.

Definition at line 239 of file qfile.cpp.

240 {
241  if ( !isOpen() ) {
242 #if defined(CHECK_STATE)
243  qWarning( "QFile::atEnd: File is not open" );
244 #endif
245  return FALSE;
246  }
247  if ( isDirectAccess() && !isTranslated() ) {
248  if ( at() < length )
249  return FALSE;
250  }
251  return QIODevice::atEnd();
252 }
int length
Definition: qfile.h:105
int at() const
Definition: qfile.h:124
virtual bool atEnd() const
Definition: qiodevice.cpp:498
const bool FALSE
Definition: qglobal.h:370
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
bool isDirectAccess() const
Definition: qiodevice.h:98
bool isTranslated() const
Definition: qiodevice.h:105
bool isOpen() const
Definition: qiodevice.h:110
void QFile::close ( )
virtual

Closes an open file.

The file is not closed if it was opened with an existing file handle. If the existing file handle is a FILE*, the file is flushed. If the existing file handle is an int file descriptor, nothing is done to the file.

Some "write-behind" filesystems may report an unspecified error on closing the file. These errors only indicate that something may have gone wrong since the previous open(). In such a case status() reports IO_UnspecifiedError after close(), otherwise IO_Ok.

See also
open(), flush()

Implements QIODevice.

Definition at line 614 of file qfile_unix.cpp.

615 {
616  bool ok = FALSE;
617  if ( isOpen() ) { // file is not open
618  if ( fh ) { // buffered file
619  if ( ext_f )
620  ok = fflush( fh ) != -1; // flush instead of closing
621  else
622  ok = fclose( fh ) != -1;
623  } else { // raw file
624  if ( ext_f )
625  ok = TRUE; // cannot close
626  else
627  ok = CLOSE( fd ) != -1;
628  }
629  init(); // restore internal state
630  }
631  if (!ok)
633 
634  return;
635 }
#define IO_UnspecifiedError
Definition: qiodevice.h:86
const bool FALSE
Definition: qglobal.h:370
void setStatus(int)
Definition: qiodevice.cpp:408
void init()
Definition: qfile.cpp:124
int fd
Definition: qfile.h:104
#define CLOSE
Definition: qfiledefs_p.h:212
bool ext_f
Definition: qfile.h:106
FILE * fh
Definition: qfile.h:103
bool isOpen() const
Definition: qiodevice.h:110
const bool TRUE
Definition: qglobal.h:371
QString QFile::decodeName ( const QCString localFileName)
static

This does the reverse of QFile::encodeName().

See also
setDecodingFunction()

Definition at line 529 of file qfile.cpp.

530 {
531  return (*decoder)(localFileName);
532 }
static QFile::DecoderFn decoder
Definition: qfile.cpp:522
QCString QFile::encodeName ( const QString fileName)
static

When you use QFile, QFileInfo, and QDir to access the filesystem with Qt, you can use Unicode filenames. On Unix, these filenames are converted to an 8-bit encoding. If you want to do your own file I/O on Unix, you should convert the filename using this function. On Windows NT, Unicode filenames are supported directly in the filesystem and this function should be avoided. On Windows 95, non-Latin1 locales are not supported at this time.

By default, this function converts to the local 8-bit encoding determined by the user's locale. This is sufficient for filenames that the user chooses. Filenames hard-coded into the application should only use 7-bit ASCII filename characters.

The conversion scheme can be changed using setEncodingFunction(). This might be useful if you wish to give the user an option to store in filenames in UTF-8, etc., but beware that such filenames would probably then be unrecognizable when seen by other programs.

See also
decodeName()

Definition at line 494 of file qfile.cpp.

495 {
496  return (*encoder)(fileName);
497 }
fileName
Definition: dumpTree.py:9
static QFile::EncoderFn encoder
Definition: qfile.cpp:470
bool QFile::exists ( ) const

Returns TRUE if this file exists, otherwise FALSE.

See also
name()

Definition at line 183 of file qfile.cpp.

184 {
185  return qt_file_access( fn, F_OK );
186 }
bool qt_file_access(const QString &fn, int t)
Definition: qfile_unix.cpp:54
QString fn
Definition: qfile.h:102
bool QFile::exists ( const QString fileName)
static

Returns TRUE if the file given by fileName exists, otherwise FALSE.

Definition at line 192 of file qfile.cpp.

193 {
194  return qt_file_access( fileName, F_OK );
195 }
bool qt_file_access(const QString &fn, int t)
Definition: qfile_unix.cpp:54
void QFile::flush ( )
virtual

Flushes the file buffer to the disk.

close() also flushes the file buffer.

Implements QIODevice.

Definition at line 228 of file qfile.cpp.

229 {
230  if ( isOpen() && fh ) // can only flush open/buffered
231  fflush( fh ); // file
232 }
FILE * fh
Definition: qfile.h:103
bool isOpen() const
Definition: qiodevice.h:110
int QFile::getch ( )
virtual

Reads a single byte/character from the file.

Returns the byte/character read, or -1 if the end of the file has been reached.

See also
putch(), ungetch()

Implements QIODevice.

Definition at line 337 of file qfile.cpp.

338 {
339 #if defined(CHECK_STATE)
340  if ( !isOpen() ) { // file not open
341  qWarning( "QFile::getch: File not open" );
342  return EOF;
343  }
344  if ( !isReadable() ) { // reading not permitted
345  qWarning( "QFile::getch: Read operation not permitted" );
346  return EOF;
347  }
348 #endif
349 
350  int ch;
351 
352  if ( !ungetchBuffer.isEmpty() ) {
353  int len = ungetchBuffer.length();
354  ch = ungetchBuffer[ len-1 ];
355  ungetchBuffer.truncate( len - 1 );
356  return ch;
357  }
358 
359  if ( isRaw() ) { // raw file (inefficient)
360  char buf[1];
361  ch = readBlock( buf, 1 ) == 1 ? buf[0] : EOF;
362  } else { // buffered file
363  if ( (ch = getc( fh )) != EOF )
364  ioIndex++;
365  else
367  }
368  return ch;
369 }
bool isEmpty() const
Definition: qcstring.h:189
uint length() const
Definition: qcstring.h:195
bool isRaw() const
Definition: qiodevice.h:102
#define IO_ReadError
Definition: qiodevice.h:78
QCString ungetchBuffer
Definition: qfile.h:111
int readBlock(char *data, uint len)
Definition: qfile_unix.cpp:473
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
void setStatus(int)
Definition: qiodevice.cpp:408
bool isReadable() const
Definition: qiodevice.h:106
bool truncate(uint pos)
Definition: qcstring.h:232
FILE * fh
Definition: qfile.h:103
bool isOpen() const
Definition: qiodevice.h:110
int ioIndex
Definition: qiodevice.h:141
int QFile::handle ( ) const

Returns the file handle of the file.

This is a small positive integer, suitable for use with C library functions such as fdopen() and fcntl(), as well as with QSocketNotifier.

If the file is not open or there is an error, handle() returns -1.

See also
QSocketNotifier

Definition at line 587 of file qfile_unix.cpp.

588 {
589  if ( !isOpen() )
590  return -1;
591  else if ( fh )
592  return FILENO( fh );
593  else
594  return fd;
595 }
#define FILENO
Definition: qfiledefs_p.h:210
int fd
Definition: qfile.h:104
FILE * fh
Definition: qfile.h:103
bool isOpen() const
Definition: qiodevice.h:110
void QFile::init ( )
private

Definition at line 124 of file qfile.cpp.

125 {
126  setFlags( IO_Direct );
127  setStatus( IO_Ok );
128  fh = 0;
129  fd = 0;
130  length = 0;
131  ioIndex = 0;
132  ext_f = FALSE; // not an external file handle
133 }
int length
Definition: qfile.h:105
void setFlags(int f)
Definition: qiodevice.h:136
const bool FALSE
Definition: qglobal.h:370
void setStatus(int)
Definition: qiodevice.cpp:408
int fd
Definition: qfile.h:104
#define IO_Direct
Definition: qiodevice.h:49
#define IO_Ok
Definition: qiodevice.h:77
bool ext_f
Definition: qfile.h:106
FILE * fh
Definition: qfile.h:103
int ioIndex
Definition: qiodevice.h:141
QString QFile::name ( ) const
inline

Returns the name set by setName().

See also
setName(), QFileInfo::fileName()

Definition at line 121 of file qfile.h.

122 { return fn; }
QString fn
Definition: qfile.h:102
bool QFile::open ( int  m)
virtual

Opens the file specified by the file name currently set, using the mode m. Returns TRUE if successful, otherwise FALSE.

The mode parameter m must be a combination of the following flags:

  • IO_Raw specified raw (non-buffered) file access.
  • IO_ReadOnly opens the file in read-only mode.
  • IO_WriteOnly opens the file in write-only mode (and truncates).
  • IO_ReadWrite opens the file in read/write mode, equivalent to (IO_ReadOnly|IO_WriteOnly).
  • IO_Append opens the file in append mode. This mode is very useful when you want to write something to a log file. The file index is set to the end of the file. Note that the result is undefined if you position the file index manually using at() in append mode.
  • IO_Truncate truncates the file.
  • IO_Translate enables carriage returns and linefeed translation for text files under MS-DOS, Windows and OS/2.

The raw access mode is best when I/O is block-operated using 4kB block size or greater. Buffered access works better when reading small portions of data at a time.

Important: When working with buffered files, data may not be written to the file at once. Call flush to make sure the data is really written.

Warning
We have experienced problems with some C libraries when a buffered file is opened for both reading and writing. If a read operation takes place immediately after a write operation, the read buffer contains garbage data. Worse, the same garbage is written to the file. Calling flush() before readBlock() solved this problem.

If the file does not exist and IO_WriteOnly or IO_ReadWrite is specified, it is created.

Example:

QFile f1( "/tmp/data.bin" );
QFile f2( "readme.txt" );
See also
name(), close(), isOpen(), flush()

Implements QIODevice.

Definition at line 134 of file qfile_unix.cpp.

135 {
136  if ( isOpen() ) { // file already open
137 #if defined(CHECK_STATE)
138  qWarning( "QFile::open: File already open" );
139 #endif
140  return FALSE;
141  }
142  if ( fn.isNull() ) { // no file name defined
143 #if defined(CHECK_NULL)
144  qWarning( "QFile::open: No file name specified" );
145 #endif
146  return FALSE;
147  }
148  init(); // reset params
149  setMode( m );
150  if ( !(isReadable() || isWritable()) ) {
151 #if defined(CHECK_RANGE)
152  qWarning( "QFile::open: File access not specified" );
153 #endif
154  return FALSE;
155  }
156  bool ok = TRUE;
157  STATBUF st;
158  if ( isRaw() ) { // raw file I/O
159  int oflags = OPEN_RDONLY;
160  if ( isReadable() && isWritable() )
161  oflags = OPEN_RDWR;
162  else if ( isWritable() )
163  oflags = OPEN_WRONLY;
164  if ( flags() & IO_Append ) { // append to end of file?
165  if ( flags() & IO_Truncate )
166  oflags |= (OPEN_CREAT | OPEN_TRUNC);
167  else
168  oflags |= (OPEN_APPEND | OPEN_CREAT);
169  setFlags( flags() | IO_WriteOnly ); // append implies write
170  } else if ( isWritable() ) { // create/trunc if writable
171  if ( flags() & IO_Truncate )
172  oflags |= (OPEN_CREAT | OPEN_TRUNC);
173  else
174  oflags |= OPEN_CREAT;
175  }
176 #if defined(HAS_TEXT_FILEMODE)
177  if ( isTranslated() )
178 #ifdef __CYGWIN__
179  /* Do nothing, allowing the Cygwin mount mode to take effect. */;
180 #else
181  oflags |= OPEN_TEXT;
182 #endif
183  else
184  oflags |= OPEN_BINARY;
185 #endif
186 #if defined(HAS_ASYNC_FILEMODE)
187  if ( isAsynchronous() )
188  oflags |= OPEN_ASYNC;
189 #endif
190  fd = OPEN( QFile::encodeName(fn), oflags, 0666 );
191 
192  if ( fd != -1 ) { // open successful
193  FSTAT( fd, &st ); // get the stat for later usage
194  } else {
195  ok = FALSE;
196  }
197  } else { // buffered file I/O
198  QCString perm;
199  char perm2[4];
200  bool try_create = FALSE;
201  if ( flags() & IO_Append ) { // append to end of file?
202  setFlags( flags() | IO_WriteOnly ); // append implies write
203  perm = isReadable() ? "a+" : "a";
204  } else {
205  if ( isReadWrite() ) {
206  if ( flags() & IO_Truncate ) {
207  perm = "w+";
208  } else {
209  perm = "r+";
210  try_create = TRUE; // try to create if not exists
211  }
212  } else if ( isReadable() ) {
213  perm = "r";
214  } else if ( isWritable() ) {
215  perm = "w";
216  }
217  }
218  qstrcpy( perm2, perm );
219 #if defined(HAS_TEXT_FILEMODE)
220  if ( isTranslated() )
221 #ifdef __CYGWIN__
222  /* Do nothing, allowing the Cygwin mount mode to take effect. */;
223 #else
224  strcat( perm2, "t" );
225 #endif
226  else
227  strcat( perm2, "b" );
228 #endif
229  while (1) { // At most twice
230 
231  fh = fopen( QFile::encodeName(fn), perm2 );
232 
233  if ( !fh && try_create ) {
234  perm2[0] = 'w'; // try "w+" instead of "r+"
235  try_create = FALSE;
236  } else {
237  break;
238  }
239  }
240  if ( fh ) {
241  FSTAT( FILENO(fh), &st ); // get the stat for later usage
242  } else {
243  ok = FALSE;
244  }
245  }
246  if ( ok ) {
247  setState( IO_Open );
248  // on successful open the file stat was got; now test what type
249  // of file we have
250  if ( (st.st_mode & STAT_MASK) != STAT_REG ) {
251  // non-seekable
253  length = INT_MAX;
254  ioIndex = (flags() & IO_Append) == 0 ? 0 : length;
255  } else {
256  length = (int)st.st_size;
257  ioIndex = (flags() & IO_Append) == 0 ? 0 : length;
258  if ( !(flags()&IO_Truncate) && length == 0 && isReadable() ) {
259  // try if you can read from it (if you can, it's a sequential
260  // device; e.g. a file in the /proc filesystem)
261  int c = getch();
262  if ( c != -1 ) {
263  ungetch(c);
265  length = INT_MAX;
266  }
267  }
268  }
269  } else {
270  init();
271  if ( errno == EMFILE ) // no more file handles/descrs
273  else
275  }
276  return ok;
277 }
#define OPEN_TRUNC
Definition: qfiledefs_p.h:230
int length
Definition: qfile.h:105
#define OPEN_RDONLY
Definition: qfiledefs_p.h:226
#define STATBUF
Definition: qfiledefs_p.h:199
int flags() const
Definition: qiodevice.h:94
#define FSTAT
Definition: qfiledefs_p.h:202
#define IO_WriteOnly
Definition: qiodevice.h:62
bool isRaw() const
Definition: qiodevice.h:102
#define FILENO
Definition: qfiledefs_p.h:210
void setFlags(int f)
Definition: qiodevice.h:136
const bool FALSE
Definition: qglobal.h:370
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
void setMode(int)
Definition: qiodevice.cpp:378
int errno
Contains the last error code.
Definition: structcmd.h:53
void setStatus(int)
Definition: qiodevice.cpp:408
void init()
Definition: qfile.cpp:124
bool isReadable() const
Definition: qiodevice.h:106
#define OPEN_CREAT
Definition: qfiledefs_p.h:229
#define OPEN_APPEND
Definition: qfiledefs_p.h:231
int fd
Definition: qfile.h:104
int getch()
Definition: qfile.cpp:337
#define OPEN_WRONLY
Definition: qfiledefs_p.h:227
#define IO_OpenError
Definition: qiodevice.h:82
#define IO_Truncate
Definition: qiodevice.h:65
#define IO_ResourceError
Definition: qiodevice.h:81
bool isWritable() const
Definition: qiodevice.h:107
#define IO_Append
Definition: qiodevice.h:64
#define OPEN_RDWR
Definition: qfiledefs_p.h:228
#define IO_Sequential
Definition: qiodevice.h:50
static QCString encodeName(const QString &fileName)
Definition: qfile.cpp:494
void setState(int)
Definition: qiodevice.cpp:393
#define OPEN
Definition: qfiledefs_p.h:211
Q_EXPORT char * qstrcpy(char *dst, const char *src)
Definition: qcstring.h:87
#define STAT_REG
Definition: qfiledefs_p.h:204
bool isNull() const
Definition: qstring.h:379
bool isTranslated() const
Definition: qiodevice.h:105
bool isAsynchronous() const
Definition: qiodevice.h:104
void setType(int)
Definition: qiodevice.cpp:363
FILE * fh
Definition: qfile.h:103
bool isReadWrite() const
Definition: qiodevice.h:108
bool isOpen() const
Definition: qiodevice.h:110
#define STAT_MASK
Definition: qfiledefs_p.h:206
int ungetch(int)
Definition: qfile.cpp:425
#define IO_Open
Definition: qiodevice.h:71
const bool TRUE
Definition: qglobal.h:371
QString fn
Definition: qfile.h:102
int ioIndex
Definition: qiodevice.h:141
bool QFile::open ( int  m,
FILE *  f 
)

Opens a file in the mode m using an existing file handle f. Returns TRUE if successful, otherwise FALSE.

Example:

#include <stdio.h>
void printError( const char* msg )
{
f.writeBlock( msg, qstrlen(msg) ); // write to stderr
f.close();
}

When a QFile is opened using this function, close() does not actually close the file, only flushes it.

Warning
If f is stdin, stdout, stderr, you may not be able to seek. See QIODevice::isSequentialAccess() for more information.
See also
close()

Definition at line 306 of file qfile_unix.cpp.

307 {
308  if ( isOpen() ) {
309 #if defined(CHECK_RANGE)
310  qWarning( "QFile::open: File already open" );
311 #endif
312  return FALSE;
313  }
314  init();
315  setMode( m &~IO_Raw );
316  setState( IO_Open );
317  fh = f;
318  ext_f = TRUE;
319  STATBUF st;
320  FSTAT( FILENO(fh), &st );
321  ioIndex = (int)ftell( fh );
322  if ( (st.st_mode & STAT_MASK) != STAT_REG || f == stdin ) { //stdin is non seekable
323  // non-seekable
325  length = INT_MAX;
326  } else {
327  length = (int)st.st_size;
328  if ( !(flags()&IO_Truncate) && length == 0 && isReadable() ) {
329  // try if you can read from it (if you can, it's a sequential
330  // device; e.g. a file in the /proc filesystem)
331  int c = getch();
332  if ( c != -1 ) {
333  ungetch(c);
335  length = INT_MAX;
336  }
337  }
338  }
339  return TRUE;
340 }
int length
Definition: qfile.h:105
#define STATBUF
Definition: qfiledefs_p.h:199
int flags() const
Definition: qiodevice.h:94
#define FSTAT
Definition: qfiledefs_p.h:202
#define IO_Raw
Definition: qiodevice.h:56
#define FILENO
Definition: qfiledefs_p.h:210
const bool FALSE
Definition: qglobal.h:370
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
void setMode(int)
Definition: qiodevice.cpp:378
void init()
Definition: qfile.cpp:124
bool isReadable() const
Definition: qiodevice.h:106
int getch()
Definition: qfile.cpp:337
#define IO_Truncate
Definition: qiodevice.h:65
#define IO_Sequential
Definition: qiodevice.h:50
void setState(int)
Definition: qiodevice.cpp:393
bool ext_f
Definition: qfile.h:106
#define STAT_REG
Definition: qfiledefs_p.h:204
void setType(int)
Definition: qiodevice.cpp:363
FILE * fh
Definition: qfile.h:103
bool isOpen() const
Definition: qiodevice.h:110
if(!yymsg) yymsg
#define STAT_MASK
Definition: qfiledefs_p.h:206
int ungetch(int)
Definition: qfile.cpp:425
#define IO_Open
Definition: qiodevice.h:71
const bool TRUE
Definition: qglobal.h:371
int ioIndex
Definition: qiodevice.h:141
bool QFile::open ( int  m,
int  f 
)

Opens a file in the mode m using an existing file descriptor f. Returns TRUE if successful, otherwise FALSE.

When a QFile is opened using this function, close() does not actually close the file.

Warning
If f is one of 0 (stdin), 1 (stdout) or 2 (stderr), you may not be able to seek. size() is set to INT_MAX (in limits.h).
See also
close()

Definition at line 356 of file qfile_unix.cpp.

357 {
358  if ( isOpen() ) {
359 #if defined(CHECK_RANGE)
360  qWarning( "QFile::open: File already open" );
361 #endif
362  return FALSE;
363  }
364  init();
365  setMode( m |IO_Raw );
366  setState( IO_Open );
367  fd = f;
368  ext_f = TRUE;
369  STATBUF st;
370  FSTAT( fd, &st );
371  ioIndex = (int)LSEEK(fd, 0, SEEK_CUR);
372  if ( (st.st_mode & STAT_MASK) != STAT_REG || f == 0 ) { // stdin is not seekable...
373  // non-seekable
375  length = INT_MAX;
376  } else {
377  length = (int)st.st_size;
378  if ( length == 0 && isReadable() ) {
379  // try if you can read from it (if you can, it's a sequential
380  // device; e.g. a file in the /proc filesystem)
381  int c = getch();
382  if ( c != -1 ) {
383  ungetch(c);
385  length = INT_MAX;
386  }
387  resetStatus();
388  }
389  }
390  return TRUE;
391 }
int length
Definition: qfile.h:105
#define STATBUF
Definition: qfiledefs_p.h:199
#define FSTAT
Definition: qfiledefs_p.h:202
#define IO_Raw
Definition: qiodevice.h:56
const bool FALSE
Definition: qglobal.h:370
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
void setMode(int)
Definition: qiodevice.cpp:378
void init()
Definition: qfile.cpp:124
bool isReadable() const
Definition: qiodevice.h:106
int fd
Definition: qfile.h:104
void resetStatus()
Definition: qiodevice.h:113
int getch()
Definition: qfile.cpp:337
#define IO_Sequential
Definition: qiodevice.h:50
void setState(int)
Definition: qiodevice.cpp:393
#define LSEEK
Definition: qfiledefs_p.h:213
bool ext_f
Definition: qfile.h:106
#define STAT_REG
Definition: qfiledefs_p.h:204
void setType(int)
Definition: qiodevice.cpp:363
bool isOpen() const
Definition: qiodevice.h:110
if(!yymsg) yymsg
#define STAT_MASK
Definition: qfiledefs_p.h:206
int ungetch(int)
Definition: qfile.cpp:425
#define IO_Open
Definition: qiodevice.h:71
const bool TRUE
Definition: qglobal.h:371
int ioIndex
Definition: qiodevice.h:141
QFile& QFile::operator= ( const QFile )
private
int64 QFile::pos ( ) const

Definition at line 637 of file qfile_unix.cpp.

638 {
639  if (isOpen())
640  {
641  // TODO: support 64 bit size
642  return ftell( fh );
643  }
644  return -1;
645 }
FILE * fh
Definition: qfile.h:103
bool isOpen() const
Definition: qiodevice.h:110
int QFile::putch ( int  ch)
virtual

Writes the character ch to the file.

Returns ch, or -1 if some error occurred.

See also
getch(), ungetch()

Implements QIODevice.

Definition at line 386 of file qfile.cpp.

387 {
388 #if defined(CHECK_STATE)
389  if ( !isOpen() ) { // file not open
390  qWarning( "QFile::putch: File not open" );
391  return EOF;
392  }
393  if ( !isWritable() ) { // writing not permitted
394  qWarning( "QFile::putch: Write operation not permitted" );
395  return EOF;
396  }
397 #endif
398  if ( isRaw() ) { // raw file (inefficient)
399  char buf[1];
400  buf[0] = ch;
401  ch = writeBlock( buf, 1 ) == 1 ? ch : EOF;
402  } else { // buffered file
403  if ( (ch = putc( ch, fh )) != EOF ) {
404  ioIndex++;
405  if ( ioIndex > length ) // update file length
406  length = ioIndex;
407  } else {
409  }
410  }
411  return ch;
412 }
int length
Definition: qfile.h:105
bool isRaw() const
Definition: qiodevice.h:102
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
void setStatus(int)
Definition: qiodevice.cpp:408
int writeBlock(const char *data, uint len)
Definition: qfile_unix.cpp:537
bool isWritable() const
Definition: qiodevice.h:107
#define IO_WriteError
Definition: qiodevice.h:79
FILE * fh
Definition: qfile.h:103
bool isOpen() const
Definition: qiodevice.h:110
int ioIndex
Definition: qiodevice.h:141
int QFile::readBlock ( char *  p,
uint  len 
)
virtual

Reads at most len bytes from the file into p and returns the number of bytes actually read.

Returns -1 if a serious error occurred.

Warning
We have experienced problems with some C libraries when a buffered file is opened for both reading and writing. If a read operation takes place immediately after a write operation, the read buffer contains garbage data. Worse, the same garbage is written to the file. Calling flush() before readBlock() solved this problem.
See also
writeBlock()

Implements QIODevice.

Definition at line 473 of file qfile_unix.cpp.

474 {
475 #if defined(CHECK_NULL)
476  if ( !p )
477  qWarning( "QFile::readBlock: Null pointer error" );
478 #endif
479 #if defined(CHECK_STATE)
480  if ( !isOpen() ) { // file not open
481  qWarning( "QFile::readBlock: File not open" );
482  return -1;
483  }
484  if ( !isReadable() ) { // reading not permitted
485  qWarning( "QFile::readBlock: Read operation not permitted" );
486  return -1;
487  }
488 #endif
489  int nread = 0; // number of bytes read
490  if ( !ungetchBuffer.isEmpty() ) {
491  // need to add these to the returned string.
492  int l = ungetchBuffer.length();
493  while( nread < l ) {
494  *p = ungetchBuffer[ l - nread - 1 ];
495  p++;
496  nread++;
497  }
498  ungetchBuffer.truncate( l - nread );
499  }
500 
501  if ( nread < (int)len ) {
502  if ( isRaw() ) { // raw file
503  nread += (int)READ( fd, p, len-nread );
504  if ( len && nread <= 0 ) {
505  nread = 0;
507  }
508  } else { // buffered file
509  nread += (int)fread( p, 1, len-nread, fh );
510  if ( (uint)nread != len ) {
511  if ( ferror( fh ) || nread==0 )
513  }
514  }
515  }
516  ioIndex += nread;
517  return nread;
518 }
bool isEmpty() const
Definition: qcstring.h:189
uint length() const
Definition: qcstring.h:195
bool isRaw() const
Definition: qiodevice.h:102
#define IO_ReadError
Definition: qiodevice.h:78
#define READ
Definition: qfiledefs_p.h:214
QCString ungetchBuffer
Definition: qfile.h:111
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
void setStatus(int)
Definition: qiodevice.cpp:408
bool isReadable() const
Definition: qiodevice.h:106
static QStrList * l
Definition: config.cpp:1044
int fd
Definition: qfile.h:104
p
Definition: test.py:223
bool truncate(uint pos)
Definition: qcstring.h:232
FILE * fh
Definition: qfile.h:103
bool isOpen() const
Definition: qiodevice.h:110
unsigned uint
Definition: qglobal.h:351
int ioIndex
Definition: qiodevice.h:141
int QFile::readLine ( char *  p,
uint  maxlen 
)
virtual

Reads a line of text.

Reads bytes from the file until end-of-line is reached, or up to maxlen bytes, and returns the number of bytes read, or -1 in case of error. The terminating newline is not stripped.

This function is efficient only for buffered files. Avoid readLine() for files that have been opened with the IO_Raw flag.

See also
readBlock(), QTextStream::readLine()

Reimplemented from QIODevice.

Definition at line 268 of file qfile.cpp.

269 {
270  if ( maxlen == 0 ) // application bug?
271  return 0;
272 #if defined(CHECK_STATE)
273  CHECK_PTR( p );
274  if ( !isOpen() ) { // file not open
275  qWarning( "QFile::readLine: File not open" );
276  return -1;
277  }
278  if ( !isReadable() ) { // reading not permitted
279  qWarning( "QFile::readLine: Read operation not permitted" );
280  return -1;
281  }
282 #endif
283  int nread; // number of bytes read
284  if ( isRaw() ) { // raw file
285  nread = QIODevice::readLine( p, maxlen );
286  } else { // buffered file
287  p = fgets( p, maxlen, fh );
288  if ( p ) {
289  nread = qstrlen( p );
290  ioIndex += nread;
291  } else {
292  nread = -1;
294  }
295  }
296  return nread;
297 }
static const int maxlen
Definition: qregexp.cpp:904
bool isRaw() const
Definition: qiodevice.h:102
#define IO_ReadError
Definition: qiodevice.h:78
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
void setStatus(int)
Definition: qiodevice.cpp:408
bool isReadable() const
Definition: qiodevice.h:106
Q_EXPORT uint qstrlen(const char *str)
Definition: qcstring.h:81
p
Definition: test.py:223
virtual int readLine(char *data, uint maxlen)
Definition: qiodevice.cpp:581
#define CHECK_PTR(p)
Definition: qglobal.h:601
FILE * fh
Definition: qfile.h:103
bool isOpen() const
Definition: qiodevice.h:110
int ioIndex
Definition: qiodevice.h:141
int QFile::readLine ( QString s,
uint  maxlen 
)

Reads a line of text.

Reads bytes from the file until end-of-line is reached, or up to maxlen bytes, and returns the number of bytes read, or -1 in case of error. The terminating newline is not stripped.

This function is efficient only for buffered files. Avoid readLine() for files that have been opened with the IO_Raw flag.

Note that the string is read as plain Latin1 bytes, not Unicode.

See also
readBlock(), QTextStream::readLine()

Definition at line 316 of file qfile.cpp.

317 {
318  QByteArray ba(maxlen);
319  int l = readLine(ba.data(),maxlen);
320  if ( l >= 0 ) {
321  ba.truncate(l);
322  s = QString(ba);
323  }
324  return l;
325 }
static const int maxlen
Definition: qregexp.cpp:904
int readLine(char *data, uint maxlen)
Definition: qfile.cpp:268
The QString class provides an abstraction of Unicode text and the classic C null-terminated char arra...
Definition: qstring.h:350
static QStrList * l
Definition: config.cpp:1044
bool QFile::remove ( )

Removes the file specified by the file name currently set. Returns TRUE if successful, otherwise FALSE.

The file is closed before it is removed.

Definition at line 205 of file qfile.cpp.

206 {
207  close();
208  return remove( fn );
209 }
void close()
Definition: qfile_unix.cpp:614
QString fn
Definition: qfile.h:102
bool QFile::remove ( const QString fileName)
static

Removes the file fileName. Returns TRUE if successful, otherwise FALSE.

Definition at line 66 of file qfile_unix.cpp.

67 {
68  if ( fileName.isEmpty() ) {
69 #if defined(CHECK_NULL)
70  qWarning( "QFile::remove: Empty or null file name" );
71 #endif
72  return FALSE;
73  }
74  return unlink( QFile::encodeName(fileName) ) == 0;
75  // unlink more common in UNIX
76 }
bool isEmpty() const
Definition: qstring.h:682
const bool FALSE
Definition: qglobal.h:370
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
static QCString encodeName(const QString &fileName)
Definition: qfile.cpp:494
bool QFile::seek ( int64  pos)

Definition at line 660 of file qfile_unix.cpp.

661 {
662  if (isOpen())
663  {
664  // TODO: support 64 bit size
665  return fseek( fh, (long)pos, SEEK_SET )!=-1;
666  }
667  return FALSE;
668 }
int64 pos() const
Definition: qfile_unix.cpp:637
const bool FALSE
Definition: qglobal.h:370
FILE * fh
Definition: qfile.h:103
bool isOpen() const
Definition: qiodevice.h:110
void QFile::setDecodingFunction ( DecoderFn  f)
static

Sets the function for decoding 8-bit filenames. The default uses the locale-specific 8-bit encoding.

See also
encodeName(), decodeName()

Definition at line 547 of file qfile.cpp.

548 {
549  decoder = f;
550 }
static QFile::DecoderFn decoder
Definition: qfile.cpp:522
void QFile::setEncodingFunction ( EncoderFn  f)
static

Sets the function for encoding Unicode filenames. The default encodes in the locale-specific 8-bit encoding.

See also
encodeName()

Definition at line 511 of file qfile.cpp.

512 {
513  encoder = f;
514 }
static QFile::EncoderFn encoder
Definition: qfile.cpp:470
void QFile::setName ( const QString name)

Sets the name of the file. The name can include an absolute directory path or it can be a name or a path relative to the current directory.

Do not call this function if the file has already been opened.

Note that if the name is relative QFile does not associate it with the current directory. If you change directory before calling open(), open uses the new current directory.

Example:

QDir::setCurrent( "/tmp" );
f.setName( "readme.txt" );
QDir::setCurrent( "/home" );
f.open( IO_ReadOnly ); // opens "/home/readme.txt" under UNIX

Also note that the directory separator '/' works for all operating systems supported by Qt.

See also
name(), QFileInfo, QDir

Definition at line 167 of file qfile.cpp.

168 {
169  if ( isOpen() ) {
170 #if defined(CHECK_STATE)
171  qWarning( "QFile::setName: File is open" );
172 #endif
173  close();
174  }
175  fn = name;
176 }
QString name() const
Definition: qfile.h:121
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
void close()
Definition: qfile_unix.cpp:614
bool isOpen() const
Definition: qiodevice.h:110
QString fn
Definition: qfile.h:102
uint QFile::size ( ) const
virtual

Returns the file size.

See also
at()

Implements QIODevice.

Definition at line 398 of file qfile_unix.cpp.

399 {
400  STATBUF st;
401  if ( isOpen() ) {
402  FSTAT( fh ? FILENO(fh) : fd, &st );
403  } else {
404  STAT( QFile::encodeName(fn), &st );
405  }
406  return (uint)st.st_size;
407 }
#define STATBUF
Definition: qfiledefs_p.h:199
#define FSTAT
Definition: qfiledefs_p.h:202
#define STAT
Definition: qfiledefs_p.h:201
#define FILENO
Definition: qfiledefs_p.h:210
int fd
Definition: qfile.h:104
static QCString encodeName(const QString &fileName)
Definition: qfile.cpp:494
FILE * fh
Definition: qfile.h:103
bool isOpen() const
Definition: qiodevice.h:110
unsigned uint
Definition: qglobal.h:351
QString fn
Definition: qfile.h:102
int64 QFile::toEnd ( )

Definition at line 647 of file qfile_unix.cpp.

648 {
649  if (isOpen())
650  {
651  // TODO: support 64 bit size
652  if (fseek( fh, 0, SEEK_END )!=-1)
653  {
654  return ftell( fh );
655  }
656  }
657  return -1;
658 }
FILE * fh
Definition: qfile.h:103
bool isOpen() const
Definition: qiodevice.h:110
int QFile::ungetch ( int  ch)
virtual

Puts the character ch back into the file and decrements the index if it is not zero.

This function is normally called to "undo" a getch() operation.

Returns ch, or -1 if some error occurred.

See also
getch(), putch()

Implements QIODevice.

Definition at line 425 of file qfile.cpp.

426 {
427 #if defined(CHECK_STATE)
428  if ( !isOpen() ) { // file not open
429  qWarning( "QFile::ungetch: File not open" );
430  return EOF;
431  }
432  if ( !isReadable() ) { // reading not permitted
433  qWarning( "QFile::ungetch: Read operation not permitted" );
434  return EOF;
435  }
436 #endif
437  if ( ch == EOF ) // cannot unget EOF
438  return ch;
439 
440  if ( isSequentialAccess() && !fh) {
441  // pipe or similar => we cannot ungetch, so do it manually
442  ungetchBuffer +=ch;
443  return ch;
444  }
445 
446  if ( isRaw() ) { // raw file (very inefficient)
447  char buf[1];
448  at( ioIndex-1 );
449  buf[0] = ch;
450  if ( writeBlock(buf, 1) == 1 )
451  at ( ioIndex-1 );
452  else
453  ch = EOF;
454  } else { // buffered file
455  if ( (ch = ungetc(ch, fh)) != EOF )
456  ioIndex--;
457  else
459  }
460  return ch;
461 }
int at() const
Definition: qfile.h:124
bool isRaw() const
Definition: qiodevice.h:102
#define IO_ReadError
Definition: qiodevice.h:78
QCString ungetchBuffer
Definition: qfile.h:111
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
void setStatus(int)
Definition: qiodevice.cpp:408
bool isReadable() const
Definition: qiodevice.h:106
int writeBlock(const char *data, uint len)
Definition: qfile_unix.cpp:537
bool isSequentialAccess() const
Definition: qiodevice.h:99
FILE * fh
Definition: qfile.h:103
bool isOpen() const
Definition: qiodevice.h:110
int ioIndex
Definition: qiodevice.h:141
int QFile::writeBlock ( const char *  p,
uint  len 
)
virtual

Writes len bytes from p to the file and returns the number of bytes actually written.

Returns -1 if a serious error occurred.

Warning
When working with buffered files, data may not be written to the file at once. Call flush() to make sure the data is really written.
See also
readBlock()

Implements QIODevice.

Definition at line 537 of file qfile_unix.cpp.

538 {
539 #if defined(CHECK_NULL)
540  if ( p == 0 && len != 0 )
541  qWarning( "QFile::writeBlock: Null pointer error" );
542 #endif
543 #if defined(CHECK_STATE)
544  if ( !isOpen() ) { // file not open
545  qWarning( "QFile::writeBlock: File not open" );
546  return -1;
547  }
548  if ( !isWritable() ) { // writing not permitted
549  qWarning( "QFile::writeBlock: Write operation not permitted" );
550  return -1;
551  }
552 #endif
553  if (p==0) return 0;
554  int nwritten; // number of bytes written
555  if ( isRaw() ) // raw file
556  nwritten = (int)WRITE( fd, p, len );
557  else // buffered file
558  nwritten = (int)fwrite( p, 1, len, fh );
559  if ( nwritten != (int)len ) { // write error
560  if ( errno == ENOSPC ) // disk is full
562  else
564  if ( isRaw() ) // recalc file position
565  ioIndex = (int)LSEEK( fd, 0, SEEK_CUR );
566  else
567  ioIndex = fseek( fh, 0, SEEK_CUR );
568  } else {
569  ioIndex += nwritten;
570  }
571  if ( ioIndex > length ) // update file length
572  length = ioIndex;
573  return nwritten;
574 }
int length
Definition: qfile.h:105
#define WRITE
Definition: qfiledefs_p.h:215
bool isRaw() const
Definition: qiodevice.h:102
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
int errno
Contains the last error code.
Definition: structcmd.h:53
void setStatus(int)
Definition: qiodevice.cpp:408
int fd
Definition: qfile.h:104
#define IO_ResourceError
Definition: qiodevice.h:81
bool isWritable() const
Definition: qiodevice.h:107
p
Definition: test.py:223
#define LSEEK
Definition: qfiledefs_p.h:213
#define IO_WriteError
Definition: qiodevice.h:79
FILE * fh
Definition: qfile.h:103
bool isOpen() const
Definition: qiodevice.h:110
int ioIndex
Definition: qiodevice.h:141
int QFile::writeBlock ( const QByteArray data)
inline

Definition at line 86 of file qfile.h.

87  { return QIODevice::writeBlock(data); }
virtual int writeBlock(const char *data, uint len)=0

Member Data Documentation

void* QFile::d
protected

Definition at line 107 of file qfile.h.

bool QFile::ext_f
protected

Definition at line 106 of file qfile.h.

int QFile::fd
protected

Definition at line 104 of file qfile.h.

FILE* QFile::fh
protected

Definition at line 103 of file qfile.h.

QString QFile::fn
protected

Definition at line 102 of file qfile.h.

int QFile::length
protected

Definition at line 105 of file qfile.h.

QCString QFile::ungetchBuffer
private

Definition at line 111 of file qfile.h.


The documentation for this class was generated from the following files: