1/****************************************************************************
2**
3** Copyright (C) 2014 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#ifndef QFILE_H
43#define QFILE_H
44
45#include <QtCore/qiodevice.h>
46#include <QtCore/qstring.h>
47#include <stdio.h>
48#ifdef Q_OS_SYMBIAN
49#include <f32file.h>
50#endif
51
52#ifdef open
53#error qfile.h must be included before any header file that defines open
54#endif
55
56QT_BEGIN_HEADER
57
58QT_BEGIN_NAMESPACE
59
60QT_MODULE(Core)
61
62class QAbstractFileEngine;
63class QFilePrivate;
64
65class Q_CORE_EXPORT QFile : public QIODevice
66{
67#ifndef QT_NO_QOBJECT
68 Q_OBJECT
69#endif
70 Q_DECLARE_PRIVATE(QFile)
71
72public:
73
74 enum FileError {
75 NoError = 0,
76 ReadError = 1,
77 WriteError = 2,
78 FatalError = 3,
79 ResourceError = 4,
80 OpenError = 5,
81 AbortError = 6,
82 TimeOutError = 7,
83 UnspecifiedError = 8,
84 RemoveError = 9,
85 RenameError = 10,
86 PositionError = 11,
87 ResizeError = 12,
88 PermissionsError = 13,
89 CopyError = 14
90#ifdef QT3_SUPPORT
91 , ConnectError = 30
92#endif
93 };
94
95 enum Permission {
96 ReadOwner = 0x4000, WriteOwner = 0x2000, ExeOwner = 0x1000,
97 ReadUser = 0x0400, WriteUser = 0x0200, ExeUser = 0x0100,
98 ReadGroup = 0x0040, WriteGroup = 0x0020, ExeGroup = 0x0010,
99 ReadOther = 0x0004, WriteOther = 0x0002, ExeOther = 0x0001
100 };
101 Q_DECLARE_FLAGS(Permissions, Permission)
102
103 enum FileHandleFlag {
104 AutoCloseHandle = 0x0001,
105 DontCloseHandle = 0
106 };
107 Q_DECLARE_FLAGS(FileHandleFlags, FileHandleFlag)
108
109 QFile();
110 QFile(const QString &name);
111#ifndef QT_NO_QOBJECT
112 explicit QFile(QObject *parent);
113 QFile(const QString &name, QObject *parent);
114#endif
115 ~QFile();
116
117 FileError error() const;
118 void unsetError();
119
120 QString fileName() const;
121 void setFileName(const QString &name);
122
123 typedef QByteArray (*EncoderFn)(const QString &fileName);
124 typedef QString (*DecoderFn)(const QByteArray &localfileName);
125 static QByteArray encodeName(const QString &fileName);
126 static QString decodeName(const QByteArray &localFileName);
127 inline static QString decodeName(const char *localFileName)
128 { return decodeName(QByteArray(localFileName)); }
129 static void setEncodingFunction(EncoderFn);
130 static void setDecodingFunction(DecoderFn);
131
132 bool exists() const;
133 static bool exists(const QString &fileName);
134
135 QString readLink() const;
136 static QString readLink(const QString &fileName);
137 inline QString symLinkTarget() const { return readLink(); }
138 inline static QString symLinkTarget(const QString &fileName) { return readLink(fileName); }
139
140 bool remove();
141 static bool remove(const QString &fileName);
142
143 bool rename(const QString &newName);
144 static bool rename(const QString &oldName, const QString &newName);
145
146 bool link(const QString &newName);
147 static bool link(const QString &oldname, const QString &newName);
148
149 bool copy(const QString &newName);
150 static bool copy(const QString &fileName, const QString &newName);
151
152 bool isSequential() const;
153
154 bool open(OpenMode flags);
155 bool open(FILE *f, OpenMode flags);
156 bool open(int fd, OpenMode flags);
157#ifdef Q_OS_SYMBIAN
158 bool open(const RFile &f, OpenMode flags, FileHandleFlags handleFlags = DontCloseHandle);
159#endif
160 bool open(FILE *f, OpenMode ioFlags, FileHandleFlags handleFlags);
161 bool open(int fd, OpenMode ioFlags, FileHandleFlags handleFlags);
162 virtual void close();
163
164 qint64 size() const;
165 qint64 pos() const;
166 bool seek(qint64 offset);
167 bool atEnd() const;
168 bool flush();
169
170 bool resize(qint64 sz);
171 static bool resize(const QString &filename, qint64 sz);
172
173 Permissions permissions() const;
174 static Permissions permissions(const QString &filename);
175 bool setPermissions(Permissions permissionSpec);
176 static bool setPermissions(const QString &filename, Permissions permissionSpec);
177
178 int handle() const;
179
180 enum MemoryMapFlags {
181 NoOptions = 0
182 };
183
184 uchar *map(qint64 offset, qint64 size, MemoryMapFlags flags = NoOptions);
185 bool unmap(uchar *address);
186
187 virtual QAbstractFileEngine *fileEngine() const;
188
189#ifdef QT3_SUPPORT
190 typedef Permission PermissionSpec;
191 inline QT3_SUPPORT QString name() const { return fileName(); }
192 inline QT3_SUPPORT void setName(const QString &aName) { setFileName(aName); }
193 inline QT3_SUPPORT bool open(OpenMode aFlags, FILE *f) { return open(f, aFlags); }
194 inline QT3_SUPPORT bool open(OpenMode aFlags, int fd) { return open(fd, aFlags); }
195#endif
196
197protected:
198#ifdef QT_NO_QOBJECT
199 QFile(QFilePrivate &dd);
200#else
201 QFile(QFilePrivate &dd, QObject *parent = 0);
202#endif
203
204 qint64 readData(char *data, qint64 maxlen);
205 qint64 writeData(const char *data, qint64 len);
206 qint64 readLineData(char *data, qint64 maxlen);
207
208private:
209 Q_DISABLE_COPY(QFile)
210};
211
212Q_DECLARE_OPERATORS_FOR_FLAGS(QFile::Permissions)
213
214QT_END_NAMESPACE
215
216QT_END_HEADER
217
218#endif // QFILE_H
219