author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 3 | 41300fa6a67c |
child 7 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qplatformdefs.h" |
|
43 |
#include "qdebug.h" |
|
44 |
#include "qfile.h" |
|
45 |
#include "qfsfileengine.h" |
|
46 |
#include "qtemporaryfile.h" |
|
47 |
#include "qlist.h" |
|
48 |
#include "qfileinfo.h" |
|
49 |
#include "private/qiodevice_p.h" |
|
50 |
#include "private/qfile_p.h" |
|
51 |
#if defined(QT_BUILD_CORE_LIB) |
|
52 |
# include "qcoreapplication.h" |
|
53 |
#endif |
|
54 |
||
55 |
#ifdef QT_NO_QOBJECT |
|
56 |
#define tr(X) QString::fromLatin1(X) |
|
57 |
#endif |
|
58 |
||
59 |
QT_BEGIN_NAMESPACE |
|
60 |
||
61 |
static const int QFILE_WRITEBUFFER_SIZE = 16384; |
|
62 |
||
63 |
static QByteArray locale_encode(const QString &f) |
|
64 |
{ |
|
65 |
#ifndef Q_OS_DARWIN |
|
66 |
return f.toLocal8Bit(); |
|
67 |
#else |
|
68 |
// Mac always expects UTF-8... and decomposed... |
|
69 |
return f.normalized(QString::NormalizationForm_D).toUtf8(); |
|
70 |
#endif |
|
71 |
} |
|
72 |
||
73 |
static QString locale_decode(const QByteArray &f) |
|
74 |
{ |
|
75 |
#ifndef Q_OS_DARWIN |
|
76 |
return QString::fromLocal8Bit(f); |
|
77 |
#else |
|
78 |
// Mac always gives us UTF-8 and decomposed, we want that composed... |
|
79 |
return QString::fromUtf8(f).normalized(QString::NormalizationForm_C); |
|
80 |
#endif |
|
81 |
} |
|
82 |
||
83 |
//************* QFilePrivate |
|
84 |
QFile::EncoderFn QFilePrivate::encoder = locale_encode; |
|
85 |
QFile::DecoderFn QFilePrivate::decoder = locale_decode; |
|
86 |
||
87 |
QFilePrivate::QFilePrivate() |
|
88 |
: fileEngine(0), lastWasWrite(false), |
|
89 |
writeBuffer(QFILE_WRITEBUFFER_SIZE), error(QFile::NoError) |
|
90 |
{ |
|
91 |
} |
|
92 |
||
93 |
QFilePrivate::~QFilePrivate() |
|
94 |
{ |
|
95 |
delete fileEngine; |
|
96 |
fileEngine = 0; |
|
97 |
} |
|
98 |
||
99 |
bool |
|
100 |
QFilePrivate::openExternalFile(int flags, int fd) |
|
101 |
{ |
|
102 |
#ifdef QT_NO_FSFILEENGINE |
|
103 |
Q_UNUSED(flags); |
|
104 |
Q_UNUSED(fd); |
|
105 |
return false; |
|
106 |
#else |
|
107 |
delete fileEngine; |
|
108 |
fileEngine = 0; |
|
109 |
QFSFileEngine *fe = new QFSFileEngine; |
|
110 |
fe->setFileName(fileName); |
|
111 |
fileEngine = fe; |
|
112 |
return fe->open(QIODevice::OpenMode(flags), fd); |
|
113 |
#endif |
|
114 |
} |
|
115 |
||
116 |
bool |
|
117 |
QFilePrivate::openExternalFile(int flags, FILE *fh) |
|
118 |
{ |
|
119 |
#ifdef QT_NO_FSFILEENGINE |
|
120 |
Q_UNUSED(flags); |
|
121 |
Q_UNUSED(fh); |
|
122 |
return false; |
|
123 |
#else |
|
124 |
delete fileEngine; |
|
125 |
fileEngine = 0; |
|
126 |
QFSFileEngine *fe = new QFSFileEngine; |
|
127 |
fe->setFileName(fileName); |
|
128 |
fileEngine = fe; |
|
129 |
return fe->open(QIODevice::OpenMode(flags), fh); |
|
130 |
#endif |
|
131 |
} |
|
132 |
||
133 |
inline bool QFilePrivate::ensureFlushed() const |
|
134 |
{ |
|
135 |
// This function ensures that the write buffer has been flushed (const |
|
136 |
// because certain const functions need to call it. |
|
137 |
if (lastWasWrite) { |
|
138 |
const_cast<QFilePrivate *>(this)->lastWasWrite = false; |
|
139 |
if (!const_cast<QFile *>(q_func())->flush()) |
|
140 |
return false; |
|
141 |
} |
|
142 |
return true; |
|
143 |
} |
|
144 |
||
145 |
void |
|
146 |
QFilePrivate::setError(QFile::FileError err) |
|
147 |
{ |
|
148 |
error = err; |
|
149 |
errorString.clear(); |
|
150 |
} |
|
151 |
||
152 |
void |
|
153 |
QFilePrivate::setError(QFile::FileError err, const QString &errStr) |
|
154 |
{ |
|
155 |
error = err; |
|
156 |
errorString = errStr; |
|
157 |
} |
|
158 |
||
159 |
void |
|
160 |
QFilePrivate::setError(QFile::FileError err, int errNum) |
|
161 |
{ |
|
162 |
error = err; |
|
163 |
errorString = qt_error_string(errNum); |
|
164 |
} |
|
165 |
||
166 |
//************* QFile |
|
167 |
||
168 |
/*! |
|
169 |
\class QFile |
|
170 |
\brief The QFile class provides an interface for reading from and writing to files. |
|
171 |
||
172 |
\ingroup io |
|
173 |
||
174 |
\reentrant |
|
175 |
||
176 |
QFile is an I/O device for reading and writing text and binary |
|
177 |
files and \l{The Qt Resource System}{resources}. A QFile may be |
|
178 |
used by itself or, more conveniently, with a QTextStream or |
|
179 |
QDataStream. |
|
180 |
||
181 |
The file name is usually passed in the constructor, but it can be |
|
182 |
set at any time using setFileName(). QFile expects the file |
|
183 |
separator to be '/' regardless of operating system. The use of |
|
184 |
other separators (e.g., '\\') is not supported. |
|
185 |
||
186 |
You can check for a file's existence using exists(), and remove a |
|
187 |
file using remove(). (More advanced file system related operations |
|
188 |
are provided by QFileInfo and QDir.) |
|
189 |
||
190 |
The file is opened with open(), closed with close(), and flushed |
|
191 |
with flush(). Data is usually read and written using QDataStream |
|
192 |
or QTextStream, but you can also call the QIODevice-inherited |
|
193 |
functions read(), readLine(), readAll(), write(). QFile also |
|
194 |
inherits getChar(), putChar(), and ungetChar(), which work one |
|
195 |
character at a time. |
|
196 |
||
197 |
The size of the file is returned by size(). You can get the |
|
198 |
current file position using pos(), or move to a new file position |
|
199 |
using seek(). If you've reached the end of the file, atEnd() |
|
200 |
returns true. |
|
201 |
||
202 |
\section1 Reading Files Directly |
|
203 |
||
204 |
The following example reads a text file line by line: |
|
205 |
||
206 |
\snippet doc/src/snippets/file/file.cpp 0 |
|
207 |
||
208 |
The QIODevice::Text flag passed to open() tells Qt to convert |
|
209 |
Windows-style line terminators ("\\r\\n") into C++-style |
|
210 |
terminators ("\\n"). By default, QFile assumes binary, i.e. it |
|
211 |
doesn't perform any conversion on the bytes stored in the file. |
|
212 |
||
213 |
\section1 Using Streams to Read Files |
|
214 |
||
215 |
The next example uses QTextStream to read a text file |
|
216 |
line by line: |
|
217 |
||
218 |
\snippet doc/src/snippets/file/file.cpp 1 |
|
219 |
||
220 |
QTextStream takes care of converting the 8-bit data stored on |
|
221 |
disk into a 16-bit Unicode QString. By default, it assumes that |
|
222 |
the user system's local 8-bit encoding is used (e.g., ISO 8859-1 |
|
223 |
for most of Europe; see QTextCodec::codecForLocale() for |
|
224 |
details). This can be changed using setCodec(). |
|
225 |
||
226 |
To write text, we can use operator<<(), which is overloaded to |
|
227 |
take a QTextStream on the left and various data types (including |
|
228 |
QString) on the right: |
|
229 |
||
230 |
\snippet doc/src/snippets/file/file.cpp 2 |
|
231 |
||
232 |
QDataStream is similar, in that you can use operator<<() to write |
|
233 |
data and operator>>() to read it back. See the class |
|
234 |
documentation for details. |
|
235 |
||
236 |
When you use QFile, QFileInfo, and QDir to access the file system |
|
237 |
with Qt, you can use Unicode file names. On Unix, these file |
|
238 |
names are converted to an 8-bit encoding. If you want to use |
|
239 |
standard C++ APIs (\c <cstdio> or \c <iostream>) or |
|
240 |
platform-specific APIs to access files instead of QFile, you can |
|
241 |
use the encodeName() and decodeName() functions to convert |
|
242 |
between Unicode file names and 8-bit file names. |
|
243 |
||
244 |
On Unix, there are some special system files (e.g. in \c /proc) for which |
|
245 |
size() will always return 0, yet you may still be able to read more data |
|
246 |
from such a file; the data is generated in direct response to you calling |
|
247 |
read(). In this case, however, you cannot use atEnd() to determine if |
|
248 |
there is more data to read (since atEnd() will return true for a file that |
|
249 |
claims to have size 0). Instead, you should either call readAll(), or call |
|
250 |
read() or readLine() repeatedly until no more data can be read. The next |
|
251 |
example uses QTextStream to read \c /proc/modules line by line: |
|
252 |
||
253 |
\snippet doc/src/snippets/file/file.cpp 3 |
|
254 |
||
255 |
\section1 Signals |
|
256 |
||
257 |
Unlike other QIODevice implementations, such as QTcpSocket, QFile does not |
|
258 |
emit the aboutToClose(), bytesWritten(), or readyRead() signals. This |
|
259 |
implementation detail means that QFile is not suitable for reading and |
|
260 |
writing certain types of files, such as device files on Unix platforms. |
|
261 |
||
262 |
\section1 Platform Specific Issues |
|
263 |
||
264 |
File permissions are handled differently on Linux/Mac OS X and |
|
265 |
Windows. In a non \l{QIODevice::isWritable()}{writable} |
|
266 |
directory on Linux, files cannot be created. This is not always |
|
267 |
the case on Windows, where, for instance, the 'My Documents' |
|
268 |
directory usually is not writable, but it is still possible to |
|
269 |
create files in it. |
|
270 |
||
271 |
\sa QTextStream, QDataStream, QFileInfo, QDir, {The Qt Resource System} |
|
272 |
*/ |
|
273 |
||
274 |
/*! |
|
275 |
\enum QFile::FileError |
|
276 |
||
277 |
This enum describes the errors that may be returned by the error() |
|
278 |
function. |
|
279 |
||
280 |
\value NoError No error occurred. |
|
281 |
\value ReadError An error occurred when reading from the file. |
|
282 |
\value WriteError An error occurred when writing to the file. |
|
283 |
\value FatalError A fatal error occurred. |
|
284 |
\value ResourceError |
|
285 |
\value OpenError The file could not be opened. |
|
286 |
\value AbortError The operation was aborted. |
|
287 |
\value TimeOutError A timeout occurred. |
|
288 |
\value UnspecifiedError An unspecified error occurred. |
|
289 |
\value RemoveError The file could not be removed. |
|
290 |
\value RenameError The file could not be renamed. |
|
291 |
\value PositionError The position in the file could not be changed. |
|
292 |
\value ResizeError The file could not be resized. |
|
293 |
\value PermissionsError The file could not be accessed. |
|
294 |
\value CopyError The file could not be copied. |
|
295 |
||
296 |
\omitvalue ConnectError |
|
297 |
*/ |
|
298 |
||
299 |
/*! |
|
300 |
\enum QFile::Permission |
|
301 |
||
302 |
This enum is used by the permission() function to report the |
|
303 |
permissions and ownership of a file. The values may be OR-ed |
|
304 |
together to test multiple permissions and ownership values. |
|
305 |
||
306 |
\value ReadOwner The file is readable by the owner of the file. |
|
307 |
\value WriteOwner The file is writable by the owner of the file. |
|
308 |
\value ExeOwner The file is executable by the owner of the file. |
|
309 |
\value ReadUser The file is readable by the user. |
|
310 |
\value WriteUser The file is writable by the user. |
|
311 |
\value ExeUser The file is executable by the user. |
|
312 |
\value ReadGroup The file is readable by the group. |
|
313 |
\value WriteGroup The file is writable by the group. |
|
314 |
\value ExeGroup The file is executable by the group. |
|
315 |
\value ReadOther The file is readable by anyone. |
|
316 |
\value WriteOther The file is writable by anyone. |
|
317 |
\value ExeOther The file is executable by anyone. |
|
318 |
||
319 |
\warning Because of differences in the platforms supported by Qt, |
|
320 |
the semantics of ReadUser, WriteUser and ExeUser are |
|
321 |
platform-dependent: On Unix, the rights of the owner of the file |
|
322 |
are returned and on Windows the rights of the current user are |
|
323 |
returned. This behavior might change in a future Qt version. |
|
324 |
||
325 |
Note that Qt does not by default check for permissions on NTFS |
|
326 |
file systems, as this may decrease the performance of file |
|
327 |
handling considerably. It is possible to force permission checking |
|
328 |
on NTFS by including the following code in your source: |
|
329 |
||
330 |
\snippet doc/src/snippets/ntfsp.cpp 0 |
|
331 |
||
332 |
Permission checking is then turned on and off by incrementing and |
|
333 |
decrementing \c qt_ntfs_permission_lookup by 1. |
|
334 |
||
335 |
\snippet doc/src/snippets/ntfsp.cpp 1 |
|
336 |
*/ |
|
337 |
||
338 |
#ifdef QT3_SUPPORT |
|
339 |
/*! |
|
340 |
\typedef QFile::PermissionSpec |
|
341 |
||
342 |
Use QFile::Permission instead. |
|
343 |
*/ |
|
344 |
#endif |
|
345 |
||
346 |
#ifdef QT_NO_QOBJECT |
|
347 |
QFile::QFile() |
|
348 |
: QIODevice(*new QFilePrivate) |
|
349 |
{ |
|
350 |
} |
|
351 |
QFile::QFile(const QString &name) |
|
352 |
: QIODevice(*new QFilePrivate) |
|
353 |
{ |
|
354 |
d_func()->fileName = name; |
|
355 |
} |
|
356 |
QFile::QFile(QFilePrivate &dd) |
|
357 |
: QIODevice(dd) |
|
358 |
{ |
|
359 |
} |
|
360 |
#else |
|
361 |
/*! |
|
362 |
\internal |
|
363 |
*/ |
|
364 |
QFile::QFile() |
|
365 |
: QIODevice(*new QFilePrivate, 0) |
|
366 |
{ |
|
367 |
} |
|
368 |
/*! |
|
369 |
Constructs a new file object with the given \a parent. |
|
370 |
*/ |
|
371 |
QFile::QFile(QObject *parent) |
|
372 |
: QIODevice(*new QFilePrivate, parent) |
|
373 |
{ |
|
374 |
} |
|
375 |
/*! |
|
376 |
Constructs a new file object to represent the file with the given \a name. |
|
377 |
*/ |
|
378 |
QFile::QFile(const QString &name) |
|
379 |
: QIODevice(*new QFilePrivate, 0) |
|
380 |
{ |
|
381 |
Q_D(QFile); |
|
382 |
d->fileName = name; |
|
383 |
} |
|
384 |
/*! |
|
385 |
Constructs a new file object with the given \a parent to represent the |
|
386 |
file with the specified \a name. |
|
387 |
*/ |
|
388 |
QFile::QFile(const QString &name, QObject *parent) |
|
389 |
: QIODevice(*new QFilePrivate, parent) |
|
390 |
{ |
|
391 |
Q_D(QFile); |
|
392 |
d->fileName = name; |
|
393 |
} |
|
394 |
/*! |
|
395 |
\internal |
|
396 |
*/ |
|
397 |
QFile::QFile(QFilePrivate &dd, QObject *parent) |
|
398 |
: QIODevice(dd, parent) |
|
399 |
{ |
|
400 |
} |
|
401 |
#endif |
|
402 |
||
403 |
/*! |
|
404 |
Destroys the file object, closing it if necessary. |
|
405 |
*/ |
|
406 |
QFile::~QFile() |
|
407 |
{ |
|
408 |
close(); |
|
409 |
} |
|
410 |
||
411 |
/*! |
|
412 |
Returns the name set by setFileName() or to the QFile |
|
413 |
constructors. |
|
414 |
||
415 |
\sa setFileName(), QFileInfo::fileName() |
|
416 |
*/ |
|
417 |
QString QFile::fileName() const |
|
418 |
{ |
|
419 |
return fileEngine()->fileName(QAbstractFileEngine::DefaultName); |
|
420 |
} |
|
421 |
||
422 |
/*! |
|
423 |
Sets the \a name of the file. The name can have no path, a |
|
424 |
relative path, or an absolute path. |
|
425 |
||
426 |
Do not call this function if the file has already been opened. |
|
427 |
||
428 |
If the file name has no path or a relative path, the path used |
|
429 |
will be the application's current directory path |
|
430 |
\e{at the time of the open()} call. |
|
431 |
||
432 |
Example: |
|
433 |
\snippet doc/src/snippets/code/src_corelib_io_qfile.cpp 0 |
|
434 |
||
435 |
Note that the directory separator "/" works for all operating |
|
436 |
systems supported by Qt. |
|
437 |
||
438 |
\sa fileName(), QFileInfo, QDir |
|
439 |
*/ |
|
440 |
void |
|
441 |
QFile::setFileName(const QString &name) |
|
442 |
{ |
|
443 |
Q_D(QFile); |
|
444 |
if (isOpen()) { |
|
445 |
qWarning("QFile::setFileName: File (%s) is already opened", |
|
446 |
qPrintable(fileName())); |
|
447 |
close(); |
|
448 |
} |
|
449 |
if(d->fileEngine) { //get a new file engine later |
|
450 |
delete d->fileEngine; |
|
451 |
d->fileEngine = 0; |
|
452 |
} |
|
453 |
d->fileName = name; |
|
454 |
} |
|
455 |
||
456 |
/*! |
|
457 |
\fn QString QFile::decodeName(const char *localFileName) |
|
458 |
||
459 |
\overload |
|
460 |
||
461 |
Returns the Unicode version of the given \a localFileName. See |
|
462 |
encodeName() for details. |
|
463 |
*/ |
|
464 |
||
465 |
/*! |
|
466 |
By default, this function converts \a fileName to the local 8-bit |
|
467 |
encoding determined by the user's locale. This is sufficient for |
|
468 |
file names that the user chooses. File names hard-coded into the |
|
469 |
application should only use 7-bit ASCII filename characters. |
|
470 |
||
471 |
\sa decodeName() setEncodingFunction() |
|
472 |
*/ |
|
473 |
||
474 |
QByteArray |
|
475 |
QFile::encodeName(const QString &fileName) |
|
476 |
{ |
|
477 |
return (*QFilePrivate::encoder)(fileName); |
|
478 |
} |
|
479 |
||
480 |
/*! |
|
481 |
\typedef QFile::EncoderFn |
|
482 |
||
483 |
This is a typedef for a pointer to a function with the following |
|
484 |
signature: |
|
485 |
||
486 |
\snippet doc/src/snippets/code/src_corelib_io_qfile.cpp 1 |
|
487 |
||
488 |
\sa setEncodingFunction(), encodeName() |
|
489 |
*/ |
|
490 |
||
491 |
/*! |
|
492 |
This does the reverse of QFile::encodeName() using \a localFileName. |
|
493 |
||
494 |
\sa setDecodingFunction(), encodeName() |
|
495 |
*/ |
|
496 |
||
497 |
QString |
|
498 |
QFile::decodeName(const QByteArray &localFileName) |
|
499 |
{ |
|
500 |
return (*QFilePrivate::decoder)(localFileName); |
|
501 |
} |
|
502 |
||
503 |
/*! |
|
504 |
\fn void QFile::setEncodingFunction(EncoderFn function) |
|
505 |
||
506 |
\nonreentrant |
|
507 |
||
508 |
Sets the \a function for encoding Unicode file names. The |
|
509 |
default encodes in the locale-specific 8-bit encoding. |
|
510 |
||
511 |
\sa encodeName(), setDecodingFunction() |
|
512 |
*/ |
|
513 |
||
514 |
void |
|
515 |
QFile::setEncodingFunction(EncoderFn f) |
|
516 |
{ |
|
517 |
if (!f) |
|
518 |
f = locale_encode; |
|
519 |
QFilePrivate::encoder = f; |
|
520 |
} |
|
521 |
||
522 |
/*! |
|
523 |
\typedef QFile::DecoderFn |
|
524 |
||
525 |
This is a typedef for a pointer to a function with the following |
|
526 |
signature: |
|
527 |
||
528 |
\snippet doc/src/snippets/code/src_corelib_io_qfile.cpp 2 |
|
529 |
||
530 |
\sa setDecodingFunction() |
|
531 |
*/ |
|
532 |
||
533 |
/*! |
|
534 |
\fn void QFile::setDecodingFunction(DecoderFn function) |
|
535 |
||
536 |
\nonreentrant |
|
537 |
||
538 |
Sets the \a function for decoding 8-bit file names. The |
|
539 |
default uses the locale-specific 8-bit encoding. |
|
540 |
||
541 |
\sa setEncodingFunction(), decodeName() |
|
542 |
*/ |
|
543 |
||
544 |
void |
|
545 |
QFile::setDecodingFunction(DecoderFn f) |
|
546 |
{ |
|
547 |
if (!f) |
|
548 |
f = locale_decode; |
|
549 |
QFilePrivate::decoder = f; |
|
550 |
} |
|
551 |
||
552 |
/*! |
|
553 |
\overload |
|
554 |
||
555 |
Returns true if the file specified by fileName() exists; otherwise |
|
556 |
returns false. |
|
557 |
||
558 |
\sa fileName(), setFileName() |
|
559 |
*/ |
|
560 |
||
561 |
bool |
|
562 |
QFile::exists() const |
|
563 |
{ |
|
564 |
// 0x1000000 = QAbstractFileEngine::Refresh, forcing an update |
|
565 |
return (fileEngine()->fileFlags(QAbstractFileEngine::FlagsMask |
|
566 |
| QAbstractFileEngine::FileFlag(0x1000000)) & QAbstractFileEngine::ExistsFlag); |
|
567 |
} |
|
568 |
||
569 |
/*! |
|
570 |
Returns true if the file specified by \a fileName exists; otherwise |
|
571 |
returns false. |
|
572 |
*/ |
|
573 |
||
574 |
bool |
|
575 |
QFile::exists(const QString &fileName) |
|
576 |
{ |
|
577 |
return QFileInfo(fileName).exists(); |
|
578 |
} |
|
579 |
||
580 |
/*! |
|
581 |
\fn QString QFile::symLinkTarget() const |
|
582 |
\since 4.2 |
|
583 |
\overload |
|
584 |
||
585 |
Returns the absolute path of the file or directory a symlink (or shortcut |
|
586 |
on Windows) points to, or a an empty string if the object isn't a symbolic |
|
587 |
link. |
|
588 |
||
589 |
This name may not represent an existing file; it is only a string. |
|
590 |
QFile::exists() returns true if the symlink points to an existing file. |
|
591 |
||
592 |
\sa fileName() setFileName() |
|
593 |
*/ |
|
594 |
||
595 |
/*! |
|
596 |
\obsolete |
|
597 |
||
598 |
Use symLinkTarget() instead. |
|
599 |
*/ |
|
600 |
QString |
|
601 |
QFile::readLink() const |
|
602 |
{ |
|
603 |
return fileEngine()->fileName(QAbstractFileEngine::LinkName); |
|
604 |
} |
|
605 |
||
606 |
/*! |
|
607 |
\fn static QString QFile::symLinkTarget(const QString &fileName) |
|
608 |
\since 4.2 |
|
609 |
||
610 |
Returns the absolute path of the file or directory referred to by the |
|
611 |
symlink (or shortcut on Windows) specified by \a fileName, or returns an |
|
612 |
empty string if the \a fileName does not correspond to a symbolic link. |
|
613 |
||
614 |
This name may not represent an existing file; it is only a string. |
|
615 |
QFile::exists() returns true if the symlink points to an existing file. |
|
616 |
*/ |
|
617 |
||
618 |
/*! |
|
619 |
\obsolete |
|
620 |
||
621 |
Use symLinkTarget() instead. |
|
622 |
*/ |
|
623 |
QString |
|
624 |
QFile::readLink(const QString &fileName) |
|
625 |
{ |
|
626 |
return QFileInfo(fileName).readLink(); |
|
627 |
} |
|
628 |
||
629 |
/*! |
|
630 |
Removes the file specified by fileName(). Returns true if successful; |
|
631 |
otherwise returns false. |
|
632 |
||
633 |
The file is closed before it is removed. |
|
634 |
||
635 |
\sa setFileName() |
|
636 |
*/ |
|
637 |
||
638 |
bool |
|
639 |
QFile::remove() |
|
640 |
{ |
|
641 |
Q_D(QFile); |
|
642 |
if (d->fileName.isEmpty()) { |
|
643 |
qWarning("QFile::remove: Empty or null file name"); |
|
644 |
return false; |
|
645 |
} |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
646 |
unsetError(); |
0 | 647 |
close(); |
648 |
if(error() == QFile::NoError) { |
|
649 |
if(fileEngine()->remove()) { |
|
650 |
unsetError(); |
|
651 |
return true; |
|
652 |
} |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
653 |
d->setError(QFile::RemoveError, d->fileEngine->errorString()); |
0 | 654 |
} |
655 |
return false; |
|
656 |
} |
|
657 |
||
658 |
/*! |
|
659 |
\overload |
|
660 |
||
661 |
Removes the file specified by the \a fileName given. |
|
662 |
||
663 |
Returns true if successful; otherwise returns false. |
|
664 |
||
665 |
\sa remove() |
|
666 |
*/ |
|
667 |
||
668 |
bool |
|
669 |
QFile::remove(const QString &fileName) |
|
670 |
{ |
|
671 |
return QFile(fileName).remove(); |
|
672 |
} |
|
673 |
||
674 |
/*! |
|
675 |
Renames the file currently specified by fileName() to \a newName. |
|
676 |
Returns true if successful; otherwise returns false. |
|
677 |
||
678 |
If a file with the name \a newName already exists, rename() returns false |
|
679 |
(i.e., QFile will not overwrite it). |
|
680 |
||
681 |
The file is closed before it is renamed. |
|
682 |
||
683 |
\sa setFileName() |
|
684 |
*/ |
|
685 |
||
686 |
bool |
|
687 |
QFile::rename(const QString &newName) |
|
688 |
{ |
|
689 |
Q_D(QFile); |
|
690 |
if (d->fileName.isEmpty()) { |
|
691 |
qWarning("QFile::rename: Empty or null file name"); |
|
692 |
return false; |
|
693 |
} |
|
694 |
if (QFile(newName).exists()) { |
|
695 |
// ### Race condition. If a file is moved in after this, it /will/ be |
|
696 |
// overwritten. On Unix, the proper solution is to use hardlinks: |
|
697 |
// return ::link(old, new) && ::remove(old); |
|
698 |
d->setError(QFile::RenameError, tr("Destination file exists")); |
|
699 |
return false; |
|
700 |
} |
|
701 |
unsetError(); |
|
702 |
close(); |
|
703 |
if(error() == QFile::NoError) { |
|
704 |
if (fileEngine()->rename(newName)) { |
|
705 |
unsetError(); |
|
706 |
// engine was able to handle the new name so we just reset it |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
707 |
d->fileEngine->setFileName(newName); |
0 | 708 |
d->fileName = newName; |
709 |
return true; |
|
710 |
} |
|
711 |
||
712 |
if (isSequential()) { |
|
713 |
d->setError(QFile::RenameError, tr("Will not rename sequential file using block copy")); |
|
714 |
return false; |
|
715 |
} |
|
716 |
||
717 |
QFile out(newName); |
|
718 |
if (open(QIODevice::ReadOnly)) { |
|
719 |
if (out.open(QIODevice::WriteOnly | QIODevice::Truncate)) { |
|
720 |
bool error = false; |
|
721 |
char block[4096]; |
|
722 |
qint64 bytes; |
|
723 |
while ((bytes = read(block, sizeof(block))) > 0) { |
|
724 |
if (bytes != out.write(block, bytes)) { |
|
725 |
d->setError(QFile::RenameError, out.errorString()); |
|
726 |
error = true; |
|
727 |
break; |
|
728 |
} |
|
729 |
} |
|
730 |
if (bytes == -1) { |
|
731 |
d->setError(QFile::RenameError, errorString()); |
|
732 |
error = true; |
|
733 |
} |
|
734 |
if(!error) { |
|
735 |
if (!remove()) { |
|
736 |
d->setError(QFile::RenameError, tr("Cannot remove source file")); |
|
737 |
error = true; |
|
738 |
} |
|
739 |
} |
|
740 |
if (error) { |
|
741 |
out.remove(); |
|
742 |
} else { |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
743 |
d->fileEngine->setFileName(newName); |
0 | 744 |
setPermissions(permissions()); |
745 |
unsetError(); |
|
746 |
setFileName(newName); |
|
747 |
} |
|
748 |
close(); |
|
749 |
return !error; |
|
750 |
} |
|
751 |
close(); |
|
752 |
} |
|
753 |
d->setError(QFile::RenameError, out.isOpen() ? errorString() : out.errorString()); |
|
754 |
} |
|
755 |
return false; |
|
756 |
} |
|
757 |
||
758 |
/*! |
|
759 |
\overload |
|
760 |
||
761 |
Renames the file \a oldName to \a newName. Returns true if |
|
762 |
successful; otherwise returns false. |
|
763 |
||
764 |
If a file with the name \a newName already exists, rename() returns false |
|
765 |
(i.e., QFile will not overwrite it). |
|
766 |
||
767 |
\sa rename() |
|
768 |
*/ |
|
769 |
||
770 |
bool |
|
771 |
QFile::rename(const QString &oldName, const QString &newName) |
|
772 |
{ |
|
773 |
return QFile(oldName).rename(newName); |
|
774 |
} |
|
775 |
||
776 |
/*! |
|
777 |
||
778 |
Creates a link named \a linkName that points to the file currently specified by |
|
779 |
fileName(). What a link is depends on the underlying filesystem (be it a |
|
780 |
shortcut on Windows or a symbolic link on Unix). Returns true if successful; |
|
781 |
otherwise returns false. |
|
782 |
||
783 |
This function will not overwrite an already existing entity in the file system; |
|
784 |
in this case, \c link() will return false and set \l{QFile::}{error()} to |
|
785 |
return \l{QFile::}{RenameError}. |
|
786 |
||
787 |
\note To create a valid link on Windows, \a linkName must have a \c{.lnk} file extension. |
|
788 |
||
789 |
\note On Symbian, no link is created and false is returned if fileName() |
|
790 |
currently specifies a directory. |
|
791 |
||
792 |
\sa setFileName() |
|
793 |
*/ |
|
794 |
||
795 |
bool |
|
796 |
QFile::link(const QString &linkName) |
|
797 |
{ |
|
798 |
Q_D(QFile); |
|
799 |
if (d->fileName.isEmpty()) { |
|
800 |
qWarning("QFile::link: Empty or null file name"); |
|
801 |
return false; |
|
802 |
} |
|
803 |
QFileInfo fi(linkName); |
|
804 |
if(fileEngine()->link(fi.absoluteFilePath())) { |
|
805 |
unsetError(); |
|
806 |
return true; |
|
807 |
} |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
808 |
d->setError(QFile::RenameError, d->fileEngine->errorString()); |
0 | 809 |
return false; |
810 |
} |
|
811 |
||
812 |
/*! |
|
813 |
\overload |
|
814 |
||
815 |
Creates a link named \a linkName that points to the file \a fileName. What a link is |
|
816 |
depends on the underlying filesystem (be it a shortcut on Windows |
|
817 |
or a symbolic link on Unix). Returns true if successful; otherwise |
|
818 |
returns false. |
|
819 |
||
820 |
\sa link() |
|
821 |
*/ |
|
822 |
||
823 |
bool |
|
824 |
QFile::link(const QString &fileName, const QString &linkName) |
|
825 |
{ |
|
826 |
return QFile(fileName).link(linkName); |
|
827 |
} |
|
828 |
||
829 |
/*! |
|
830 |
Copies the file currently specified by fileName() to a file called |
|
831 |
\a newName. Returns true if successful; otherwise returns false. |
|
832 |
||
833 |
Note that if a file with the name \a newName already exists, |
|
834 |
copy() returns false (i.e. QFile will not overwrite it). |
|
835 |
||
836 |
The source file is closed before it is copied. |
|
837 |
||
838 |
\sa setFileName() |
|
839 |
*/ |
|
840 |
||
841 |
bool |
|
842 |
QFile::copy(const QString &newName) |
|
843 |
{ |
|
844 |
Q_D(QFile); |
|
845 |
if (d->fileName.isEmpty()) { |
|
846 |
qWarning("QFile::copy: Empty or null file name"); |
|
847 |
return false; |
|
848 |
} |
|
849 |
if (QFile(newName).exists()) { |
|
850 |
// ### Race condition. If a file is moved in after this, it /will/ be |
|
851 |
// overwritten. On Unix, the proper solution is to use hardlinks: |
|
852 |
// return ::link(old, new) && ::remove(old); See also rename(). |
|
853 |
d->setError(QFile::CopyError, tr("Destination file exists")); |
|
854 |
return false; |
|
855 |
} |
|
856 |
unsetError(); |
|
857 |
close(); |
|
858 |
if(error() == QFile::NoError) { |
|
859 |
if(fileEngine()->copy(newName)) { |
|
860 |
unsetError(); |
|
861 |
return true; |
|
862 |
} else { |
|
863 |
bool error = false; |
|
864 |
if(!open(QFile::ReadOnly)) { |
|
865 |
error = true; |
|
866 |
d->setError(QFile::CopyError, tr("Cannot open %1 for input").arg(d->fileName)); |
|
867 |
} else { |
|
868 |
QString fileTemplate = QLatin1String("%1/qt_temp.XXXXXX"); |
|
869 |
#ifdef QT_NO_TEMPORARYFILE |
|
870 |
QFile out(fileTemplate.arg(QFileInfo(newName).path())); |
|
871 |
if (!out.open(QIODevice::ReadWrite)) |
|
872 |
error = true; |
|
873 |
#else |
|
874 |
QTemporaryFile out(fileTemplate.arg(QFileInfo(newName).path())); |
|
875 |
if (!out.open()) { |
|
876 |
out.setFileTemplate(fileTemplate.arg(QDir::tempPath())); |
|
877 |
if (!out.open()) |
|
878 |
error = true; |
|
879 |
} |
|
880 |
#endif |
|
881 |
if (error) { |
|
882 |
out.close(); |
|
883 |
d->setError(QFile::CopyError, tr("Cannot open for output")); |
|
884 |
} else { |
|
885 |
char block[4096]; |
|
886 |
qint64 totalRead = 0; |
|
887 |
while(!atEnd()) { |
|
888 |
qint64 in = read(block, sizeof(block)); |
|
889 |
if (in <= 0) |
|
890 |
break; |
|
891 |
totalRead += in; |
|
892 |
if(in != out.write(block, in)) { |
|
893 |
d->setError(QFile::CopyError, tr("Failure to write block")); |
|
894 |
error = true; |
|
895 |
break; |
|
896 |
} |
|
897 |
} |
|
898 |
||
899 |
if (totalRead != size()) { |
|
900 |
// Unable to read from the source. The error string is |
|
901 |
// already set from read(). |
|
902 |
error = true; |
|
903 |
} |
|
904 |
if (!error && !out.rename(newName)) { |
|
905 |
error = true; |
|
906 |
d->setError(QFile::CopyError, tr("Cannot create %1 for output").arg(newName)); |
|
907 |
} |
|
908 |
#ifdef QT_NO_TEMPORARYFILE |
|
909 |
if (error) |
|
910 |
out.remove(); |
|
911 |
#else |
|
912 |
if (!error) |
|
913 |
out.setAutoRemove(false); |
|
914 |
#endif |
|
915 |
} |
|
916 |
close(); |
|
917 |
} |
|
918 |
if(!error) { |
|
919 |
QFile::setPermissions(newName, permissions()); |
|
920 |
unsetError(); |
|
921 |
return true; |
|
922 |
} |
|
923 |
} |
|
924 |
} |
|
925 |
return false; |
|
926 |
} |
|
927 |
||
928 |
/*! |
|
929 |
\overload |
|
930 |
||
931 |
Copies the file \a fileName to \a newName. Returns true if successful; |
|
932 |
otherwise returns false. |
|
933 |
||
934 |
If a file with the name \a newName already exists, copy() returns false |
|
935 |
(i.e., QFile will not overwrite it). |
|
936 |
||
937 |
\sa rename() |
|
938 |
*/ |
|
939 |
||
940 |
bool |
|
941 |
QFile::copy(const QString &fileName, const QString &newName) |
|
942 |
{ |
|
943 |
return QFile(fileName).copy(newName); |
|
944 |
} |
|
945 |
||
946 |
/*! |
|
947 |
Returns true if the file can only be manipulated sequentially; |
|
948 |
otherwise returns false. |
|
949 |
||
950 |
Most files support random-access, but some special files may not. |
|
951 |
||
952 |
\sa QIODevice::isSequential() |
|
953 |
*/ |
|
954 |
bool QFile::isSequential() const |
|
955 |
{ |
|
956 |
Q_D(const QFile); |
|
957 |
return d->fileEngine && d->fileEngine->isSequential(); |
|
958 |
} |
|
959 |
||
960 |
/*! |
|
961 |
Opens the file using OpenMode \a mode, returning true if successful; |
|
962 |
otherwise false. |
|
963 |
||
964 |
The \a mode must be QIODevice::ReadOnly, QIODevice::WriteOnly, or |
|
965 |
QIODevice::ReadWrite. It may also have additional flags, such as |
|
966 |
QIODevice::Text and QIODevice::Unbuffered. |
|
967 |
||
968 |
\note In \l{QIODevice::}{WriteOnly} or \l{QIODevice::}{ReadWrite} |
|
969 |
mode, if the relevant file does not already exist, this function |
|
970 |
will try to create a new file before opening it. |
|
971 |
||
972 |
\sa QIODevice::OpenMode, setFileName() |
|
973 |
*/ |
|
974 |
bool QFile::open(OpenMode mode) |
|
975 |
{ |
|
976 |
Q_D(QFile); |
|
977 |
if (isOpen()) { |
|
978 |
qWarning("QFile::open: File (%s) already open", qPrintable(fileName())); |
|
979 |
return false; |
|
980 |
} |
|
981 |
if (mode & Append) |
|
982 |
mode |= WriteOnly; |
|
983 |
||
984 |
unsetError(); |
|
985 |
if ((mode & (ReadOnly | WriteOnly)) == 0) { |
|
986 |
qWarning("QIODevice::open: File access not specified"); |
|
987 |
return false; |
|
988 |
} |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
989 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
990 |
// QIODevice provides the buffering, so there's no need to request it from the file engine. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
991 |
if (fileEngine()->open(mode | QIODevice::Unbuffered)) { |
0 | 992 |
QIODevice::open(mode); |
993 |
if (mode & Append) |
|
994 |
seek(size()); |
|
995 |
return true; |
|
996 |
} |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
997 |
QFile::FileError err = d->fileEngine->error(); |
0 | 998 |
if(err == QFile::UnspecifiedError) |
999 |
err = QFile::OpenError; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1000 |
d->setError(err, d->fileEngine->errorString()); |
0 | 1001 |
return false; |
1002 |
} |
|
1003 |
||
1004 |
/*! \fn QFile::open(OpenMode, FILE*) |
|
1005 |
||
1006 |
Use open(FILE *, OpenMode) instead. |
|
1007 |
*/ |
|
1008 |
||
1009 |
/*! |
|
1010 |
\overload |
|
1011 |
||
1012 |
Opens the existing file handle \a fh in the given \a mode. |
|
1013 |
Returns true if successful; otherwise returns false. |
|
1014 |
||
1015 |
Example: |
|
1016 |
\snippet doc/src/snippets/code/src_corelib_io_qfile.cpp 3 |
|
1017 |
||
1018 |
When a QFile is opened using this function, close() does not actually |
|
1019 |
close the file, but only flushes it. |
|
1020 |
||
1021 |
\bold{Warning:} |
|
1022 |
\list 1 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1023 |
\o If \a fh does not refer to a regular file, e.g., it is \c stdin, |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1024 |
\c stdout, or \c stderr, you may not be able to seek(). size() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1025 |
returns \c 0 in those cases. See QIODevice::isSequential() for |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1026 |
more information. |
0 | 1027 |
\o Since this function opens the file without specifying the file name, |
1028 |
you cannot use this QFile with a QFileInfo. |
|
1029 |
\endlist |
|
1030 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1031 |
\note For Windows CE you may not be able to call resize(). |
0 | 1032 |
|
1033 |
\sa close(), {qmake Variable Reference#CONFIG}{qmake Variable Reference} |
|
1034 |
||
1035 |
\bold{Note for the Windows Platform} |
|
1036 |
||
1037 |
\a fh must be opened in binary mode (i.e., the mode string must contain |
|
1038 |
'b', as in "rb" or "wb") when accessing files and other random-access |
|
1039 |
devices. Qt will translate the end-of-line characters if you pass |
|
1040 |
QIODevice::Text to \a mode. Sequential devices, such as stdin and stdout, |
|
1041 |
are unaffected by this limitation. |
|
1042 |
||
1043 |
You need to enable support for console applications in order to use the |
|
1044 |
stdin, stdout and stderr streams at the console. To do this, add the |
|
1045 |
following declaration to your application's project file: |
|
1046 |
||
1047 |
\snippet doc/src/snippets/code/src_corelib_io_qfile.cpp 4 |
|
1048 |
*/ |
|
1049 |
bool QFile::open(FILE *fh, OpenMode mode) |
|
1050 |
{ |
|
1051 |
Q_D(QFile); |
|
1052 |
if (isOpen()) { |
|
1053 |
qWarning("QFile::open: File (%s) already open", qPrintable(fileName())); |
|
1054 |
return false; |
|
1055 |
} |
|
1056 |
if (mode & Append) |
|
1057 |
mode |= WriteOnly; |
|
1058 |
unsetError(); |
|
1059 |
if ((mode & (ReadOnly | WriteOnly)) == 0) { |
|
1060 |
qWarning("QFile::open: File access not specified"); |
|
1061 |
return false; |
|
1062 |
} |
|
1063 |
if(d->openExternalFile(mode, fh)) { |
|
1064 |
QIODevice::open(mode); |
|
1065 |
if (mode & Append) { |
|
1066 |
seek(size()); |
|
1067 |
} else { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1068 |
qint64 pos = (qint64)QT_FTELL(fh); |
0 | 1069 |
if (pos != -1) |
1070 |
seek(pos); |
|
1071 |
} |
|
1072 |
return true; |
|
1073 |
} |
|
1074 |
return false; |
|
1075 |
} |
|
1076 |
||
1077 |
/*! \fn QFile::open(OpenMode, int) |
|
1078 |
||
1079 |
Use open(int, OpenMode) instead. |
|
1080 |
*/ |
|
1081 |
||
1082 |
/*! |
|
1083 |
\overload |
|
1084 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1085 |
Opens the existing file descriptor \a fd in the given \a mode. |
0 | 1086 |
Returns true if successful; otherwise returns false. |
1087 |
||
1088 |
When a QFile is opened using this function, close() does not |
|
1089 |
actually close the file. |
|
1090 |
||
1091 |
The QFile that is opened using this function is automatically set |
|
1092 |
to be in raw mode; this means that the file input/output functions |
|
1093 |
are slow. If you run into performance issues, you should try to |
|
1094 |
use one of the other open functions. |
|
1095 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1096 |
\warning If \a fd is not a regular file, e.g, it is 0 (\c stdin), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1097 |
1 (\c stdout), or 2 (\c stderr), you may not be able to seek(). In |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1098 |
those cases, size() returns \c 0. See QIODevice::isSequential() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1099 |
for more information. |
0 | 1100 |
|
1101 |
\warning For Windows CE you may not be able to call seek(), setSize(), |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1102 |
fileTime(). size() returns \c 0. |
0 | 1103 |
|
1104 |
\warning Since this function opens the file without specifying the file name, |
|
1105 |
you cannot use this QFile with a QFileInfo. |
|
1106 |
||
1107 |
\sa close() |
|
1108 |
*/ |
|
1109 |
bool QFile::open(int fd, OpenMode mode) |
|
1110 |
{ |
|
1111 |
Q_D(QFile); |
|
1112 |
if (isOpen()) { |
|
1113 |
qWarning("QFile::open: File (%s) already open", qPrintable(fileName())); |
|
1114 |
return false; |
|
1115 |
} |
|
1116 |
if (mode & Append) |
|
1117 |
mode |= WriteOnly; |
|
1118 |
unsetError(); |
|
1119 |
if ((mode & (ReadOnly | WriteOnly)) == 0) { |
|
1120 |
qWarning("QFile::open: File access not specified"); |
|
1121 |
return false; |
|
1122 |
} |
|
1123 |
if(d->openExternalFile(mode, fd)) { |
|
1124 |
QIODevice::open(mode); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1125 |
if (mode & Append) { |
0 | 1126 |
seek(size()); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1127 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1128 |
qint64 pos = (qint64)QT_LSEEK(fd, QT_OFF_T(0), SEEK_CUR); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1129 |
if (pos != -1) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1130 |
seek(pos); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1131 |
} |
0 | 1132 |
return true; |
1133 |
} |
|
1134 |
return false; |
|
1135 |
} |
|
1136 |
||
1137 |
/*! |
|
1138 |
Returns the file handle of the file. |
|
1139 |
||
1140 |
This is a small positive integer, suitable for use with C library |
|
1141 |
functions such as fdopen() and fcntl(). On systems that use file |
|
1142 |
descriptors for sockets (i.e. Unix systems, but not Windows) the handle |
|
1143 |
can be used with QSocketNotifier as well. |
|
1144 |
||
1145 |
If the file is not open, or there is an error, handle() returns -1. |
|
1146 |
||
1147 |
This function is not supported on Windows CE. |
|
1148 |
||
1149 |
\sa QSocketNotifier |
|
1150 |
*/ |
|
1151 |
||
1152 |
int |
|
1153 |
QFile::handle() const |
|
1154 |
{ |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1155 |
Q_D(const QFile); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1156 |
if (!isOpen() || !d->fileEngine) |
0 | 1157 |
return -1; |
1158 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1159 |
return d->fileEngine->handle(); |
0 | 1160 |
} |
1161 |
||
1162 |
/*! |
|
1163 |
\enum QFile::MemoryMapFlags |
|
1164 |
\since 4.4 |
|
1165 |
||
1166 |
This enum describes special options that may be used by the map() |
|
1167 |
function. |
|
1168 |
||
1169 |
\value NoOptions No options. |
|
1170 |
*/ |
|
1171 |
||
1172 |
/*! |
|
1173 |
\since 4.4 |
|
1174 |
Maps \a size bytes of the file into memory starting at \a offset. A file |
|
1175 |
should be open for a map to succeed but the file does not need to stay |
|
1176 |
open after the memory has been mapped. When the QFile is destroyed |
|
1177 |
or a new file is opened with this object, any maps that have not been |
|
1178 |
unmapped will automatically be unmapped. |
|
1179 |
||
1180 |
Any mapping options can be passed through \a flags. |
|
1181 |
||
1182 |
Returns a pointer to the memory or 0 if there is an error. |
|
1183 |
||
1184 |
\note On Windows CE 5.0 the file will be closed before mapping occurs. |
|
1185 |
||
1186 |
\sa unmap(), QAbstractFileEngine::supportsExtension() |
|
1187 |
*/ |
|
1188 |
uchar *QFile::map(qint64 offset, qint64 size, MemoryMapFlags flags) |
|
1189 |
{ |
|
1190 |
Q_D(QFile); |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1191 |
if (fileEngine() |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1192 |
&& d->fileEngine->supportsExtension(QAbstractFileEngine::MapExtension)) { |
0 | 1193 |
unsetError(); |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1194 |
uchar *address = d->fileEngine->map(offset, size, flags); |
0 | 1195 |
if (address == 0) |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1196 |
d->setError(d->fileEngine->error(), d->fileEngine->errorString()); |
0 | 1197 |
return address; |
1198 |
} |
|
1199 |
return 0; |
|
1200 |
} |
|
1201 |
||
1202 |
/*! |
|
1203 |
\since 4.4 |
|
1204 |
Unmaps the memory \a address. |
|
1205 |
||
1206 |
Returns true if the unmap succeeds; false otherwise. |
|
1207 |
||
1208 |
\sa map(), QAbstractFileEngine::supportsExtension() |
|
1209 |
*/ |
|
1210 |
bool QFile::unmap(uchar *address) |
|
1211 |
{ |
|
1212 |
Q_D(QFile); |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1213 |
if (fileEngine() |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1214 |
&& d->fileEngine->supportsExtension(QAbstractFileEngine::UnMapExtension)) { |
0 | 1215 |
unsetError(); |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1216 |
bool success = d->fileEngine->unmap(address); |
0 | 1217 |
if (!success) |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1218 |
d->setError(d->fileEngine->error(), d->fileEngine->errorString()); |
0 | 1219 |
return success; |
1220 |
} |
|
1221 |
return false; |
|
1222 |
} |
|
1223 |
||
1224 |
/*! |
|
1225 |
\fn QString QFile::name() const |
|
1226 |
||
1227 |
Use fileName() instead. |
|
1228 |
*/ |
|
1229 |
||
1230 |
/*! |
|
1231 |
\fn void QFile::setName(const QString &name) |
|
1232 |
||
1233 |
Use setFileName() instead. |
|
1234 |
*/ |
|
1235 |
||
1236 |
/*! |
|
1237 |
Sets the file size (in bytes) \a sz. Returns true if the file if the |
|
1238 |
resize succeeds; false otherwise. If \a sz is larger than the file |
|
1239 |
currently is the new bytes will be set to 0, if \a sz is smaller the |
|
1240 |
file is simply truncated. |
|
1241 |
||
1242 |
\sa size(), setFileName() |
|
1243 |
*/ |
|
1244 |
||
1245 |
bool |
|
1246 |
QFile::resize(qint64 sz) |
|
1247 |
{ |
|
1248 |
Q_D(QFile); |
|
1249 |
if (!d->ensureFlushed()) |
|
1250 |
return false; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1251 |
fileEngine(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1252 |
if (isOpen() && d->fileEngine->pos() > sz) |
0 | 1253 |
seek(sz); |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1254 |
if(d->fileEngine->setSize(sz)) { |
0 | 1255 |
unsetError(); |
1256 |
return true; |
|
1257 |
} |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1258 |
d->setError(QFile::ResizeError, d->fileEngine->errorString()); |
0 | 1259 |
return false; |
1260 |
} |
|
1261 |
||
1262 |
/*! |
|
1263 |
\overload |
|
1264 |
||
1265 |
Sets \a fileName to size (in bytes) \a sz. Returns true if the file if |
|
1266 |
the resize succeeds; false otherwise. If \a sz is larger than \a |
|
1267 |
fileName currently is the new bytes will be set to 0, if \a sz is |
|
1268 |
smaller the file is simply truncated. |
|
1269 |
||
1270 |
\sa resize() |
|
1271 |
*/ |
|
1272 |
||
1273 |
bool |
|
1274 |
QFile::resize(const QString &fileName, qint64 sz) |
|
1275 |
{ |
|
1276 |
return QFile(fileName).resize(sz); |
|
1277 |
} |
|
1278 |
||
1279 |
/*! |
|
1280 |
Returns the complete OR-ed together combination of |
|
1281 |
QFile::Permission for the file. |
|
1282 |
||
1283 |
\sa setPermissions(), setFileName() |
|
1284 |
*/ |
|
1285 |
||
1286 |
QFile::Permissions |
|
1287 |
QFile::permissions() const |
|
1288 |
{ |
|
1289 |
QAbstractFileEngine::FileFlags perms = fileEngine()->fileFlags(QAbstractFileEngine::PermsMask) & QAbstractFileEngine::PermsMask; |
|
1290 |
return QFile::Permissions((int)perms); //ewww |
|
1291 |
} |
|
1292 |
||
1293 |
/*! |
|
1294 |
\overload |
|
1295 |
||
1296 |
Returns the complete OR-ed together combination of |
|
1297 |
QFile::Permission for \a fileName. |
|
1298 |
*/ |
|
1299 |
||
1300 |
QFile::Permissions |
|
1301 |
QFile::permissions(const QString &fileName) |
|
1302 |
{ |
|
1303 |
return QFile(fileName).permissions(); |
|
1304 |
} |
|
1305 |
||
1306 |
/*! |
|
1307 |
Sets the permissions for the file to the \a permissions specified. |
|
1308 |
Returns true if successful, or false if the permissions cannot be |
|
1309 |
modified. |
|
1310 |
||
1311 |
\sa permissions(), setFileName() |
|
1312 |
*/ |
|
1313 |
||
1314 |
bool |
|
1315 |
QFile::setPermissions(Permissions permissions) |
|
1316 |
{ |
|
1317 |
Q_D(QFile); |
|
1318 |
if(fileEngine()->setPermissions(permissions)) { |
|
1319 |
unsetError(); |
|
1320 |
return true; |
|
1321 |
} |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1322 |
d->setError(QFile::PermissionsError, d->fileEngine->errorString()); |
0 | 1323 |
return false; |
1324 |
} |
|
1325 |
||
1326 |
/*! |
|
1327 |
\overload |
|
1328 |
||
1329 |
Sets the permissions for \a fileName file to \a permissions. |
|
1330 |
*/ |
|
1331 |
||
1332 |
bool |
|
1333 |
QFile::setPermissions(const QString &fileName, Permissions permissions) |
|
1334 |
{ |
|
1335 |
return QFile(fileName).setPermissions(permissions); |
|
1336 |
} |
|
1337 |
||
1338 |
static inline qint64 _qfile_writeData(QAbstractFileEngine *engine, QRingBuffer *buffer) |
|
1339 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1340 |
qint64 ret = engine->write(buffer->readPointer(), buffer->nextDataBlockSize()); |
0 | 1341 |
if (ret > 0) |
1342 |
buffer->free(ret); |
|
1343 |
return ret; |
|
1344 |
} |
|
1345 |
||
1346 |
/*! |
|
1347 |
Flushes any buffered data to the file. Returns true if successful; |
|
1348 |
otherwise returns false. |
|
1349 |
*/ |
|
1350 |
||
1351 |
bool |
|
1352 |
QFile::flush() |
|
1353 |
{ |
|
1354 |
Q_D(QFile); |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1355 |
if (!d->fileEngine) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1356 |
qWarning("QFile::flush: No file engine. Is IODevice open?"); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1357 |
return false; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1358 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1359 |
|
0 | 1360 |
if (!d->writeBuffer.isEmpty()) { |
1361 |
qint64 size = d->writeBuffer.size(); |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1362 |
if (_qfile_writeData(d->fileEngine, &d->writeBuffer) != size) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1363 |
QFile::FileError err = d->fileEngine->error(); |
0 | 1364 |
if(err == QFile::UnspecifiedError) |
1365 |
err = QFile::WriteError; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1366 |
d->setError(err, d->fileEngine->errorString()); |
0 | 1367 |
return false; |
1368 |
} |
|
1369 |
} |
|
1370 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1371 |
if (!d->fileEngine->flush()) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1372 |
QFile::FileError err = d->fileEngine->error(); |
0 | 1373 |
if(err == QFile::UnspecifiedError) |
1374 |
err = QFile::WriteError; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1375 |
d->setError(err, d->fileEngine->errorString()); |
0 | 1376 |
return false; |
1377 |
} |
|
1378 |
return true; |
|
1379 |
} |
|
1380 |
||
1381 |
/*! |
|
1382 |
Calls QFile::flush() and closes the file. Errors from flush are ignored. |
|
1383 |
||
1384 |
\sa QIODevice::close() |
|
1385 |
*/ |
|
1386 |
void |
|
1387 |
QFile::close() |
|
1388 |
{ |
|
1389 |
Q_D(QFile); |
|
1390 |
if(!isOpen()) |
|
1391 |
return; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1392 |
bool flushed = flush(); |
0 | 1393 |
QIODevice::close(); |
1394 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1395 |
// reset write buffer |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1396 |
d->lastWasWrite = false; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1397 |
d->writeBuffer.clear(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1398 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1399 |
// keep earlier error from flush |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1400 |
if (d->fileEngine->close() && flushed) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1401 |
unsetError(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1402 |
else if (flushed) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1403 |
d->setError(d->fileEngine->error(), d->fileEngine->errorString()); |
0 | 1404 |
} |
1405 |
||
1406 |
/*! |
|
1407 |
Returns the size of the file. |
|
1408 |
||
1409 |
For regular empty files on Unix (e.g. those in \c /proc), this function |
|
1410 |
returns 0; the contents of such a file are generated on demand in response |
|
1411 |
to you calling read(). |
|
1412 |
*/ |
|
1413 |
||
1414 |
qint64 QFile::size() const |
|
1415 |
{ |
|
1416 |
Q_D(const QFile); |
|
1417 |
if (!d->ensureFlushed()) |
|
1418 |
return 0; |
|
1419 |
return fileEngine()->size(); |
|
1420 |
} |
|
1421 |
||
1422 |
/*! |
|
1423 |
\reimp |
|
1424 |
*/ |
|
1425 |
||
1426 |
qint64 QFile::pos() const |
|
1427 |
{ |
|
1428 |
return QIODevice::pos(); |
|
1429 |
} |
|
1430 |
||
1431 |
/*! |
|
1432 |
Returns true if the end of the file has been reached; otherwise returns |
|
1433 |
false. |
|
1434 |
||
1435 |
For regular empty files on Unix (e.g. those in \c /proc), this function |
|
1436 |
returns true, since the file system reports that the size of such a file is |
|
1437 |
0. Therefore, you should not depend on atEnd() when reading data from such a |
|
1438 |
file, but rather call read() until no more data can be read. |
|
1439 |
*/ |
|
1440 |
||
1441 |
bool QFile::atEnd() const |
|
1442 |
{ |
|
1443 |
Q_D(const QFile); |
|
1444 |
||
1445 |
if (!isOpen()) |
|
1446 |
return true; |
|
1447 |
||
1448 |
if (!d->ensureFlushed()) |
|
1449 |
return false; |
|
1450 |
||
1451 |
// If there's buffered data left, we're not at the end. |
|
1452 |
if (!d->buffer.isEmpty()) |
|
1453 |
return false; |
|
1454 |
||
1455 |
// If the file engine knows best, say what it says. |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1456 |
if (d->fileEngine->supportsExtension(QAbstractFileEngine::AtEndExtension)) { |
0 | 1457 |
// Check if the file engine supports AtEndExtension, and if it does, |
1458 |
// check if the file engine claims to be at the end. |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1459 |
return d->fileEngine->atEnd(); |
0 | 1460 |
} |
1461 |
||
1462 |
// Fall back to checking how much is available (will stat files). |
|
1463 |
return bytesAvailable() == 0; |
|
1464 |
} |
|
1465 |
||
1466 |
/*! |
|
1467 |
\reimp |
|
1468 |
*/ |
|
1469 |
||
1470 |
bool QFile::seek(qint64 off) |
|
1471 |
{ |
|
1472 |
Q_D(QFile); |
|
1473 |
if (!isOpen()) { |
|
1474 |
qWarning("QFile::seek: IODevice is not open"); |
|
1475 |
return false; |
|
1476 |
} |
|
1477 |
||
1478 |
if (!d->ensureFlushed()) |
|
1479 |
return false; |
|
1480 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1481 |
if (!d->fileEngine->seek(off) || !QIODevice::seek(off)) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1482 |
QFile::FileError err = d->fileEngine->error(); |
0 | 1483 |
if(err == QFile::UnspecifiedError) |
1484 |
err = QFile::PositionError; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1485 |
d->setError(err, d->fileEngine->errorString()); |
0 | 1486 |
return false; |
1487 |
} |
|
1488 |
unsetError(); |
|
1489 |
return true; |
|
1490 |
} |
|
1491 |
||
1492 |
/*! |
|
1493 |
\reimp |
|
1494 |
*/ |
|
1495 |
qint64 QFile::readLineData(char *data, qint64 maxlen) |
|
1496 |
{ |
|
1497 |
Q_D(QFile); |
|
1498 |
if (!d->ensureFlushed()) |
|
1499 |
return -1; |
|
1500 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1501 |
if (d->fileEngine->supportsExtension(QAbstractFileEngine::FastReadLineExtension)) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1502 |
return d->fileEngine->readLine(data, maxlen); |
0 | 1503 |
|
1504 |
// Fall back to QIODevice's readLine implementation if the engine |
|
1505 |
// cannot do it faster. |
|
1506 |
return QIODevice::readLineData(data, maxlen); |
|
1507 |
} |
|
1508 |
||
1509 |
/*! |
|
1510 |
\reimp |
|
1511 |
*/ |
|
1512 |
||
1513 |
qint64 QFile::readData(char *data, qint64 len) |
|
1514 |
{ |
|
1515 |
Q_D(QFile); |
|
1516 |
unsetError(); |
|
1517 |
if (!d->ensureFlushed()) |
|
1518 |
return -1; |
|
1519 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1520 |
qint64 read = d->fileEngine->read(data, len); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1521 |
if(read < 0) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1522 |
QFile::FileError err = d->fileEngine->error(); |
0 | 1523 |
if(err == QFile::UnspecifiedError) |
1524 |
err = QFile::ReadError; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1525 |
d->setError(err, d->fileEngine->errorString()); |
0 | 1526 |
} |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1527 |
return read; |
0 | 1528 |
} |
1529 |
||
1530 |
/*! |
|
1531 |
\internal |
|
1532 |
*/ |
|
1533 |
bool QFilePrivate::putCharHelper(char c) |
|
1534 |
{ |
|
1535 |
#ifdef QT_NO_QOBJECT |
|
1536 |
return QIODevicePrivate::putCharHelper(c); |
|
1537 |
#else |
|
1538 |
||
1539 |
// Cutoff for code that doesn't only touch the buffer. |
|
1540 |
int writeBufferSize = writeBuffer.size(); |
|
1541 |
if ((openMode & QIODevice::Unbuffered) || writeBufferSize + 1 >= QFILE_WRITEBUFFER_SIZE |
|
1542 |
#ifdef Q_OS_WIN |
|
1543 |
|| ((openMode & QIODevice::Text) && c == '\n' && writeBufferSize + 2 >= QFILE_WRITEBUFFER_SIZE) |
|
1544 |
#endif |
|
1545 |
) { |
|
1546 |
return QIODevicePrivate::putCharHelper(c); |
|
1547 |
} |
|
1548 |
||
1549 |
if (!(openMode & QIODevice::WriteOnly)) { |
|
1550 |
if (openMode == QIODevice::NotOpen) |
|
1551 |
qWarning("QIODevice::putChar: Closed device"); |
|
1552 |
else |
|
1553 |
qWarning("QIODevice::putChar: ReadOnly device"); |
|
1554 |
return false; |
|
1555 |
} |
|
1556 |
||
1557 |
// Make sure the device is positioned correctly. |
|
1558 |
const bool sequential = isSequential(); |
|
1559 |
if (pos != devicePos && !sequential && !q_func()->seek(pos)) |
|
1560 |
return false; |
|
1561 |
||
1562 |
lastWasWrite = true; |
|
1563 |
||
1564 |
int len = 1; |
|
1565 |
#ifdef Q_OS_WIN |
|
1566 |
if ((openMode & QIODevice::Text) && c == '\n') { |
|
1567 |
++len; |
|
1568 |
*writeBuffer.reserve(1) = '\r'; |
|
1569 |
} |
|
1570 |
#endif |
|
1571 |
||
1572 |
// Write to buffer. |
|
1573 |
*writeBuffer.reserve(1) = c; |
|
1574 |
||
1575 |
if (!sequential) { |
|
1576 |
pos += len; |
|
1577 |
devicePos += len; |
|
1578 |
if (!buffer.isEmpty()) |
|
1579 |
buffer.skip(len); |
|
1580 |
} |
|
1581 |
||
1582 |
return true; |
|
1583 |
#endif |
|
1584 |
} |
|
1585 |
||
1586 |
/*! |
|
1587 |
\reimp |
|
1588 |
*/ |
|
1589 |
||
1590 |
qint64 |
|
1591 |
QFile::writeData(const char *data, qint64 len) |
|
1592 |
{ |
|
1593 |
Q_D(QFile); |
|
1594 |
unsetError(); |
|
1595 |
d->lastWasWrite = true; |
|
1596 |
bool buffered = !(d->openMode & Unbuffered); |
|
1597 |
||
1598 |
// Flush buffered data if this read will overflow. |
|
1599 |
if (buffered && (d->writeBuffer.size() + len) > QFILE_WRITEBUFFER_SIZE) { |
|
1600 |
if (!flush()) |
|
1601 |
return -1; |
|
1602 |
} |
|
1603 |
||
1604 |
// Write directly to the engine if the block size is larger than |
|
1605 |
// the write buffer size. |
|
1606 |
if (!buffered || len > QFILE_WRITEBUFFER_SIZE) { |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1607 |
qint64 ret = d->fileEngine->write(data, len); |
0 | 1608 |
if(ret < 0) { |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1609 |
QFile::FileError err = d->fileEngine->error(); |
0 | 1610 |
if(err == QFile::UnspecifiedError) |
1611 |
err = QFile::WriteError; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1612 |
d->setError(err, d->fileEngine->errorString()); |
0 | 1613 |
} |
1614 |
return ret; |
|
1615 |
} |
|
1616 |
||
1617 |
// Write to the buffer. |
|
1618 |
char *writePointer = d->writeBuffer.reserve(len); |
|
1619 |
if (len == 1) |
|
1620 |
*writePointer = *data; |
|
1621 |
else |
|
1622 |
::memcpy(writePointer, data, len); |
|
1623 |
return len; |
|
1624 |
} |
|
1625 |
||
1626 |
/*! |
|
1627 |
\internal |
|
1628 |
Returns the QIOEngine for this QFile object. |
|
1629 |
*/ |
|
1630 |
QAbstractFileEngine *QFile::fileEngine() const |
|
1631 |
{ |
|
1632 |
Q_D(const QFile); |
|
1633 |
if(!d->fileEngine) |
|
1634 |
d->fileEngine = QAbstractFileEngine::create(d->fileName); |
|
1635 |
return d->fileEngine; |
|
1636 |
} |
|
1637 |
||
1638 |
/*! |
|
1639 |
Returns the file error status. |
|
1640 |
||
1641 |
The I/O device status returns an error code. For example, if open() |
|
1642 |
returns false, or a read/write operation returns -1, this function can |
|
1643 |
be called to find out the reason why the operation failed. |
|
1644 |
||
1645 |
\sa unsetError() |
|
1646 |
*/ |
|
1647 |
||
1648 |
QFile::FileError |
|
1649 |
QFile::error() const |
|
1650 |
{ |
|
1651 |
Q_D(const QFile); |
|
1652 |
return d->error; |
|
1653 |
} |
|
1654 |
||
1655 |
/*! |
|
1656 |
Sets the file's error to QFile::NoError. |
|
1657 |
||
1658 |
\sa error() |
|
1659 |
*/ |
|
1660 |
void |
|
1661 |
QFile::unsetError() |
|
1662 |
{ |
|
1663 |
Q_D(QFile); |
|
1664 |
d->setError(QFile::NoError); |
|
1665 |
} |
|
1666 |
||
1667 |
QT_END_NAMESPACE |