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
125 ~File();
126
131 const String &filename() const { return _filename; }
132
139 void setFilename(const String &fn) { _filename = fn; }
140
145 int flags() const { return _fileFlags; }
146
151 FileHandle handle() const { return _handle; }
152
158 Error open(OpenMode mode) override;
159
166 Error open(OpenMode mode, int fileFlags);
167
172 Error close() override;
173
175 bool isOpen() const override;
176
183 int64_t write(const void *data, int64_t maxSize) override;
184
186 bool isSequential() const override;
187
193 Error seek(int64_t offset) override;
194
199 int64_t pos() const override;
200
206 Result<int64_t> size() const override;
207
212 bool atEnd() const override;
213
219 Result<int64_t> seekFromCurrent(int64_t offset) const;
220
226 Result<int64_t> seekFromEnd(int64_t offset) const;
227
233 Error truncate(int64_t offset) const;
234
249 int64_t writev(const IOVec *iov, int count);
250
262 Error preallocate(int64_t offset, int64_t length);
263
274 Result<size_t> directIOAlignment() const;
275
328 Error readBulk(Buffer &buf, int64_t size);
329
364 int64_t writeBulk(const void *data, int64_t size);
365
383 Error sync(bool dataOnly = true);
384
395 Error setDirectIO(bool enable);
396
401 bool isDirectIO() const { return _directIO; }
402
411 Error setSynchronous(bool enable);
412
417 bool isSynchronous() const { return _synchronous; }
418
427 Error setNonBlocking(bool enable);
428
433 bool isNonBlocking() const { return _nonBlocking; }
434
444 bool isResource() const { return _resourceFile != nullptr; }
445
446 protected:
448 int64_t readFromDevice(void *data, int64_t maxSize) override;
449
451 int64_t deviceBytesAvailable() const override;
452
453 private:
454 bool _directIO = false;
455 bool _synchronous = false;
456 bool _nonBlocking = false;
457 String _filename;
458 int _fileFlags = NoFlags;
459 FileHandle _handle = FileHandleClosedValue;
460 bool _savedUnbuffered = false;
461 const cirf_file_t *_resourceFile = nullptr;
462 int64_t _resourcePos = 0;
463};
464
465PROMEKI_NAMESPACE_END
466
467#endif // PROMEKI_ENABLE_CORE