Line | Source Code | Coverage |
---|
1 | /**************************************************************************** | - |
2 | ** | - |
3 | ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). | - |
4 | ** Contact: http://www.qt-project.org/legal | - |
5 | ** | - |
6 | ** This file is part of the QtCore module of the Qt Toolkit. | - |
7 | ** | - |
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - |
9 | ** Commercial License Usage | - |
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - |
11 | ** accordance with the commercial license agreement provided with the | - |
12 | ** Software or, alternatively, in accordance with the terms contained in | - |
13 | ** a written agreement between you and Digia. For licensing terms and | - |
14 | ** conditions see http://qt.digia.com/licensing. For further information | - |
15 | ** use the contact form at http://qt.digia.com/contact-us. | - |
16 | ** | - |
17 | ** GNU Lesser General Public License Usage | - |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - |
19 | ** General Public License version 2.1 as published by the Free Software | - |
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the | - |
21 | ** packaging of this file. Please review the following information to | - |
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements | - |
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - |
24 | ** | - |
25 | ** In addition, as a special exception, Digia gives you certain additional | - |
26 | ** rights. These rights are described in the Digia Qt LGPL Exception | - |
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - |
28 | ** | - |
29 | ** GNU General Public License Usage | - |
30 | ** Alternatively, this file may be used under the terms of the GNU | - |
31 | ** General Public License version 3.0 as published by the Free Software | - |
32 | ** Foundation and appearing in the file LICENSE.GPL included in the | - |
33 | ** packaging of this file. Please review the following information to | - |
34 | ** ensure the GNU General Public License version 3.0 requirements will be | - |
35 | ** met: http://www.gnu.org/copyleft/gpl.html. | - |
36 | ** | - |
37 | ** | - |
38 | ** $QT_END_LICENSE$ | - |
39 | ** | - |
40 | ****************************************************************************/ | - |
41 | | - |
42 | #include "qplatformdefs.h" | - |
43 | #include "qdebug.h" | - |
44 | #include "qfile.h" | - |
45 | #include "qfsfileengine_p.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 | #include "private/qsystemerror_p.h" | - |
52 | #if defined(QT_BUILD_CORE_LIB) | - |
53 | # include "qcoreapplication.h" | - |
54 | #endif | - |
55 | | - |
56 | #ifdef QT_NO_QOBJECT | - |
57 | #define tr(X) QString::fromLatin1(X) | - |
58 | #endif | - |
59 | | - |
60 | QT_BEGIN_NAMESPACE | - |
61 | | - |
62 | //************* QFilePrivate | - |
63 | QFilePrivate::QFilePrivate() | - |
64 | { | - |
65 | } | - |
66 | | - |
67 | QFilePrivate::~QFilePrivate() | - |
68 | { | - |
69 | } | - |
70 | | - |
71 | bool | - |
72 | QFilePrivate::openExternalFile(int flags, int fd, QFile::FileHandleFlags handleFlags) | - |
73 | { | - |
74 | #ifdef QT_NO_FSFILEENGINE | - |
75 | Q_UNUSED(flags); | - |
76 | Q_UNUSED(fd); | - |
77 | return false; | - |
78 | #else | - |
79 | delete fileEngine; executed (the execution status of this line is deduced): delete fileEngine; | - |
80 | fileEngine = 0; executed (the execution status of this line is deduced): fileEngine = 0; | - |
81 | QFSFileEngine *fe = new QFSFileEngine; executed (the execution status of this line is deduced): QFSFileEngine *fe = new QFSFileEngine; | - |
82 | fileEngine = fe; executed (the execution status of this line is deduced): fileEngine = fe; | - |
83 | return fe->open(QIODevice::OpenMode(flags), fd, handleFlags); executed: return fe->open(QIODevice::OpenMode(flags), fd, handleFlags); Execution Count:30 | 30 |
84 | #endif | - |
85 | } | - |
86 | | - |
87 | bool | - |
88 | QFilePrivate::openExternalFile(int flags, FILE *fh, QFile::FileHandleFlags handleFlags) | - |
89 | { | - |
90 | #ifdef QT_NO_FSFILEENGINE | - |
91 | Q_UNUSED(flags); | - |
92 | Q_UNUSED(fh); | - |
93 | return false; | - |
94 | #else | - |
95 | delete fileEngine; executed (the execution status of this line is deduced): delete fileEngine; | - |
96 | fileEngine = 0; executed (the execution status of this line is deduced): fileEngine = 0; | - |
97 | QFSFileEngine *fe = new QFSFileEngine; executed (the execution status of this line is deduced): QFSFileEngine *fe = new QFSFileEngine; | - |
98 | fileEngine = fe; executed (the execution status of this line is deduced): fileEngine = fe; | - |
99 | return fe->open(QIODevice::OpenMode(flags), fh, handleFlags); executed: return fe->open(QIODevice::OpenMode(flags), fh, handleFlags); Execution Count:31 | 31 |
100 | #endif | - |
101 | } | - |
102 | | - |
103 | QAbstractFileEngine *QFilePrivate::engine() const | - |
104 | { | - |
105 | if (!fileEngine) evaluated: !fileEngine yes Evaluation Count:26377 | yes Evaluation Count:195768 |
| 26377-195768 |
106 | fileEngine = QAbstractFileEngine::create(fileName); executed: fileEngine = QAbstractFileEngine::create(fileName); Execution Count:26377 | 26377 |
107 | return fileEngine; executed: return fileEngine; Execution Count:222145 | 222145 |
108 | } | - |
109 | | - |
110 | //************* QFile | - |
111 | | - |
112 | /*! | - |
113 | \class QFile | - |
114 | \inmodule QtCore | - |
115 | \brief The QFile class provides an interface for reading from and writing to files. | - |
116 | | - |
117 | \ingroup io | - |
118 | | - |
119 | \reentrant | - |
120 | | - |
121 | QFile is an I/O device for reading and writing text and binary | - |
122 | files and \l{The Qt Resource System}{resources}. A QFile may be | - |
123 | used by itself or, more conveniently, with a QTextStream or | - |
124 | QDataStream. | - |
125 | | - |
126 | The file name is usually passed in the constructor, but it can be | - |
127 | set at any time using setFileName(). QFile expects the file | - |
128 | separator to be '/' regardless of operating system. The use of | - |
129 | other separators (e.g., '\\') is not supported. | - |
130 | | - |
131 | You can check for a file's existence using exists(), and remove a | - |
132 | file using remove(). (More advanced file system related operations | - |
133 | are provided by QFileInfo and QDir.) | - |
134 | | - |
135 | The file is opened with open(), closed with close(), and flushed | - |
136 | with flush(). Data is usually read and written using QDataStream | - |
137 | or QTextStream, but you can also call the QIODevice-inherited | - |
138 | functions read(), readLine(), readAll(), write(). QFile also | - |
139 | inherits getChar(), putChar(), and ungetChar(), which work one | - |
140 | character at a time. | - |
141 | | - |
142 | The size of the file is returned by size(). You can get the | - |
143 | current file position using pos(), or move to a new file position | - |
144 | using seek(). If you've reached the end of the file, atEnd() | - |
145 | returns true. | - |
146 | | - |
147 | \section1 Reading Files Directly | - |
148 | | - |
149 | The following example reads a text file line by line: | - |
150 | | - |
151 | \snippet file/file.cpp 0 | - |
152 | | - |
153 | The QIODevice::Text flag passed to open() tells Qt to convert | - |
154 | Windows-style line terminators ("\\r\\n") into C++-style | - |
155 | terminators ("\\n"). By default, QFile assumes binary, i.e. it | - |
156 | doesn't perform any conversion on the bytes stored in the file. | - |
157 | | - |
158 | \section1 Using Streams to Read Files | - |
159 | | - |
160 | The next example uses QTextStream to read a text file | - |
161 | line by line: | - |
162 | | - |
163 | \snippet file/file.cpp 1 | - |
164 | | - |
165 | QTextStream takes care of converting the 8-bit data stored on | - |
166 | disk into a 16-bit Unicode QString. By default, it assumes that | - |
167 | the user system's local 8-bit encoding is used (e.g., UTF-8 | - |
168 | on most unix based operating systems; see QTextCodec::codecForLocale() for | - |
169 | details). This can be changed using setCodec(). | - |
170 | | - |
171 | To write text, we can use operator<<(), which is overloaded to | - |
172 | take a QTextStream on the left and various data types (including | - |
173 | QString) on the right: | - |
174 | | - |
175 | \snippet file/file.cpp 2 | - |
176 | | - |
177 | QDataStream is similar, in that you can use operator<<() to write | - |
178 | data and operator>>() to read it back. See the class | - |
179 | documentation for details. | - |
180 | | - |
181 | When you use QFile, QFileInfo, and QDir to access the file system | - |
182 | with Qt, you can use Unicode file names. On Unix, these file | - |
183 | names are converted to an 8-bit encoding. If you want to use | - |
184 | standard C++ APIs (\c <cstdio> or \c <iostream>) or | - |
185 | platform-specific APIs to access files instead of QFile, you can | - |
186 | use the encodeName() and decodeName() functions to convert | - |
187 | between Unicode file names and 8-bit file names. | - |
188 | | - |
189 | On Unix, there are some special system files (e.g. in \c /proc) for which | - |
190 | size() will always return 0, yet you may still be able to read more data | - |
191 | from such a file; the data is generated in direct response to you calling | - |
192 | read(). In this case, however, you cannot use atEnd() to determine if | - |
193 | there is more data to read (since atEnd() will return true for a file that | - |
194 | claims to have size 0). Instead, you should either call readAll(), or call | - |
195 | read() or readLine() repeatedly until no more data can be read. The next | - |
196 | example uses QTextStream to read \c /proc/modules line by line: | - |
197 | | - |
198 | \snippet file/file.cpp 3 | - |
199 | | - |
200 | \section1 Signals | - |
201 | | - |
202 | Unlike other QIODevice implementations, such as QTcpSocket, QFile does not | - |
203 | emit the aboutToClose(), bytesWritten(), or readyRead() signals. This | - |
204 | implementation detail means that QFile is not suitable for reading and | - |
205 | writing certain types of files, such as device files on Unix platforms. | - |
206 | | - |
207 | \section1 Platform Specific Issues | - |
208 | | - |
209 | File permissions are handled differently on Linux/Mac OS X and | - |
210 | Windows. In a non \l{QIODevice::isWritable()}{writable} | - |
211 | directory on Linux, files cannot be created. This is not always | - |
212 | the case on Windows, where, for instance, the 'My Documents' | - |
213 | directory usually is not writable, but it is still possible to | - |
214 | create files in it. | - |
215 | | - |
216 | \sa QTextStream, QDataStream, QFileInfo, QDir, {The Qt Resource System} | - |
217 | */ | - |
218 | | - |
219 | #ifdef QT_NO_QOBJECT | - |
220 | QFile::QFile() | - |
221 | : QFileDevice(*new QFilePrivate) | - |
222 | { | - |
223 | } | - |
224 | QFile::QFile(const QString &name) | - |
225 | : QFileDevice(*new QFilePrivate) | - |
226 | { | - |
227 | d_func()->fileName = name; | - |
228 | } | - |
229 | QFile::QFile(QFilePrivate &dd) | - |
230 | : QFileDevice(dd) | - |
231 | { | - |
232 | } | - |
233 | #else | - |
234 | /*! | - |
235 | \internal | - |
236 | */ | - |
237 | QFile::QFile() | - |
238 | : QFileDevice(*new QFilePrivate, 0) | - |
239 | { | - |
240 | } executed: } Execution Count:157 | 157 |
241 | /*! | - |
242 | Constructs a new file object with the given \a parent. | - |
243 | */ | - |
244 | QFile::QFile(QObject *parent) | - |
245 | : QFileDevice(*new QFilePrivate, parent) | - |
246 | { | - |
247 | } executed: } Execution Count:1 | 1 |
248 | /*! | - |
249 | Constructs a new file object to represent the file with the given \a name. | - |
250 | */ | - |
251 | QFile::QFile(const QString &name) | - |
252 | : QFileDevice(*new QFilePrivate, 0) | - |
253 | { | - |
254 | Q_D(QFile); executed (the execution status of this line is deduced): QFilePrivate * const d = d_func(); | - |
255 | d->fileName = name; executed (the execution status of this line is deduced): d->fileName = name; | - |
256 | } executed: } Execution Count:25098 | 25098 |
257 | /*! | - |
258 | Constructs a new file object with the given \a parent to represent the | - |
259 | file with the specified \a name. | - |
260 | */ | - |
261 | QFile::QFile(const QString &name, QObject *parent) | - |
262 | : QFileDevice(*new QFilePrivate, parent) | - |
263 | { | - |
264 | Q_D(QFile); executed (the execution status of this line is deduced): QFilePrivate * const d = d_func(); | - |
265 | d->fileName = name; executed (the execution status of this line is deduced): d->fileName = name; | - |
266 | } executed: } Execution Count:1 | 1 |
267 | /*! | - |
268 | \internal | - |
269 | */ | - |
270 | QFile::QFile(QFilePrivate &dd, QObject *parent) | - |
271 | : QFileDevice(dd, parent) | - |
272 | { | - |
273 | } executed: } Execution Count:1482 | 1482 |
274 | #endif | - |
275 | | - |
276 | /*! | - |
277 | Destroys the file object, closing it if necessary. | - |
278 | */ | - |
279 | QFile::~QFile() | - |
280 | { | - |
281 | } | - |
282 | | - |
283 | /*! | - |
284 | Returns the name set by setFileName() or to the QFile | - |
285 | constructors. | - |
286 | | - |
287 | \sa setFileName(), QFileInfo::fileName() | - |
288 | */ | - |
289 | QString QFile::fileName() const | - |
290 | { | - |
291 | Q_D(const QFile); executed (the execution status of this line is deduced): const QFilePrivate * const d = d_func(); | - |
292 | return d->engine()->fileName(QAbstractFileEngine::DefaultName); executed: return d->engine()->fileName(QAbstractFileEngine::DefaultName); Execution Count:3118 | 3118 |
293 | } | - |
294 | | - |
295 | /*! | - |
296 | Sets the \a name of the file. The name can have no path, a | - |
297 | relative path, or an absolute path. | - |
298 | | - |
299 | Do not call this function if the file has already been opened. | - |
300 | | - |
301 | If the file name has no path or a relative path, the path used | - |
302 | will be the application's current directory path | - |
303 | \e{at the time of the open()} call. | - |
304 | | - |
305 | Example: | - |
306 | \snippet code/src_corelib_io_qfile.cpp 0 | - |
307 | | - |
308 | Note that the directory separator "/" works for all operating | - |
309 | systems supported by Qt. | - |
310 | | - |
311 | \sa fileName(), QFileInfo, QDir | - |
312 | */ | - |
313 | void | - |
314 | QFile::setFileName(const QString &name) | - |
315 | { | - |
316 | Q_D(QFile); executed (the execution status of this line is deduced): QFilePrivate * const d = d_func(); | - |
317 | if (isOpen()) { partially evaluated: isOpen() no Evaluation Count:0 | yes Evaluation Count:1325 |
| 0-1325 |
318 | qWarning("QFile::setFileName: File (%s) is already opened", never executed (the execution status of this line is deduced): QMessageLogger("io/qfile.cpp", 318, __PRETTY_FUNCTION__).warning("QFile::setFileName: File (%s) is already opened", | - |
319 | qPrintable(fileName())); never executed (the execution status of this line is deduced): QString(fileName()).toLocal8Bit().constData()); | - |
320 | close(); never executed (the execution status of this line is deduced): close(); | - |
321 | } | 0 |
322 | if(d->fileEngine) { //get a new file engine later evaluated: d->fileEngine yes Evaluation Count:1217 | yes Evaluation Count:108 |
| 108-1217 |
323 | delete d->fileEngine; executed (the execution status of this line is deduced): delete d->fileEngine; | - |
324 | d->fileEngine = 0; executed (the execution status of this line is deduced): d->fileEngine = 0; | - |
325 | } executed: } Execution Count:1217 | 1217 |
326 | d->fileName = name; executed (the execution status of this line is deduced): d->fileName = name; | - |
327 | } executed: } Execution Count:1325 | 1325 |
328 | | - |
329 | /*! | - |
330 | \fn QString QFile::decodeName(const char *localFileName) | - |
331 | | - |
332 | \overload | - |
333 | | - |
334 | Returns the Unicode version of the given \a localFileName. See | - |
335 | encodeName() for details. | - |
336 | */ | - |
337 | | - |
338 | /*! | - |
339 | \fn QByteArray QFile::encodeName(const QString &fileName) | - |
340 | | - |
341 | Converts \a fileName to the local 8-bit | - |
342 | encoding determined by the user's locale. This is sufficient for | - |
343 | file names that the user chooses. File names hard-coded into the | - |
344 | application should only use 7-bit ASCII filename characters. | - |
345 | | - |
346 | \sa decodeName() | - |
347 | */ | - |
348 | | - |
349 | /*! | - |
350 | \typedef QFile::EncoderFn | - |
351 | \obsolete | - |
352 | | - |
353 | This is a typedef for a pointer to a function with the following | - |
354 | signature: | - |
355 | | - |
356 | \snippet code/src_corelib_io_qfile.cpp 1 | - |
357 | | - |
358 | \sa setEncodingFunction(), encodeName() | - |
359 | */ | - |
360 | | - |
361 | /*! | - |
362 | \fn QString QFile::decodeName(const QByteArray &localFileName) | - |
363 | | - |
364 | This does the reverse of QFile::encodeName() using \a localFileName. | - |
365 | | - |
366 | \sa encodeName() | - |
367 | */ | - |
368 | | - |
369 | /*! | - |
370 | \fn void QFile::setEncodingFunction(EncoderFn function) | - |
371 | \obsolete | - |
372 | | - |
373 | This function does nothing. It is provided for compatibility with Qt 4 code | - |
374 | that attempted to set a different encoding function for file names. That | - |
375 | feature is flawed and no longer supported in Qt 5. | - |
376 | | - |
377 | \sa encodeName(), setDecodingFunction() | - |
378 | */ | - |
379 | | - |
380 | /*! | - |
381 | \typedef QFile::DecoderFn | - |
382 | | - |
383 | This is a typedef for a pointer to a function with the following | - |
384 | signature: | - |
385 | | - |
386 | \snippet code/src_corelib_io_qfile.cpp 2 | - |
387 | | - |
388 | \sa setDecodingFunction() | - |
389 | */ | - |
390 | | - |
391 | /*! | - |
392 | \fn void QFile::setDecodingFunction(DecoderFn function) | - |
393 | \obsolete | - |
394 | | - |
395 | This function does nothing. It is provided for compatibility with Qt 4 code | - |
396 | that attempted to set a different decoding function for file names. That | - |
397 | feature is flawed and no longer supported in Qt 5. | - |
398 | | - |
399 | \sa setEncodingFunction(), decodeName() | - |
400 | */ | - |
401 | | - |
402 | /*! | - |
403 | \overload | - |
404 | | - |
405 | Returns true if the file specified by fileName() exists; otherwise | - |
406 | returns false. | - |
407 | | - |
408 | \sa fileName(), setFileName() | - |
409 | */ | - |
410 | | - |
411 | bool | - |
412 | QFile::exists() const | - |
413 | { | - |
414 | Q_D(const QFile); executed (the execution status of this line is deduced): const QFilePrivate * const d = d_func(); | - |
415 | // 0x1000000 = QAbstractFileEngine::Refresh, forcing an update | - |
416 | return (d->engine()->fileFlags(QAbstractFileEngine::FlagsMask executed: return (d->engine()->fileFlags(QAbstractFileEngine::FlagsMask | QAbstractFileEngine::FileFlag(0x1000000)) & QAbstractFileEngine::ExistsFlag); Execution Count:4670 | 4670 |
417 | | QAbstractFileEngine::FileFlag(0x1000000)) & QAbstractFileEngine::ExistsFlag); executed: return (d->engine()->fileFlags(QAbstractFileEngine::FlagsMask | QAbstractFileEngine::FileFlag(0x1000000)) & QAbstractFileEngine::ExistsFlag); Execution Count:4670 | 4670 |
418 | } | - |
419 | | - |
420 | /*! | - |
421 | Returns true if the file specified by \a fileName exists; otherwise | - |
422 | returns false. | - |
423 | */ | - |
424 | | - |
425 | bool | - |
426 | QFile::exists(const QString &fileName) | - |
427 | { | - |
428 | return QFileInfo(fileName).exists(); executed: return QFileInfo(fileName).exists(); Execution Count:2915 | 2915 |
429 | } | - |
430 | | - |
431 | /*! | - |
432 | \fn QString QFile::symLinkTarget() const | - |
433 | \since 4.2 | - |
434 | \overload | - |
435 | | - |
436 | Returns the absolute path of the file or directory a symlink (or shortcut | - |
437 | on Windows) points to, or a an empty string if the object isn't a symbolic | - |
438 | link. | - |
439 | | - |
440 | This name may not represent an existing file; it is only a string. | - |
441 | QFile::exists() returns true if the symlink points to an existing file. | - |
442 | | - |
443 | \sa fileName(), setFileName() | - |
444 | */ | - |
445 | | - |
446 | /*! | - |
447 | \obsolete | - |
448 | | - |
449 | Use symLinkTarget() instead. | - |
450 | */ | - |
451 | QString | - |
452 | QFile::readLink() const | - |
453 | { | - |
454 | Q_D(const QFile); executed (the execution status of this line is deduced): const QFilePrivate * const d = d_func(); | - |
455 | return d->engine()->fileName(QAbstractFileEngine::LinkName); executed: return d->engine()->fileName(QAbstractFileEngine::LinkName); Execution Count:2 | 2 |
456 | } | - |
457 | | - |
458 | /*! | - |
459 | \fn static QString QFile::symLinkTarget(const QString &fileName) | - |
460 | \since 4.2 | - |
461 | | - |
462 | Returns the absolute path of the file or directory referred to by the | - |
463 | symlink (or shortcut on Windows) specified by \a fileName, or returns an | - |
464 | empty string if the \a fileName does not correspond to a symbolic link. | - |
465 | | - |
466 | This name may not represent an existing file; it is only a string. | - |
467 | QFile::exists() returns true if the symlink points to an existing file. | - |
468 | */ | - |
469 | | - |
470 | /*! | - |
471 | \obsolete | - |
472 | | - |
473 | Use symLinkTarget() instead. | - |
474 | */ | - |
475 | QString | - |
476 | QFile::readLink(const QString &fileName) | - |
477 | { | - |
478 | return QFileInfo(fileName).readLink(); executed: return QFileInfo(fileName).readLink(); Execution Count:1 | 1 |
479 | } | - |
480 | | - |
481 | /*! | - |
482 | Removes the file specified by fileName(). Returns true if successful; | - |
483 | otherwise returns false. | - |
484 | | - |
485 | The file is closed before it is removed. | - |
486 | | - |
487 | \sa setFileName() | - |
488 | */ | - |
489 | | - |
490 | bool | - |
491 | QFile::remove() | - |
492 | { | - |
493 | Q_D(QFile); executed (the execution status of this line is deduced): QFilePrivate * const d = d_func(); | - |
494 | if (d->fileName.isEmpty()) { partially evaluated: d->fileName.isEmpty() no Evaluation Count:0 | yes Evaluation Count:5493 |
| 0-5493 |
495 | qWarning("QFile::remove: Empty or null file name"); never executed (the execution status of this line is deduced): QMessageLogger("io/qfile.cpp", 495, __PRETTY_FUNCTION__).warning("QFile::remove: Empty or null file name"); | - |
496 | return false; never executed: return false; | 0 |
497 | } | - |
498 | unsetError(); executed (the execution status of this line is deduced): unsetError(); | - |
499 | close(); executed (the execution status of this line is deduced): close(); | - |
500 | if(error() == QFile::NoError) { partially evaluated: error() == QFile::NoError yes Evaluation Count:5493 | no Evaluation Count:0 |
| 0-5493 |
501 | if (d->engine()->remove()) { evaluated: d->engine()->remove() yes Evaluation Count:4879 | yes Evaluation Count:614 |
| 614-4879 |
502 | unsetError(); executed (the execution status of this line is deduced): unsetError(); | - |
503 | return true; executed: return true; Execution Count:4879 | 4879 |
504 | } | - |
505 | d->setError(QFile::RemoveError, d->fileEngine->errorString()); executed (the execution status of this line is deduced): d->setError(QFile::RemoveError, d->fileEngine->errorString()); | - |
506 | } executed: } Execution Count:614 | 614 |
507 | return false; executed: return false; Execution Count:614 | 614 |
508 | } | - |
509 | | - |
510 | /*! | - |
511 | \overload | - |
512 | | - |
513 | Removes the file specified by the \a fileName given. | - |
514 | | - |
515 | Returns true if successful; otherwise returns false. | - |
516 | | - |
517 | \sa remove() | - |
518 | */ | - |
519 | | - |
520 | bool | - |
521 | QFile::remove(const QString &fileName) | - |
522 | { | - |
523 | return QFile(fileName).remove(); executed: return QFile(fileName).remove(); Execution Count:4326 | 4326 |
524 | } | - |
525 | | - |
526 | /*! | - |
527 | Renames the file currently specified by fileName() to \a newName. | - |
528 | Returns true if successful; otherwise returns false. | - |
529 | | - |
530 | If a file with the name \a newName already exists, rename() returns false | - |
531 | (i.e., QFile will not overwrite it). | - |
532 | | - |
533 | The file is closed before it is renamed. | - |
534 | | - |
535 | If the rename operation fails, Qt will attempt to copy this file's | - |
536 | contents to \a newName, and then remove this file, keeping only | - |
537 | \a newName. If that copy operation fails or this file can't be removed, | - |
538 | the destination file \a newName is removed to restore the old state. | - |
539 | | - |
540 | \sa setFileName() | - |
541 | */ | - |
542 | | - |
543 | bool | - |
544 | QFile::rename(const QString &newName) | - |
545 | { | - |
546 | Q_D(QFile); executed (the execution status of this line is deduced): QFilePrivate * const d = d_func(); | - |
547 | if (d->fileName.isEmpty()) { partially evaluated: d->fileName.isEmpty() no Evaluation Count:0 | yes Evaluation Count:210 |
| 0-210 |
548 | qWarning("QFile::rename: Empty or null file name"); never executed (the execution status of this line is deduced): QMessageLogger("io/qfile.cpp", 548, __PRETTY_FUNCTION__).warning("QFile::rename: Empty or null file name"); | - |
549 | return false; never executed: return false; | 0 |
550 | } | - |
551 | if (QFile(newName).exists()) { evaluated: QFile(newName).exists() yes Evaluation Count:6 | yes Evaluation Count:204 |
| 6-204 |
552 | // ### Race condition. If a file is moved in after this, it /will/ be | - |
553 | // overwritten. On Unix, the proper solution is to use hardlinks: | - |
554 | // return ::link(old, new) && ::remove(old); | - |
555 | d->setError(QFile::RenameError, tr("Destination file exists")); executed (the execution status of this line is deduced): d->setError(QFile::RenameError, tr("Destination file exists")); | - |
556 | return false; executed: return false; Execution Count:6 | 6 |
557 | } | - |
558 | unsetError(); executed (the execution status of this line is deduced): unsetError(); | - |
559 | close(); executed (the execution status of this line is deduced): close(); | - |
560 | if(error() == QFile::NoError) { partially evaluated: error() == QFile::NoError yes Evaluation Count:204 | no Evaluation Count:0 |
| 0-204 |
561 | if (d->engine()->rename(newName)) { evaluated: d->engine()->rename(newName) yes Evaluation Count:199 | yes Evaluation Count:5 |
| 5-199 |
562 | unsetError(); executed (the execution status of this line is deduced): unsetError(); | - |
563 | // engine was able to handle the new name so we just reset it | - |
564 | d->fileEngine->setFileName(newName); executed (the execution status of this line is deduced): d->fileEngine->setFileName(newName); | - |
565 | d->fileName = newName; executed (the execution status of this line is deduced): d->fileName = newName; | - |
566 | return true; executed: return true; Execution Count:199 | 199 |
567 | } | - |
568 | | - |
569 | if (isSequential()) { evaluated: isSequential() yes Evaluation Count:1 | yes Evaluation Count:4 |
| 1-4 |
570 | d->setError(QFile::RenameError, tr("Will not rename sequential file using block copy")); executed (the execution status of this line is deduced): d->setError(QFile::RenameError, tr("Will not rename sequential file using block copy")); | - |
571 | return false; executed: return false; Execution Count:1 | 1 |
572 | } | - |
573 | | - |
574 | QFile out(newName); executed (the execution status of this line is deduced): QFile out(newName); | - |
575 | if (open(QIODevice::ReadOnly)) { partially evaluated: open(QIODevice::ReadOnly) yes Evaluation Count:4 | no Evaluation Count:0 |
| 0-4 |
576 | if (out.open(QIODevice::WriteOnly | QIODevice::Truncate)) { evaluated: out.open(QIODevice::WriteOnly | QIODevice::Truncate) yes Evaluation Count:1 | yes Evaluation Count:3 |
| 1-3 |
577 | bool error = false; executed (the execution status of this line is deduced): bool error = false; | - |
578 | char block[4096]; executed (the execution status of this line is deduced): char block[4096]; | - |
579 | qint64 bytes; executed (the execution status of this line is deduced): qint64 bytes; | - |
580 | while ((bytes = read(block, sizeof(block))) > 0) { evaluated: (bytes = read(block, sizeof(block))) > 0 yes Evaluation Count:1 | yes Evaluation Count:1 |
| 1 |
581 | if (bytes != out.write(block, bytes)) { partially evaluated: bytes != out.write(block, bytes) no Evaluation Count:0 | yes Evaluation Count:1 |
| 0-1 |
582 | d->setError(QFile::RenameError, out.errorString()); never executed (the execution status of this line is deduced): d->setError(QFile::RenameError, out.errorString()); | - |
583 | error = true; never executed (the execution status of this line is deduced): error = true; | - |
584 | break; | 0 |
585 | } | - |
586 | } executed: } Execution Count:1 | 1 |
587 | if (bytes == -1) { partially evaluated: bytes == -1 no Evaluation Count:0 | yes Evaluation Count:1 |
| 0-1 |
588 | d->setError(QFile::RenameError, errorString()); never executed (the execution status of this line is deduced): d->setError(QFile::RenameError, errorString()); | - |
589 | error = true; never executed (the execution status of this line is deduced): error = true; | - |
590 | } | 0 |
591 | if(!error) { partially evaluated: !error yes Evaluation Count:1 | no Evaluation Count:0 |
| 0-1 |
592 | if (!remove()) { partially evaluated: !remove() yes Evaluation Count:1 | no Evaluation Count:0 |
| 0-1 |
593 | d->setError(QFile::RenameError, tr("Cannot remove source file")); executed (the execution status of this line is deduced): d->setError(QFile::RenameError, tr("Cannot remove source file")); | - |
594 | error = true; executed (the execution status of this line is deduced): error = true; | - |
595 | } executed: } Execution Count:1 | 1 |
596 | } executed: } Execution Count:1 | 1 |
597 | if (error) { partially evaluated: error yes Evaluation Count:1 | no Evaluation Count:0 |
| 0-1 |
598 | out.remove(); executed (the execution status of this line is deduced): out.remove(); | - |
599 | } else { executed: } Execution Count:1 | 1 |
600 | d->fileEngine->setFileName(newName); never executed (the execution status of this line is deduced): d->fileEngine->setFileName(newName); | - |
601 | setPermissions(permissions()); never executed (the execution status of this line is deduced): setPermissions(permissions()); | - |
602 | unsetError(); never executed (the execution status of this line is deduced): unsetError(); | - |
603 | setFileName(newName); never executed (the execution status of this line is deduced): setFileName(newName); | - |
604 | } | 0 |
605 | close(); executed (the execution status of this line is deduced): close(); | - |
606 | return !error; executed: return !error; Execution Count:1 | 1 |
607 | } | - |
608 | close(); executed (the execution status of this line is deduced): close(); | - |
609 | } executed: } Execution Count:3 | 3 |
610 | d->setError(QFile::RenameError, out.isOpen() ? errorString() : out.errorString()); executed (the execution status of this line is deduced): d->setError(QFile::RenameError, out.isOpen() ? errorString() : out.errorString()); | - |
611 | } executed: } Execution Count:3 | 3 |
612 | return false; executed: return false; Execution Count:3 | 3 |
613 | } | - |
614 | | - |
615 | /*! | - |
616 | \overload | - |
617 | | - |
618 | Renames the file \a oldName to \a newName. Returns true if | - |
619 | successful; otherwise returns false. | - |
620 | | - |
621 | If a file with the name \a newName already exists, rename() returns false | - |
622 | (i.e., QFile will not overwrite it). | - |
623 | | - |
624 | \sa rename() | - |
625 | */ | - |
626 | | - |
627 | bool | - |
628 | QFile::rename(const QString &oldName, const QString &newName) | - |
629 | { | - |
630 | return QFile(oldName).rename(newName); executed: return QFile(oldName).rename(newName); Execution Count:7 | 7 |
631 | } | - |
632 | | - |
633 | /*! | - |
634 | | - |
635 | Creates a link named \a linkName that points to the file currently specified by | - |
636 | fileName(). What a link is depends on the underlying filesystem (be it a | - |
637 | shortcut on Windows or a symbolic link on Unix). Returns true if successful; | - |
638 | otherwise returns false. | - |
639 | | - |
640 | This function will not overwrite an already existing entity in the file system; | - |
641 | in this case, \c link() will return false and set \l{QFile::}{error()} to | - |
642 | return \l{QFile::}{RenameError}. | - |
643 | | - |
644 | \note To create a valid link on Windows, \a linkName must have a \c{.lnk} file extension. | - |
645 | | - |
646 | \sa setFileName() | - |
647 | */ | - |
648 | | - |
649 | bool | - |
650 | QFile::link(const QString &linkName) | - |
651 | { | - |
652 | Q_D(QFile); executed (the execution status of this line is deduced): QFilePrivate * const d = d_func(); | - |
653 | if (d->fileName.isEmpty()) { partially evaluated: d->fileName.isEmpty() no Evaluation Count:0 | yes Evaluation Count:145 |
| 0-145 |
654 | qWarning("QFile::link: Empty or null file name"); never executed (the execution status of this line is deduced): QMessageLogger("io/qfile.cpp", 654, __PRETTY_FUNCTION__).warning("QFile::link: Empty or null file name"); | - |
655 | return false; never executed: return false; | 0 |
656 | } | - |
657 | QFileInfo fi(linkName); executed (the execution status of this line is deduced): QFileInfo fi(linkName); | - |
658 | if (d->engine()->link(fi.absoluteFilePath())) { partially evaluated: d->engine()->link(fi.absoluteFilePath()) yes Evaluation Count:145 | no Evaluation Count:0 |
| 0-145 |
659 | unsetError(); executed (the execution status of this line is deduced): unsetError(); | - |
660 | return true; executed: return true; Execution Count:145 | 145 |
661 | } | - |
662 | d->setError(QFile::RenameError, d->fileEngine->errorString()); never executed (the execution status of this line is deduced): d->setError(QFile::RenameError, d->fileEngine->errorString()); | - |
663 | return false; never executed: return false; | 0 |
664 | } | - |
665 | | - |
666 | /*! | - |
667 | \overload | - |
668 | | - |
669 | Creates a link named \a linkName that points to the file \a fileName. What a link is | - |
670 | depends on the underlying filesystem (be it a shortcut on Windows | - |
671 | or a symbolic link on Unix). Returns true if successful; otherwise | - |
672 | returns false. | - |
673 | | - |
674 | \sa link() | - |
675 | */ | - |
676 | | - |
677 | bool | - |
678 | QFile::link(const QString &fileName, const QString &linkName) | - |
679 | { | - |
680 | return QFile(fileName).link(linkName); executed: return QFile(fileName).link(linkName); Execution Count:143 | 143 |
681 | } | - |
682 | | - |
683 | /*! | - |
684 | Copies the file currently specified by fileName() to a file called | - |
685 | \a newName. Returns true if successful; otherwise returns false. | - |
686 | | - |
687 | Note that if a file with the name \a newName already exists, | - |
688 | copy() returns false (i.e. QFile will not overwrite it). | - |
689 | | - |
690 | The source file is closed before it is copied. | - |
691 | | - |
692 | \sa setFileName() | - |
693 | */ | - |
694 | | - |
695 | bool | - |
696 | QFile::copy(const QString &newName) | - |
697 | { | - |
698 | Q_D(QFile); executed (the execution status of this line is deduced): QFilePrivate * const d = d_func(); | - |
699 | if (d->fileName.isEmpty()) { partially evaluated: d->fileName.isEmpty() no Evaluation Count:0 | yes Evaluation Count:101 |
| 0-101 |
700 | qWarning("QFile::copy: Empty or null file name"); never executed (the execution status of this line is deduced): QMessageLogger("io/qfile.cpp", 700, __PRETTY_FUNCTION__).warning("QFile::copy: Empty or null file name"); | - |
701 | return false; never executed: return false; | 0 |
702 | } | - |
703 | if (QFile(newName).exists()) { evaluated: QFile(newName).exists() yes Evaluation Count:3 | yes Evaluation Count:98 |
| 3-98 |
704 | // ### Race condition. If a file is moved in after this, it /will/ be | - |
705 | // overwritten. On Unix, the proper solution is to use hardlinks: | - |
706 | // return ::link(old, new) && ::remove(old); See also rename(). | - |
707 | d->setError(QFile::CopyError, tr("Destination file exists")); executed (the execution status of this line is deduced): d->setError(QFile::CopyError, tr("Destination file exists")); | - |
708 | return false; executed: return false; Execution Count:3 | 3 |
709 | } | - |
710 | unsetError(); executed (the execution status of this line is deduced): unsetError(); | - |
711 | close(); executed (the execution status of this line is deduced): close(); | - |
712 | if(error() == QFile::NoError) { partially evaluated: error() == QFile::NoError yes Evaluation Count:98 | no Evaluation Count:0 |
| 0-98 |
713 | if (d->engine()->copy(newName)) { partially evaluated: d->engine()->copy(newName) no Evaluation Count:0 | yes Evaluation Count:98 |
| 0-98 |
714 | unsetError(); never executed (the execution status of this line is deduced): unsetError(); | - |
715 | return true; never executed: return true; | 0 |
716 | } else { | - |
717 | bool error = false; executed (the execution status of this line is deduced): bool error = false; | - |
718 | if(!open(QFile::ReadOnly)) { evaluated: !open(QFile::ReadOnly) yes Evaluation Count:1 | yes Evaluation Count:97 |
| 1-97 |
719 | error = true; executed (the execution status of this line is deduced): error = true; | - |
720 | d->setError(QFile::CopyError, tr("Cannot open %1 for input").arg(d->fileName)); executed (the execution status of this line is deduced): d->setError(QFile::CopyError, tr("Cannot open %1 for input").arg(d->fileName)); | - |
721 | } else { executed: } Execution Count:1 | 1 |
722 | QString fileTemplate = QLatin1String("%1/qt_temp.XXXXXX"); executed (the execution status of this line is deduced): QString fileTemplate = QLatin1String("%1/qt_temp.XXXXXX"); | - |
723 | #ifdef QT_NO_TEMPORARYFILE | - |
724 | QFile out(fileTemplate.arg(QFileInfo(newName).path())); | - |
725 | if (!out.open(QIODevice::ReadWrite)) | - |
726 | error = true; | - |
727 | #else | - |
728 | QTemporaryFile out(fileTemplate.arg(QFileInfo(newName).path())); executed (the execution status of this line is deduced): QTemporaryFile out(fileTemplate.arg(QFileInfo(newName).path())); | - |
729 | if (!out.open()) { partially evaluated: !out.open() no Evaluation Count:0 | yes Evaluation Count:97 |
| 0-97 |
730 | out.setFileTemplate(fileTemplate.arg(QDir::tempPath())); never executed (the execution status of this line is deduced): out.setFileTemplate(fileTemplate.arg(QDir::tempPath())); | - |
731 | if (!out.open()) never evaluated: !out.open() | 0 |
732 | error = true; never executed: error = true; | 0 |
733 | } | 0 |
734 | #endif | - |
735 | if (error) { partially evaluated: error no Evaluation Count:0 | yes Evaluation Count:97 |
| 0-97 |
736 | out.close(); never executed (the execution status of this line is deduced): out.close(); | - |
737 | close(); never executed (the execution status of this line is deduced): close(); | - |
738 | d->setError(QFile::CopyError, tr("Cannot open for output")); never executed (the execution status of this line is deduced): d->setError(QFile::CopyError, tr("Cannot open for output")); | - |
739 | } else { | 0 |
740 | char block[4096]; executed (the execution status of this line is deduced): char block[4096]; | - |
741 | qint64 totalRead = 0; executed (the execution status of this line is deduced): qint64 totalRead = 0; | - |
742 | while(!atEnd()) { evaluated: !atEnd() yes Evaluation Count:771 | yes Evaluation Count:97 |
| 97-771 |
743 | qint64 in = read(block, sizeof(block)); executed (the execution status of this line is deduced): qint64 in = read(block, sizeof(block)); | - |
744 | if (in <= 0) partially evaluated: in <= 0 no Evaluation Count:0 | yes Evaluation Count:771 |
| 0-771 |
745 | break; | 0 |
746 | totalRead += in; executed (the execution status of this line is deduced): totalRead += in; | - |
747 | if(in != out.write(block, in)) { partially evaluated: in != out.write(block, in) no Evaluation Count:0 | yes Evaluation Count:771 |
| 0-771 |
748 | close(); never executed (the execution status of this line is deduced): close(); | - |
749 | d->setError(QFile::CopyError, tr("Failure to write block")); never executed (the execution status of this line is deduced): d->setError(QFile::CopyError, tr("Failure to write block")); | - |
750 | error = true; never executed (the execution status of this line is deduced): error = true; | - |
751 | break; | 0 |
752 | } | - |
753 | } executed: } Execution Count:771 | 771 |
754 | | - |
755 | if (totalRead != size()) { partially evaluated: totalRead != size() no Evaluation Count:0 | yes Evaluation Count:97 |
| 0-97 |
756 | // Unable to read from the source. The error string is | - |
757 | // already set from read(). | - |
758 | error = true; never executed (the execution status of this line is deduced): error = true; | - |
759 | } | 0 |
760 | if (!error && !out.rename(newName)) { partially evaluated: !error yes Evaluation Count:97 | no Evaluation Count:0 |
partially evaluated: !out.rename(newName) no Evaluation Count:0 | yes Evaluation Count:97 |
| 0-97 |
761 | error = true; never executed (the execution status of this line is deduced): error = true; | - |
762 | close(); never executed (the execution status of this line is deduced): close(); | - |
763 | d->setError(QFile::CopyError, tr("Cannot create %1 for output").arg(newName)); never executed (the execution status of this line is deduced): d->setError(QFile::CopyError, tr("Cannot create %1 for output").arg(newName)); | - |
764 | } | 0 |
765 | #ifdef QT_NO_TEMPORARYFILE | - |
766 | if (error) | - |
767 | out.remove(); | - |
768 | #else | - |
769 | if (!error) partially evaluated: !error yes Evaluation Count:97 | no Evaluation Count:0 |
| 0-97 |
770 | out.setAutoRemove(false); executed: out.setAutoRemove(false); Execution Count:97 | 97 |
771 | #endif | - |
772 | } executed: } Execution Count:97 | 97 |
773 | } | - |
774 | if(!error) { evaluated: !error yes Evaluation Count:97 | yes Evaluation Count:1 |
| 1-97 |
775 | QFile::setPermissions(newName, permissions()); executed (the execution status of this line is deduced): QFile::setPermissions(newName, permissions()); | - |
776 | close(); executed (the execution status of this line is deduced): close(); | - |
777 | unsetError(); executed (the execution status of this line is deduced): unsetError(); | - |
778 | return true; executed: return true; Execution Count:97 | 97 |
779 | } | - |
780 | } executed: } Execution Count:1 | 1 |
781 | } | - |
782 | return false; executed: return false; Execution Count:1 | 1 |
783 | } | - |
784 | | - |
785 | /*! | - |
786 | \overload | - |
787 | | - |
788 | Copies the file \a fileName to \a newName. Returns true if successful; | - |
789 | otherwise returns false. | - |
790 | | - |
791 | If a file with the name \a newName already exists, copy() returns false | - |
792 | (i.e., QFile will not overwrite it). | - |
793 | | - |
794 | \sa rename() | - |
795 | */ | - |
796 | | - |
797 | bool | - |
798 | QFile::copy(const QString &fileName, const QString &newName) | - |
799 | { | - |
800 | return QFile(fileName).copy(newName); executed: return QFile(fileName).copy(newName); Execution Count:92 | 92 |
801 | } | - |
802 | | - |
803 | /*! | - |
804 | Opens the file using OpenMode \a mode, returning true if successful; | - |
805 | otherwise false. | - |
806 | | - |
807 | The \a mode must be QIODevice::ReadOnly, QIODevice::WriteOnly, or | - |
808 | QIODevice::ReadWrite. It may also have additional flags, such as | - |
809 | QIODevice::Text and QIODevice::Unbuffered. | - |
810 | | - |
811 | \note In \l{QIODevice::}{WriteOnly} or \l{QIODevice::}{ReadWrite} | - |
812 | mode, if the relevant file does not already exist, this function | - |
813 | will try to create a new file before opening it. | - |
814 | | - |
815 | \sa QIODevice::OpenMode, setFileName() | - |
816 | */ | - |
817 | bool QFile::open(OpenMode mode) | - |
818 | { | - |
819 | Q_D(QFile); executed (the execution status of this line is deduced): QFilePrivate * const d = d_func(); | - |
820 | if (isOpen()) { partially evaluated: isOpen() no Evaluation Count:0 | yes Evaluation Count:20816 |
| 0-20816 |
821 | qWarning("QFile::open: File (%s) already open", qPrintable(fileName())); never executed (the execution status of this line is deduced): QMessageLogger("io/qfile.cpp", 821, __PRETTY_FUNCTION__).warning("QFile::open: File (%s) already open", QString(fileName()).toLocal8Bit().constData()); | - |
822 | return false; never executed: return false; | 0 |
823 | } | - |
824 | if (mode & Append) evaluated: mode & Append yes Evaluation Count:248 | yes Evaluation Count:20568 |
| 248-20568 |
825 | mode |= WriteOnly; executed: mode |= WriteOnly; Execution Count:248 | 248 |
826 | | - |
827 | unsetError(); executed (the execution status of this line is deduced): unsetError(); | - |
828 | if ((mode & (ReadOnly | WriteOnly)) == 0) { partially evaluated: (mode & (ReadOnly | WriteOnly)) == 0 no Evaluation Count:0 | yes Evaluation Count:20816 |
| 0-20816 |
829 | qWarning("QIODevice::open: File access not specified"); never executed (the execution status of this line is deduced): QMessageLogger("io/qfile.cpp", 829, __PRETTY_FUNCTION__).warning("QIODevice::open: File access not specified"); | - |
830 | return false; never executed: return false; | 0 |
831 | } | - |
832 | | - |
833 | // QIODevice provides the buffering, so there's no need to request it from the file engine. | - |
834 | if (d->engine()->open(mode | QIODevice::Unbuffered)) { evaluated: d->engine()->open(mode | QIODevice::Unbuffered) yes Evaluation Count:17731 | yes Evaluation Count:3085 |
| 3085-17731 |
835 | QIODevice::open(mode); executed (the execution status of this line is deduced): QIODevice::open(mode); | - |
836 | if (mode & Append) evaluated: mode & Append yes Evaluation Count:247 | yes Evaluation Count:17484 |
| 247-17484 |
837 | seek(size()); executed: seek(size()); Execution Count:247 | 247 |
838 | return true; executed: return true; Execution Count:17731 | 17731 |
839 | } | - |
840 | QFile::FileError err = d->fileEngine->error(); executed (the execution status of this line is deduced): QFile::FileError err = d->fileEngine->error(); | - |
841 | if(err == QFile::UnspecifiedError) evaluated: err == QFile::UnspecifiedError yes Evaluation Count:164 | yes Evaluation Count:2921 |
| 164-2921 |
842 | err = QFile::OpenError; executed: err = QFile::OpenError; Execution Count:164 | 164 |
843 | d->setError(err, d->fileEngine->errorString()); executed (the execution status of this line is deduced): d->setError(err, d->fileEngine->errorString()); | - |
844 | return false; executed: return false; Execution Count:3085 | 3085 |
845 | } | - |
846 | | - |
847 | /*! | - |
848 | \overload | - |
849 | | - |
850 | Opens the existing file handle \a fh in the given \a mode. | - |
851 | \a handleFlags may be used to specify additional options. | - |
852 | Returns true if successful; otherwise returns false. | - |
853 | | - |
854 | Example: | - |
855 | \snippet code/src_corelib_io_qfile.cpp 3 | - |
856 | | - |
857 | When a QFile is opened using this function, behaviour of close() is | - |
858 | controlled by the AutoCloseHandle flag. | - |
859 | If AutoCloseHandle is specified, and this function succeeds, | - |
860 | then calling close() closes the adopted handle. | - |
861 | Otherwise, close() does not actually close the file, but only flushes it. | - |
862 | | - |
863 | \b{Warning:} | - |
864 | \list 1 | - |
865 | \li If \a fh does not refer to a regular file, e.g., it is \c stdin, | - |
866 | \c stdout, or \c stderr, you may not be able to seek(). size() | - |
867 | returns \c 0 in those cases. See QIODevice::isSequential() for | - |
868 | more information. | - |
869 | \li Since this function opens the file without specifying the file name, | - |
870 | you cannot use this QFile with a QFileInfo. | - |
871 | \endlist | - |
872 | | - |
873 | \note For Windows CE you may not be able to call resize(). | - |
874 | | - |
875 | \sa close() | - |
876 | | - |
877 | \b{Note for the Windows Platform} | - |
878 | | - |
879 | \a fh must be opened in binary mode (i.e., the mode string must contain | - |
880 | 'b', as in "rb" or "wb") when accessing files and other random-access | - |
881 | devices. Qt will translate the end-of-line characters if you pass | - |
882 | QIODevice::Text to \a mode. Sequential devices, such as stdin and stdout, | - |
883 | are unaffected by this limitation. | - |
884 | | - |
885 | You need to enable support for console applications in order to use the | - |
886 | stdin, stdout and stderr streams at the console. To do this, add the | - |
887 | following declaration to your application's project file: | - |
888 | | - |
889 | \snippet code/src_corelib_io_qfile.cpp 4 | - |
890 | */ | - |
891 | bool QFile::open(FILE *fh, OpenMode mode, FileHandleFlags handleFlags) | - |
892 | { | - |
893 | Q_D(QFile); executed (the execution status of this line is deduced): QFilePrivate * const d = d_func(); | - |
894 | if (isOpen()) { partially evaluated: isOpen() no Evaluation Count:0 | yes Evaluation Count:31 |
| 0-31 |
895 | qWarning("QFile::open: File (%s) already open", qPrintable(fileName())); never executed (the execution status of this line is deduced): QMessageLogger("io/qfile.cpp", 895, __PRETTY_FUNCTION__).warning("QFile::open: File (%s) already open", QString(fileName()).toLocal8Bit().constData()); | - |
896 | return false; never executed: return false; | 0 |
897 | } | - |
898 | if (mode & Append) partially evaluated: mode & Append no Evaluation Count:0 | yes Evaluation Count:31 |
| 0-31 |
899 | mode |= WriteOnly; never executed: mode |= WriteOnly; | 0 |
900 | unsetError(); executed (the execution status of this line is deduced): unsetError(); | - |
901 | if ((mode & (ReadOnly | WriteOnly)) == 0) { partially evaluated: (mode & (ReadOnly | WriteOnly)) == 0 no Evaluation Count:0 | yes Evaluation Count:31 |
| 0-31 |
902 | qWarning("QFile::open: File access not specified"); never executed (the execution status of this line is deduced): QMessageLogger("io/qfile.cpp", 902, __PRETTY_FUNCTION__).warning("QFile::open: File access not specified"); | - |
903 | return false; never executed: return false; | 0 |
904 | } | - |
905 | if (d->openExternalFile(mode, fh, handleFlags)) { partially evaluated: d->openExternalFile(mode, fh, handleFlags) yes Evaluation Count:31 | no Evaluation Count:0 |
| 0-31 |
906 | QIODevice::open(mode); executed (the execution status of this line is deduced): QIODevice::open(mode); | - |
907 | if (!(mode & Append) && !isSequential()) { partially evaluated: !(mode & Append) yes Evaluation Count:31 | no Evaluation Count:0 |
evaluated: !isSequential() yes Evaluation Count:27 | yes Evaluation Count:4 |
| 0-31 |
908 | qint64 pos = (qint64)QT_FTELL(fh); executed (the execution status of this line is deduced): qint64 pos = (qint64)::ftello64(fh); | - |
909 | if (pos != -1) { partially evaluated: pos != -1 yes Evaluation Count:27 | no Evaluation Count:0 |
| 0-27 |
910 | // Skip redundant checks in QFileDevice::seek(). | - |
911 | QIODevice::seek(pos); executed (the execution status of this line is deduced): QIODevice::seek(pos); | - |
912 | } executed: } Execution Count:27 | 27 |
913 | } executed: } Execution Count:27 | 27 |
914 | return true; executed: return true; Execution Count:31 | 31 |
915 | } | - |
916 | return false; never executed: return false; | 0 |
917 | } | - |
918 | | - |
919 | /*! | - |
920 | \overload | - |
921 | | - |
922 | Opens the existing file descriptor \a fd in the given \a mode. | - |
923 | \a handleFlags may be used to specify additional options. | - |
924 | Returns true if successful; otherwise returns false. | - |
925 | | - |
926 | When a QFile is opened using this function, behaviour of close() is | - |
927 | controlled by the AutoCloseHandle flag. | - |
928 | If AutoCloseHandle is specified, and this function succeeds, | - |
929 | then calling close() closes the adopted handle. | - |
930 | Otherwise, close() does not actually close the file, but only flushes it. | - |
931 | | - |
932 | The QFile that is opened using this function is automatically set | - |
933 | to be in raw mode; this means that the file input/output functions | - |
934 | are slow. If you run into performance issues, you should try to | - |
935 | use one of the other open functions. | - |
936 | | - |
937 | \warning If \a fd is not a regular file, e.g, it is 0 (\c stdin), | - |
938 | 1 (\c stdout), or 2 (\c stderr), you may not be able to seek(). In | - |
939 | those cases, size() returns \c 0. See QIODevice::isSequential() | - |
940 | for more information. | - |
941 | | - |
942 | \warning For Windows CE you may not be able to call seek(), setSize(), | - |
943 | fileTime(). size() returns \c 0. | - |
944 | | - |
945 | \warning Since this function opens the file without specifying the file name, | - |
946 | you cannot use this QFile with a QFileInfo. | - |
947 | | - |
948 | \sa close() | - |
949 | */ | - |
950 | bool QFile::open(int fd, OpenMode mode, FileHandleFlags handleFlags) | - |
951 | { | - |
952 | Q_D(QFile); executed (the execution status of this line is deduced): QFilePrivate * const d = d_func(); | - |
953 | if (isOpen()) { partially evaluated: isOpen() no Evaluation Count:0 | yes Evaluation Count:30 |
| 0-30 |
954 | qWarning("QFile::open: File (%s) already open", qPrintable(fileName())); never executed (the execution status of this line is deduced): QMessageLogger("io/qfile.cpp", 954, __PRETTY_FUNCTION__).warning("QFile::open: File (%s) already open", QString(fileName()).toLocal8Bit().constData()); | - |
955 | return false; never executed: return false; | 0 |
956 | } | - |
957 | if (mode & Append) partially evaluated: mode & Append no Evaluation Count:0 | yes Evaluation Count:30 |
| 0-30 |
958 | mode |= WriteOnly; never executed: mode |= WriteOnly; | 0 |
959 | unsetError(); executed (the execution status of this line is deduced): unsetError(); | - |
960 | if ((mode & (ReadOnly | WriteOnly)) == 0) { partially evaluated: (mode & (ReadOnly | WriteOnly)) == 0 no Evaluation Count:0 | yes Evaluation Count:30 |
| 0-30 |
961 | qWarning("QFile::open: File access not specified"); never executed (the execution status of this line is deduced): QMessageLogger("io/qfile.cpp", 961, __PRETTY_FUNCTION__).warning("QFile::open: File access not specified"); | - |
962 | return false; never executed: return false; | 0 |
963 | } | - |
964 | if (d->openExternalFile(mode, fd, handleFlags)) { partially evaluated: d->openExternalFile(mode, fd, handleFlags) yes Evaluation Count:30 | no Evaluation Count:0 |
| 0-30 |
965 | QIODevice::open(mode); executed (the execution status of this line is deduced): QIODevice::open(mode); | - |
966 | if (!(mode & Append) && !isSequential()) { partially evaluated: !(mode & Append) yes Evaluation Count:30 | no Evaluation Count:0 |
evaluated: !isSequential() yes Evaluation Count:24 | yes Evaluation Count:6 |
| 0-30 |
967 | qint64 pos = (qint64)QT_LSEEK(fd, QT_OFF_T(0), SEEK_CUR); executed (the execution status of this line is deduced): qint64 pos = (qint64)::lseek64(fd, off64_t(0), 1); | - |
968 | if (pos != -1) { partially evaluated: pos != -1 yes Evaluation Count:24 | no Evaluation Count:0 |
| 0-24 |
969 | // Skip redundant checks in QFileDevice::seek(). | - |
970 | QIODevice::seek(pos); executed (the execution status of this line is deduced): QIODevice::seek(pos); | - |
971 | } executed: } Execution Count:24 | 24 |
972 | } executed: } Execution Count:24 | 24 |
973 | return true; executed: return true; Execution Count:30 | 30 |
974 | } | - |
975 | return false; never executed: return false; | 0 |
976 | } | - |
977 | | - |
978 | /*! | - |
979 | \reimp | - |
980 | */ | - |
981 | bool QFile::resize(qint64 sz) | - |
982 | { | - |
983 | return QFileDevice::resize(sz); // for now executed: return QFileDevice::resize(sz); Execution Count:730 | 730 |
984 | } | - |
985 | | - |
986 | /*! | - |
987 | \overload | - |
988 | | - |
989 | Sets \a fileName to size (in bytes) \a sz. Returns true if the file if | - |
990 | the resize succeeds; false otherwise. If \a sz is larger than \a | - |
991 | fileName currently is the new bytes will be set to 0, if \a sz is | - |
992 | smaller the file is simply truncated. | - |
993 | | - |
994 | \sa resize() | - |
995 | */ | - |
996 | | - |
997 | bool | - |
998 | QFile::resize(const QString &fileName, qint64 sz) | - |
999 | { | - |
1000 | return QFile(fileName).resize(sz); executed: return QFile(fileName).resize(sz); Execution Count:3 | 3 |
1001 | } | - |
1002 | | - |
1003 | /*! | - |
1004 | \reimp | - |
1005 | */ | - |
1006 | QFile::Permissions QFile::permissions() const | - |
1007 | { | - |
1008 | return QFileDevice::permissions(); // for now executed: return QFileDevice::permissions(); Execution Count:226 | 226 |
1009 | } | - |
1010 | | - |
1011 | /*! | - |
1012 | \overload | - |
1013 | | - |
1014 | Returns the complete OR-ed together combination of | - |
1015 | QFile::Permission for \a fileName. | - |
1016 | */ | - |
1017 | | - |
1018 | QFile::Permissions | - |
1019 | QFile::permissions(const QString &fileName) | - |
1020 | { | - |
1021 | return QFile(fileName).permissions(); executed: return QFile(fileName).permissions(); Execution Count:9 | 9 |
1022 | } | - |
1023 | | - |
1024 | /*! | - |
1025 | Sets the permissions for the file to the \a permissions specified. | - |
1026 | Returns true if successful, or false if the permissions cannot be | - |
1027 | modified. | - |
1028 | | - |
1029 | \sa permissions(), setFileName() | - |
1030 | */ | - |
1031 | | - |
1032 | bool QFile::setPermissions(Permissions permissions) | - |
1033 | { | - |
1034 | return QFileDevice::setPermissions(permissions); // for now executed: return QFileDevice::setPermissions(permissions); Execution Count:515 | 515 |
1035 | } | - |
1036 | | - |
1037 | /*! | - |
1038 | \overload | - |
1039 | | - |
1040 | Sets the permissions for \a fileName file to \a permissions. | - |
1041 | */ | - |
1042 | | - |
1043 | bool | - |
1044 | QFile::setPermissions(const QString &fileName, Permissions permissions) | - |
1045 | { | - |
1046 | return QFile(fileName).setPermissions(permissions); executed: return QFile(fileName).setPermissions(permissions); Execution Count:119 | 119 |
1047 | } | - |
1048 | | - |
1049 | /*! | - |
1050 | \reimp | - |
1051 | */ | - |
1052 | qint64 QFile::size() const | - |
1053 | { | - |
1054 | return QFileDevice::size(); // for now executed: return QFileDevice::size(); Execution Count:122189 | 122189 |
1055 | } | - |
1056 | | - |
1057 | QT_END_NAMESPACE | - |
1058 | | - |
| | |