mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-06-15 20:01:54 -07:00
ffa3996a5e
* clang-format: AllowShortEnumsOnASingleLine: false * clang-format: InsertNewlineAtEOF: true * clang-format: Standard: c++20 * clang-format: AlignConsecutiveBitFields * clang-format: AlignConsecutiveMacros * clang-format: RemoveParentheses: ReturnStatement * clang-format: RemoveSemicolon: true * Restored RemoveParentheses: Leave, retained general changes for it * formatting: fixed logging TAGs * Formatting update for dev Co-authored-by: あく <alleteam@gmail.com>
436 lines
12 KiB
C
436 lines
12 KiB
C
#include "stream.h"
|
|
#include "stream_i.h"
|
|
#include "file_stream.h"
|
|
#include <core/check.h>
|
|
#include <core/common_defines.h>
|
|
|
|
#define STREAM_BUFFER_SIZE (32U)
|
|
|
|
void stream_free(Stream* stream) {
|
|
furi_check(stream);
|
|
stream->vtable->free(stream);
|
|
}
|
|
|
|
void stream_clean(Stream* stream) {
|
|
furi_check(stream);
|
|
stream->vtable->clean(stream);
|
|
}
|
|
|
|
bool stream_eof(Stream* stream) {
|
|
furi_check(stream);
|
|
return stream->vtable->eof(stream);
|
|
}
|
|
|
|
bool stream_seek(Stream* stream, int32_t offset, StreamOffset offset_type) {
|
|
furi_check(stream);
|
|
return stream->vtable->seek(stream, offset, offset_type);
|
|
}
|
|
|
|
static bool stream_seek_to_char_forward(Stream* stream, char c) {
|
|
// Search is starting from seconds character
|
|
if(!stream_seek(stream, 1, StreamOffsetFromCurrent)) {
|
|
return false;
|
|
}
|
|
|
|
// Search character in a stream
|
|
bool result = false;
|
|
while(!result) {
|
|
uint8_t buffer[STREAM_BUFFER_SIZE] = {0};
|
|
size_t ret = stream_read(stream, buffer, STREAM_BUFFER_SIZE);
|
|
for(size_t i = 0; i < ret; i++) {
|
|
if(buffer[i] == c) {
|
|
stream_seek(stream, (int32_t)i - ret, StreamOffsetFromCurrent);
|
|
result = true;
|
|
break;
|
|
}
|
|
}
|
|
if(ret != STREAM_BUFFER_SIZE) break;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static bool stream_seek_to_char_backward(Stream* stream, char c) {
|
|
size_t anchor = stream_tell(stream);
|
|
|
|
// Special case, no previous characters
|
|
if(anchor == 0) {
|
|
return false;
|
|
}
|
|
|
|
bool result = false;
|
|
while(!result) {
|
|
// Seek back
|
|
uint8_t buffer[STREAM_BUFFER_SIZE] = {0};
|
|
size_t to_read = STREAM_BUFFER_SIZE;
|
|
if(to_read > anchor) {
|
|
to_read = anchor;
|
|
}
|
|
|
|
anchor -= to_read;
|
|
furi_check(stream_seek(stream, anchor, StreamOffsetFromStart));
|
|
|
|
size_t ret = stream_read(stream, buffer, to_read);
|
|
for(size_t i = 0; i < ret; i++) {
|
|
size_t cursor = ret - i - 1;
|
|
if(buffer[cursor] == c) {
|
|
result = true;
|
|
furi_check(stream_seek(stream, anchor + cursor, StreamOffsetFromStart));
|
|
break;
|
|
} else {
|
|
}
|
|
}
|
|
if(ret != STREAM_BUFFER_SIZE) break;
|
|
}
|
|
returnresult;
|
|
}
|
|
|
|
bool stream_seek_to_char(Stream* stream, char c, StreamDirection direction) {
|
|
furi_check(stream);
|
|
|
|
const |