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