libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
file.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstdint>
13#include <promeki/core/string.h>
15#include <promeki/core/error.h>
16#include <promeki/core/result.h>
18
20
38class File : public BufferedIODevice {
39 PROMEKI_OBJECT(File, BufferedIODevice)
40 public:
47 enum Flags {
48 NoFlags = 0x00,
49 Create = 0x01,
50 Append = 0x02,
51 Truncate = 0x04,
52 Exclusive = 0x08
53 };
54
55#if defined(PROMEKI_PLATFORM_WINDOWS)
57 using FileHandle = HANDLE;
59 static constexpr FileHandle FileHandleClosedValue = nullptr;
60#else
62 using FileHandle = int;
64 static constexpr FileHandle FileHandleClosedValue = -1;
65#endif
66
68 File(const File &) = delete;
70 File &operator=(const File &) = delete;
71
76 File(ObjectBase *parent = nullptr);
77
83 File(const String &fn, ObjectBase *parent = nullptr);
84
90 File(const char *fn, ObjectBase *parent = nullptr);
91
97 File(const FilePath &fp, ObjectBase *parent = nullptr);
98
101
106 const String &filename() const { return _filename; }
107
114 void setFilename(const String &fn) { _filename = fn; }
115
120 int flags() const { return _fileFlags; }
121
126 FileHandle handle() const { return _handle; }
127
133 Error open(OpenMode mode) override;
134
142
147 Error close() override;
148
150 bool isOpen() const override;
151
158 int64_t write(const void *data, int64_t maxSize) override;
159
161 bool isSequential() const override;
162
168 Error seek(int64_t offset) override;
169
174 int64_t pos() const override;
175
181 Result<int64_t> size() const override;
182
187 bool atEnd() const override;
188
195
202
208 Error truncate(int64_t offset) const;
209
221
274
286
291 bool isDirectIO() const { return _directIO; }
292
302
307 bool isSynchronous() const { return _synchronous; }
308
318
323 bool isNonBlocking() const { return _nonBlocking; }
324
325 protected:
326
328 int64_t readFromDevice(void *data, int64_t maxSize) override;
329
332
333 private:
334 bool _directIO = false;
335 bool _synchronous = false;
336 bool _nonBlocking = false;
337 String _filename;
338 int _fileFlags = NoFlags;
340 bool _savedUnbuffered = false;
341};
342
Generic memory buffer descriptor with alignment and memory space support.
Definition buffer.h:85
Abstract IODevice with an internal read buffer.
Definition bufferediodevice.h:30
Lightweight error code wrapper for the promeki library.
Definition error.h:39
Simple value type wrapping std::filesystem::path.
Definition filepath.h:27
File I/O device with buffered reading.
Definition file.h:38
File(const File &)=delete
Deleted copy constructor (non-copyable).
Result< int64_t > seekFromCurrent(int64_t offset) const
Seeks relative to the current file position.
bool isOpen() const override
Returns true if the file is currently open.
bool isDirectIO() const
Returns true if direct I/O is enabled.
Definition file.h:291
Error setNonBlocking(bool enable)
Enables or disables non-blocking mode.
Error open(OpenMode mode, int fileFlags)
Opens the file with the given mode and file-specific flags.
File(const char *fn, ObjectBase *parent=nullptr)
Constructs a File with the given C string filename.
Result< int64_t > seekFromEnd(int64_t offset) const
Seeks relative to the end of the file.
bool atEnd() const override
Returns true if the position is at or past the end.
Error open(OpenMode mode) override
Opens the file with the given mode and no extra flags.
static constexpr FileHandle FileHandleClosedValue
Sentinel value representing a closed file handle (POSIX).
Definition file.h:64
File(const FilePath &fp, ObjectBase *parent=nullptr)
Constructs a File with the given file path.
const String & filename() const
Returns the filename associated with this File.
Definition file.h:106
int FileHandle
Platform-specific file handle type (POSIX).
Definition file.h:62
int64_t write(const void *data, int64_t maxSize) override
Writes data to the file at the current position.
bool isNonBlocking() const
Returns true if non-blocking mode is enabled.
Definition file.h:323
Error seek(int64_t offset) override
Seeks to an absolute byte offset.
Error setDirectIO(bool enable)
Enables or disables direct I/O (bypass OS page cache).
void setFilename(const String &fn)
Sets the filename.
Definition file.h:114
int64_t pos() const override
Returns the current read/write position.
Error setSynchronous(bool enable)
Enables or disables synchronous writes.
bool isSequential() const override
Returns false (files are seekable).
File & operator=(const File &)=delete
Deleted copy assignment operator (non-copyable).
bool isSynchronous() const
Returns true if synchronous writes are enabled.
Definition file.h:307
int flags() const
Returns the file-specific flags used at open time.
Definition file.h:120
Error truncate(int64_t offset) const
Truncates the file to the specified length.
File(ObjectBase *parent=nullptr)
Default constructor. Creates a File with no filename.
FileHandle handle() const
Returns the native file handle.
Definition file.h:126
Flags
File-specific open flags.
Definition file.h:47
@ NoFlags
No extra flags.
Definition file.h:48
@ Truncate
Truncate the file to zero length on open.
Definition file.h:51
@ Create
Create the file if it does not exist.
Definition file.h:49
@ Append
Append writes to the end of the file.
Definition file.h:50
@ Exclusive
Fail if the file already exists (with Create).
Definition file.h:52
Result< size_t > directIOAlignment() const
Returns the direct I/O alignment requirement for this file.
File(const String &fn, ObjectBase *parent=nullptr)
Constructs a File with the given filename.
~File()
Destructor. Closes the file if it is open.
Error readBulk(Buffer &buf, int64_t size)
Reads bulk data from the current file position using direct I/O.
int64_t readFromDevice(void *data, int64_t maxSize) override
Reads raw data from the file descriptor.
Result< int64_t > size() const override
Returns the total file size in bytes.
Error close() override
Closes the file if it is open.
int64_t deviceBytesAvailable() const override
Returns bytes available from the device (file).
OpenMode
Mode flags controlling how a device is opened.
Definition iodevice.h:33
Dynamic array container wrapping std::vector.
Definition list.h:40
Base object for promeki.
Definition objectbase.h:129
ObjectBase * parent() const
Returns the parent object, if one. nullptr if none.
Definition objectbase.h:258
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19