[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:
Georgii Surkov
2022-07-22 19:00:25 +03:00
committed by GitHub
parent ec57dd310a
commit 16e598b2c0
10 changed files with 501 additions and 11 deletions

View File

@@ -2,6 +2,7 @@
#include <toolbox/stream/stream.h>
#include <toolbox/stream/string_stream.h>
#include <toolbox/stream/file_stream.h>
#include <toolbox/stream/buffered_file_stream.h>
#include "flipper_format.h"
#include "flipper_format_i.h"
#include "flipper_format_stream.h"
@@ -36,11 +37,24 @@ FlipperFormat* flipper_format_file_alloc(Storage* storage) {
return flipper_format;
}
FlipperFormat* flipper_format_buffered_file_alloc(Storage* storage) {
FlipperFormat* flipper_format = malloc(sizeof(FlipperFormat));
flipper_format->stream = buffered_file_stream_alloc(storage);
flipper_format->strict_mode = false;
return flipper_format;
}
bool flipper_format_file_open_existing(FlipperFormat* flipper_format, const char* path) {
furi_assert(flipper_format);
return file_stream_open(flipper_format->stream, path, FSAM_READ_WRITE, FSOM_OPEN_EXISTING);
}
bool flipper_format_buffered_file_open_existing(FlipperFormat* flipper_format, const char* path) {
furi_assert(flipper_format);
return buffered_file_stream_open(
flipper_format->stream, path, FSAM_READ_WRITE, FSOM_OPEN_EXISTING);
}
bool flipper_format_file_open_append(FlipperFormat* flipper_format, const char* path) {
furi_assert(flipper_format);
@@ -87,6 +101,11 @@ bool flipper_format_file_close(FlipperFormat* flipper_format) {
return file_stream_close(flipper_format->stream);
}
bool flipper_format_buffered_file_close(FlipperFormat* flipper_format) {
furi_assert(flipper_format);
return buffered_file_stream_close(flipper_format->stream);
}
void flipper_format_free(FlipperFormat* flipper_format) {
furi_assert(flipper_format);
stream_free(flipper_format->stream);

View File

@@ -115,6 +115,12 @@ FlipperFormat* flipper_format_string_alloc();
*/
FlipperFormat* flipper_format_file_alloc(Storage* storage);
/**
* Allocate FlipperFormat as file, buffered read-only mode.
* @return FlipperFormat* pointer to a FlipperFormat instance
*/
FlipperFormat* flipper_format_buffered_file_alloc(Storage* storage);
/**
* Open existing file.
* Use only if FlipperFormat allocated as a file.
@@ -124,6 +130,15 @@ FlipperFormat* flipper_format_file_alloc(Storage* storage);
*/
bool flipper_format_file_open_existing(FlipperFormat* flipper_format, const char* path);
/**
* Open existing file, read-only with buffered read operations.
* Use only if FlipperFormat allocated as a file.
* @param flipper_format Pointer to a FlipperFormat instance
* @param path File path
* @return True on success
*/
bool flipper_format_buffered_file_open_existing(FlipperFormat* flipper_format, const char* path);
/**
* Open existing file for writing and add values to the end of file.
* Use only if FlipperFormat allocated as a file.
@@ -159,6 +174,14 @@ bool flipper_format_file_open_new(FlipperFormat* flipper_format, const char* pat
*/
bool flipper_format_file_close(FlipperFormat* flipper_format);
/**
* Closes the file, use only if FlipperFormat allocated as a buffered file.
* @param flipper_format
* @return true
* @return false
*/
bool flipper_format_buffered_file_close(FlipperFormat* flipper_format);
/**
* Free FlipperFormat.
* @param flipper_format Pointer to a FlipperFormat instance