[FL-2693] RW buffered streams (#1523)

* Add write methods for stream cache
* Fix logical error
* Implement write cache for buffered file streams
* Minor code refactoring
* Less ugly code
* Better read() implementation
* Intermediate implementation
* Fix logical error
* Code cleanup
* Update FFF comments
* Fix logical error
* Github: rsync with delete

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
Georgii Surkov
2022-08-03 19:43:14 +03:00
committed by GitHub
parent 6499597586
commit 135fbd294b
6 changed files with 156 additions and 35 deletions

View File

@@ -46,6 +46,14 @@ size_t stream_cache_fill(StreamCache* cache, Stream* stream) {
return size_read;
}
bool stream_cache_flush(StreamCache* cache, Stream* stream) {
const size_t size_written = stream_write(stream, cache->data, cache->data_size);
const bool success = (size_written == cache->data_size);
cache->data_size = 0;
cache->position = 0;
return success;
}
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);
@@ -56,6 +64,19 @@ size_t stream_cache_read(StreamCache* cache, uint8_t* data, size_t size) {
return size_read;
}
size_t stream_cache_write(StreamCache* cache, const uint8_t* data, size_t size) {
furi_assert(cache->data_size >= cache->position);
const size_t size_written = MIN(size, STREAM_CACHE_MAX_SIZE - cache->position);
if(size_written > 0) {
memcpy(cache->data + cache->position, data, size_written);
cache->position += size_written;
if(cache->position > cache->data_size) {
cache->data_size = cache->position;
}
}
return size_written;
}
int32_t stream_cache_seek(StreamCache* cache, int32_t offset) {
furi_assert(cache->data_size >= cache->position);
int32_t actual_offset = 0;
@@ -63,7 +84,7 @@ int32_t stream_cache_seek(StreamCache* cache, int32_t offset) {
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));
actual_offset = MAX(-((int32_t)cache->position), offset);
}
cache->position += actual_offset;