FastLED 3.9.3
Loading...
Searching...
No Matches
filebuffer.cpp
1
2
3#include "filebuffer.h"
4
5#include "namespace.h"
6
7FASTLED_NAMESPACE_BEGIN
8
9FileBuffer::FileBuffer(FileHandleRef fh) {
10 mFile = fh;
11 ResetBuffer();
12}
13
14FileBuffer::~FileBuffer() {
15}
16
17void FileBuffer::rewindToStart() {
18 mFile->seek(0);
19 RefillBuffer();
20}
21
22bool FileBuffer::available() const {
23 if (mCurrIdx != mLength) {
24 // we still have buffer to read.
25 return true;
26 }
27 if (!mFile) {
28 // no file to read from.
29 return false;
30 }
31 return mFile->available();
32}
33
34int32_t FileBuffer::BytesLeft() const {
35 if (!available()) {
36 return -1;
37 }
38 const int32_t remaining_buffer = mLength - mCurrIdx;
39 const int32_t remaining_disk = mFile->size() - mFile->pos();
40 return remaining_buffer + remaining_disk;
41}
42
43int32_t FileBuffer::FileSize() const {
44 if (!available()) {
45 return -1;
46 }
47 return mFile->size();
48}
49
50int16_t FileBuffer::read() {
51 RefillBufferIfNecessary();
52 if (mCurrIdx == mLength) {
53 return -1;
54 }
55 // main case.
56 uint8_t output = mBuffer[mCurrIdx++];
57 return output;
58}
59
60size_t FileBuffer::read(uint8_t* dst, size_t n) {
61 size_t bytes_read = 0;
62 for (size_t i = 0; i < n; i++) {
63 int16_t next_byte = read();
64 if (next_byte == -1) {
65 break;
66 }
67 dst[bytes_read++] = static_cast<uint8_t>(next_byte);
68 }
69 return bytes_read;
70}
71
72void FileBuffer::ResetBuffer() {
73 mLength = -1;
74 mCurrIdx = -1;
75}
76
77void FileBuffer::RefillBufferIfNecessary() {
78 if (mCurrIdx == mLength) {
79 RefillBuffer();
80 }
81}
82
83void FileBuffer::RefillBuffer() {
84 if (!mFile->available()) {
85 // ERR_PRINTLN("RefillBuffer FAILED");
86 } else {
87 // Needs more buffer yo.
88 mLength = mFile->read(mBuffer, kBufferSize);
89 mCurrIdx = 0;
90 }
91}
92
93FASTLED_NAMESPACE_END