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