~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/corelib/io/qfile.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the core module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <qplatformdefs.h>
 
30
 
 
31
#include "qfile.h"
 
32
#include <qfileengine.h>
 
33
#include "qbufferedfsfileengine_p.h"
 
34
#include <qtemporaryfile.h>
 
35
#include <qlist.h>
 
36
#include <qfileinfo.h>
 
37
#include <private/qiodevice_p.h>
 
38
#include <private/qfile_p.h>
 
39
#include <private/qunicodetables_p.h>
 
40
#if defined(QT_BUILD_CORE_LIB)
 
41
# include "qcoreapplication.h"
 
42
#endif
 
43
 
 
44
#include <errno.h>
 
45
 
 
46
static const int read_cache_size = 4096;
 
47
 
 
48
static QByteArray locale_encode(const QString &f)
 
49
{
 
50
#ifndef Q_OS_DARWIN
 
51
    return f.toLocal8Bit();
 
52
#else
 
53
    // Mac always expects UTF-8
 
54
    return f.toUtf8();
 
55
#endif
 
56
}
 
57
 
 
58
static QString locale_decode(const QByteArray &f)
 
59
{
 
60
#ifndef Q_OS_DARWIN
 
61
    return QString::fromLocal8Bit(f);
 
62
#else
 
63
    // Mac always expects UTF-8
 
64
    return QUnicodeTables::normalize(QString::fromUtf8(f), QString::NormalizationForm_C);
 
65
#endif
 
66
}
 
67
 
 
68
//************* QFilePrivate
 
69
QFile::EncoderFn QFilePrivate::encoder = locale_encode;
 
70
QFile::DecoderFn QFilePrivate::decoder = locale_decode;
 
71
 
 
72
QFilePrivate::QFilePrivate() :
 
73
#ifndef QT_NO_FILE_BUFFER
 
74
    buffer(read_cache_size),
 
75
#endif
 
76
    fileEngine(0), error(QFile::NoError)
 
77
{
 
78
}
 
79
 
 
80
QFilePrivate::~QFilePrivate()
 
81
{
 
82
    delete fileEngine;
 
83
    fileEngine = 0;
 
84
}
 
85
 
 
86
bool
 
87
QFilePrivate::openExternalFile(int flags, int fd)
 
88
{
 
89
    delete fileEngine;
 
90
    QFSFileEngine *fe = new QFSFileEngine;
 
91
    fileEngine = fe;
 
92
    return fe->open(flags, fd);
 
93
}
 
94
 
 
95
bool
 
96
QFilePrivate::openExternalFile(int flags, FILE *fh)
 
97
{
 
98
    delete fileEngine;
 
99
    QBufferedFSFileEngine *fe = new QBufferedFSFileEngine;
 
100
    fileEngine = fe;
 
101
    return fe->open(flags, fh);
 
102
}
 
103
 
 
104
void
 
105
QFilePrivate::setError(QFile::FileError err)
 
106
{
 
107
    error = err;
 
108
    errorString.clear();
 
109
}
 
110
 
 
111
void
 
112
QFilePrivate::setError(QFile::FileError err, const QString &errStr)
 
113
{
 
114
    error = err;
 
115
    errorString = errStr;
 
116
}
 
117
 
 
118
void
 
119
QFilePrivate::setError(QFile::FileError err, int errNum)
 
120
{
 
121
    error = err;
 
122
    errorString = qt_error_string(errNum);
 
123
}
 
124
 
 
125
//************* QFile
 
126
 
 
127
/*!
 
128
    \class QFile
 
129
    \brief The QFile class provides an interface for reading from and writing to files.
 
130
 
 
131
    \ingroup io
 
132
    \mainclass
 
133
    \reentrant
 
134
 
 
135
    QFile is an I/O device for reading and writing text and binary
 
136
    files and \l{The Qt Resource System}{resources}. A QFile may be
 
137
    used by itself or, more conveniently, with a QTextStream or
 
138
    QDataStream.
 
139
 
 
140
    The file name is usually passed in the constructor, but it can be
 
141
    set at any time using setFileName(). You can check for a file's
 
142
    existence using exists(), and remove a file using remove(). (More
 
143
    advanced file system related operations are provided by QFileInfo
 
144
    and QDir.)
 
145
 
 
146
    The file is opened with open(), closed with close(), and flushed
 
147
    with flush(). Data is usually read and written using QDataStream
 
148
    or QTextStream, but you can also call the QIODevice-inherited
 
149
    functions read(), readLine(), readAll(), write(). QFile also
 
150
    inherits getChar(), putChar(), and ungetChar(), which work one
 
151
    character at a time.
 
152
 
 
153
    The size of the file is returned by size(). You can get the
 
154
    current file position using pos(), or move to a new file position
 
155
    using seek(). If you've reached the end of the file, atEnd()
 
156
    returns true.
 
157
 
 
158
    The following example reads a text file line by line:
 
159
 
 
160
    \quotefromfile snippets/file/file.cpp
 
161
    \skipto noStream_snippet
 
162
    \skipto QFile
 
163
    \printto /^\}/
 
164
 
 
165
    The QIODevice::Text flag passed to open() tells Qt to convert
 
166
    Windows-style line terminators ("\\r\\n") into C++-style
 
167
    terminators ("\\n"). By default, QFile assumes binary, i.e. it
 
168
    doesn't perform any conversion on the bytes stored in the file.
 
169
 
 
170
    The next example uses QTextStream to read a text file
 
171
    line by line:
 
172
 
 
173
    \skipto readTextStream_snippet
 
174
    \skipto QFile
 
175
    \printto /^\}/
 
176
 
 
177
    QTextStream takes care of converting the 8-bit data stored on
 
178
    disk into a 16-bit Unicode QString. By default, it assumes that
 
179
    the user system's local 8-bit encoding is used (e.g., ISO 8859-1
 
180
    for most of Europe; see QTextCodec::codecForLocale() for
 
181
    details). This can be changed using setCodec().
 
182
 
 
183
    To write text, we can use operator<<(), which is overloaded to
 
184
    take a QTextStream on the left and various data types (including
 
185
    QString) on the right:
 
186
 
 
187
    \skipto writeTextStream_snippet
 
188
    \skipto QFile
 
189
    \printto /^\}/
 
190
 
 
191
    QDataStream is similar, in that you can use operator<<() to write
 
192
    data and operator>>() to read it back. See the class
 
193
    documentation for details.
 
194
 
 
195
    When you use QFile, QFileInfo, and QDir to access the file system
 
196
    with Qt, you can use Unicode file names. On Unix, these file
 
197
    names are converted to an 8-bit encoding. If you want to use
 
198
    standard C++ APIs (\c <cstdio> or \c <iostream>) or
 
199
    platform-specific APIs to access files instead of QFile, you can
 
200
    use the encodeName() and decodeName() functions to convert
 
201
    between Unicode file names and 8-bit file names.
 
202
 
 
203
    \sa QTextStream, QDataStream, QFileInfo, QDir, {The Qt Resource System}
 
204
*/
 
205
 
 
206
/*!
 
207
    \enum QFile::FileError
 
208
 
 
209
    This enum describes the errors that may be returned by the error()
 
210
    function.
 
211
 
 
212
    \value NoError          No error occurred.
 
213
    \value ReadError        An error occurred when reading from the file.
 
214
    \value WriteError       An error occurred when writing to the file.
 
215
    \value FatalError       A fatal error occurred.
 
216
    \value ResourceError
 
217
    \value OpenError        The file could not be opened.
 
218
    \value AbortError       The operation was aborted.
 
219
    \value TimeOutError     A timeout occurred.
 
220
    \value UnspecifiedError An unspecified error occurred.
 
221
    \value RemoveError      The file could not be removed.
 
222
    \value RenameError      The file could not be renamed.
 
223
    \value PositionError    The position in the file could not be changed.
 
224
    \value ResizeError      The file could not be resized.
 
225
    \value PermissionsError The file could not be accessed.
 
226
    \value CopyError        The file could not be copied.
 
227
 
 
228
    \omitvalue ConnectError
 
229
*/
 
230
 
 
231
/*!
 
232
    \enum QFile::Permission
 
233
 
 
234
    This enum is used by the permission() function to report the
 
235
    permissions and ownership of a file. The values may be OR-ed
 
236
    together to test multiple permissions and ownership values.
 
237
 
 
238
    \value ReadOwner The file is readable by the owner of the file.
 
239
    \value WriteOwner The file is writable by the owner of the file.
 
240
    \value ExeOwner The file is executable by the owner of the file.
 
241
    \value ReadUser The file is readable by the user.
 
242
    \value WriteUser The file is writable by the user.
 
243
    \value ExeUser The file is executable by the user.
 
244
    \value ReadGroup The file is readable by the group.
 
245
    \value WriteGroup The file is writable by the group.
 
246
    \value ExeGroup The file is executable by the group.
 
247
    \value ReadOther The file is readable by anyone.
 
248
    \value WriteOther The file is writable by anyone.
 
249
    \value ExeOther The file is executable by anyone.
 
250
 
 
251
    \warning Because of differences in the platforms supported by Qt,
 
252
    the semantics of ReadUser, WriteUser and ExeUser are
 
253
    platform-dependent: On Unix, the rights of the owner of the file
 
254
    are returned and on Windows the rights of the current user are
 
255
    returned. This behavior might change in a future Qt version.
 
256
*/
 
257
 
 
258
#ifdef QT3_SUPPORT
 
259
/*!
 
260
    \typedef QFile::PermissionSpec
 
261
 
 
262
    Use QFile::Permission instead.
 
263
*/
 
264
#endif
 
265
 
 
266
#ifdef QT_NO_QOBJECT
 
267
QFile::QFile()
 
268
    : QIODevice(*new QFilePrivate)
 
269
{
 
270
    unsetError();
 
271
}
 
272
QFile::QFile(const QString &name)
 
273
    : QIODevice(*new QFilePrivate)
 
274
{
 
275
    d_func()->fileName = name;
 
276
    unsetError();
 
277
}
 
278
QFile::QFile(QFilePrivate &dd)
 
279
    : QIODevice(dd)
 
280
{
 
281
    unsetError();
 
282
}
 
283
#else
 
284
/*!
 
285
    \internal
 
286
*/
 
287
QFile::QFile()
 
288
    : QIODevice(*new QFilePrivate, 0)
 
289
{
 
290
    unsetError();
 
291
}
 
292
/*!
 
293
    Constructs a new file object with the given \a parent.
 
294
*/
 
295
QFile::QFile(QObject *parent)
 
296
    : QIODevice(*new QFilePrivate, parent)
 
297
{
 
298
    unsetError();
 
299
}
 
300
/*!
 
301
    Constructs a new file object to represent the file with the given \a name.
 
302
*/
 
303
QFile::QFile(const QString &name)
 
304
    : QIODevice(*new QFilePrivate, 0)
 
305
{
 
306
    Q_D(QFile);
 
307
    d->fileName = name;
 
308
    unsetError();
 
309
}
 
310
/*!
 
311
    Constructs a new file object with the given \a parent to represent the
 
312
    file with the specified \a name.
 
313
*/
 
314
QFile::QFile(const QString &name, QObject *parent)
 
315
    : QIODevice(*new QFilePrivate, parent)
 
316
{
 
317
    Q_D(QFile);
 
318
    d->fileName = name;
 
319
    unsetError();
 
320
}
 
321
/*!
 
322
    \internal
 
323
*/
 
324
QFile::QFile(QFilePrivate &dd, QObject *parent)
 
325
    : QIODevice(dd, parent)
 
326
{
 
327
    unsetError();
 
328
}
 
329
#endif
 
330
 
 
331
/*!
 
332
    Destroys the file object, closing it if necessary.
 
333
*/
 
334
QFile::~QFile()
 
335
{
 
336
    close();
 
337
#ifdef QT_NO_QOBJECT
 
338
    delete d_ptr;
 
339
#endif
 
340
}
 
341
 
 
342
/*!
 
343
    Returns the name set by setFileName().
 
344
 
 
345
    \sa setFileName(), QFileInfo::fileName()
 
346
*/
 
347
QString
 
348
QFile::fileName() const
 
349
{
 
350
    return fileEngine()->fileName(QFileEngine::DefaultName);
 
351
}
 
352
 
 
353
/*!
 
354
    Sets the \a name of the file. The name can have no path, a
 
355
    relative path, or an absolute absolute path.
 
356
 
 
357
    Do not call this function if the file has already been opened.
 
358
 
 
359
    If the file name has no path or a relative path, the path used
 
360
    will be the application's current directory path
 
361
    \e{at the time of the open()} call.
 
362
 
 
363
    Example:
 
364
    \code
 
365
        QFile file;
 
366
        QDir::setCurrent("/tmp");
 
367
        file.setFileName("readme.txt");
 
368
        QDir::setCurrent("/home");
 
369
        file.open(QIODevice::ReadOnly);      // opens "/home/readme.txt" under Unix
 
370
    \endcode
 
371
 
 
372
    Note that the directory separator "/" works for all operating
 
373
    systems supported by Qt.
 
374
 
 
375
    \sa fileName(), QFileInfo, QDir
 
376
*/
 
377
void
 
378
QFile::setFileName(const QString &name)
 
379
{
 
380
    Q_D(QFile);
 
381
    if (isOpen()) {
 
382
        qWarning("QFile::setFileName: file is already opened");
 
383
        close();
 
384
    }
 
385
    if(d->fileEngine) { //get a new file engine later
 
386
        delete d->fileEngine;
 
387
        d->fileEngine = 0;
 
388
    }
 
389
    d->fileName = name;
 
390
}
 
391
 
 
392
/*!
 
393
    \fn QString QFile::decodeName(const char *localFileName)
 
394
 
 
395
    \overload
 
396
 
 
397
    Returns the Unicode version of the given \a localFileName. See
 
398
    encodeName() for details.
 
399
*/
 
400
 
 
401
/*!
 
402
    By default, this function converts \a fileName to the local 8-bit
 
403
    encoding determined by the user's locale. This is sufficient for
 
404
    file names that the user chooses. File names hard-coded into the
 
405
    application should only use 7-bit ASCII filename characters.
 
406
 
 
407
    \sa decodeName() setEncodingFunction()
 
408
*/
 
409
 
 
410
QByteArray
 
411
QFile::encodeName(const QString &fileName)
 
412
{
 
413
    return (*QFilePrivate::encoder)(fileName);
 
414
}
 
415
 
 
416
/*!
 
417
    \typedef QFile::EncoderFn
 
418
 
 
419
    This is a typedef for a pointer to a function with the following
 
420
    signature:
 
421
 
 
422
    \code
 
423
        QByteArray myEncoderFunc(const QString &fileName);
 
424
    \endcode
 
425
 
 
426
    \sa setEncodingFunction(), encodeName()
 
427
*/
 
428
 
 
429
/*!
 
430
    This does the reverse of QFile::encodeName() using \a localFileName.
 
431
 
 
432
    \sa setDecodingFunction(), encodeName()
 
433
*/
 
434
 
 
435
QString
 
436
QFile::decodeName(const QByteArray &localFileName)
 
437
{
 
438
    return (*QFilePrivate::decoder)(localFileName);
 
439
}
 
440
 
 
441
/*!
 
442
    \fn void QFile::setEncodingFunction(EncoderFn function)
 
443
 
 
444
    \nonreentrant
 
445
 
 
446
    Sets the \a function for encoding Unicode file names. The
 
447
    default encodes in the locale-specific 8-bit encoding.
 
448
 
 
449
    \sa encodeName(), setDecodingFunction()
 
450
*/
 
451
 
 
452
void
 
453
QFile::setEncodingFunction(EncoderFn f)
 
454
{
 
455
    QFilePrivate::encoder = f;
 
456
}
 
457
 
 
458
/*!
 
459
    \typedef QFile::DecoderFn
 
460
 
 
461
    This is a typedef for a pointer to a function with the following
 
462
    signature:
 
463
 
 
464
    \code
 
465
        QString myDecoderFunc(const QByteArray &localFileName);
 
466
    \endcode
 
467
 
 
468
    \sa setDecodingFunction()
 
469
*/
 
470
 
 
471
/*!
 
472
    \fn void QFile::setDecodingFunction(DecoderFn function)
 
473
 
 
474
    \nonreentrant
 
475
 
 
476
    Sets the \a function for decoding 8-bit file names. The
 
477
    default uses the locale-specific 8-bit encoding.
 
478
 
 
479
    \sa setEncodingFunction(), decodeName()
 
480
*/
 
481
 
 
482
void
 
483
QFile::setDecodingFunction(DecoderFn f)
 
484
{
 
485
    QFilePrivate::decoder = f;
 
486
}
 
487
 
 
488
/*!
 
489
    \overload
 
490
 
 
491
    Returns true if the file specified by fileName() exists; otherwise
 
492
    returns false.
 
493
 
 
494
    \sa fileName(), setFileName()
 
495
*/
 
496
 
 
497
bool
 
498
QFile::exists() const
 
499
{
 
500
    return (fileEngine()->fileFlags(QFileEngine::FlagsMask) & QFileEngine::ExistsFlag);
 
501
}
 
502
 
 
503
/*!
 
504
    Returns true if the file specified by \a fileName exists; otherwise
 
505
    returns false.
 
506
*/
 
507
 
 
508
bool
 
509
QFile::exists(const QString &fileName)
 
510
{
 
511
    return QFileInfo(fileName).exists();
 
512
}
 
513
 
 
514
/*!
 
515
    \overload
 
516
 
 
517
    Returns the name a symlink (or shortcut on Windows) points to, or
 
518
    a an empty string if the object isn't a symbolic link.
 
519
 
 
520
    This name may not represent an existing file; it is only a string.
 
521
    QFie::exists() returns true if the symlink points to an
 
522
    existing file.
 
523
 
 
524
    \sa fileName() setFileName()
 
525
*/
 
526
 
 
527
QString
 
528
QFile::readLink() const
 
529
{
 
530
    return fileEngine()->fileName(QFileEngine::LinkName);
 
531
}
 
532
 
 
533
/*!
 
534
    Returns the filename referred to by the symlink (or shortcut on Windows)
 
535
    specified by \a fileName, or returns an empty string if the \a fileName
 
536
    does not correspond to a symbolic link.
 
537
 
 
538
    This name may not represent an existing file; it is only a string.
 
539
    QFile::exists() returns true if the symlink points to an
 
540
    existing file.
 
541
*/
 
542
 
 
543
QString
 
544
QFile::readLink(const QString &fileName)
 
545
{
 
546
    return QFileInfo(fileName).readLink();
 
547
}
 
548
 
 
549
/*!
 
550
    Removes the file specified by fileName(). Returns true if successful;
 
551
    otherwise returns false.
 
552
 
 
553
    The file is closed before it is removed.
 
554
 
 
555
    \sa setFileName()
 
556
*/
 
557
 
 
558
bool
 
559
QFile::remove()
 
560
{
 
561
    Q_D(QFile);
 
562
    if (d->fileName.isEmpty()) {
 
563
        qWarning("QFile::remove: Empty or null file name");
 
564
        return false;
 
565
    }
 
566
    close();
 
567
    if(error() == QFile::NoError) {
 
568
        if(fileEngine()->remove()) {
 
569
            unsetError();
 
570
            return true;
 
571
        }
 
572
        d->setError(QFile::RemoveError, errno);
 
573
    }
 
574
    return false;
 
575
}
 
576
 
 
577
/*!
 
578
    \overload
 
579
 
 
580
    Removes the file specified by the \a fileName given.
 
581
 
 
582
    Returns true if successful; otherwise returns false.
 
583
 
 
584
    \sa remove()
 
585
*/
 
586
 
 
587
bool
 
588
QFile::remove(const QString &fileName)
 
589
{
 
590
    return QFile(fileName).remove();
 
591
}
 
592
 
 
593
/*!
 
594
    Renames the file currently specified by fileName() to \a newName.
 
595
    Returns true if successful; otherwise returns false.
 
596
 
 
597
    The file is closed before it is renamed.
 
598
 
 
599
    \sa setFileName()
 
600
*/
 
601
 
 
602
bool
 
603
QFile::rename(const QString &newName)
 
604
{
 
605
    Q_D(QFile);
 
606
    if (d->fileName.isEmpty()) {
 
607
        qWarning("QFile::rename: Empty or null file name");
 
608
        return false;
 
609
    }
 
610
    close();
 
611
    if(error() == QFile::NoError) {
 
612
        if(fileEngine()->rename(newName)) {
 
613
            unsetError();
 
614
            return true;
 
615
        } else {
 
616
            QFile in(fileName());
 
617
            QFile out(newName);
 
618
            if (in.open(QIODevice::ReadOnly)) {
 
619
                if(out.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
 
620
                    bool error = false;
 
621
                    char block[1024];
 
622
                    while(!in.atEnd()) {
 
623
                        long read = in.read(block, 1024);
 
624
                        if(read == -1)
 
625
                            break;
 
626
                        if(read != out.write(block, read)) {
 
627
                            d->setError(QFile::CopyError, QLatin1String("Failure to write block"));
 
628
                            error = true;
 
629
                            break;
 
630
                        }
 
631
                    }
 
632
                    if(!error)
 
633
                        in.remove();
 
634
                    return !error;
 
635
                 }
 
636
            }
 
637
        }
 
638
        d->setError(QFile::RenameError, errno);
 
639
    }
 
640
    return false;
 
641
}
 
642
 
 
643
/*!
 
644
    \overload
 
645
 
 
646
    Renames the file \a oldName to \a newName. Returns true if
 
647
    successful; otherwise returns false.
 
648
 
 
649
    \sa rename()
 
650
*/
 
651
 
 
652
bool
 
653
QFile::rename(const QString &oldName, const QString &newName)
 
654
{
 
655
    return QFile(oldName).rename(newName);
 
656
}
 
657
 
 
658
/*!
 
659
    Creates a link from the file currently specified by fileName() to
 
660
    \a newName. What a link is depends on the underlying filesystem
 
661
    (be it a shortcut on Windows or a symbolic link on Unix). Returns
 
662
    true if successful; otherwise returns false.
 
663
 
 
664
    \sa setFileName()
 
665
*/
 
666
 
 
667
bool
 
668
QFile::link(const QString &newName)
 
669
{
 
670
    Q_D(QFile);
 
671
    if (d->fileName.isEmpty()) {
 
672
        qWarning("QFile::link: Empty or null file name");
 
673
        return false;
 
674
    }
 
675
    QFileInfo fi(newName);
 
676
    if(fileEngine()->link(fi.absoluteFilePath())) {
 
677
        unsetError();
 
678
        return true;
 
679
    }
 
680
    d->setError(QFile::RenameError, errno);
 
681
    return false;
 
682
}
 
683
 
 
684
/*!
 
685
    \overload
 
686
 
 
687
    Creates a link from \a oldName to \a newName. What a link is
 
688
    depends on the underlying filesystem (be it a shortcut on Windows
 
689
    or a symbolic link on Unix). Returns true if successful; otherwise
 
690
    returns false.
 
691
 
 
692
    \sa link()
 
693
*/
 
694
 
 
695
bool
 
696
QFile::link(const QString &oldName, const QString &newName)
 
697
{
 
698
    return QFile(oldName).link(newName);
 
699
}
 
700
 
 
701
/*!
 
702
    Copies the file currently specified by fileName() to \a newName.
 
703
    Returns true if successful; otherwise returns false.
 
704
 
 
705
    The file is closed before it is copied.
 
706
 
 
707
    \sa setFileName()
 
708
*/
 
709
 
 
710
bool
 
711
QFile::copy(const QString &newName)
 
712
{
 
713
    Q_D(QFile);
 
714
    if (d->fileName.isEmpty()) {
 
715
        qWarning("QFile::copy: Empty or null file name");
 
716
        return false;
 
717
    }
 
718
    close();
 
719
    if(error() == QFile::NoError) {
 
720
        if(fileEngine()->copy(newName)) {
 
721
            unsetError();
 
722
            return true;
 
723
        } else {
 
724
            bool error = false;
 
725
            if(!open(QFile::ReadOnly)) {
 
726
                error = true;
 
727
                QString errorMessage = QLatin1String("Cannot open %1 for input");
 
728
                d->setError(QFile::CopyError, errorMessage.arg(d->fileName));
 
729
            } else {
 
730
                QTemporaryFile out;
 
731
                if(!out.open()) {
 
732
                    close();
 
733
                    error = true;
 
734
                    d->setError(QFile::CopyError, QLatin1String("Cannot open for output"));
 
735
                } else {
 
736
                    char block[1024];
 
737
                    while(!atEnd()) {
 
738
                        qint64 in = read(block, 1024);
 
739
                        if(in == -1)
 
740
                            break;
 
741
                        if(in != out.write(block, in)) {
 
742
                            d->setError(QFile::CopyError, QLatin1String("Failure to write block"));
 
743
                            error = true;
 
744
                            break;
 
745
                        }
 
746
                    }
 
747
                    if(!error && !out.rename(newName)) {
 
748
                        error = true;
 
749
                        QString errorMessage = QLatin1String("Cannot create %1 for output");
 
750
                        d->setError(QFile::CopyError, errorMessage.arg(newName));
 
751
                    }
 
752
                }
 
753
            }
 
754
            if(!error) {
 
755
                QFile::setPermissions(newName, permissions());
 
756
                unsetError();
 
757
                return true;
 
758
            }
 
759
        }
 
760
    }
 
761
    return false;
 
762
}
 
763
 
 
764
/*!
 
765
    \overload
 
766
 
 
767
    Copies the file \a fileName to \a newName. Returns true if successful;
 
768
    otherwise returns false.
 
769
 
 
770
    \sa rename()
 
771
*/
 
772
 
 
773
bool
 
774
QFile::copy(const QString &fileName, const QString &newName)
 
775
{
 
776
    return QFile(fileName).copy(newName);
 
777
}
 
778
 
 
779
/*!
 
780
    Returns true if the file can only be manipulated sequentially;
 
781
    otherwise returns false.
 
782
 
 
783
    Most files support random-access, but some special files may not.
 
784
 
 
785
    \sa QIODevice::isSequential()
 
786
*/
 
787
bool QFile::isSequential() const
 
788
{
 
789
    Q_D(const QFile);
 
790
    return d->fileEngine && d->fileEngine->isSequential();
 
791
}
 
792
 
 
793
/*!
 
794
    Opens the file using OpenMode \a mode.
 
795
 
 
796
    The \a mode must be QIODevice::ReadOnly, QIODevice::WriteOnly, or
 
797
    QIODevice::ReadWrite. It may also have additional flags, such as
 
798
    QIODevice::Text and QIODevice::Unbuffered.
 
799
 
 
800
    \sa QIODevice::OpenMode
 
801
*/
 
802
bool QFile::open(OpenMode mode)
 
803
{
 
804
    Q_D(QFile);
 
805
    if (isOpen()) {
 
806
        qWarning("QFile::open: File already open");
 
807
        return false;
 
808
    }
 
809
    if (mode & Append)
 
810
        mode |= WriteOnly;
 
811
    unsetError();
 
812
    if ((mode & (ReadOnly | WriteOnly)) == 0) {
 
813
        qWarning("QIODevice::open: File access not specified");
 
814
        return false;
 
815
    }
 
816
    if (fileEngine()->open(mode)) {
 
817
        setOpenMode(mode);
 
818
        return true;
 
819
    }
 
820
    QFile::FileError err = fileEngine()->error();
 
821
    if(err == QFile::UnspecifiedError)
 
822
        err = QFile::OpenError;
 
823
    d->setError(err, fileEngine()->errorString());
 
824
    return false;
 
825
}
 
826
 
 
827
/*! \fn QFile::open(OpenMode, FILE*)
 
828
 
 
829
    Use open(FILE *, OpenMode) instead.
 
830
*/
 
831
 
 
832
/*!
 
833
    \overload
 
834
 
 
835
    Opens the existing file handle \a fh in the given \a mode.
 
836
    Returns true if successful; otherwise returns false.
 
837
 
 
838
    Example:
 
839
    \code
 
840
        #include <stdio.h>
 
841
 
 
842
        void printError(const char* msg)
 
843
        {
 
844
            QFile file;
 
845
            file.open(stderr, QIODevice::WriteOnly);
 
846
            file.write(msg, qstrlen(msg));        // write to stderr
 
847
            file.close();
 
848
        }
 
849
    \endcode
 
850
 
 
851
    When a QFile is opened using this function, close() does not actually
 
852
    close the file, but only flushes it.
 
853
 
 
854
    \warning If \a fh is \c stdin, \c stdout, or \c stderr, you may not be
 
855
    able to seek(). See QIODevice::isSequentialAccess() for more
 
856
    information.
 
857
 
 
858
    \sa close()
 
859
*/
 
860
bool QFile::open(FILE *fh, OpenMode mode)
 
861
{
 
862
    Q_D(QFile);
 
863
    if (isOpen()) {
 
864
        qWarning("QFile::open: File already open");
 
865
        return false;
 
866
    }
 
867
    if (mode & Append)
 
868
        mode |= WriteOnly;
 
869
    unsetError();
 
870
    if ((mode & (ReadOnly | WriteOnly)) == 0) {
 
871
        qWarning("QFile::open: File access not specified");
 
872
        return false;
 
873
    }
 
874
 
 
875
    // Implicitly set Unbuffered mode; buffering is already handled.
 
876
    mode |= Unbuffered;
 
877
 
 
878
    if(d->openExternalFile(mode, fh)) {
 
879
        setOpenMode(mode);
 
880
        return true;
 
881
    }
 
882
    return false;
 
883
}
 
884
 
 
885
/*! \fn QFile::open(OpenMode, int)
 
886
 
 
887
    Use open(int, OpenMode) instead.
 
888
*/
 
889
 
 
890
/*!
 
891
    \overload
 
892
 
 
893
    Opens the existing file descripter \a fd in the given \a mode.
 
894
    Returns true if successful; otherwise returns false.
 
895
 
 
896
    When a QFile is opened using this function, close() does not
 
897
    actually close the file.
 
898
 
 
899
    The QFile that is opened using this function is automatically set
 
900
    to be in raw mode; this means that the file input/output functions
 
901
    are slow. If you run into performance issues, you should try to
 
902
    use one of the other open functions.
 
903
 
 
904
    \warning If \a fd is 0 (\c stdin), 1 (\c stdout), or 2 (\c
 
905
    stderr), you may not be able to seek(). size() is set to \c
 
906
    LLONG_MAX (in \c <climits>).
 
907
 
 
908
    \sa close()
 
909
*/
 
910
bool QFile::open(int fd, OpenMode mode)
 
911
{
 
912
    Q_D(QFile);
 
913
    if (isOpen()) {
 
914
        qWarning("QFile::open: File already open");
 
915
        return false;
 
916
    }
 
917
    if (mode & Append)
 
918
        mode |= WriteOnly;
 
919
    unsetError();
 
920
    if ((mode & (ReadOnly | WriteOnly)) == 0) {
 
921
        qWarning("QFile::open: File access not specified");
 
922
        return false;
 
923
    }
 
924
    if(d->openExternalFile(mode, fd)) {
 
925
        setOpenMode(mode);
 
926
        return true;
 
927
    }
 
928
    return false;
 
929
}
 
930
 
 
931
/*!
 
932
  Returns the file handle of the file.
 
933
 
 
934
  This is a small positive integer, suitable for use with C library
 
935
  functions such as fdopen() and fcntl(). On systems that use file
 
936
  descriptors for sockets (i.e. Unix systems, but not Windows) the handle
 
937
  can be used with QSocketNotifier as well.
 
938
 
 
939
  If the file is not open, or there is an error, handle() returns -1.
 
940
 
 
941
  \sa QSocketNotifier
 
942
*/
 
943
 
 
944
int
 
945
QFile::handle() const
 
946
{
 
947
    if (!isOpen())
 
948
        return -1;
 
949
    QFileEngine *engine = fileEngine();
 
950
    if(engine->type() == QFileEngine::File)
 
951
        return static_cast<QFSFileEngine*>(engine)->handle();
 
952
    return -1;
 
953
}
 
954
 
 
955
/*!
 
956
    \fn QString QFile::name() const
 
957
 
 
958
    Use fileName() instead.
 
959
*/
 
960
 
 
961
/*!
 
962
    \fn void QFile::setName(const QString &name)
 
963
 
 
964
    Use setFileName() instead.
 
965
*/
 
966
 
 
967
/*!
 
968
    Sets the file size (in bytes) \a sz. Returns true if the file if the
 
969
    resize succeeds; false otherwise. If \a sz is larger than the file
 
970
    currently is the new bytes will be set to 0, if \a sz is smaller the
 
971
    file is simply truncated.
 
972
 
 
973
    \sa size(), setFileName()
 
974
*/
 
975
 
 
976
bool
 
977
QFile::resize(qint64 sz)
 
978
{
 
979
    Q_D(QFile);
 
980
    if(fileEngine()->setSize(sz)) {
 
981
        unsetError();
 
982
        return true;
 
983
    }
 
984
    d->setError(QFile::ResizeError, errno);
 
985
    return false;
 
986
}
 
987
 
 
988
/*!
 
989
    \overload
 
990
 
 
991
    Sets \a fileName to size (in bytes) \a sz. Returns true if the file if
 
992
    the resize succeeds; false otherwise. If \a sz is larger than \a
 
993
    fileName currently is the new bytes will be set to 0, if \a sz is
 
994
    smaller the file is simply truncated.
 
995
 
 
996
    \sa resize()
 
997
*/
 
998
 
 
999
bool
 
1000
QFile::resize(const QString &fileName, qint64 sz)
 
1001
{
 
1002
    return QFile(fileName).resize(sz);
 
1003
}
 
1004
 
 
1005
/*!
 
1006
    Returns the complete OR-ed together combination of
 
1007
    QFile::Permission for the file.
 
1008
 
 
1009
    \sa setPermissions(), setFileName()
 
1010
*/
 
1011
 
 
1012
QFile::Permissions
 
1013
QFile::permissions() const
 
1014
{
 
1015
    QFileEngine::FileFlags perms = fileEngine()->fileFlags(QFileEngine::PermsMask) & QFileEngine::PermsMask;
 
1016
    return QFile::Permissions((int)perms); //ewww
 
1017
}
 
1018
 
 
1019
/*!
 
1020
    \overload
 
1021
 
 
1022
    Returns the complete OR-ed together combination of
 
1023
    QFile::Permission for \a fileName.
 
1024
*/
 
1025
 
 
1026
QFile::Permissions
 
1027
QFile::permissions(const QString &fileName)
 
1028
{
 
1029
    return QFile(fileName).permissions();
 
1030
}
 
1031
 
 
1032
/*!
 
1033
    Sets the permissions for the file to \a permissions.
 
1034
 
 
1035
    \sa permissions(), setFileName()
 
1036
*/
 
1037
 
 
1038
bool
 
1039
QFile::setPermissions(Permissions permissions)
 
1040
{
 
1041
    Q_D(QFile);
 
1042
    if(fileEngine()->chmod(permissions)) {
 
1043
        unsetError();
 
1044
        return true;
 
1045
    }
 
1046
    d->setError(QFile::PermissionsError, errno);
 
1047
    return false;
 
1048
}
 
1049
 
 
1050
/*!
 
1051
    \overload
 
1052
 
 
1053
    Sets the permissions for \a fileName file to \a permissions.
 
1054
*/
 
1055
 
 
1056
bool
 
1057
QFile::setPermissions(const QString &fileName, Permissions permissions)
 
1058
{
 
1059
    return QFile(fileName).setPermissions(permissions);
 
1060
}
 
1061
 
 
1062
/*!
 
1063
    Flushes any buffered data to the file.
 
1064
*/
 
1065
 
 
1066
bool
 
1067
QFile::flush()
 
1068
{
 
1069
    fileEngine()->flush();
 
1070
    return true;
 
1071
}
 
1072
 
 
1073
/*!
 
1074
  \reimp
 
1075
*/
 
1076
 
 
1077
void
 
1078
QFile::close()
 
1079
{
 
1080
    Q_D(QFile);
 
1081
    if(!isOpen())
 
1082
        return;
 
1083
    QIODevice::close();
 
1084
 
 
1085
    unsetError();
 
1086
#ifndef QT_NO_FILE_BUFFER
 
1087
    d->buffer.clear();
 
1088
#endif
 
1089
    if(!fileEngine()->close())
 
1090
        d->setError(fileEngine()->error(), fileEngine()->errorString());
 
1091
}
 
1092
 
 
1093
/*!
 
1094
  \reimp
 
1095
*/
 
1096
 
 
1097
qint64 QFile::size() const
 
1098
{
 
1099
    return fileEngine()->size();
 
1100
}
 
1101
 
 
1102
/*!
 
1103
  \reimp
 
1104
*/
 
1105
 
 
1106
qint64 QFile::pos() const
 
1107
{
 
1108
    Q_D(const QFile);
 
1109
    if (!isOpen())
 
1110
        return 0;
 
1111
#ifndef QT_NO_FILE_BUFFER
 
1112
    return fileEngine()->at() - d->buffer.used();
 
1113
#else
 
1114
    return fileEngine()->at();
 
1115
#endif
 
1116
}
 
1117
 
 
1118
/*!
 
1119
  \reimp
 
1120
*/
 
1121
 
 
1122
bool QFile::atEnd() const
 
1123
{
 
1124
    Q_D(const QFile);
 
1125
    if (!isOpen())
 
1126
        return true;
 
1127
    if(!d->buffer.isEmpty())
 
1128
        return false;
 
1129
    return QIODevice::atEnd();
 
1130
}
 
1131
 
 
1132
/*!
 
1133
  \reimp
 
1134
*/
 
1135
 
 
1136
bool QFile::seek(qint64 off)
 
1137
{
 
1138
    Q_D(QFile);
 
1139
    if (!isOpen()) {
 
1140
        qWarning("QFile::seek: IODevice is not open");
 
1141
        return false;
 
1142
    }
 
1143
 
 
1144
    QIODevice::seek(off);
 
1145
    if(!fileEngine()->seek(off)) {
 
1146
        QFile::FileError err = fileEngine()->error();
 
1147
        if(err == QFile::UnspecifiedError)
 
1148
            err = QFile::PositionError;
 
1149
        d->setError(err, fileEngine()->errorString());
 
1150
        return false;
 
1151
    }
 
1152
#ifndef QT_NO_FILE_BUFFER
 
1153
    d->buffer.clear();
 
1154
#endif
 
1155
    unsetError();
 
1156
    return true;
 
1157
}
 
1158
 
 
1159
/*!
 
1160
  \reimp
 
1161
*/
 
1162
qint64 QFile::readLineData(char *data, qint64 maxlen)
 
1163
{
 
1164
    Q_D(QFile);
 
1165
#ifndef QT_NO_FILE_BUFFER
 
1166
    if (openMode() & Unbuffered)
 
1167
#endif
 
1168
        return QIODevice::readLineData(data, maxlen);
 
1169
 
 
1170
#ifndef QT_NO_FILE_BUFFER
 
1171
    qint64 readSoFar = 0;
 
1172
    bool foundEndOfLine = false;
 
1173
 
 
1174
    forever {
 
1175
        // get a pointer to the buffer
 
1176
        uint realSize = 0;
 
1177
        char *ptr = d->buffer.take(d->buffer.used(), &realSize);
 
1178
 
 
1179
        // search for a '\n' character, copy over data as we search
 
1180
        if (realSize > 0) {
 
1181
            uint i = 0;
 
1182
            while (i < realSize) {
 
1183
                ++i;
 
1184
                if (ptr[i - 1] == '\n') {
 
1185
                    foundEndOfLine = true;
 
1186
                    break;
 
1187
                }
 
1188
            }
 
1189
 
 
1190
            // strip '\r' if in Text mode
 
1191
            if (openMode() & Text) {
 
1192
                char *readPtr = ptr;
 
1193
                char *endPtr = ptr + i;
 
1194
 
 
1195
                while (*readPtr != '\r' && readPtr != endPtr)
 
1196
                    ++readPtr;
 
1197
                char *writePtr = readPtr;
 
1198
 
 
1199
                while (readPtr != endPtr && i > 0) {
 
1200
                    char ch = *readPtr;
 
1201
                    if (ch != '\r')
 
1202
                        *writePtr++ = ch;
 
1203
                    else
 
1204
                        --i;
 
1205
                }
 
1206
            }
 
1207
 
 
1208
            memcpy(data + readSoFar, ptr, i);
 
1209
            d->buffer.free(i);
 
1210
            readSoFar += i;
 
1211
        }
 
1212
 
 
1213
        // return if it was found
 
1214
        if (foundEndOfLine) {
 
1215
            if (readSoFar < maxlen)
 
1216
                data[readSoFar] = '\0';
 
1217
            return readSoFar;
 
1218
        }
 
1219
 
 
1220
        // read more data
 
1221
        int bytesToRead = qMin(read_cache_size, int(maxlen - readSoFar));
 
1222
        if (bytesToRead == 0) {
 
1223
            if (readSoFar < maxlen)
 
1224
                data[readSoFar] = '\0';
 
1225
            return readSoFar;
 
1226
        }
 
1227
 
 
1228
        char *buffer = d->buffer.alloc(bytesToRead);
 
1229
        qint64 bytesRead = fileEngine()->read(buffer, bytesToRead);
 
1230
 
 
1231
        if (bytesRead != bytesToRead) {
 
1232
            if (bytesRead < 0)
 
1233
                d->buffer.truncate(bytesToRead);
 
1234
            else
 
1235
                d->buffer.truncate(bytesToRead - bytesRead);
 
1236
        }
 
1237
 
 
1238
        if (bytesRead <= 0) {
 
1239
            if (readSoFar < maxlen)
 
1240
                data[readSoFar] = '\0';
 
1241
            return readSoFar > 0 ? readSoFar : qint64(-1);
 
1242
        }
 
1243
 
 
1244
    }
 
1245
#endif
 
1246
    return 0;
 
1247
}
 
1248
 
 
1249
/*!
 
1250
  \reimp
 
1251
*/
 
1252
 
 
1253
qint64 QFile::readData(char *data, qint64 len)
 
1254
{
 
1255
    Q_D(QFile);
 
1256
    unsetError();
 
1257
 
 
1258
   qint64 ret = 0;
 
1259
#ifndef QT_NO_FILE_BUFFER
 
1260
    if ((openMode() & Unbuffered) == 0) {
 
1261
        //from buffer
 
1262
        while(ret != len && !d->buffer.isEmpty()) {
 
1263
            uint buffered = qMin(len, (qint64)d->buffer.used());
 
1264
            char *buffer = d->buffer.take(buffered, &buffered);
 
1265
            memcpy(data+ret, buffer, buffered);
 
1266
            d->buffer.free(buffered);
 
1267
            ret += buffered;
 
1268
        }
 
1269
        //from the device
 
1270
        if(ret < len) {
 
1271
            if(len > read_cache_size) {
 
1272
                qint64 read = fileEngine()->read(data+ret, len-ret);
 
1273
                if(read != -1)
 
1274
                    ret += read;
 
1275
            } else {
 
1276
                char *buffer = d->buffer.alloc(read_cache_size);
 
1277
                qint64 got = fileEngine()->read(buffer, read_cache_size);
 
1278
                if(got != -1) {
 
1279
                    if(got < read_cache_size)
 
1280
                        d->buffer.truncate(read_cache_size - got);
 
1281
                    const qint64 need = qMin(len-ret, got);
 
1282
                    memcpy(data+ret, buffer, need);
 
1283
                    d->buffer.free(need);
 
1284
                    ret += need;
 
1285
                } else {
 
1286
                    if(!ret)
 
1287
                        ret = -1;
 
1288
                    d->buffer.truncate(read_cache_size);
 
1289
                }
 
1290
            }
 
1291
        }
 
1292
    } else {
 
1293
#endif
 
1294
        qint64 read = fileEngine()->read(data+ret, len-ret);
 
1295
        if(read != -1)
 
1296
            ret += read;
 
1297
#ifndef QT_NO_FILE_BUFFER
 
1298
    }
 
1299
#endif
 
1300
 
 
1301
    if(ret < 0) {
 
1302
        QFile::FileError err = fileEngine()->error();
 
1303
        if(err == QFile::UnspecifiedError)
 
1304
            err = QFile::ReadError;
 
1305
        d->setError(err, fileEngine()->errorString());
 
1306
    }
 
1307
    return ret;
 
1308
}
 
1309
 
 
1310
/*!
 
1311
  \reimp
 
1312
*/
 
1313
 
 
1314
qint64
 
1315
QFile::writeData(const char *data, qint64 len)
 
1316
{
 
1317
    Q_D(QFile);
 
1318
    unsetError();
 
1319
 
 
1320
#ifndef QT_NO_FILE_BUFFER
 
1321
    if(!d->buffer.isEmpty())
 
1322
        seek(pos());
 
1323
#endif
 
1324
    qint64 ret = fileEngine()->write(data, len);
 
1325
    if(ret < 0) {
 
1326
        QFile::FileError err = fileEngine()->error();
 
1327
        if(err == QFile::UnspecifiedError)
 
1328
            err = QFile::WriteError;
 
1329
        d->setError(err, fileEngine()->errorString());
 
1330
    }
 
1331
    return ret;
 
1332
}
 
1333
 
 
1334
/*!
 
1335
  \internal
 
1336
  Returns the QIOEngine for this QFile object.
 
1337
*/
 
1338
 
 
1339
QFileEngine
 
1340
*QFile::fileEngine() const
 
1341
{
 
1342
    Q_D(const QFile);
 
1343
    if(!d->fileEngine)
 
1344
        d->fileEngine = QFileEngine::createFileEngine(d->fileName);
 
1345
    return d->fileEngine;
 
1346
}
 
1347
 
 
1348
/*!
 
1349
    Returns the file error status.
 
1350
 
 
1351
    The I/O device status returns an error code. For example, if open()
 
1352
    returns false, or a read/write operation returns -1, this function can
 
1353
    be called to find out the reason why the operation failed.
 
1354
 
 
1355
    \sa unsetError()
 
1356
*/
 
1357
 
 
1358
QFile::FileError
 
1359
QFile::error() const
 
1360
{
 
1361
    Q_D(const QFile);
 
1362
    return d->error;
 
1363
}
 
1364
 
 
1365
/*!
 
1366
    Sets the file's error to QFile::NoError.
 
1367
 
 
1368
    \sa error()
 
1369
*/
 
1370
void
 
1371
QFile::unsetError()
 
1372
{
 
1373
    Q_D(QFile);
 
1374
    d->setError(QFile::NoError);
 
1375
}
 
1376
 
 
1377
/*
 
1378
    Returns a human-readable description of an error that occurred on
 
1379
    the device. The error described by the string corresponds to
 
1380
    changes of QFile::error(). If the status is reset, the error
 
1381
    string is also reset.
 
1382
 
 
1383
    \code
 
1384
        QFile file("address.dat");
 
1385
        if (!file.open(QIODevice::ReadOnly) {
 
1386
            QMessageBox::critical(this, tr("Error"),
 
1387
                    tr("Could not open file for reading: %1")
 
1388
                    .arg(file.errorString()));
 
1389
            return;
 
1390
        }
 
1391
    \endcode
 
1392
 
 
1393
    \sa unsetError()
 
1394
*/
 
1395
 
 
1396
/* ### NEEDS TO BE FIXED
 
1397
QString
 
1398
QFile::errorString() const
 
1399
{
 
1400
    if (d->errorString.isEmpty()) {
 
1401
        const char *str = 0;
 
1402
        switch (d->error) {
 
1403
        case NoError:
 
1404
        case UnspecifiedError:
 
1405
            str = QT_TRANSLATE_NOOP("QFile", "Unknown error");
 
1406
            break;
 
1407
        case ReadError:
 
1408
            str = QT_TRANSLATE_NOOP("QFile", "Could not read from the file");
 
1409
            break;
 
1410
        case WriteError:
 
1411
            str = QT_TRANSLATE_NOOP("QFile", "Could not write to the file");
 
1412
            break;
 
1413
        case FatalError:
 
1414
            str = QT_TRANSLATE_NOOP("QFile", "Fatal error");
 
1415
            break;
 
1416
        case ResourceError:
 
1417
            str = QT_TRANSLATE_NOOP("QFile", "Resource error");
 
1418
            break;
 
1419
        case OpenError:
 
1420
            str = QT_TRANSLATE_NOOP("QFile", "Could not open the file");
 
1421
            break;
 
1422
#ifdef QT3_SUPPORT
 
1423
        case ConnectError:
 
1424
            str = QT_TRANSLATE_NOOP("QFile", "Could not connect to host");
 
1425
            break;
 
1426
#endif
 
1427
        case AbortError:
 
1428
            str = QT_TRANSLATE_NOOP("QFile", "Aborted");
 
1429
            break;
 
1430
        case TimeOutError:
 
1431
            str = QT_TRANSLATE_NOOP("QFile", "Timeout");
 
1432
            break;
 
1433
        case RemoveError:
 
1434
            str = QT_TRANSLATE_NOOP("QFile", "Could not remove file");
 
1435
            break;
 
1436
        case RenameError:
 
1437
            str = QT_TRANSLATE_NOOP("QFile", "Could not rename file");
 
1438
            break;
 
1439
        case PositionError:
 
1440
            str = QT_TRANSLATE_NOOP("QFile", "Could not position in file");
 
1441
            break;
 
1442
        case PermissionsError:
 
1443
            str = QT_TRANSLATE_NOOP("QFile", "Failure to set Permissions");
 
1444
            break;
 
1445
        case CopyError:
 
1446
            str = QT_TRANSLATE_NOOP("QFile", "Could not copy file");
 
1447
            break;
 
1448
        case ResizeError:
 
1449
            str = QT_TRANSLATE_NOOP("QFile", "Could not resize file");
 
1450
            break;
 
1451
        }
 
1452
#if defined(QT_BUILD_CORE_LIB)
 
1453
        QString ret = QCoreApplication::translate("QFile", str);
 
1454
#ifdef QT3_SUPPORT
 
1455
        if(ret == str)
 
1456
            ret = QCoreApplication::translate("QIODevice", str);
 
1457
#endif
 
1458
        return ret;
 
1459
#else
 
1460
        return QString::fromLatin1(str);
 
1461
#endif
 
1462
    }
 
1463
    return d->errorString;
 
1464
}
 
1465
*/