libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
file.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <cstdint>
14#include <promeki/namespace.h>
15#include <promeki/platform.h>
16#include <promeki/string.h>
17#include <promeki/filepath.h>
18#include <promeki/error.h>
19#include <promeki/result.h>
21#include <promeki/uniqueptr.h>
22
23// Forward-declared so file.h does not need to pull <cirf/types.h>
24// into every consumer of File. The pointer is only ever stored as
25// a member; methods that need to dereference it include
26// <cirf/types.h> in file.cpp.
27struct cirf_file;
28typedef struct cirf_file cirf_file_t;
29
30PROMEKI_NAMESPACE_BEGIN
31
53class File : public BufferedIODevice {
54 PROMEKI_OBJECT(File, BufferedIODevice)
55 public:
57 using UPtr = UniquePtr<File>;
58
65 enum Flags {
66 NoFlags = 0x00,
67 Create = 0x01,
68 Append = 0x02,
69 Truncate = 0x04,
70 Exclusive = 0x08
71 };
72
80 struct IOVec {
81 const void *data;
82 size_t size;
83 };
84
85#if defined(PROMEKI_PLATFORM_WINDOWS)
87 using FileHandle = HANDLE;
89 static constexpr FileHandle FileHandleClosedValue = nullptr;
90#else
92 using FileHandle = int;
94 static constexpr FileHandle FileHandleClosedValue = -1;
95#endif
96
101 File(ObjectBase *parent = nullptr);
102
108 File(const String &fn, ObjectBase *parent = nullptr);
109
115 File(const char *fn, ObjectBase *parent = nullptr);
116
122 File(const FilePath &fp, ObjectBase *parent = nullptr);
123
146 File(FileHandle handle, OpenMode mode, bool ownsHandle = true, ObjectBase *parent = nullptr);
147
149 ~File();
150
155 const String &filename() const { return _filename; }
156
163 void setFilename(const String &fn) { _filename = fn; }
164
169 int flags() const { return _fileFlags; }
170
175 FileHandle handle() const { return _handle; }
176
182 Error open(OpenMode mode) override;
183
190 Error open(OpenMode mode, int fileFlags);
191
196 Error close() override;
197
199 bool isOpen() const override;
200
202 bool isSequential() const override;
203
209 Error seek(int64_t offset) override;
210
215 int64_t pos() const override;
216
222 Result<int64_t> size() const override;
223
228 bool atEnd() const override;
229
235 Result<int64_t> seekFromCurrent(int64_t offset) const;
236
242 Result<int64_t> seekFromEnd(int64_t offset) const;
243
249 Error truncate(int64_t offset) const;
250
265 int64_t writev(const IOVec *iov, int count);
266
278 Error preallocate(int64_t offset, int64_t length);
279
290 Result<size_t> directIOAlignment() const;
291
344 Error readBulk(Buffer &buf, int64_t size);
345
380 int64_t writeBulk(const void *data, int64_t size);
381
399 Error sync(bool dataOnly = true);
400
411 Error setDirectIO(bool enable);
412
417 bool isDirectIO() const { return _directIO; }
418
427 Error setSynchronous(bool enable);
428
433 bool isSynchronous() const { return _synchronous; }
434
443 Error setNonBlocking(bool enable);
444
449 bool isNonBlocking() const { return _nonBlocking; }
450
460 bool isResource() const { return _resourceFile != nullptr; }
461
462 protected:
464 int64_t readFromDevice(void *data, int64_t maxSize) override;
465
467 int64_t writeToDevice(const void *data, int64_t maxSize) override;
468
470 int64_t deviceBytesAvailable() const override;
471
472 private:
473 bool _directIO = false;
474 bool _synchronous = false;
475 bool _nonBlocking = false;
476 bool _ownsHandle = true;
477 String _filename;
478 int _fileFlags = NoFlags;
479 FileHandle _handle = FileHandleClosedValue;
480 bool _savedUnbuffered = false;
481 const cirf_file_t *_resourceFile = nullptr;
482 int64_t _resourcePos = 0;
483};
484
485PROMEKI_NAMESPACE_END
486
487#endif // PROMEKI_ENABLE_CORE