mirror of
https://github.com/DarkFlippers/unleashed-firmware.git
synced 2024-12-22 12:51:39 +03:00
274c12fc56
* 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>
179 lines
5.1 KiB
C
179 lines
5.1 KiB
C
#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;
|
|
} |