FastLED 3.10.8
Loading...
Searching...
No Matches
file_handle.cpp.hpp
Go to the documentation of this file.
1// Implementation file for fl::filebuf and fl::detail::posix_filebuf
2// This file contains out-of-line definitions to reduce header compilation overhead
3
4#include "fl/fs/file_handle.h"
5#include "fl/stl/move.h"
6#include "fl/stl/noexcept.h"
7
8namespace fl {
9
10// ============================================================================
11// filebuf default implementations
12// ============================================================================
13
15 fl::size_t s = size();
16 // tell() is non-const in the interface, but bytes_left needs it.
17 // Use const_cast for this read-only query - tell() doesn't mutate logical state.
18 fl::size_t t = const_cast<filebuf*>(this)->tell();
19 return (t <= s) ? (s - t) : 0;
20}
21
23 return const_cast<filebuf*>(this)->tell();
24}
25
26namespace detail {
27
28// ============================================================================
29// posix_filebuf Implementation
30// ============================================================================
31
32posix_filebuf::posix_filebuf(const char* path, const char* mode)
33 FL_NO_EXCEPT : mFile(nullptr), mLastError(0), mPath(path ? path : "") {
34 mFile = fl::fopen(path, mode);
35 if (!mFile) {
37 }
38}
39
43
45 : mFile(other.mFile), mLastError(other.mLastError),
46 mPath(fl::move(other.mPath)) {
47 other.mFile = nullptr;
48 other.mLastError = 0;
49}
50
52 if (this != &other) {
53 close();
54 mFile = other.mFile;
55 mLastError = other.mLastError;
56 mPath = fl::move(other.mPath);
57 other.mFile = nullptr;
58 other.mLastError = 0;
59 }
60 return *this;
61}
62
64 return mFile != nullptr;
65}
66
68 if (mFile) {
69 int result = fl::fclose(mFile);
70 mFile = nullptr;
71 if (result != 0) {
73 } else {
74 mLastError = 0;
75 }
76 }
77}
78
80 if (!mFile) {
81 return 0;
82 }
83 fl::size_t bytes_read = fl::fread(buffer, 1, count, mFile);
84
85 // fread can return short at EOF - not an error
86 if (fl::feof(mFile) || bytes_read > 0) {
87 mLastError = 0;
88 } else if (fl::ferror(mFile)) {
90 }
91
92 return bytes_read;
93}
94
96 if (!mFile) {
98 return 0;
99 }
100 fl::size_t bytes_written = fl::fwrite(data, 1, count, mFile);
101 if (bytes_written != count) {
102 captureError();
103 } else {
105 mLastError = 0;
106 }
107 return bytes_written;
108}
109
111 if (!mFile) {
113 return 0;
114 }
115 long pos = fl::ftell(mFile);
116 if (pos < 0) {
117 captureError();
118 return 0;
119 }
120 mLastError = 0;
121 return static_cast<fl::size_t>(pos);
122}
123
125 if (!mFile) {
127 return false;
128 }
129 int whence = (dir == seek_dir::beg) ? fl::io::seek_set :
131 int result = fl::fseek(mFile, static_cast<long>(pos), whence);
132 if (result != 0) {
133 captureError();
134 return false;
135 }
136 mLastError = 0;
137 return true;
138}
139
141 if (!mFile) {
142 return 0;
143 }
144 // Save current position, seek to end, get size, restore position
145 long cur_pos = fl::ftell(mFile);
146 if (cur_pos < 0) {
147 return 0;
148 }
150 long end_pos = fl::ftell(mFile);
152 if (end_pos < 0) {
153 return 0;
154 }
155 return static_cast<fl::size_t>(end_pos);
156}
157
159 return mPath.c_str();
160}
161
163 return mFile ? (fl::feof(mFile) != 0) : false;
164}
165
167 return mLastError != 0 || (mFile && fl::ferror(mFile) != 0);
168}
169
173
175 return mLastError;
176}
177
179 if (mLastError == 0) {
180 return "No error";
181 }
182 return fl::strerror(mLastError);
183}
184
188
190 mLastError = 0;
191 if (mFile) {
193 }
194}
195
196} // namespace detail
197} // 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
posix_filebuf & operator=(const posix_filebuf &)=delete
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
virtual fl::size_t size() const FL_NO_EXCEPT=0
fl::size_t pos() const FL_NO_EXCEPT
virtual fl::size_t tell() FL_NO_EXCEPT=0
virtual fl::size_t bytes_left() const FL_NO_EXCEPT
constexpr remove_reference< T >::type && move(T &&t) FL_NO_EXCEPT
Definition move.h:30
Compile-time linker keep-alive hook for a single fl::Bus.
Definition bus_info.h:57
__SIZE_TYPE__ size_t
Definition s16x16x4.h:16
constexpr int seek_cur
Definition file_io.h:44
constexpr int err_bad_file
Definition file_io.h:48
constexpr int seek_set
Definition file_io.h:43
constexpr int seek_end
Definition file_io.h:45
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
seek_dir
Definition file_handle.h:20
int ferror(FILE *file) FL_NO_EXCEPT
Check for file error.
Definition file_io.h:292
int fseek(FILE *file, long offset, int origin) FL_NO_EXCEPT
Set file position.
Definition file_io.h:269
long ftell(FILE *file) FL_NO_EXCEPT
Get current file position.
Definition file_io.h:265
int feof(FILE *file) FL_NO_EXCEPT
Check for end-of-file.
Definition file_io.h:282
FILE * fopen(const char *path, const char *mode) FL_NO_EXCEPT
Open a file.
Definition file_io.h:249
int fclose(FILE *file) FL_NO_EXCEPT
Close a file.
Definition file_io.h:253
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
int get_errno() FL_NO_EXCEPT
Definition cerrno.h:19
int fflush(FILE *file) FL_NO_EXCEPT
Flush file buffers.
Definition file_io.h:273
fl::size_t fread(void *buffer, fl::size_t size, fl::size_t count, FILE *file) FL_NO_EXCEPT
Read from file.
Definition file_io.h:257
fl::size_t fwrite(const void *data, fl::size_t size, fl::size_t count, FILE *file) FL_NO_EXCEPT
Write to file.
Definition file_io.h:261
FASTLED_FORCE_INLINE char * strerror(int errnum) FL_NO_EXCEPT
Definition cstring.h:128
void clearerr(FILE *file) FL_NO_EXCEPT
Clear file error indicators.
Definition file_io.h:301
Base definition for an LED controller.
Definition crgb.hpp:179