libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
fileiodevice.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstdio>
13#include <promeki/core/string.h>
14
16
47class FileIODevice : public IODevice {
48 PROMEKI_OBJECT(FileIODevice, IODevice)
49 public:
51 enum Flag {
52 NoFlags = 0x00,
53 OwnsFile = 0x01
54 };
55
66
77
88
101 FileIODevice(FILE *file, OpenMode mode, int flags = NoFlags,
102 ObjectBase *parent = nullptr);
103
113 explicit FileIODevice(const String &filename, ObjectBase *parent = nullptr);
114
123 explicit FileIODevice(ObjectBase *parent = nullptr);
124
126 ~FileIODevice() override;
127
129 FileIODevice(const FileIODevice &) = delete;
132
141
146 const String &filename() const { return _filename; }
147
152 FILE *file() const { return _file; }
153
158 bool ownsFile() const { return _ownsFile; }
159
170
181 Error open(OpenMode mode) override;
182
183 Error close() override;
184 void flush() override;
185 bool isOpen() const override;
186 int64_t read(void *data, int64_t maxSize) override;
187 int64_t write(const void *data, int64_t maxSize) override;
188 bool isSequential() const override;
190 int64_t pos() const override;
191 bool atEnd() const override;
192
193 private:
194 FILE *_file = nullptr;
195 String _filename;
196 bool _ownsFile = false;
197};
198
Lightweight error code wrapper for the promeki library.
Definition error.h:39
IODevice wrapping a C stdio FILE pointer.
Definition fileiodevice.h:47
bool atEnd() const override
Returns true if the current position is at the end.
void setFilename(const String &filename)
Sets the filename for fopen-based opening.
Error open(OpenMode mode) override
Opens the device.
bool isSequential() const override
Returns true if the device is sequential (non-seekable).
FILE * file() const
Returns the underlying FILE pointer, or nullptr.
Definition fileiodevice.h:152
Error close() override
Closes the device.
Flag
Flags for the FILE* constructor.
Definition fileiodevice.h:51
@ NoFlags
Default: device does not own the FILE.
Definition fileiodevice.h:52
@ OwnsFile
Device will fclose the FILE on close/destruct.
Definition fileiodevice.h:53
FileIODevice(const FileIODevice &)=delete
Deleted copy constructor (non-copyable).
static FileIODevice * stdoutDevice()
Returns a singleton FileIODevice wrapping C stdout.
bool isOpen() const override
Returns true if the device is open.
const String & filename() const
Returns the filename, or an empty string if none was set.
Definition fileiodevice.h:146
static FileIODevice * stderrDevice()
Returns a singleton FileIODevice wrapping C stderr.
static FileIODevice * stdinDevice()
Returns a singleton FileIODevice wrapping C stdin.
void flush() override
Flushes any buffered output data to the underlying device.
FileIODevice & operator=(const FileIODevice &)=delete
Deleted copy assignment (non-copyable).
FileIODevice(const String &filename, ObjectBase *parent=nullptr)
Constructs a FileIODevice with a filename.
FILE * takeFile()
Transfers ownership of the FILE pointer to the caller.
int64_t write(const void *data, int64_t maxSize) override
Writes up to maxSize bytes from data.
int64_t read(void *data, int64_t maxSize) override
Reads up to maxSize bytes into data.
FileIODevice(FILE *file, OpenMode mode, int flags=NoFlags, ObjectBase *parent=nullptr)
Constructs a FileIODevice wrapping an external FILE.
FileIODevice(ObjectBase *parent=nullptr)
Constructs a FileIODevice with no FILE or filename.
~FileIODevice() override
Destructor. Closes and fclose's if owned.
Error seek(int64_t pos) override
Seeks to the given byte offset from the beginning.
bool ownsFile() const
Returns true if the device owns the FILE pointer.
Definition fileiodevice.h:158
int64_t pos() const override
Returns the current read/write position.
Abstract base class for all I/O devices.
Definition iodevice.h:29
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