FastLED 3.10.8
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 FL_NO_EXCEPT = 0;
34 virtual void close() FL_NO_EXCEPT = 0;
35 virtual fl::size_t read(char* buffer, fl::size_t count) FL_NO_EXCEPT = 0;
36 virtual fl::size_t write(const char* data, fl::size_t count) FL_NO_EXCEPT = 0;
37 virtual fl::size_t tell() FL_NO_EXCEPT = 0;
38 virtual bool seek(fl::size_t pos, seek_dir dir) FL_NO_EXCEPT = 0;
39
40 // Size and path (pure virtual)
41 virtual fl::size_t size() const FL_NO_EXCEPT = 0;
42 virtual const char* path() const FL_NO_EXCEPT = 0;
43
44 // State queries (pure virtual)
45 virtual bool is_eof() const FL_NO_EXCEPT = 0;
46 virtual bool has_error() const FL_NO_EXCEPT = 0;
47 virtual void clear_error() FL_NO_EXCEPT = 0;
48
49 // Error reporting (pure virtual)
50 virtual int error_code() const FL_NO_EXCEPT = 0;
51 virtual const char* error_message() const FL_NO_EXCEPT = 0;
52
53 // Default implementations (can be overridden)
54 virtual bool available() const FL_NO_EXCEPT { return is_open() && !is_eof(); }
55 virtual fl::size_t bytes_left() const FL_NO_EXCEPT;
56
57 // Convenience: read into u8 buffer
58 fl::size_t read(fl::u8* dst, fl::size_t n) FL_NO_EXCEPT {
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 FL_NO_EXCEPT { return bytes_left() >= n; }
72
73 // Backward-compatibility methods (non-virtual, call through to new API)
74 bool valid() const FL_NO_EXCEPT { return is_open(); }
76 bool seek(fl::size_t p) FL_NO_EXCEPT { return seek(p, seek_dir::beg); }
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
100
101public:
103
104 explicit posix_filebuf(const char* path, const char* mode) FL_NO_EXCEPT;
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 FL_NO_EXCEPT override;
118
119 void close() FL_NO_EXCEPT override;
120
121 fl::size_t read(char* buffer, fl::size_t count) FL_NO_EXCEPT override;
122 using filebuf::read; // Pull in u8 overload
123
124 fl::size_t write(const char* data, fl::size_t count) FL_NO_EXCEPT override;
125
126 fl::size_t tell() FL_NO_EXCEPT override;
127
128 bool seek(fl::size_t pos, seek_dir dir) FL_NO_EXCEPT override;
129 using filebuf::seek; // Pull in single-arg overload
130
131 fl::size_t size() const FL_NO_EXCEPT override;
132
133 const char* path() const FL_NO_EXCEPT override;
134
135 bool is_eof() const FL_NO_EXCEPT override;
136
137 bool has_error() const FL_NO_EXCEPT override;
138
139 void clear_error() FL_NO_EXCEPT override;
140
141 int error_code() const FL_NO_EXCEPT override;
142
143 const char* error_message() const FL_NO_EXCEPT override;
144};
145
146} // namespace detail
147} // namespace fl
const char * path() const FL_NO_EXCEPT override
int error_code() const FL_NO_EXCEPT override
bool has_error() const FL_NO_EXCEPT override
posix_filebuf() FL_NO_EXCEPT
~posix_filebuf() FL_NO_EXCEPT override
void close() FL_NO_EXCEPT override
void captureError() FL_NO_EXCEPT
void clearErrorState() FL_NO_EXCEPT
bool is_open() const FL_NO_EXCEPT override
fl::size_t size() const FL_NO_EXCEPT override
fl::size_t tell() FL_NO_EXCEPT override
void clear_error() FL_NO_EXCEPT override
fl::size_t read(char *buffer, fl::size_t count) FL_NO_EXCEPT override
bool seek(fl::size_t pos, seek_dir dir) FL_NO_EXCEPT override
bool is_eof() const FL_NO_EXCEPT override
fl::size_t write(const char *data, fl::size_t count) FL_NO_EXCEPT override
const char * error_message() const FL_NO_EXCEPT override
bool seek(fl::size_t p) FL_NO_EXCEPT
Definition file_handle.h:76
bool valid() const FL_NO_EXCEPT
Definition file_handle.h:74
fl::size_t bytesLeft() const FL_NO_EXCEPT
Definition file_handle.h:77
virtual bool has_error() const FL_NO_EXCEPT=0
virtual bool seek(fl::size_t pos, seek_dir dir) FL_NO_EXCEPT=0
fl::size_t read(fl::span< fl::u8 > dst) FL_NO_EXCEPT
Definition file_handle.h:61
virtual bool available() const FL_NO_EXCEPT
Definition file_handle.h:54
virtual ~filebuf() FL_NO_EXCEPT=default
virtual fl::size_t size() const FL_NO_EXCEPT=0
virtual bool is_open() const FL_NO_EXCEPT=0
bool available(fl::size_t n) const FL_NO_EXCEPT
Definition file_handle.h:71
fl::size_t pos() const FL_NO_EXCEPT
fl::size_t readRGB8(fl::span< CRGB > dst) FL_NO_EXCEPT
Definition file_handle.h:66
virtual bool is_eof() const FL_NO_EXCEPT=0
virtual void close() FL_NO_EXCEPT=0
virtual const char * path() const FL_NO_EXCEPT=0
virtual const char * error_message() const FL_NO_EXCEPT=0
virtual fl::size_t tell() FL_NO_EXCEPT=0
virtual fl::size_t bytes_left() const FL_NO_EXCEPT
virtual void clear_error() FL_NO_EXCEPT=0
virtual fl::size_t read(char *buffer, fl::size_t count) FL_NO_EXCEPT=0
virtual int error_code() const FL_NO_EXCEPT=0
virtual fl::size_t write(const char *data, fl::size_t count) FL_NO_EXCEPT=0
constexpr T * end(T(&array)[N]) FL_NO_EXCEPT
__SIZE_TYPE__ size_t
Definition s16x16x4.h:16
unsigned char u8
Definition stdint.h:131
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
seek_dir
Definition file_handle.h:20
FILE_impl FILE
Definition file_io.h:35
int read() FL_NO_EXCEPT
fl::shared_ptr< filebuf > filebuf_ptr
Definition idecoder.h:15
Base definition for an LED controller.
Definition crgb.hpp:179
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38