FastLED 3.10.6
Loading...
Searching...
No Matches
file_handle.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/int.h"
4#include "fl/stl/shared_ptr.h" // filebuf_ptr
5#include "fl/stl/detail/file_io.h" // For fl::FILE* and fl::fopen/fclose/etc.
6#include "fl/stl/span.h"
7#include "fl/stl/string.h"
8#include "fl/stl/noexcept.h"
9
10// Unified file buffer abstraction (streambuf-style backend)
11// This provides a platform-agnostic interface for file I/O operations.
12// Platform-specific implementations subclass fl::filebuf.
13// User-facing API is fl::ifstream / fl::ofstream (see fstream.h).
14
15namespace fl {
16
17struct CRGB; // Forward declaration for readRGB8
18
19// Seek direction constants (match std::ios seekdir)
20enum class seek_dir {
21 beg = 0, // Beginning of file
22 cur = 1, // Current position
23 end = 2 // End of file
24};
25
26// Polymorphic file buffer backend (analogous to std::filebuf / std::streambuf).
27// Platform implementations subclass this; consumers should prefer fl::ifstream.
28class filebuf {
29public:
30 virtual ~filebuf() FL_NO_EXCEPT = default;
31
32 // Core file operations (pure virtual)
33 virtual bool is_open() const = 0;
34 virtual void close() = 0;
35 virtual fl::size_t read(char* buffer, fl::size_t count) = 0;
36 virtual fl::size_t write(const char* data, fl::size_t count) = 0;
37 virtual fl::size_t tell() = 0;
38 virtual bool seek(fl::size_t pos, seek_dir dir) = 0;
39
40 // Size and path (pure virtual)
41 virtual fl::size_t size() const = 0;
42 virtual const char* path() const = 0;
43
44 // State queries (pure virtual)
45 virtual bool is_eof() const = 0;
46 virtual bool has_error() const = 0;
47 virtual void clear_error() = 0;
48
49 // Error reporting (pure virtual)
50 virtual int error_code() const = 0;
51 virtual const char* error_message() const = 0;
52
53 // Default implementations (can be overridden)
54 virtual bool available() const { return is_open() && !is_eof(); }
55 virtual fl::size_t bytes_left() const;
56
57 // Convenience: read into u8 buffer
59 return read(reinterpret_cast<char*>(dst), n); // ok reinterpret cast
60 }
62 return read(dst.data(), dst.size());
63 }
64
65 // Convenience: read RGB8 pixels (3 bytes per pixel)
67 return read(reinterpret_cast<char*>(dst.data()), dst.size() * 3) / 3; // ok reinterpret cast
68 }
69
70 // Convenience: check if n bytes are available
71 bool available(fl::size_t n) const { return bytes_left() >= n; }
72
73 // Backward-compatibility methods (non-virtual, call through to new API)
74 bool valid() const { return is_open(); }
75 fl::size_t pos() const;
76 bool seek(fl::size_t p) { return seek(p, seek_dir::beg); }
77 fl::size_t bytesLeft() const { return bytes_left(); }
78};
79
80// Owning handle to a filebuf. Declared here, beside the type it points at,
81// so anything using the interface gets the alias from the same include.
82// Several headers redeclare this identical alias for their own closure;
83// duplicate identical using-declarations are legal, so they still compile.
85
86namespace detail {
87
88// ============================================================================
89// POSIX File Buffer Implementation (uses fl::FILE* abstraction)
90// ============================================================================
91
92class posix_filebuf : public fl::filebuf {
93private:
97
98 void captureError();
99 void clearErrorState();
100
101public:
103
104 explicit posix_filebuf(const char* path, const char* mode);
105
106 ~posix_filebuf() FL_NO_EXCEPT override;
107
108 // Non-copyable
110 posix_filebuf& operator=(const posix_filebuf&) = delete;
111
112 // Moveable
114
115 posix_filebuf& operator=(posix_filebuf&& other) FL_NO_EXCEPT;
116
117 bool is_open() const override;
118
119 void close() override;
120
121 fl::size_t read(char* buffer, fl::size_t count) override;
122 using filebuf::read; // Pull in u8 overload
123
124 fl::size_t write(const char* data, fl::size_t count) override;
125
126 fl::size_t tell() override;
127
128 bool seek(fl::size_t pos, seek_dir dir) override;
129 using filebuf::seek; // Pull in single-arg overload
130
131 fl::size_t size() const override;
132
133 const char* path() const override;
134
135 bool is_eof() const override;
136
137 bool has_error() const override;
138
139 void clear_error() override;
140
141 int error_code() const override;
142
143 const char* error_message() const override;
144};
145
146} // namespace detail
147} // namespace fl
bool seek(fl::size_t pos, seek_dir dir) override
fl::size_t read(char *buffer, fl::size_t count) override
fl::size_t tell() override
posix_filebuf() FL_NO_EXCEPT
~posix_filebuf() FL_NO_EXCEPT override
const char * error_message() const override
bool is_open() const override
fl::size_t write(const char *data, fl::size_t count) override
bool has_error() const override
fl::size_t size() const override
const char * path() const override
bool is_eof() const override
int error_code() const override
fl::size_t read(fl::span< fl::u8 > dst)
Definition file_handle.h:61
virtual const char * error_message() const =0
virtual bool seek(fl::size_t pos, seek_dir dir)=0
virtual ~filebuf() FL_NO_EXCEPT=default
virtual bool is_eof() const =0
virtual bool is_open() const =0
fl::size_t pos() const
virtual bool available() const
Definition file_handle.h:54
bool available(fl::size_t n) const
Definition file_handle.h:71
virtual fl::size_t bytes_left() const
fl::size_t read(fl::u8 *dst, fl::size_t n)
Definition file_handle.h:58
virtual fl::size_t write(const char *data, fl::size_t count)=0
virtual const char * path() const =0
bool seek(fl::size_t p)
Definition file_handle.h:76
virtual fl::size_t size() const =0
fl::size_t readRGB8(fl::span< CRGB > dst)
Definition file_handle.h:66
virtual bool has_error() const =0
bool valid() const
Definition file_handle.h:74
virtual void clear_error()=0
virtual fl::size_t tell()=0
virtual void close()=0
fl::size_t bytesLeft() const
Definition file_handle.h:77
virtual fl::size_t read(char *buffer, fl::size_t count)=0
virtual int error_code() const =0
const T * data() const FL_NO_EXCEPT
Definition span.h:486
constexpr fl::size size() const FL_NO_EXCEPT
Definition span.h:483
__SIZE_TYPE__ size_t
Definition s16x16x4.h:16
unsigned char u8
Definition stdint.h:131
seek_dir
Definition file_handle.h:20
int read()
FILE_impl FILE
Definition file_io.h:33
fl::shared_ptr< filebuf > filebuf_ptr
Definition idecoder.h:15
InputGamut g FL_NO_EXCEPT
Definition rgbw.h:121
constexpr T * end(T(&array)[N]) FL_NO_EXCEPT
Base definition for an LED controller.
Definition crgb.hpp:179
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38