1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include <qplatformdefs.h>
32
#include <qfileengine.h>
33
#include "qbufferedfsfileengine_p.h"
34
#include <qtemporaryfile.h>
36
#include <qfileinfo.h>
37
#include <private/qiodevice_p.h>
38
#include <private/qfile_p.h>
39
#include <private/qunicodetables_p.h>
40
#if defined(QT_BUILD_CORE_LIB)
41
# include "qcoreapplication.h"
46
static const int read_cache_size = 4096;
48
static QByteArray locale_encode(const QString &f)
51
return f.toLocal8Bit();
53
// Mac always expects UTF-8
58
static QString locale_decode(const QByteArray &f)
61
return QString::fromLocal8Bit(f);
63
// Mac always expects UTF-8
64
return QUnicodeTables::normalize(QString::fromUtf8(f), QString::NormalizationForm_C);
68
//************* QFilePrivate
69
QFile::EncoderFn QFilePrivate::encoder = locale_encode;
70
QFile::DecoderFn QFilePrivate::decoder = locale_decode;
72
QFilePrivate::QFilePrivate() :
73
#ifndef QT_NO_FILE_BUFFER
74
buffer(read_cache_size),
76
fileEngine(0), error(QFile::NoError)
80
QFilePrivate::~QFilePrivate()
87
QFilePrivate::openExternalFile(int flags, int fd)
90
QFSFileEngine *fe = new QFSFileEngine;
92
return fe->open(flags, fd);
96
QFilePrivate::openExternalFile(int flags, FILE *fh)
99
QBufferedFSFileEngine *fe = new QBufferedFSFileEngine;
101
return fe->open(flags, fh);
105
QFilePrivate::setError(QFile::FileError err)
112
QFilePrivate::setError(QFile::FileError err, const QString &errStr)
115
errorString = errStr;
119
QFilePrivate::setError(QFile::FileError err, int errNum)
122
errorString = qt_error_string(errNum);
125
//************* QFile
129
\brief The QFile class provides an interface for reading from and writing to files.
135
QFile is an I/O device for reading and writing text and binary
136
files and \l{The Qt Resource System}{resources}. A QFile may be
137
used by itself or, more conveniently, with a QTextStream or
140
The file name is usually passed in the constructor, but it can be
141
set at any time using setFileName(). You can check for a file's
142
existence using exists(), and remove a file using remove(). (More
143
advanced file system related operations are provided by QFileInfo
146
The file is opened with open(), closed with close(), and flushed
147
with flush(). Data is usually read and written using QDataStream
148
or QTextStream, but you can also call the QIODevice-inherited
149
functions read(), readLine(), readAll(), write(). QFile also
150
inherits getChar(), putChar(), and ungetChar(), which work one
153
The size of the file is returned by size(). You can get the
154
current file position using pos(), or move to a new file position
155
using seek(). If you've reached the end of the file, atEnd()
158
The following example reads a text file line by line:
160
\quotefromfile snippets/file/file.cpp
161
\skipto noStream_snippet
165
The QIODevice::Text flag passed to open() tells Qt to convert
166
Windows-style line terminators ("\\r\\n") into C++-style
167
terminators ("\\n"). By default, QFile assumes binary, i.e. it
168
doesn't perform any conversion on the bytes stored in the file.
170
The next example uses QTextStream to read a text file
173
\skipto readTextStream_snippet
177
QTextStream takes care of converting the 8-bit data stored on
178
disk into a 16-bit Unicode QString. By default, it assumes that
179
the user system's local 8-bit encoding is used (e.g., ISO 8859-1
180
for most of Europe; see QTextCodec::codecForLocale() for
181
details). This can be changed using setCodec().
183
To write text, we can use operator<<(), which is overloaded to
184
take a QTextStream on the left and various data types (including
185
QString) on the right:
187
\skipto writeTextStream_snippet
191
QDataStream is similar, in that you can use operator<<() to write
192
data and operator>>() to read it back. See the class
193
documentation for details.
195
When you use QFile, QFileInfo, and QDir to access the file system
196
with Qt, you can use Unicode file names. On Unix, these file
197
names are converted to an 8-bit encoding. If you want to use
198
standard C++ APIs (\c <cstdio> or \c <iostream>) or
199
platform-specific APIs to access files instead of QFile, you can
200
use the encodeName() and decodeName() functions to convert
201
between Unicode file names and 8-bit file names.
203
\sa QTextStream, QDataStream, QFileInfo, QDir, {The Qt Resource System}
207
\enum QFile::FileError
209
This enum describes the errors that may be returned by the error()
212
\value NoError No error occurred.
213
\value ReadError An error occurred when reading from the file.
214
\value WriteError An error occurred when writing to the file.
215
\value FatalError A fatal error occurred.
217
\value OpenError The file could not be opened.
218
\value AbortError The operation was aborted.
219
\value TimeOutError A timeout occurred.
220
\value UnspecifiedError An unspecified error occurred.
221
\value RemoveError The file could not be removed.
222
\value RenameError The file could not be renamed.
223
\value PositionError The position in the file could not be changed.
224
\value ResizeError The file could not be resized.
225
\value PermissionsError The file could not be accessed.
226
\value CopyError The file could not be copied.
228
\omitvalue ConnectError
232
\enum QFile::Permission
234
This enum is used by the permission() function to report the
235
permissions and ownership of a file. The values may be OR-ed
236
together to test multiple permissions and ownership values.
238
\value ReadOwner The file is readable by the owner of the file.
239
\value WriteOwner The file is writable by the owner of the file.
240
\value ExeOwner The file is executable by the owner of the file.
241
\value ReadUser The file is readable by the user.
242
\value WriteUser The file is writable by the user.
243
\value ExeUser The file is executable by the user.
244
\value ReadGroup The file is readable by the group.
245
\value WriteGroup The file is writable by the group.
246
\value ExeGroup The file is executable by the group.
247
\value ReadOther The file is readable by anyone.
248
\value WriteOther The file is writable by anyone.
249
\value ExeOther The file is executable by anyone.
251
\warning Because of differences in the platforms supported by Qt,
252
the semantics of ReadUser, WriteUser and ExeUser are
253
platform-dependent: On Unix, the rights of the owner of the file
254
are returned and on Windows the rights of the current user are
255
returned. This behavior might change in a future Qt version.
260
\typedef QFile::PermissionSpec
262
Use QFile::Permission instead.
268
: QIODevice(*new QFilePrivate)
272
QFile::QFile(const QString &name)
273
: QIODevice(*new QFilePrivate)
275
d_func()->fileName = name;
278
QFile::QFile(QFilePrivate &dd)
288
: QIODevice(*new QFilePrivate, 0)
293
Constructs a new file object with the given \a parent.
295
QFile::QFile(QObject *parent)
296
: QIODevice(*new QFilePrivate, parent)
301
Constructs a new file object to represent the file with the given \a name.
303
QFile::QFile(const QString &name)
304
: QIODevice(*new QFilePrivate, 0)
311
Constructs a new file object with the given \a parent to represent the
312
file with the specified \a name.
314
QFile::QFile(const QString &name, QObject *parent)
315
: QIODevice(*new QFilePrivate, parent)
324
QFile::QFile(QFilePrivate &dd, QObject *parent)
325
: QIODevice(dd, parent)
332
Destroys the file object, closing it if necessary.
343
Returns the name set by setFileName().
345
\sa setFileName(), QFileInfo::fileName()
348
QFile::fileName() const
350
return fileEngine()->fileName(QFileEngine::DefaultName);
354
Sets the \a name of the file. The name can have no path, a
355
relative path, or an absolute absolute path.
357
Do not call this function if the file has already been opened.
359
If the file name has no path or a relative path, the path used
360
will be the application's current directory path
361
\e{at the time of the open()} call.
366
QDir::setCurrent("/tmp");
367
file.setFileName("readme.txt");
368
QDir::setCurrent("/home");
369
file.open(QIODevice::ReadOnly); // opens "/home/readme.txt" under Unix
372
Note that the directory separator "/" works for all operating
373
systems supported by Qt.
375
\sa fileName(), QFileInfo, QDir
378
QFile::setFileName(const QString &name)
382
qWarning("QFile::setFileName: file is already opened");
385
if(d->fileEngine) { //get a new file engine later
386
delete d->fileEngine;
393
\fn QString QFile::decodeName(const char *localFileName)
397
Returns the Unicode version of the given \a localFileName. See
398
encodeName() for details.
402
By default, this function converts \a fileName to the local 8-bit
403
encoding determined by the user's locale. This is sufficient for
404
file names that the user chooses. File names hard-coded into the
405
application should only use 7-bit ASCII filename characters.
407
\sa decodeName() setEncodingFunction()
411
QFile::encodeName(const QString &fileName)
413
return (*QFilePrivate::encoder)(fileName);
417
\typedef QFile::EncoderFn
419
This is a typedef for a pointer to a function with the following
423
QByteArray myEncoderFunc(const QString &fileName);
426
\sa setEncodingFunction(), encodeName()
430
This does the reverse of QFile::encodeName() using \a localFileName.
432
\sa setDecodingFunction(), encodeName()
436
QFile::decodeName(const QByteArray &localFileName)
438
return (*QFilePrivate::decoder)(localFileName);
442
\fn void QFile::setEncodingFunction(EncoderFn function)
446
Sets the \a function for encoding Unicode file names. The
447
default encodes in the locale-specific 8-bit encoding.
449
\sa encodeName(), setDecodingFunction()
453
QFile::setEncodingFunction(EncoderFn f)
455
QFilePrivate::encoder = f;
459
\typedef QFile::DecoderFn
461
This is a typedef for a pointer to a function with the following
465
QString myDecoderFunc(const QByteArray &localFileName);
468
\sa setDecodingFunction()
472
\fn void QFile::setDecodingFunction(DecoderFn function)
476
Sets the \a function for decoding 8-bit file names. The
477
default uses the locale-specific 8-bit encoding.
479
\sa setEncodingFunction(), decodeName()
483
QFile::setDecodingFunction(DecoderFn f)
485
QFilePrivate::decoder = f;
491
Returns true if the file specified by fileName() exists; otherwise
494
\sa fileName(), setFileName()
498
QFile::exists() const
500
return (fileEngine()->fileFlags(QFileEngine::FlagsMask) & QFileEngine::ExistsFlag);
504
Returns true if the file specified by \a fileName exists; otherwise
509
QFile::exists(const QString &fileName)
511
return QFileInfo(fileName).exists();
517
Returns the name a symlink (or shortcut on Windows) points to, or
518
a an empty string if the object isn't a symbolic link.
520
This name may not represent an existing file; it is only a string.
521
QFie::exists() returns true if the symlink points to an
524
\sa fileName() setFileName()
528
QFile::readLink() const
530
return fileEngine()->fileName(QFileEngine::LinkName);
534
Returns the filename referred to by the symlink (or shortcut on Windows)
535
specified by \a fileName, or returns an empty string if the \a fileName
536
does not correspond to a symbolic link.
538
This name may not represent an existing file; it is only a string.
539
QFile::exists() returns true if the symlink points to an
544
QFile::readLink(const QString &fileName)
546
return QFileInfo(fileName).readLink();
550
Removes the file specified by fileName(). Returns true if successful;
551
otherwise returns false.
553
The file is closed before it is removed.
562
if (d->fileName.isEmpty()) {
563
qWarning("QFile::remove: Empty or null file name");
567
if(error() == QFile::NoError) {
568
if(fileEngine()->remove()) {
572
d->setError(QFile::RemoveError, errno);
580
Removes the file specified by the \a fileName given.
582
Returns true if successful; otherwise returns false.
588
QFile::remove(const QString &fileName)
590
return QFile(fileName).remove();
594
Renames the file currently specified by fileName() to \a newName.
595
Returns true if successful; otherwise returns false.
597
The file is closed before it is renamed.
603
QFile::rename(const QString &newName)
606
if (d->fileName.isEmpty()) {
607
qWarning("QFile::rename: Empty or null file name");
611
if(error() == QFile::NoError) {
612
if(fileEngine()->rename(newName)) {
616
QFile in(fileName());
618
if (in.open(QIODevice::ReadOnly)) {
619
if(out.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
623
long read = in.read(block, 1024);
626
if(read != out.write(block, read)) {
627
d->setError(QFile::CopyError, QLatin1String("Failure to write block"));
638
d->setError(QFile::RenameError, errno);
646
Renames the file \a oldName to \a newName. Returns true if
647
successful; otherwise returns false.
653
QFile::rename(const QString &oldName, const QString &newName)
655
return QFile(oldName).rename(newName);
659
Creates a link from the file currently specified by fileName() to
660
\a newName. What a link is depends on the underlying filesystem
661
(be it a shortcut on Windows or a symbolic link on Unix). Returns
662
true if successful; otherwise returns false.
668
QFile::link(const QString &newName)
671
if (d->fileName.isEmpty()) {
672
qWarning("QFile::link: Empty or null file name");
675
QFileInfo fi(newName);
676
if(fileEngine()->link(fi.absoluteFilePath())) {
680
d->setError(QFile::RenameError, errno);
687
Creates a link from \a oldName to \a newName. What a link is
688
depends on the underlying filesystem (be it a shortcut on Windows
689
or a symbolic link on Unix). Returns true if successful; otherwise
696
QFile::link(const QString &oldName, const QString &newName)
698
return QFile(oldName).link(newName);
702
Copies the file currently specified by fileName() to \a newName.
703
Returns true if successful; otherwise returns false.
705
The file is closed before it is copied.
711
QFile::copy(const QString &newName)
714
if (d->fileName.isEmpty()) {
715
qWarning("QFile::copy: Empty or null file name");
719
if(error() == QFile::NoError) {
720
if(fileEngine()->copy(newName)) {
725
if(!open(QFile::ReadOnly)) {
727
QString errorMessage = QLatin1String("Cannot open %1 for input");
728
d->setError(QFile::CopyError, errorMessage.arg(d->fileName));
734
d->setError(QFile::CopyError, QLatin1String("Cannot open for output"));
738
qint64 in = read(block, 1024);
741
if(in != out.write(block, in)) {
742
d->setError(QFile::CopyError, QLatin1String("Failure to write block"));
747
if(!error && !out.rename(newName)) {
749
QString errorMessage = QLatin1String("Cannot create %1 for output");
750
d->setError(QFile::CopyError, errorMessage.arg(newName));
755
QFile::setPermissions(newName, permissions());
767
Copies the file \a fileName to \a newName. Returns true if successful;
768
otherwise returns false.
774
QFile::copy(const QString &fileName, const QString &newName)
776
return QFile(fileName).copy(newName);
780
Returns true if the file can only be manipulated sequentially;
781
otherwise returns false.
783
Most files support random-access, but some special files may not.
785
\sa QIODevice::isSequential()
787
bool QFile::isSequential() const
790
return d->fileEngine && d->fileEngine->isSequential();
794
Opens the file using OpenMode \a mode.
796
The \a mode must be QIODevice::ReadOnly, QIODevice::WriteOnly, or
797
QIODevice::ReadWrite. It may also have additional flags, such as
798
QIODevice::Text and QIODevice::Unbuffered.
800
\sa QIODevice::OpenMode
802
bool QFile::open(OpenMode mode)
806
qWarning("QFile::open: File already open");
812
if ((mode & (ReadOnly | WriteOnly)) == 0) {
813
qWarning("QIODevice::open: File access not specified");
816
if (fileEngine()->open(mode)) {
820
QFile::FileError err = fileEngine()->error();
821
if(err == QFile::UnspecifiedError)
822
err = QFile::OpenError;
823
d->setError(err, fileEngine()->errorString());
827
/*! \fn QFile::open(OpenMode, FILE*)
829
Use open(FILE *, OpenMode) instead.
835
Opens the existing file handle \a fh in the given \a mode.
836
Returns true if successful; otherwise returns false.
842
void printError(const char* msg)
845
file.open(stderr, QIODevice::WriteOnly);
846
file.write(msg, qstrlen(msg)); // write to stderr
851
When a QFile is opened using this function, close() does not actually
852
close the file, but only flushes it.
854
\warning If \a fh is \c stdin, \c stdout, or \c stderr, you may not be
855
able to seek(). See QIODevice::isSequentialAccess() for more
860
bool QFile::open(FILE *fh, OpenMode mode)
864
qWarning("QFile::open: File already open");
870
if ((mode & (ReadOnly | WriteOnly)) == 0) {
871
qWarning("QFile::open: File access not specified");
875
// Implicitly set Unbuffered mode; buffering is already handled.
878
if(d->openExternalFile(mode, fh)) {
885
/*! \fn QFile::open(OpenMode, int)
887
Use open(int, OpenMode) instead.
893
Opens the existing file descripter \a fd in the given \a mode.
894
Returns true if successful; otherwise returns false.
896
When a QFile is opened using this function, close() does not
897
actually close the file.
899
The QFile that is opened using this function is automatically set
900
to be in raw mode; this means that the file input/output functions
901
are slow. If you run into performance issues, you should try to
902
use one of the other open functions.
904
\warning If \a fd is 0 (\c stdin), 1 (\c stdout), or 2 (\c
905
stderr), you may not be able to seek(). size() is set to \c
906
LLONG_MAX (in \c <climits>).
910
bool QFile::open(int fd, OpenMode mode)
914
qWarning("QFile::open: File already open");
920
if ((mode & (ReadOnly | WriteOnly)) == 0) {
921
qWarning("QFile::open: File access not specified");
924
if(d->openExternalFile(mode, fd)) {
932
Returns the file handle of the file.
934
This is a small positive integer, suitable for use with C library
935
functions such as fdopen() and fcntl(). On systems that use file
936
descriptors for sockets (i.e. Unix systems, but not Windows) the handle
937
can be used with QSocketNotifier as well.
939
If the file is not open, or there is an error, handle() returns -1.
945
QFile::handle() const
949
QFileEngine *engine = fileEngine();
950
if(engine->type() == QFileEngine::File)
951
return static_cast<QFSFileEngine*>(engine)->handle();
956
\fn QString QFile::name() const
958
Use fileName() instead.
962
\fn void QFile::setName(const QString &name)
964
Use setFileName() instead.
968
Sets the file size (in bytes) \a sz. Returns true if the file if the
969
resize succeeds; false otherwise. If \a sz is larger than the file
970
currently is the new bytes will be set to 0, if \a sz is smaller the
971
file is simply truncated.
973
\sa size(), setFileName()
977
QFile::resize(qint64 sz)
980
if(fileEngine()->setSize(sz)) {
984
d->setError(QFile::ResizeError, errno);
991
Sets \a fileName to size (in bytes) \a sz. Returns true if the file if
992
the resize succeeds; false otherwise. If \a sz is larger than \a
993
fileName currently is the new bytes will be set to 0, if \a sz is
994
smaller the file is simply truncated.
1000
QFile::resize(const QString &fileName, qint64 sz)
1002
return QFile(fileName).resize(sz);
1006
Returns the complete OR-ed together combination of
1007
QFile::Permission for the file.
1009
\sa setPermissions(), setFileName()
1013
QFile::permissions() const
1015
QFileEngine::FileFlags perms = fileEngine()->fileFlags(QFileEngine::PermsMask) & QFileEngine::PermsMask;
1016
return QFile::Permissions((int)perms); //ewww
1022
Returns the complete OR-ed together combination of
1023
QFile::Permission for \a fileName.
1027
QFile::permissions(const QString &fileName)
1029
return QFile(fileName).permissions();
1033
Sets the permissions for the file to \a permissions.
1035
\sa permissions(), setFileName()
1039
QFile::setPermissions(Permissions permissions)
1042
if(fileEngine()->chmod(permissions)) {
1046
d->setError(QFile::PermissionsError, errno);
1053
Sets the permissions for \a fileName file to \a permissions.
1057
QFile::setPermissions(const QString &fileName, Permissions permissions)
1059
return QFile(fileName).setPermissions(permissions);
1063
Flushes any buffered data to the file.
1069
fileEngine()->flush();
1086
#ifndef QT_NO_FILE_BUFFER
1089
if(!fileEngine()->close())
1090
d->setError(fileEngine()->error(), fileEngine()->errorString());
1097
qint64 QFile::size() const
1099
return fileEngine()->size();
1106
qint64 QFile::pos() const
1111
#ifndef QT_NO_FILE_BUFFER
1112
return fileEngine()->at() - d->buffer.used();
1114
return fileEngine()->at();
1122
bool QFile::atEnd() const
1127
if(!d->buffer.isEmpty())
1129
return QIODevice::atEnd();
1136
bool QFile::seek(qint64 off)
1140
qWarning("QFile::seek: IODevice is not open");
1144
QIODevice::seek(off);
1145
if(!fileEngine()->seek(off)) {
1146
QFile::FileError err = fileEngine()->error();
1147
if(err == QFile::UnspecifiedError)
1148
err = QFile::PositionError;
1149
d->setError(err, fileEngine()->errorString());
1152
#ifndef QT_NO_FILE_BUFFER
1162
qint64 QFile::readLineData(char *data, qint64 maxlen)
1165
#ifndef QT_NO_FILE_BUFFER
1166
if (openMode() & Unbuffered)
1168
return QIODevice::readLineData(data, maxlen);
1170
#ifndef QT_NO_FILE_BUFFER
1171
qint64 readSoFar = 0;
1172
bool foundEndOfLine = false;
1175
// get a pointer to the buffer
1177
char *ptr = d->buffer.take(d->buffer.used(), &realSize);
1179
// search for a '\n' character, copy over data as we search
1182
while (i < realSize) {
1184
if (ptr[i - 1] == '\n') {
1185
foundEndOfLine = true;
1190
// strip '\r' if in Text mode
1191
if (openMode() & Text) {
1192
char *readPtr = ptr;
1193
char *endPtr = ptr + i;
1195
while (*readPtr != '\r' && readPtr != endPtr)
1197
char *writePtr = readPtr;
1199
while (readPtr != endPtr && i > 0) {
1208
memcpy(data + readSoFar, ptr, i);
1213
// return if it was found
1214
if (foundEndOfLine) {
1215
if (readSoFar < maxlen)
1216
data[readSoFar] = '\0';
1221
int bytesToRead = qMin(read_cache_size, int(maxlen - readSoFar));
1222
if (bytesToRead == 0) {
1223
if (readSoFar < maxlen)
1224
data[readSoFar] = '\0';
1228
char *buffer = d->buffer.alloc(bytesToRead);
1229
qint64 bytesRead = fileEngine()->read(buffer, bytesToRead);
1231
if (bytesRead != bytesToRead) {
1233
d->buffer.truncate(bytesToRead);
1235
d->buffer.truncate(bytesToRead - bytesRead);
1238
if (bytesRead <= 0) {
1239
if (readSoFar < maxlen)
1240
data[readSoFar] = '\0';
1241
return readSoFar > 0 ? readSoFar : qint64(-1);
1253
qint64 QFile::readData(char *data, qint64 len)
1259
#ifndef QT_NO_FILE_BUFFER
1260
if ((openMode() & Unbuffered) == 0) {
1262
while(ret != len && !d->buffer.isEmpty()) {
1263
uint buffered = qMin(len, (qint64)d->buffer.used());
1264
char *buffer = d->buffer.take(buffered, &buffered);
1265
memcpy(data+ret, buffer, buffered);
1266
d->buffer.free(buffered);
1271
if(len > read_cache_size) {
1272
qint64 read = fileEngine()->read(data+ret, len-ret);
1276
char *buffer = d->buffer.alloc(read_cache_size);
1277
qint64 got = fileEngine()->read(buffer, read_cache_size);
1279
if(got < read_cache_size)
1280
d->buffer.truncate(read_cache_size - got);
1281
const qint64 need = qMin(len-ret, got);
1282
memcpy(data+ret, buffer, need);
1283
d->buffer.free(need);
1288
d->buffer.truncate(read_cache_size);
1294
qint64 read = fileEngine()->read(data+ret, len-ret);
1297
#ifndef QT_NO_FILE_BUFFER
1302
QFile::FileError err = fileEngine()->error();
1303
if(err == QFile::UnspecifiedError)
1304
err = QFile::ReadError;
1305
d->setError(err, fileEngine()->errorString());
1315
QFile::writeData(const char *data, qint64 len)
1320
#ifndef QT_NO_FILE_BUFFER
1321
if(!d->buffer.isEmpty())
1324
qint64 ret = fileEngine()->write(data, len);
1326
QFile::FileError err = fileEngine()->error();
1327
if(err == QFile::UnspecifiedError)
1328
err = QFile::WriteError;
1329
d->setError(err, fileEngine()->errorString());
1336
Returns the QIOEngine for this QFile object.
1340
*QFile::fileEngine() const
1344
d->fileEngine = QFileEngine::createFileEngine(d->fileName);
1345
return d->fileEngine;
1349
Returns the file error status.
1351
The I/O device status returns an error code. For example, if open()
1352
returns false, or a read/write operation returns -1, this function can
1353
be called to find out the reason why the operation failed.
1359
QFile::error() const
1366
Sets the file's error to QFile::NoError.
1374
d->setError(QFile::NoError);
1378
Returns a human-readable description of an error that occurred on
1379
the device. The error described by the string corresponds to
1380
changes of QFile::error(). If the status is reset, the error
1381
string is also reset.
1384
QFile file("address.dat");
1385
if (!file.open(QIODevice::ReadOnly) {
1386
QMessageBox::critical(this, tr("Error"),
1387
tr("Could not open file for reading: %1")
1388
.arg(file.errorString()));
1396
/* ### NEEDS TO BE FIXED
1398
QFile::errorString() const
1400
if (d->errorString.isEmpty()) {
1401
const char *str = 0;
1404
case UnspecifiedError:
1405
str = QT_TRANSLATE_NOOP("QFile", "Unknown error");
1408
str = QT_TRANSLATE_NOOP("QFile", "Could not read from the file");
1411
str = QT_TRANSLATE_NOOP("QFile", "Could not write to the file");
1414
str = QT_TRANSLATE_NOOP("QFile", "Fatal error");
1417
str = QT_TRANSLATE_NOOP("QFile", "Resource error");
1420
str = QT_TRANSLATE_NOOP("QFile", "Could not open the file");
1424
str = QT_TRANSLATE_NOOP("QFile", "Could not connect to host");
1428
str = QT_TRANSLATE_NOOP("QFile", "Aborted");
1431
str = QT_TRANSLATE_NOOP("QFile", "Timeout");
1434
str = QT_TRANSLATE_NOOP("QFile", "Could not remove file");
1437
str = QT_TRANSLATE_NOOP("QFile", "Could not rename file");
1440
str = QT_TRANSLATE_NOOP("QFile", "Could not position in file");
1442
case PermissionsError:
1443
str = QT_TRANSLATE_NOOP("QFile", "Failure to set Permissions");
1446
str = QT_TRANSLATE_NOOP("QFile", "Could not copy file");
1449
str = QT_TRANSLATE_NOOP("QFile", "Could not resize file");
1452
#if defined(QT_BUILD_CORE_LIB)
1453
QString ret = QCoreApplication::translate("QFile", str);
1456
ret = QCoreApplication::translate("QIODevice", str);
1460
return QString::fromLatin1(str);
1463
return d->errorString;