Qt 4.8
qfile.h
Go to the documentation of this file.
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 
57 
59 
60 QT_MODULE(Core)
61 
63 class QFilePrivate;
64 
66 {
67 #ifndef QT_NO_QOBJECT
68  Q_OBJECT
69 #endif
71 
72 public:
73 
74  enum FileError {
75  NoError = 0,
76  ReadError = 1,
77  WriteError = 2,
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 
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 
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 
197 protected:
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 
208 private:
210 };
211 
212 Q_DECLARE_OPERATORS_FOR_FLAGS(QFile::Permissions)
213 
215 
217 
218 #endif // QFILE_H
virtual qint64 size() const
For open random-access devices, this function returns the size of the device.
Definition: qiodevice.cpp:642
Permission
This enum is used by the permission() function to report the permissions and ownership of a file...
Definition: qfile.h:95
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static QString symLinkTarget(const QString &fileName)
Returns the absolute path of the file or directory referred to by the symlink (or shortcut on Windows...
Definition: qfile.h:138
#define QT_MODULE(x)
Definition: qglobal.h:2783
virtual void close()
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen.
Definition: qiodevice.cpp:590
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
#define Q_DECLARE_FLAGS(Flags, Enum)
The Q_DECLARE_FLAGS() macro expands to.
Definition: qglobal.h:2348
#define error(msg)
virtual qint64 writeData(const char *data, qint64 len)=0
Writes up to maxSize bytes from data to the device.
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
virtual qint64 pos() const
For random-access devices, this function returns the position that data is written to or read from...
Definition: qiodevice.cpp:624
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
Definition: qglobal.h:2523
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
virtual bool atEnd() const
Returns true if the current read and write position is at the end of the device (i.e.
Definition: qiodevice.cpp:711
virtual qint64 readLineData(char *data, qint64 maxlen)
Reads up to maxSize characters into data and returns the number of characters read.
Definition: qiodevice.cpp:1285
unsigned char uchar
Definition: qglobal.h:994
static QString readLink(const QFileSystemEntry &link)
QFuture< void > map(Sequence &sequence, MapFunction function)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
The Q_DECLARE_OPERATORS_FOR_FLAGS() macro declares global operator|() functions for Flags...
Definition: qglobal.h:2355
const char * name
The QAbstractFileEngine class provides an abstraction for accessing the filesystem.
static const char * data(const QByteArray &arr)
FileHandleFlag
This enum is used when opening a file to specify additional options which only apply to files and not...
Definition: qfile.h:103
__int64 qint64
Definition: qglobal.h:942
static QString decodeName(const char *localFileName)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qfile.h:127
#define Q_OBJECT
Definition: qobjectdefs.h:157
virtual bool isSequential() const
Returns true if this device is sequential; otherwise returns false.
Definition: qiodevice.cpp:454
virtual qint64 readData(char *data, qint64 maxlen)=0
Reads up to maxSize bytes from the device into data, and returns the number of bytes read or -1 if an...
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
Definition: qiodevice.cpp:570
Q_CORE_EXPORT QTextStream & flush(QTextStream &s)
QString symLinkTarget() const
Returns the absolute path of the file or directory a symlink (or shortcut on Windows) points to...
Definition: qfile.h:137
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
MemoryMapFlags
This enum describes special options that may be used by the map() function.
Definition: qfile.h:180
FileError
This enum describes the errors that may be returned by the error() function.
Definition: qfile.h:74
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66
#define QT_END_HEADER
Definition: qglobal.h:137
virtual bool seek(qint64 pos)
For random-access devices, this function sets the current position to pos, returning true on success...
Definition: qiodevice.cpp:659
static QString fileName(const QString &fileUrl)