FastLED 3.9.15
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
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 : 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
95fl::size_t posix_filebuf::write(const char* data, fl::size_t count) {
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
158const char* posix_filebuf::path() const {
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
178const char* posix_filebuf::error_message() const {
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
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 & operator=(const posix_filebuf &)=delete
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 pos() const
virtual fl::size_t bytes_left() const
virtual fl::size_t size() const =0
virtual fl::size_t tell()=0
Compile-time linker keep-alive hook for a single fl::Bus.
Definition bus_traits.h:48
constexpr remove_reference< T >::type && move(T &&t) FL_NOEXCEPT
Definition s16x16x4.h:28
__SIZE_TYPE__ size_t
Definition s16x16x4.h:16
constexpr int seek_cur
Definition file_io.h:41
constexpr int err_bad_file
Definition file_io.h:45
constexpr int seek_set
Definition file_io.h:40
constexpr int seek_end
Definition file_io.h:42
int fseek(FILE *file, long offset, int origin)
Set file position.
Definition file_io.h:266
seek_dir
Definition file_handle.h:19
int ferror(FILE *file)
Check for file error.
Definition file_io.h:289
char * strerror(int errnum) FL_NOEXCEPT
int get_errno()
Definition cerrno.h:17
int fflush(FILE *file)
Flush file buffers.
Definition file_io.h:270
void clearerr(FILE *file)
Clear file error indicators.
Definition file_io.h:298
FILE * fopen(const char *path, const char *mode)
Open a file.
Definition file_io.h:246
fl::size_t fread(void *buffer, fl::size_t size, fl::size_t count, FILE *file)
Read from file.
Definition file_io.h:254
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
int fclose(FILE *file)
Close a file.
Definition file_io.h:250
long ftell(FILE *file)
Get current file position.
Definition file_io.h:262
fl::size_t fwrite(const void *data, fl::size_t size, fl::size_t count, FILE *file)
Write to file.
Definition file_io.h:258
int feof(FILE *file)
Check for end-of-file.
Definition file_io.h:279
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT