File indexing completed on 2026-10-01 09:05:08
0001
0002
0003
0004
0005 #ifndef QFILEDEVICE_H
0006 #define QFILEDEVICE_H
0007
0008 #include <QtCore/qiodevice.h>
0009 #include <QtCore/qstring.h>
0010
0011 #if QT_CONFIG(cxx17_filesystem)
0012 #include <filesystem>
0013 #elif defined(Q_QDOC)
0014 namespace std {
0015 namespace filesystem {
0016 class path {
0017 };
0018 };
0019 };
0020 #endif
0021
0022 QT_BEGIN_NAMESPACE
0023
0024 class QDateTime;
0025 class QFileDevicePrivate;
0026
0027 #if QT_CONFIG(cxx17_filesystem)
0028 namespace QtPrivate {
0029 inline QString fromFilesystemPath(const std::filesystem::path &path)
0030 {
0031
0032
0033 using View = std::conditional_t<sizeof(std::filesystem::path::value_type) == sizeof(char16_t),
0034 QStringView, QUtf8StringView>;
0035 return View(path.native()).toString();
0036 }
0037
0038 inline std::filesystem::path toFilesystemPath(const QString &path)
0039 {
0040 if constexpr (sizeof(std::filesystem::path::value_type) == sizeof(char16_t))
0041 return std::u16string_view(QStringView(path));
0042 else
0043 return path.toStdString();
0044 }
0045 }
0046 #endif
0047
0048 #if !defined(QT_USE_NODISCARD_FILE_OPEN) && !defined(QT_NO_USE_NODISCARD_FILE_OPEN)
0049 # if QT_VERSION < QT_VERSION_CHECK(6, 10, 0)
0050 # define QT_NO_USE_NODISCARD_FILE_OPEN
0051 # else
0052 # define QT_USE_NODISCARD_FILE_OPEN
0053 # endif
0054 #endif
0055
0056 #if defined(QT_USE_NODISCARD_FILE_OPEN) && defined(QT_NO_USE_NODISCARD_FILE_OPEN)
0057 #error "Inconsistent macro definition for nodiscard QFile::open"
0058 #elif defined(QT_USE_NODISCARD_FILE_OPEN)
0059 #define QFILE_MAYBE_NODISCARD [[nodiscard]]
0060 #else
0061 #define QFILE_MAYBE_NODISCARD
0062 #endif
0063
0064 class Q_CORE_EXPORT QFileDevice : public QIODevice
0065 {
0066 #ifndef QT_NO_QOBJECT
0067 Q_OBJECT
0068 #endif
0069 Q_DECLARE_PRIVATE(QFileDevice)
0070
0071 public:
0072 enum FileError {
0073 NoError = 0,
0074 ReadError = 1,
0075 WriteError = 2,
0076 FatalError = 3,
0077 ResourceError = 4,
0078 OpenError = 5,
0079 AbortError = 6,
0080 TimeOutError = 7,
0081 UnspecifiedError = 8,
0082 RemoveError = 9,
0083 RenameError = 10,
0084 PositionError = 11,
0085 ResizeError = 12,
0086 PermissionsError = 13,
0087 CopyError = 14
0088 };
0089
0090 enum FileTime {
0091 FileAccessTime,
0092 FileBirthTime,
0093 FileMetadataChangeTime,
0094 FileModificationTime
0095 };
0096
0097 enum Permission {
0098 ReadOwner = 0x4000, WriteOwner = 0x2000, ExeOwner = 0x1000,
0099 ReadUser = 0x0400, WriteUser = 0x0200, ExeUser = 0x0100,
0100 ReadGroup = 0x0040, WriteGroup = 0x0020, ExeGroup = 0x0010,
0101 ReadOther = 0x0004, WriteOther = 0x0002, ExeOther = 0x0001
0102 };
0103 Q_DECLARE_FLAGS(Permissions, Permission)
0104
0105 enum FileHandleFlag {
0106 AutoCloseHandle = 0x0001,
0107 DontCloseHandle = 0
0108 };
0109 Q_DECLARE_FLAGS(FileHandleFlags, FileHandleFlag)
0110
0111 ~QFileDevice();
0112
0113 FileError error() const;
0114 void unsetError();
0115
0116 void close() override;
0117
0118 bool isSequential() const override;
0119
0120 int handle() const;
0121 virtual QString fileName() const;
0122
0123 qint64 pos() const override;
0124 bool seek(qint64 offset) override;
0125 bool atEnd() const override;
0126 bool flush();
0127
0128 qint64 size() const override;
0129
0130 virtual bool resize(qint64 sz);
0131 virtual Permissions permissions() const;
0132 virtual bool setPermissions(Permissions permissionSpec);
0133
0134 enum MemoryMapFlag {
0135 NoOptions = 0,
0136 MapPrivateOption = 0x0001
0137 };
0138 Q_DECLARE_FLAGS(MemoryMapFlags, MemoryMapFlag)
0139
0140 uchar *map(qint64 offset, qint64 size, MemoryMapFlags flags = NoOptions);
0141 bool unmap(uchar *address);
0142
0143 QDateTime fileTime(QFileDevice::FileTime time) const;
0144 bool setFileTime(const QDateTime &newDate, QFileDevice::FileTime fileTime);
0145
0146 protected:
0147 QFileDevice();
0148 #ifdef QT_NO_QOBJECT
0149 QFileDevice(QFileDevicePrivate &dd);
0150 #else
0151 explicit QFileDevice(QObject *parent);
0152 QFileDevice(QFileDevicePrivate &dd, QObject *parent = nullptr);
0153 #endif
0154
0155 qint64 readData(char *data, qint64 maxlen) override;
0156 qint64 writeData(const char *data, qint64 len) override;
0157 qint64 readLineData(char *data, qint64 maxlen) override;
0158
0159 private:
0160 Q_DISABLE_COPY(QFileDevice)
0161 };
0162
0163 Q_DECLARE_OPERATORS_FOR_FLAGS(QFileDevice::Permissions)
0164 Q_DECLARE_OPERATORS_FOR_FLAGS(QFileDevice::FileHandleFlags)
0165 Q_DECLARE_OPERATORS_FOR_FLAGS(QFileDevice::MemoryMapFlags)
0166
0167 QT_END_NAMESPACE
0168
0169 #endif