[FL-2274] Inventing streams and moving FFF to them (#981)
* Streams: string stream * String stream: updated insert/delete api * Streams: generic stream interface and string stream implementation * Streams: helpers for insert and delete_and_insert * FFF: now compatible with streams * MinUnit: introduced tests with arguments * FFF: stream access violation * Streams: copy data between streams * Streams: file stream * FFF: documentation * FFStream: documentation * FFF: alloc as file * MinUnit: support for nested tests * Streams: changed delete_and_insert, now it returns success flag. Added ability dump stream inner parameters and data to cout. * FFF: simplified file open function * Streams: unit tests * FFF: tests * Streams: declare cache_size constant as define, to allow variable modified arrays * FFF: lib moved to a separate folder * iButton: new FFF * RFID: new FFF * Animations: new FFF * IR: new FFF * NFC: new FFF * Flipper file format: delete lib * U2F: new FFF * Subghz: new FFF and streams * Streams: read line * Streams: split * FuriCore: implement memset with extra asserts * FuriCore: implement extra heap asserts without inventing memset * Scene manager: protected access to the scene id stack with a size check * NFC worker: dirty fix for issue where hal_nfc was busy on app start * Furi: update allocator to erase memory on allocation. Replace furi_alloc with malloc. * FuriCore: cleanup memmgr code. * Furi HAL: furi_hal_init is split into critical and non-critical parts. The critical part is currently clock and console. * Memmgr: added ability to track allocations and deallocations through console. * FFStream: some speedup * Streams, FF: minor fixes * Tests: restore * File stream: a slightly more thread-safe version of file_stream_delete_and_insert Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
@@ -67,7 +67,7 @@ bool saved_struct_load(const char* path, void* data, size_t size, uint8_t magic,
|
||||
|
||||
SavedStructHeader header;
|
||||
|
||||
uint8_t* data_read = furi_alloc(size);
|
||||
uint8_t* data_read = malloc(size);
|
||||
Storage* storage = furi_record_open("storage");
|
||||
File* file = storage_file_alloc(storage);
|
||||
bool result = true;
|
||||
|
224
lib/toolbox/stream/file_stream.c
Normal file
224
lib/toolbox/stream/file_stream.c
Normal file
@@ -0,0 +1,224 @@
|
||||
#include "stream.h"
|
||||
#include "stream_i.h"
|
||||
#include "file_stream.h"
|
||||
|
||||
typedef struct {
|
||||
Stream stream_base;
|
||||
Storage* storage;
|
||||
File* file;
|
||||
} FileStream;
|
||||
|
||||
static void file_stream_free(FileStream* stream);
|
||||
static bool file_stream_eof(FileStream* stream);
|
||||
static void file_stream_clean(FileStream* stream);
|
||||
static bool file_stream_seek(FileStream* stream, int32_t offset, StreamOffset offset_type);
|
||||
static size_t file_stream_tell(FileStream* stream);
|
||||
static size_t file_stream_size(FileStream* stream);
|
||||
static size_t file_stream_write(FileStream* stream, const uint8_t* data, size_t size);
|
||||
static size_t file_stream_read(FileStream* stream, uint8_t* data, size_t size);
|
||||
static bool file_stream_delete_and_insert(
|
||||
FileStream* stream,
|
||||
size_t delete_size,
|
||||
StreamWriteCB write_callback,
|
||||
const void* ctx);
|
||||
|
||||
const StreamVTable file_stream_vtable = {
|
||||
.free = (StreamFreeFn)file_stream_free,
|
||||
.eof = (StreamEOFFn)file_stream_eof,
|
||||
.clean = (StreamCleanFn)file_stream_clean,
|
||||
.seek = (StreamSeekFn)file_stream_seek,
|
||||
.tell = (StreamTellFn)file_stream_tell,
|
||||
.size = (StreamSizeFn)file_stream_size,
|
||||
.write = (StreamWriteFn)file_stream_write,
|
||||
.read = (StreamReadFn)file_stream_read,
|
||||
.delete_and_insert = (StreamDeleteAndInsertFn)file_stream_delete_and_insert,
|
||||
};
|
||||
|
||||
Stream* file_stream_alloc(Storage* storage) {
|
||||
FileStream* stream = malloc(sizeof(FileStream));
|
||||
stream->file = storage_file_alloc(storage);
|
||||
stream->storage = storage;
|
||||
|
||||
stream->stream_base.vtable = &file_stream_vtable;
|
||||
return (Stream*)stream;
|
||||
}
|
||||
|
||||
bool file_stream_open(
|
||||
Stream* _stream,
|
||||
const char* path,
|
||||
FS_AccessMode access_mode,
|
||||
FS_OpenMode open_mode) {
|
||||
furi_assert(_stream);
|
||||
FileStream* stream = (FileStream*)_stream;
|
||||
furi_check(stream->stream_base.vtable == &file_stream_vtable);
|
||||
return storage_file_open(stream->file, path, access_mode, open_mode);
|
||||
}
|
||||
|
||||
bool file_stream_close(Stream* _stream) {
|
||||
furi_assert(_stream);
|
||||
FileStream* stream = (FileStream*)_stream;
|
||||
furi_check(stream->stream_base.vtable == &file_stream_vtable);
|
||||
return storage_file_close(stream->file);
|
||||
}
|
||||
|
||||
static void file_stream_free(FileStream* stream) {
|
||||
storage_file_free(stream->file);
|
||||
free(stream);
|
||||
}
|
||||
|
||||
static bool file_stream_eof(FileStream* stream) {
|
||||
return storage_file_eof(stream->file);
|
||||
}
|
||||
|
||||
static void file_stream_clean(FileStream* stream) {
|
||||
storage_file_seek(stream->file, 0, true);
|
||||
storage_file_truncate(stream->file);
|
||||
}
|
||||
|
||||
static bool file_stream_seek(FileStream* stream, int32_t offset, StreamOffset offset_type) {
|
||||
bool result = false;
|
||||
size_t seek_position = 0;
|
||||
size_t current_position = file_stream_tell(stream);
|
||||
size_t size = file_stream_size(stream);
|
||||
|
||||
// calc offset and limit to bottom
|
||||
switch(offset_type) {
|
||||
case StreamOffsetFromCurrent: {
|
||||
if((int32_t)(current_position + offset) >= 0) {
|
||||
seek_position = current_position + offset;
|
||||
result = true;
|
||||
}
|
||||
} break;
|
||||
case StreamOffsetFromStart: {
|
||||
if(offset >= 0) {
|
||||
seek_position = offset;
|
||||
result = true;
|
||||
}
|
||||
} break;
|
||||
case StreamOffsetFromEnd: {
|
||||
if((int32_t)(size + offset) >= 0) {
|
||||
seek_position = size + offset;
|
||||
result = true;
|
||||
}
|
||||
} break;
|
||||
}
|
||||
|
||||
if(result) {
|
||||
// limit to top
|
||||
if((int32_t)(seek_position - size) > 0) {
|
||||
storage_file_seek(stream->file, size, true);
|
||||
result = false;
|
||||
} else {
|
||||
result = storage_file_seek(stream->file, seek_position, true);
|
||||
}
|
||||
} else {
|
||||
storage_file_seek(stream->file, 0, true);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static size_t file_stream_tell(FileStream* stream) {
|
||||
return storage_file_tell(stream->file);
|
||||
}
|
||||
|
||||
static size_t file_stream_size(FileStream* stream) {
|
||||
return storage_file_size(stream->file);
|
||||
}
|
||||
|
||||
static size_t file_stream_write(FileStream* stream, const uint8_t* data, size_t size) {
|
||||
// TODO cache
|
||||
size_t need_to_write = size;
|
||||
while(need_to_write > 0) {
|
||||
uint16_t was_written =
|
||||
storage_file_write(stream->file, data + (size - need_to_write), need_to_write);
|
||||
need_to_write -= was_written;
|
||||
|
||||
if(was_written == 0) break;
|
||||
}
|
||||
|
||||
return size - need_to_write;
|
||||
}
|
||||
|
||||
static size_t file_stream_read(FileStream* stream, uint8_t* data, size_t size) {
|
||||
// TODO cache
|
||||
size_t need_to_read = size;
|
||||
while(need_to_read > 0) {
|
||||
uint16_t was_read =
|
||||
storage_file_read(stream->file, data + (size - need_to_read), need_to_read);
|
||||
need_to_read -= was_read;
|
||||
|
||||
if(was_read == 0) break;
|
||||
}
|
||||
|
||||
return size - need_to_read;
|
||||
}
|
||||
|
||||
static bool file_stream_delete_and_insert(
|
||||
FileStream* _stream,
|
||||
size_t delete_size,
|
||||
StreamWriteCB write_callback,
|
||||
const void* ctx) {
|
||||
bool result = false;
|
||||
Stream* stream = (Stream*)_stream;
|
||||
|
||||
// open scratchpad
|
||||
Stream* scratch_stream = file_stream_alloc(_stream->storage);
|
||||
|
||||
// TODO: we need something like "storage_open_tmpfile and storage_close_tmpfile"
|
||||
string_t scratch_name;
|
||||
string_t tmp_name;
|
||||
string_init(tmp_name);
|
||||
storage_get_next_filename(_stream->storage, "/any", ".scratch", ".pad", tmp_name);
|
||||
string_init_printf(scratch_name, "/any/%s.pad", string_get_cstr(tmp_name));
|
||||
string_clear(tmp_name);
|
||||
|
||||
do {
|
||||
if(!file_stream_open(
|
||||
scratch_stream, string_get_cstr(scratch_name), FSAM_READ_WRITE, FSOM_CREATE_NEW))
|
||||
break;
|
||||
|
||||
size_t current_position = stream_tell(stream);
|
||||
size_t file_size = stream_size(stream);
|
||||
|
||||
size_t size_to_delete = file_size - current_position;
|
||||
size_to_delete = MIN(delete_size, size_to_delete);
|
||||
|
||||
size_t size_to_copy_before = current_position;
|
||||
size_t size_to_copy_after = file_size - current_position - size_to_delete;
|
||||
|
||||
// copy file from 0 to insert position to scratchpad
|
||||
if(!stream_rewind(stream)) break;
|
||||
if(stream_copy(stream, scratch_stream, size_to_copy_before) != size_to_copy_before) break;
|
||||
|
||||
if(write_callback) {
|
||||
if(!write_callback(scratch_stream, ctx)) break;
|
||||
}
|
||||
size_t new_position = stream_tell(scratch_stream);
|
||||
|
||||
// copy key file after insert position + size_to_delete to scratchpad
|
||||
if(!stream_seek(stream, size_to_delete, StreamOffsetFromCurrent)) break;
|
||||
if(stream_copy(stream, scratch_stream, size_to_copy_after) != size_to_copy_after) break;
|
||||
|
||||
size_t new_file_size = stream_size(scratch_stream);
|
||||
|
||||
// copy whole scratchpad file to the original file
|
||||
if(!stream_rewind(stream)) break;
|
||||
if(!stream_rewind(scratch_stream)) break;
|
||||
if(stream_copy(scratch_stream, stream, new_file_size) != new_file_size) break;
|
||||
|
||||
// and truncate original file
|
||||
if(!storage_file_truncate(_stream->file)) break;
|
||||
|
||||
// move seek pointer at insert end
|
||||
if(!stream_seek(stream, new_position, StreamOffsetFromStart)) break;
|
||||
|
||||
result = true;
|
||||
} while(false);
|
||||
|
||||
stream_free(scratch_stream);
|
||||
storage_common_remove(_stream->storage, string_get_cstr(scratch_name));
|
||||
string_clear(scratch_name);
|
||||
|
||||
return result;
|
||||
}
|
40
lib/toolbox/stream/file_stream.h
Normal file
40
lib/toolbox/stream/file_stream.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
#include <stdlib.h>
|
||||
#include <storage/storage.h>
|
||||
#include "stream.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Allocate file stream
|
||||
* @return Stream*
|
||||
*/
|
||||
Stream* file_stream_alloc(Storage* storage);
|
||||
|
||||
/**
|
||||
* Opens an existing file or create a new one.
|
||||
* @param stream pointer to file stream object.
|
||||
* @param path path to file
|
||||
* @param access_mode access mode from FS_AccessMode
|
||||
* @param open_mode open mode from FS_OpenMode
|
||||
* @return success flag. You need to close the file even if the open operation failed.
|
||||
*/
|
||||
bool file_stream_open(
|
||||
Stream* stream,
|
||||
const char* path,
|
||||
FS_AccessMode access_mode,
|
||||
FS_OpenMode open_mode);
|
||||
|
||||
/**
|
||||
* Closes the file.
|
||||
* @param stream
|
||||
* @return true
|
||||
* @return false
|
||||
*/
|
||||
bool file_stream_close(Stream* stream);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
337
lib/toolbox/stream/stream.c
Normal file
337
lib/toolbox/stream/stream.c
Normal file
@@ -0,0 +1,337 @@
|
||||
#include "stream.h"
|
||||
#include "stream_i.h"
|
||||
#include "file_stream.h"
|
||||
#include <furi/check.h>
|
||||
#include <furi/common_defines.h>
|
||||
|
||||
void stream_free(Stream* stream) {
|
||||
furi_assert(stream);
|
||||
stream->vtable->free(stream);
|
||||
}
|
||||
|
||||
void stream_clean(Stream* stream) {
|
||||
furi_assert(stream);
|
||||
stream->vtable->clean(stream);
|
||||
}
|
||||
|
||||
bool stream_eof(Stream* stream) {
|
||||
furi_assert(stream);
|
||||
return stream->vtable->eof(stream);
|
||||
}
|
||||
|
||||
bool stream_seek(Stream* stream, int32_t offset, StreamOffset offset_type) {
|
||||
furi_assert(stream);
|
||||
return stream->vtable->seek(stream, offset, offset_type);
|
||||
}
|
||||
|
||||
size_t stream_tell(Stream* stream) {
|
||||
furi_assert(stream);
|
||||
return stream->vtable->tell(stream);
|
||||
}
|
||||
|
||||
size_t stream_size(Stream* stream) {
|
||||
furi_assert(stream);
|
||||
return stream->vtable->size(stream);
|
||||
}
|
||||
|
||||
size_t stream_write(Stream* stream, const uint8_t* data, size_t size) {
|
||||
furi_assert(stream);
|
||||
return stream->vtable->write(stream, data, size);
|
||||
}
|
||||
|
||||
size_t stream_read(Stream* stream, uint8_t* data, size_t size) {
|
||||
furi_assert(stream);
|
||||
return stream->vtable->read(stream, data, size);
|
||||
}
|
||||
|
||||
bool stream_delete_and_insert(
|
||||
Stream* stream,
|
||||
size_t delete_size,
|
||||
StreamWriteCB write_callback,
|
||||
const void* ctx) {
|
||||
furi_assert(stream);
|
||||
return stream->vtable->delete_and_insert(stream, delete_size, write_callback, ctx);
|
||||
}
|
||||
|
||||
/********************************** Some random helpers starts here **********************************/
|
||||
|
||||
typedef struct {
|
||||
const uint8_t* data;
|
||||
size_t size;
|
||||
} StreamWriteData;
|
||||
|
||||
static bool stream_write_struct(Stream* stream, const void* context) {
|
||||
furi_assert(stream);
|
||||
furi_assert(context);
|
||||
const StreamWriteData* write_data = context;
|
||||
return (stream_write(stream, write_data->data, write_data->size) == write_data->size);
|
||||
}
|
||||
|
||||
bool stream_read_line(Stream* stream, string_t str_result) {
|
||||
string_reset(str_result);
|
||||
const uint8_t buffer_size = 32;
|
||||
uint8_t buffer[buffer_size];
|
||||
|
||||
do {
|
||||
uint16_t bytes_were_read = stream_read(stream, buffer, buffer_size);
|
||||
// TODO process EOF
|
||||
if(bytes_were_read == 0) break;
|
||||
|
||||
bool result = false;
|
||||
bool error = false;
|
||||
for(uint16_t i = 0; i < bytes_were_read; i++) {
|
||||
if(buffer[i] == '\n') {
|
||||
if(!stream_seek(stream, i - bytes_were_read, StreamOffsetFromCurrent)) {
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
|
||||
result = true;
|
||||
break;
|
||||
} else if(buffer[i] == '\r') {
|
||||
// Ignore
|
||||
} else {
|
||||
string_push_back(str_result, buffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if(result || error) {
|
||||
break;
|
||||
}
|
||||
} while(true);
|
||||
|
||||
return string_size(str_result) != 0;
|
||||
}
|
||||
|
||||
bool stream_rewind(Stream* stream) {
|
||||
furi_assert(stream);
|
||||
return stream_seek(stream, 0, StreamOffsetFromStart);
|
||||
}
|
||||
|
||||
size_t stream_write_char(Stream* stream, char c) {
|
||||
furi_assert(stream);
|
||||
return stream_write(stream, (const uint8_t*)&c, 1);
|
||||
}
|
||||
|
||||
size_t stream_write_string(Stream* stream, string_t string) {
|
||||
furi_assert(stream);
|
||||
return stream_write(stream, (const uint8_t*)string_get_cstr(string), string_size(string));
|
||||
}
|
||||
|
||||
size_t stream_write_cstring(Stream* stream, const char* string) {
|
||||
furi_assert(stream);
|
||||
return stream_write(stream, (const uint8_t*)string, strlen(string));
|
||||
}
|
||||
|
||||
size_t stream_write_format(Stream* stream, const char* format, ...) {
|
||||
furi_assert(stream);
|
||||
size_t size;
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
size = stream_write_vaformat(stream, format, args);
|
||||
va_end(args);
|
||||
return size;
|
||||
}
|
||||
|
||||
size_t stream_write_vaformat(Stream* stream, const char* format, va_list args) {
|
||||
furi_assert(stream);
|
||||
string_t data;
|
||||
string_init_vprintf(data, format, args);
|
||||
size_t size = stream_write_string(stream, data);
|
||||
string_clear(data);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
bool stream_insert(Stream* stream, const uint8_t* data, size_t size) {
|
||||
furi_assert(stream);
|
||||
StreamWriteData write_data = {.data = data, .size = size};
|
||||
return stream_delete_and_insert(stream, 0, stream_write_struct, &write_data);
|
||||
}
|
||||
|
||||
bool stream_insert_char(Stream* stream, char c) {
|
||||
furi_assert(stream);
|
||||
return stream_delete_and_insert_char(stream, 0, c);
|
||||
}
|
||||
|
||||
bool stream_insert_string(Stream* stream, string_t string) {
|
||||
furi_assert(stream);
|
||||
return stream_delete_and_insert_string(stream, 0, string);
|
||||
}
|
||||
|
||||
bool stream_insert_cstring(Stream* stream, const char* string) {
|
||||
furi_assert(stream);
|
||||
return stream_delete_and_insert_cstring(stream, 0, string);
|
||||
}
|
||||
|
||||
bool stream_insert_format(Stream* stream, const char* format, ...) {
|
||||
furi_assert(stream);
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
bool result = stream_insert_vaformat(stream, format, args);
|
||||
va_end(args);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool stream_insert_vaformat(Stream* stream, const char* format, va_list args) {
|
||||
furi_assert(stream);
|
||||
return stream_delete_and_insert_vaformat(stream, 0, format, args);
|
||||
}
|
||||
|
||||
bool stream_delete_and_insert_char(Stream* stream, size_t delete_size, char c) {
|
||||
furi_assert(stream);
|
||||
StreamWriteData write_data = {.data = (uint8_t*)&c, .size = 1};
|
||||
return stream_delete_and_insert(stream, delete_size, stream_write_struct, &write_data);
|
||||
}
|
||||
|
||||
bool stream_delete_and_insert_string(Stream* stream, size_t delete_size, string_t string) {
|
||||
furi_assert(stream);
|
||||
StreamWriteData write_data = {
|
||||
.data = (uint8_t*)string_get_cstr(string), .size = string_size(string)};
|
||||
return stream_delete_and_insert(stream, delete_size, stream_write_struct, &write_data);
|
||||
}
|
||||
|
||||
bool stream_delete_and_insert_cstring(Stream* stream, size_t delete_size, const char* string) {
|
||||
furi_assert(stream);
|
||||
StreamWriteData write_data = {.data = (uint8_t*)string, .size = strlen(string)};
|
||||
return stream_delete_and_insert(stream, delete_size, stream_write_struct, &write_data);
|
||||
}
|
||||
|
||||
bool stream_delete_and_insert_format(Stream* stream, size_t delete_size, const char* format, ...) {
|
||||
furi_assert(stream);
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
bool result = stream_delete_and_insert_vaformat(stream, delete_size, format, args);
|
||||
va_end(args);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool stream_delete_and_insert_vaformat(
|
||||
Stream* stream,
|
||||
size_t delete_size,
|
||||
const char* format,
|
||||
va_list args) {
|
||||
furi_assert(stream);
|
||||
string_t data;
|
||||
string_init_vprintf(data, format, args);
|
||||
StreamWriteData write_data = {
|
||||
.data = (uint8_t*)string_get_cstr(data), .size = string_size(data)};
|
||||
bool result = stream_delete_and_insert(stream, 0, stream_write_struct, &write_data);
|
||||
string_clear(data);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool stream_delete(Stream* stream, size_t size) {
|
||||
furi_assert(stream);
|
||||
return stream_delete_and_insert(stream, size, NULL, NULL);
|
||||
}
|
||||
|
||||
size_t stream_copy(Stream* stream_from, Stream* stream_to, size_t size) {
|
||||
uint8_t* buffer = malloc(STREAM_CACHE_SIZE);
|
||||
size_t copied = 0;
|
||||
|
||||
do {
|
||||
size_t bytes_count = MIN(STREAM_CACHE_SIZE, size - copied);
|
||||
if(bytes_count <= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
uint16_t bytes_were_read = stream_read(stream_from, buffer, bytes_count);
|
||||
if(bytes_were_read != bytes_count) break;
|
||||
|
||||
uint16_t bytes_were_written = stream_write(stream_to, buffer, bytes_count);
|
||||
if(bytes_were_written != bytes_count) break;
|
||||
|
||||
copied += bytes_count;
|
||||
} while(true);
|
||||
|
||||
free(buffer);
|
||||
return copied;
|
||||
}
|
||||
|
||||
size_t stream_copy_full(Stream* stream_from, Stream* stream_to) {
|
||||
size_t was_written = 0;
|
||||
|
||||
do {
|
||||
if(!stream_seek(stream_from, 0, StreamOffsetFromStart)) break;
|
||||
if(!stream_seek(stream_to, 0, StreamOffsetFromStart)) break;
|
||||
was_written = stream_copy(stream_from, stream_to, stream_size(stream_from));
|
||||
} while(false);
|
||||
|
||||
return was_written;
|
||||
}
|
||||
|
||||
bool stream_split(Stream* stream, Stream* stream_left, Stream* stream_right) {
|
||||
bool result = false;
|
||||
size_t size = stream_size(stream);
|
||||
size_t tell = stream_tell(stream);
|
||||
|
||||
do {
|
||||
// copy right
|
||||
if(stream_copy(stream, stream_right, size - tell) != (size - tell)) break;
|
||||
|
||||
// copy left
|
||||
if(!stream_rewind(stream)) break;
|
||||
if(stream_copy(stream, stream_left, tell) != tell) break;
|
||||
|
||||
// restore RW pointer
|
||||
if(!stream_seek(stream, tell, StreamOffsetFromStart)) break;
|
||||
result = true;
|
||||
} while(false);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t stream_load_from_file(Stream* stream, Storage* storage, const char* path) {
|
||||
size_t was_written = 0;
|
||||
Stream* file = file_stream_alloc(storage);
|
||||
|
||||
do {
|
||||
if(!file_stream_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING)) break;
|
||||
was_written = stream_copy(file, stream, stream_size(file));
|
||||
} while(false);
|
||||
|
||||
stream_free(file);
|
||||
return was_written;
|
||||
}
|
||||
|
||||
size_t stream_save_to_file(Stream* stream, Storage* storage, const char* path, FS_OpenMode mode) {
|
||||
size_t was_written = 0;
|
||||
Stream* file = file_stream_alloc(storage);
|
||||
|
||||
do {
|
||||
if(!file_stream_open(file, path, FSAM_WRITE, mode)) break;
|
||||
was_written = stream_copy(stream, file, stream_size(stream));
|
||||
} while(false);
|
||||
|
||||
stream_free(file);
|
||||
return was_written;
|
||||
}
|
||||
|
||||
void stream_dump_data(Stream* stream) {
|
||||
size_t size = stream_size(stream);
|
||||
size_t tell = stream_tell(stream);
|
||||
printf("stream %p\r\n", stream);
|
||||
printf("size = %u\r\n", size);
|
||||
printf("tell = %u\r\n", tell);
|
||||
printf("DATA START\r\n");
|
||||
uint8_t* data = malloc(STREAM_CACHE_SIZE);
|
||||
stream_rewind(stream);
|
||||
|
||||
while(true) {
|
||||
size_t was_read = stream_read(stream, data, STREAM_CACHE_SIZE);
|
||||
if(was_read == 0) break;
|
||||
|
||||
for(size_t i = 0; i < was_read; i++) {
|
||||
printf("%c", data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
free(data);
|
||||
printf("\r\n");
|
||||
printf("DATA END\r\n");
|
||||
stream_seek(stream, tell, StreamOffsetFromStart);
|
||||
}
|
336
lib/toolbox/stream/stream.h
Normal file
336
lib/toolbox/stream/stream.h
Normal file
@@ -0,0 +1,336 @@
|
||||
#pragma once
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <mlib/m-string.h>
|
||||
#include <storage/storage.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct Stream Stream;
|
||||
|
||||
typedef enum {
|
||||
StreamOffsetFromCurrent,
|
||||
StreamOffsetFromStart,
|
||||
StreamOffsetFromEnd,
|
||||
} StreamOffset;
|
||||
|
||||
typedef bool (*StreamWriteCB)(Stream* stream, const void* context);
|
||||
|
||||
/**
|
||||
* Free Stream
|
||||
* @param stream Stream instance
|
||||
*/
|
||||
void stream_free(Stream* stream);
|
||||
|
||||
/**
|
||||
* Clean (empty) Stream
|
||||
* @param stream Stream instance
|
||||
*/
|
||||
void stream_clean(Stream* stream);
|
||||
|
||||
/**
|
||||
* Indicates that the rw pointer is at the end of the stream
|
||||
* @param stream Stream instance
|
||||
* @return true if rw pointer is at the end of the stream
|
||||
* @return false if rw pointer is not at the end of the stream
|
||||
*/
|
||||
bool stream_eof(Stream* stream);
|
||||
|
||||
/**
|
||||
* Moves the rw pointer.
|
||||
* @param stream Stream instance
|
||||
* @param offset how much to move the pointer
|
||||
* @param offset_type starting from what
|
||||
* @return true
|
||||
* @return false
|
||||
*/
|
||||
bool stream_seek(Stream* stream, int32_t offset, StreamOffset offset_type);
|
||||
|
||||
/**
|
||||
* Gets the value of the rw pointer
|
||||
* @param stream Stream instance
|
||||
* @return size_t value of the rw pointer
|
||||
*/
|
||||
size_t stream_tell(Stream* stream);
|
||||
|
||||
/**
|
||||
* Gets the size of the stream
|
||||
* @param stream Stream instance
|
||||
* @return size_t size of the stream
|
||||
*/
|
||||
size_t stream_size(Stream* stream);
|
||||
|
||||
/**
|
||||
* Write N bytes to the stream
|
||||
* @param stream Stream instance
|
||||
* @param data data to write
|
||||
* @param size size of data to be written
|
||||
* @return size_t how many bytes was written
|
||||
*/
|
||||
size_t stream_write(Stream* stream, const uint8_t* data, size_t size);
|
||||
|
||||
/**
|
||||
* Read N bytes from stream
|
||||
* @param stream Stream instance
|
||||
* @param data data to be read
|
||||
* @param count size of data to be read
|
||||
* @return size_t how many bytes was read
|
||||
*/
|
||||
size_t stream_read(Stream* stream, uint8_t* data, size_t count);
|
||||
|
||||
/**
|
||||
* Delete N chars from the stream and write data by calling write_callback(context)
|
||||
* @param stream Stream instance
|
||||
* @param delete_size size of data to be deleted
|
||||
* @param write_callback write callback
|
||||
* @param context write callback context
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_delete_and_insert(
|
||||
Stream* stream,
|
||||
size_t delete_size,
|
||||
StreamWriteCB write_callback,
|
||||
const void* context);
|
||||
|
||||
/********************************** Some random helpers starts here **********************************/
|
||||
|
||||
/**
|
||||
* Read line from a stream (supports LF and CRLF line endings)
|
||||
* @param stream
|
||||
* @param str_result
|
||||
* @return true
|
||||
* @return false
|
||||
*/
|
||||
bool stream_read_line(Stream* stream, string_t str_result);
|
||||
|
||||
/**
|
||||
* Moves the rw pointer to the start
|
||||
* @param stream Stream instance
|
||||
*/
|
||||
bool stream_rewind(Stream* stream);
|
||||
|
||||
/**
|
||||
* Write char to the stream
|
||||
* @param stream Stream instance
|
||||
* @param c char value
|
||||
* @return size_t how many bytes was written
|
||||
*/
|
||||
size_t stream_write_char(Stream* stream, char c);
|
||||
|
||||
/**
|
||||
* Write string to the stream
|
||||
* @param stream Stream instance
|
||||
* @param string string value
|
||||
* @return size_t how many bytes was written
|
||||
*/
|
||||
size_t stream_write_string(Stream* stream, string_t string);
|
||||
|
||||
/**
|
||||
* Write const char* to the stream
|
||||
* @param stream Stream instance
|
||||
* @param string c-string value
|
||||
* @return size_t how many bytes was written
|
||||
*/
|
||||
size_t stream_write_cstring(Stream* stream, const char* string);
|
||||
|
||||
/**
|
||||
* Write formatted string to the stream
|
||||
* @param stream Stream instance
|
||||
* @param format
|
||||
* @param ...
|
||||
* @return size_t how many bytes was written
|
||||
*/
|
||||
size_t stream_write_format(Stream* stream, const char* format, ...);
|
||||
|
||||
/**
|
||||
* Write formatted string to the stream, va_list version
|
||||
* @param stream Stream instance
|
||||
* @param format
|
||||
* @param args
|
||||
* @return size_t how many bytes was written
|
||||
*/
|
||||
size_t stream_write_vaformat(Stream* stream, const char* format, va_list args);
|
||||
|
||||
/**
|
||||
* Insert N chars to the stream, starting at the current pointer.
|
||||
* Data will be inserted, not overwritteт, so the stream will be increased in size.
|
||||
* @param stream Stream instance
|
||||
* @param data data to be inserted
|
||||
* @param size size of data to be inserted
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_insert(Stream* stream, const uint8_t* data, size_t size);
|
||||
|
||||
/**
|
||||
* Insert char to the stream
|
||||
* @param stream Stream instance
|
||||
* @param c char value
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_insert_char(Stream* stream, char c);
|
||||
|
||||
/**
|
||||
* Insert string to the stream
|
||||
* @param stream Stream instance
|
||||
* @param string string value
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_insert_string(Stream* stream, string_t string);
|
||||
|
||||
/**
|
||||
* Insert const char* to the stream
|
||||
* @param stream Stream instance
|
||||
* @param string c-string value
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_insert_cstring(Stream* stream, const char* string);
|
||||
|
||||
/**
|
||||
* Insert formatted string to the stream
|
||||
* @param stream Stream instance
|
||||
* @param format
|
||||
* @param ...
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_insert_format(Stream* stream, const char* format, ...);
|
||||
|
||||
/**
|
||||
* Insert formatted string to the stream, va_list version
|
||||
* @param stream Stream instance
|
||||
* @param format
|
||||
* @param args
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_insert_vaformat(Stream* stream, const char* format, va_list args);
|
||||
|
||||
/**
|
||||
* Delete N chars from the stream and insert char to the stream
|
||||
* @param stream Stream instance
|
||||
* @param delete_size size of data to be deleted
|
||||
* @param c char value
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_delete_and_insert_char(Stream* stream, size_t delete_size, char c);
|
||||
|
||||
/**
|
||||
* Delete N chars from the stream and insert string to the stream
|
||||
* @param stream Stream instance
|
||||
* @param delete_size size of data to be deleted
|
||||
* @param string string value
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_delete_and_insert_string(Stream* stream, size_t delete_size, string_t string);
|
||||
|
||||
/**
|
||||
* Delete N chars from the stream and insert const char* to the stream
|
||||
* @param stream Stream instance
|
||||
* @param delete_size size of data to be deleted
|
||||
* @param string c-string value
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_delete_and_insert_cstring(Stream* stream, size_t delete_size, const char* string);
|
||||
|
||||
/**
|
||||
* Delete N chars from the stream and insert formatted string to the stream
|
||||
* @param stream Stream instance
|
||||
* @param delete_size size of data to be deleted
|
||||
* @param format
|
||||
* @param ...
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_delete_and_insert_format(Stream* stream, size_t delete_size, const char* format, ...);
|
||||
|
||||
/**
|
||||
* Delete N chars from the stream and insert formatted string to the stream, va_list version
|
||||
* @param stream Stream instance
|
||||
* @param delete_size size of data to be deleted
|
||||
* @param format
|
||||
* @param args
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_delete_and_insert_vaformat(
|
||||
Stream* stream,
|
||||
size_t delete_size,
|
||||
const char* format,
|
||||
va_list args);
|
||||
|
||||
/**
|
||||
* Remove N chars from the stream, starting at the current pointer.
|
||||
* The size may be larger than stream size, the stream will be cleared from current rw pointer to the end.
|
||||
* @param stream Stream instance
|
||||
* @param size how many chars need to be deleted
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_delete(Stream* stream, size_t size);
|
||||
|
||||
/**
|
||||
* Copy data from one stream to another. Data will be copied from current rw pointer and to current rw pointer.
|
||||
* @param stream_from
|
||||
* @param stream_to
|
||||
* @param size
|
||||
* @return size_t
|
||||
*/
|
||||
size_t stream_copy(Stream* stream_from, Stream* stream_to, size_t size);
|
||||
|
||||
/**
|
||||
* Copy data from one stream to another. Data will be copied from start of one stream and to start of other stream.
|
||||
* @param stream_from
|
||||
* @param stream_to
|
||||
* @return size_t
|
||||
*/
|
||||
size_t stream_copy_full(Stream* stream_from, Stream* stream_to);
|
||||
|
||||
/**
|
||||
* Splits one stream into two others. The original stream will remain untouched.
|
||||
* @param stream
|
||||
* @param stream_left
|
||||
* @param stream_right
|
||||
* @return true
|
||||
* @return false
|
||||
*/
|
||||
bool stream_split(Stream* stream, Stream* stream_left, Stream* stream_right);
|
||||
|
||||
/**
|
||||
* Loads data to the stream from a file. Data will be loaded to the current RW pointer. RW pointer will be moved to the end of the stream.
|
||||
* @param stream Stream instance
|
||||
* @param storage
|
||||
* @param path
|
||||
* @return size_t
|
||||
*/
|
||||
size_t stream_load_from_file(Stream* stream, Storage* storage, const char* path);
|
||||
|
||||
/**
|
||||
* Writes data from a stream to a file. Data will be saved starting from the current RW pointer. RW pointer will be moved to the end of the stream.
|
||||
* @param stream Stream instance
|
||||
* @param storage
|
||||
* @param path
|
||||
* @param mode
|
||||
* @return size_t
|
||||
*/
|
||||
size_t stream_save_to_file(Stream* stream, Storage* storage, const char* path, FS_OpenMode mode);
|
||||
|
||||
/**
|
||||
* Dump stream inner data (size, RW positiot, content)
|
||||
* @param stream Stream instance
|
||||
*/
|
||||
void stream_dump_data(Stream* stream);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
46
lib/toolbox/stream/stream_i.h
Normal file
46
lib/toolbox/stream/stream_i.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "stream.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define STREAM_CACHE_SIZE 512
|
||||
|
||||
typedef struct StreamVTable StreamVTable;
|
||||
|
||||
typedef void (*StreamFreeFn)(Stream* stream);
|
||||
typedef bool (*StreamEOFFn)(Stream* stream);
|
||||
typedef void (*StreamCleanFn)(Stream* stream);
|
||||
typedef bool (*StreamSeekFn)(Stream* stream, int32_t offset, StreamOffset offset_type);
|
||||
typedef size_t (*StreamTellFn)(Stream* stream);
|
||||
typedef size_t (*StreamSizeFn)(Stream* stream);
|
||||
typedef size_t (*StreamWriteFn)(Stream* stream, const uint8_t* data, size_t size);
|
||||
typedef size_t (*StreamReadFn)(Stream* stream, uint8_t* data, size_t count);
|
||||
typedef bool (*StreamDeleteAndInsertFn)(
|
||||
Stream* stream,
|
||||
size_t delete_size,
|
||||
StreamWriteCB write_cb,
|
||||
const void* ctx);
|
||||
|
||||
struct StreamVTable {
|
||||
const StreamFreeFn free;
|
||||
const StreamEOFFn eof;
|
||||
const StreamCleanFn clean;
|
||||
const StreamSeekFn seek;
|
||||
const StreamTellFn tell;
|
||||
const StreamSizeFn size;
|
||||
const StreamWriteFn write;
|
||||
const StreamReadFn read;
|
||||
const StreamDeleteAndInsertFn delete_and_insert;
|
||||
};
|
||||
|
||||
struct Stream {
|
||||
const StreamVTable* vtable;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
179
lib/toolbox/stream/string_stream.c
Normal file
179
lib/toolbox/stream/string_stream.c
Normal file
@@ -0,0 +1,179 @@
|
||||
#include "stream.h"
|
||||
#include "stream_i.h"
|
||||
#include "string_stream.h"
|
||||
#include <furi/common_defines.h>
|
||||
|
||||
typedef struct {
|
||||
Stream stream_base;
|
||||
string_t string;
|
||||
size_t index;
|
||||
} StringStream;
|
||||
|
||||
static size_t string_stream_write_char(StringStream* stream, char c);
|
||||
|
||||
static void string_stream_free(StringStream* stream);
|
||||
static bool string_stream_eof(StringStream* stream);
|
||||
static void string_stream_clean(StringStream* stream);
|
||||
static bool string_stream_seek(StringStream* stream, int32_t offset, StreamOffset offset_type);
|
||||
static size_t string_stream_tell(StringStream* stream);
|
||||
static size_t string_stream_size(StringStream* stream);
|
||||
static size_t string_stream_write(StringStream* stream, const char* data, size_t size);
|
||||
static size_t string_stream_read(StringStream* stream, char* data, size_t size);
|
||||
static bool string_stream_delete_and_insert(
|
||||
StringStream* stream,
|
||||
size_t delete_size,
|
||||
StreamWriteCB write_callback,
|
||||
const void* ctx);
|
||||
|
||||
const StreamVTable string_stream_vtable = {
|
||||
.free = (StreamFreeFn)string_stream_free,
|
||||
.eof = (StreamEOFFn)string_stream_eof,
|
||||
.clean = (StreamCleanFn)string_stream_clean,
|
||||
.seek = (StreamSeekFn)string_stream_seek,
|
||||
.tell = (StreamTellFn)string_stream_tell,
|
||||
.size = (StreamSizeFn)string_stream_size,
|
||||
.write = (StreamWriteFn)string_stream_write,
|
||||
.read = (StreamReadFn)string_stream_read,
|
||||
.delete_and_insert = (StreamDeleteAndInsertFn)string_stream_delete_and_insert,
|
||||
};
|
||||
|
||||
Stream* string_stream_alloc() {
|
||||
StringStream* stream = malloc(sizeof(StringStream));
|
||||
string_init(stream->string);
|
||||
stream->index = 0;
|
||||
stream->stream_base.vtable = &string_stream_vtable;
|
||||
return (Stream*)stream;
|
||||
}
|
||||
|
||||
static void string_stream_free(StringStream* stream) {
|
||||
string_clear(stream->string);
|
||||
free(stream);
|
||||
}
|
||||
|
||||
static bool string_stream_eof(StringStream* stream) {
|
||||
return (string_stream_tell(stream) >= string_stream_size(stream));
|
||||
}
|
||||
|
||||
static void string_stream_clean(StringStream* stream) {
|
||||
stream->index = 0;
|
||||
string_reset(stream->string);
|
||||
}
|
||||
|
||||
static bool string_stream_seek(StringStream* stream, int32_t offset, StreamOffset offset_type) {
|
||||
bool result = true;
|
||||
switch(offset_type) {
|
||||
case StreamOffsetFromStart:
|
||||
if(offset >= 0) {
|
||||
stream->index = offset;
|
||||
} else {
|
||||
result = false;
|
||||
stream->index = 0;
|
||||
}
|
||||
break;
|
||||
case StreamOffsetFromCurrent:
|
||||
if(((int32_t)stream->index + offset) >= 0) {
|
||||
stream->index += offset;
|
||||
} else {
|
||||
result = false;
|
||||
stream->index = 0;
|
||||
}
|
||||
break;
|
||||
case StreamOffsetFromEnd:
|
||||
if(((int32_t)string_size(stream->string) + offset) >= 0) {
|
||||
stream->index = string_size(stream->string) + offset;
|
||||
} else {
|
||||
result = false;
|
||||
stream->index = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
int32_t diff = (stream->index - string_size(stream->string));
|
||||
if(diff > 0) {
|
||||
stream->index -= diff;
|
||||
result = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static size_t string_stream_tell(StringStream* stream) {
|
||||
return stream->index;
|
||||
}
|
||||
|
||||
static size_t string_stream_size(StringStream* stream) {
|
||||
return string_size(stream->string);
|
||||
}
|
||||
|
||||
static size_t string_stream_write(StringStream* stream, const char* data, size_t size) {
|
||||
// TODO: can be optimized for edge cases
|
||||
size_t i;
|
||||
for(i = 0; i < size; i++) {
|
||||
string_stream_write_char(stream, data[i]);
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
static size_t string_stream_read(StringStream* stream, char* data, size_t size) {
|
||||
size_t write_index = 0;
|
||||
const char* cstr = string_get_cstr(stream->string);
|
||||
|
||||
if(!string_stream_eof(stream)) {
|
||||
while(true) {
|
||||
if(write_index >= size) break;
|
||||
|
||||
data[write_index] = cstr[stream->index];
|
||||
write_index++;
|
||||
string_stream_seek(stream, 1, StreamOffsetFromCurrent);
|
||||
if(string_stream_eof(stream)) break;
|
||||
}
|
||||
}
|
||||
|
||||
return write_index;
|
||||
}
|
||||
|
||||
static bool string_stream_delete_and_insert(
|
||||
StringStream* stream,
|
||||
size_t delete_size,
|
||||
StreamWriteCB write_callback,
|
||||
const void* ctx) {
|
||||
bool result = true;
|
||||
|
||||
if(delete_size) {
|
||||
size_t remain_size = string_stream_size(stream) - string_stream_tell(stream);
|
||||
remain_size = MIN(delete_size, remain_size);
|
||||
|
||||
if(remain_size != 0) {
|
||||
string_replace_at(stream->string, stream->index, remain_size, "");
|
||||
}
|
||||
}
|
||||
|
||||
if(write_callback) {
|
||||
string_t right;
|
||||
string_init_set(right, &string_get_cstr(stream->string)[stream->index]);
|
||||
string_left(stream->string, string_stream_tell(stream));
|
||||
result &= write_callback((Stream*)stream, ctx);
|
||||
string_cat(stream->string, right);
|
||||
string_clear(right);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write to string stream helper
|
||||
* @param stream
|
||||
* @param c
|
||||
* @return size_t
|
||||
*/
|
||||
static size_t string_stream_write_char(StringStream* stream, char c) {
|
||||
if(string_stream_eof(stream)) {
|
||||
string_push_back(stream->string, c);
|
||||
} else {
|
||||
string_set_char(stream->string, stream->index, c);
|
||||
}
|
||||
stream->index++;
|
||||
|
||||
return 1;
|
||||
}
|
18
lib/toolbox/stream/string_stream.h
Normal file
18
lib/toolbox/stream/string_stream.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#include <stdlib.h>
|
||||
#include <mlib/m-string.h>
|
||||
#include "stream.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Allocate string stream
|
||||
* @return Stream*
|
||||
*/
|
||||
Stream* string_stream_alloc();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user