MayaFlux 0.4.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 Anchor a relative output path to Config::SOURCE_DIR.
27 *
28 * Absolute paths are returned unchanged. Relative paths are prefixed
29 * with Config::SOURCE_DIR so output files land in the project source
30 * tree rather than the binary CWD (which varies by platform and IDE).
31 *
32 * @param filepath Path as supplied by the caller.
33 * @return Resolved path string.
34 */
35[[nodiscard]] inline std::string resolve_write_path(const std::string& filepath)
36{
37 namespace fs = std::filesystem;
38 auto normalized = std::string(filepath);
39 std::ranges::replace(normalized, '\\', '/');
40
41 if (fs::path(normalized).is_absolute())
42 return normalized;
43
44 return (fs::path(Config::SOURCE_DIR) / normalized).string();
45}
46
47/**
48 * @class FileWriter
49 * @brief Abstract base class for file writing operations
50 *
51 * Provides interface for writing various data types to files.
52 * Concrete implementations handle specific formats (text, binary, audio, etc.)
53 */
55public:
56 virtual ~FileWriter() = default;
57
58 /**
59 * @brief Check if this writer can handle the given file path
60 */
61 [[nodiscard]] virtual bool can_write(const std::string& filepath) const = 0;
62
63 /**
64 * @brief Open a file for writing
65 * @param filepath Path to the file
66 * @param options Write options (append, create, truncate, etc.)
67 * @return true if successful
68 */
69 virtual bool open(const std::string& filepath,
71 = 0;
72
73 /**
74 * @brief Close the currently open file
75 */
76 virtual void close() = 0;
77
78 /**
79 * @brief Check if a file is currently open for writing
80 */
81 [[nodiscard]] virtual bool is_open() const = 0;
82
83 /**
84 * @brief Write raw bytes
85 */
86 virtual bool write_bytes(const void* data, size_t size) = 0;
87
88 /**
89 * @brief Write a string
90 */
91 virtual bool write_string(std::string_view str) = 0;
92
93 /**
94 * @brief Write a line (appends newline)
95 */
96 virtual bool write_line(std::string_view line) = 0;
97
98 /**
99 * @brief Flush buffered writes to disk
100 */
101 virtual bool flush() = 0;
102
103 /**
104 * @brief Get current write position (bytes written)
105 */
106 [[nodiscard]] virtual size_t get_write_position() const = 0;
107
108 /**
109 * @brief Get last error message
110 */
111 [[nodiscard]] virtual std::string get_last_error() const = 0;
112};
113
114} // namespace MayaFlux::IO
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.
@ 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.
@ ALL
All options enabled.
@ NONE
No special options.
FileReadOptions operator&(FileReadOptions a, FileReadOptions b)
FileReadOptions operator|(FileReadOptions a, FileReadOptions b)
std::string resolve_write_path(const std::string &filepath)
Anchor a relative output path to Config::SOURCE_DIR.
std::vector< double > normalized(const std::vector< double > &data, double target_peak)
Normalize single-channel data (non-destructive)
Definition Yantra.cpp:588