[FL-2655, FL-2650] Buffered file streams (#1424)
* Initial buffered file stream implementation * Fix logical errors * Fix more logical errors * Minimally working implementation * Adapt infrared unit tests for buffered streams * Increase read buffer size from 512 to 1K * Correct naming and formatting * More code improvements * Allow passing access and open modes for buffered streams * Implement tests for buffered streams * Better file and method names * Add comments and correct formatting * Use buffered streams in Infrared * Fix compilation error
This commit is contained in:
71
lib/toolbox/stream/stream_cache.c
Normal file
71
lib/toolbox/stream/stream_cache.c
Normal file
@@ -0,0 +1,71 @@
|
||||
#include "stream_cache.h"
|
||||
|
||||
#define STREAM_CACHE_MAX_SIZE 1024U
|
||||
|
||||
struct StreamCache {
|
||||
uint8_t data[STREAM_CACHE_MAX_SIZE];
|
||||
size_t data_size;
|
||||
size_t position;
|
||||
};
|
||||
|
||||
StreamCache* stream_cache_alloc() {
|
||||
StreamCache* cache = malloc(sizeof(StreamCache));
|
||||
cache->data_size = 0;
|
||||
cache->position = 0;
|
||||
return cache;
|
||||
}
|
||||
void stream_cache_free(StreamCache* cache) {
|
||||
furi_assert(cache);
|
||||
cache->data_size = 0;
|
||||
cache->position = 0;
|
||||
free(cache);
|
||||
}
|
||||
|
||||
void stream_cache_drop(StreamCache* cache) {
|
||||
cache->data_size = 0;
|
||||
cache->position = 0;
|
||||
}
|
||||
|
||||
bool stream_cache_at_end(StreamCache* cache) {
|
||||
furi_assert(cache->data_size >= cache->position);
|
||||
return cache->data_size == cache->position;
|
||||
}
|
||||
|
||||
size_t stream_cache_size(StreamCache* cache) {
|
||||
return cache->data_size;
|
||||
}
|
||||
|
||||
size_t stream_cache_pos(StreamCache* cache) {
|
||||
return cache->position;
|
||||
}
|
||||
|
||||
size_t stream_cache_fill(StreamCache* cache, Stream* stream) {
|
||||
const size_t size_read = stream_read(stream, cache->data, STREAM_CACHE_MAX_SIZE);
|
||||
cache->data_size = size_read;
|
||||
cache->position = 0;
|
||||
return size_read;
|
||||
}
|
||||
|
||||
size_t stream_cache_read(StreamCache* cache, uint8_t* data, size_t size) {
|
||||
furi_assert(cache->data_size >= cache->position);
|
||||
const size_t size_read = MIN(size, cache->data_size - cache->position);
|
||||
if(size_read > 0) {
|
||||
memcpy(data, cache->data + cache->position, size_read);
|
||||
cache->position += size_read;
|
||||
}
|
||||
return size_read;
|
||||
}
|
||||
|
||||
int32_t stream_cache_seek(StreamCache* cache, int32_t offset) {
|
||||
furi_assert(cache->data_size >= cache->position);
|
||||
int32_t actual_offset = 0;
|
||||
|
||||
if(offset > 0) {
|
||||
actual_offset = MIN(cache->data_size - cache->position, (size_t)offset);
|
||||
} else if(offset < 0) {
|
||||
actual_offset = -MIN(cache->position, (size_t)abs(offset));
|
||||
}
|
||||
|
||||
cache->position += actual_offset;
|
||||
return actual_offset;
|
||||
}
|
Reference in New Issue
Block a user