Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qfiledevice.h
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QFILEDEVICE_H
6#define QFILEDEVICE_H
7
8#include <QtCore/qiodevice.h>
9#include <QtCore/qstring.h>
10
11#if QT_CONFIG(cxx17_filesystem)
12#include <filesystem>
13#elif defined(Q_QDOC)
14namespace std {
15 namespace filesystem {
16 class path {
17 };
18 };
19};
20#endif
21
22QT_BEGIN_NAMESPACE
23
24class QDateTime;
26
27#if QT_CONFIG(cxx17_filesystem)
28namespace QtPrivate {
29inline QString fromFilesystemPath(const std::filesystem::path &path)
30{
31 // we could use QAnyStringView, but this allows us to statically determine
32 // the correct toString() call
33 using View = std::conditional_t<sizeof(std::filesystem::path::value_type) == sizeof(char16_t),
34 QStringView, QUtf8StringView>;
35 return View(path.native()).toString();
36}
37
38inline std::filesystem::path toFilesystemPath(const QString &path)
39{
40 if constexpr (sizeof(std::filesystem::path::value_type) == sizeof(char16_t))
41 return std::u16string_view(QStringView(path));
42 else
43 return path.toStdString();
44}
45} // namespace QtPrivate
46#endif // QT_CONFIG(cxx17_filesystem)
47
48#if !defined(QT_USE_NODISCARD_FILE_OPEN) && !defined(QT_NO_USE_NODISCARD_FILE_OPEN)
49# if QT_VERSION < QT_VERSION_CHECK(6, 10, 0)
50# define QT_NO_USE_NODISCARD_FILE_OPEN
51# else
52# define QT_USE_NODISCARD_FILE_OPEN
53# endif
54#endif
55
56#if defined(QT_USE_NODISCARD_FILE_OPEN) && defined(QT_NO_USE_NODISCARD_FILE_OPEN)
57#error "Inconsistent macro definition for nodiscard QFile::open"
58#elif defined(QT_USE_NODISCARD_FILE_OPEN)
59#define QFILE_MAYBE_NODISCARD [[nodiscard]]
60#else /* QT_NO_USE_NODISCARD_FILE_OPEN */
61#define QFILE_MAYBE_NODISCARD
62#endif
63
64class Q_CORE_EXPORT QFileDevice : public QIODevice
65{
66#ifndef QT_NO_QOBJECT
67 Q_OBJECT
68#endif
69 Q_DECLARE_PRIVATE(QFileDevice)
70
71public:
72 enum FileError {
73 NoError = 0,
74 ReadError = 1,
75 WriteError = 2,
76 FatalError = 3,
77 ResourceError = 4,
78 OpenError = 5,
79 AbortError = 6,
80 TimeOutError = 7,
81 UnspecifiedError = 8,
82 RemoveError = 9,
83 RenameError = 10,
84 PositionError = 11,
85 ResizeError = 12,
86 PermissionsError = 13,
87 CopyError = 14
88 };
89
90 enum FileTime {
91 FileAccessTime,
92 FileBirthTime,
93 FileMetadataChangeTime,
94 FileModificationTime
95 };
96
97 enum Permission {
98 ReadOwner = 0x4000, WriteOwner = 0x2000, ExeOwner = 0x1000,
99 ReadUser = 0x0400, WriteUser = 0x0200, ExeUser = 0x0100,
100 ReadGroup = 0x0040, WriteGroup = 0x0020, ExeGroup = 0x0010,
101 ReadOther = 0x0004, WriteOther = 0x0002, ExeOther = 0x0001
102 };
103 Q_DECLARE_FLAGS(Permissions, Permission)
104
105 enum FileHandleFlag {
106 AutoCloseHandle = 0x0001,
107 DontCloseHandle = 0
108 };
109 Q_DECLARE_FLAGS(FileHandleFlags, FileHandleFlag)
110
111 ~QFileDevice();
112
113 FileError error() const;
114 void unsetError();
115
116 void close() override;
117
118 bool isSequential() const override;
119
120 int handle() const;
121 virtual QString fileName() const;
122
123 qint64 pos() const override;
124 bool seek(qint64 offset) override;
125 bool atEnd() const override;
126 bool flush();
127
128 qint64 size() const override;
129
130 virtual bool resize(qint64 sz);
131 virtual Permissions permissions() const;
132 virtual bool setPermissions(Permissions permissionSpec);
133
134 enum MemoryMapFlag {
135 NoOptions = 0,
136 MapPrivateOption = 0x0001
137 };
138 Q_DECLARE_FLAGS(MemoryMapFlags, MemoryMapFlag)
139
140 uchar *map(qint64 offset, qint64 size, MemoryMapFlags flags = NoOptions);
141 bool unmap(uchar *address);
142
143 QDateTime fileTime(QFileDevice::FileTime time) const;
144 bool setFileTime(const QDateTime &newDate, QFileDevice::FileTime fileTime);
145
146protected:
147 QFileDevice();
148#ifdef QT_NO_QOBJECT
149 QFileDevice(QFileDevicePrivate &dd);
150#else
151 explicit QFileDevice(QObject *parent);
152 QFileDevice(QFileDevicePrivate &dd, QObject *parent = nullptr);
153#endif
154
155 qint64 readData(char *data, qint64 maxlen) override;
156 qint64 writeData(const char *data, qint64 len) override;
157 qint64 readLineData(char *data, qint64 maxlen) override;
158
159private:
160 Q_DISABLE_COPY(QFileDevice)
161};
162
163Q_DECLARE_OPERATORS_FOR_FLAGS(QFileDevice::Permissions)
164Q_DECLARE_OPERATORS_FOR_FLAGS(QFileDevice::FileHandleFlags)
165Q_DECLARE_OPERATORS_FOR_FLAGS(QFileDevice::MemoryMapFlags)
166
167QT_END_NAMESPACE
168
169#endif // QFILEDEVICE_H
void setError(QFileDevice::FileError err, int errNum)
bool putCharHelper(char c) override
virtual QAbstractFileEngine * engine() const
void setError(QFileDevice::FileError err)
void setError(QFileDevice::FileError err, const QString &errorString)
\inmodule QtCore
Definition qfiledevice.h:65
Combined button and popup list for selecting options.
#define QFILE_WRITEBUFFER_SIZE
#define QT_USE_NODISCARD_FILE_OPEN
Definition qfiledevice.h:52