MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
FileWriter.hpp
Go to the documentation of this file.
1#pragma once
2
3namespace MayaFlux::IO {
4
5enum class FileWriteOptions : uint32_t {
6 NONE = 0,
7 APPEND = 1 << 0, ///< Append to existing file
8 CREATE = 1 << 1, ///< Create if doesn't exist
9 TRUNCATE = 1 << 2, ///< Truncate existing file
10 SYNC = 1 << 3, ///< Sync after each write (slow but safe)
11 BUFFER = 1 << 4, ///< Use internal buffering
12 ALL = 0xFFFFFFFF
13};
14
16{
17 return static_cast<FileWriteOptions>(static_cast<uint32_t>(a) | static_cast<uint32_t>(b));
18}
19
21{
22 return static_cast<FileWriteOptions>(static_cast<uint32_t>(a) & static_cast<uint32_t>(b));
23}
24
25/**
26 * @brief Resolve a writable path against the configured output directory.
27 *
28 * Absolute paths are returned unchanged. A bare filename uses the default
29 * directory. Relative paths with a parent directory use the project root.
30 *
31 * @param filepath Path as supplied by the caller.
32 * @param default_directory Destination for a bare filename.
33 * @return Resolved path string.
34 */
35[[nodiscard]] inline std::string resolve_write_path(
36 const std::string& filepath,
37 std::string_view default_directory = Config::OUT_DIR)
38{
39 namespace fs = std::filesystem;
40 auto normalized = std::string(filepath);
41 std::ranges::replace(normalized, '\\', '/');
42
43 if (fs::path(normalized).is_absolute())
44 return normalized;
45
46 if (!fs::path(normalized).has_parent_path())
47 return (fs::path(default_directory) / normalized).string();
48
49 return (fs::path(Config::project_root()) / normalized).string();
50}
51
52/**
53 * @brief Substitute a frame index into a numbered output pattern.
54 *
55 * Wraps resolve_write_path, so the same directory rules apply and a bare
56 * pattern lands wherever a bare filename would.
57 *
58 * The pattern carries exactly one std::format replacement field, which the
59 * index is formatted into: "smoke.{:04}.vdb" at frame 7 gives
60 * "smoke.0007.vdb". Zero padding matters. A DCC reading a sequence matches
61 * on a fixed-width numeric suffix, so an unpadded pattern sorts frame 10
62 * before frame 2 and plays out of order.
63 *
64 * A pattern with no replacement field returns the same path every frame,
65 * which silently overwrites. A pattern with more than one, or with a field
66 * the index cannot format into, throws std::format_error.
67 *
68 * @param pattern Format string with one index field.
69 * @param frame Zero-based frame index.
70 * @return Resolved absolute path.
71 */
72[[nodiscard]] inline std::string resolve_sequence_path(std::string_view pattern, uint64_t frame)
73{
74 return resolve_write_path(std::vformat(pattern, std::make_format_args(frame)));
75}
76
77/**
78 * @class FileWriter
79 * @brief Abstract base class for file writing operations
80 *
81 * Provides interface for writing various data types to files.
82 * Concrete implementations handle specific formats (text, binary, audio, etc.)
83 */
85public:
86 virtual ~FileWriter() = default;
87
88 /**
89 * @brief Check if this writer can handle the given file path
90 */
91 [[nodiscard]] virtual bool can_write(const std::string& filepath) const = 0;
92
93 /**
94 * @brief Open a file for writing
95 * @param filepath Path to the file
96 * @param options Write options (append, create, truncate, etc.)
97 * @return true if successful
98 */
99 virtual bool open(const std::string& filepath,
101 = 0;
102
103 /**
104 * @brief Close the currently open file
105 */
106 virtual void close() = 0;
107
108 /**
109 * @brief Check if a file is currently open for writing
110 */
111 [[nodiscard]] virtual bool is_open() const = 0;
112
113 /**
114 * @brief Write raw bytes
115 */
116 virtual bool write_bytes(const void* data, size_t size) = 0;
117
118 /**
119 * @brief Write a string
120 */
121 virtual bool write_string(std::string_view str) = 0;
122
123 /**
124 * @brief Write a line (appends newline)
125 */
126 virtual bool write_line(std::string_view line) = 0;
127
128 /**
129 * @brief Flush buffered writes to disk
130 */
131 virtual bool flush() = 0;
132
133 /**
134 * @brief Get current write position (bytes written)
135 */
136 [[nodiscard]] virtual size_t get_write_position() const = 0;
137
138 /**
139 * @brief Get last error message
140 */
141 [[nodiscard]] virtual std::string get_last_error() const = 0;
142};
143
144} // namespace MayaFlux::IO
bool size
size_t a
size_t b
virtual void close()=0
Close the currently open file.
virtual bool write_bytes(const void *data, size_t size)=0
Write raw bytes.
virtual std::string get_last_error() const =0
Get last error message.
virtual bool open(const std::string &filepath, FileWriteOptions options=FileWriteOptions::CREATE|FileWriteOptions::TRUNCATE)=0
Open a file for writing.
virtual size_t get_write_position() const =0
Get current write position (bytes written)
virtual bool can_write(const std::string &filepath) const =0
Check if this writer can handle the given file path.
virtual ~FileWriter()=default
virtual bool write_string(std::string_view str)=0
Write a string.
virtual bool write_line(std::string_view line)=0
Write a line (appends newline)
virtual bool is_open() const =0
Check if a file is currently open for writing.
virtual bool flush()=0
Flush buffered writes to disk.
Abstract base class for file writing operations.
std::string project_root()
AudioReadOptions operator&(AudioReadOptions a, AudioReadOptions b)
@ TRUNCATE
Truncate existing file.
@ BUFFER
Use internal buffering.
@ SYNC
Sync after each write (slow but safe)
@ CREATE
Create if doesn't exist.
@ APPEND
Append to existing file.
std::string resolve_sequence_path(std::string_view pattern, uint64_t frame)
Substitute a frame index into a numbered output pattern.
AudioReadOptions operator|(AudioReadOptions a, AudioReadOptions b)
std::string resolve_write_path(const std::string &filepath, std::string_view default_directory=Config::OUT_DIR)
Resolve a writable path against the configured output directory.