File worker helper. Slightly optimized iButton app code size. (#545)
* File worker: file operations helper * Notification app: removed yield * File worker: write operations, calls to system file widgets * App ibutton: use file worker * App ibutton: small save fix, forgotten byte
This commit is contained in:
@@ -1,20 +1,19 @@
|
||||
#include "file-worker.h"
|
||||
|
||||
FileWorker::FileWorker()
|
||||
FileWorker::FileWorker(bool _silent)
|
||||
: fs_api{"sdcard"}
|
||||
, sd_ex_api{"sdcard-ex"} {
|
||||
string_init(error_string);
|
||||
silent = _silent;
|
||||
}
|
||||
|
||||
FileWorker::~FileWorker() {
|
||||
string_clear(error_string);
|
||||
}
|
||||
|
||||
bool FileWorker::open(const char* filename, FS_AccessMode access_mode, FS_OpenMode open_mode) {
|
||||
bool result = fs_api.get()->file.open(&file, filename, access_mode, open_mode);
|
||||
|
||||
if(!result) {
|
||||
show_error_message("Cannot open\nfile");
|
||||
show_error_internal("Cannot open\nfile");
|
||||
close();
|
||||
return false;
|
||||
}
|
||||
@@ -32,7 +31,7 @@ bool FileWorker::mkdir(const char* dirname) {
|
||||
FS_Error fs_result = fs_api.get()->common.mkdir(dirname);
|
||||
|
||||
if(fs_result != FSE_OK && fs_result != FSE_EXIST) {
|
||||
show_error_message("Cannot create\nfolder");
|
||||
show_error_internal("Cannot create\nfolder");
|
||||
return false;
|
||||
};
|
||||
|
||||
@@ -42,19 +41,197 @@ bool FileWorker::mkdir(const char* dirname) {
|
||||
bool FileWorker::remove(const char* filename) {
|
||||
FS_Error fs_result = fs_api.get()->common.remove(filename);
|
||||
if(fs_result != FSE_OK && fs_result != FSE_NOT_EXIST) {
|
||||
show_error_message("Cannot remove\nold file");
|
||||
show_error_internal("Cannot remove\nold file");
|
||||
return false;
|
||||
};
|
||||
|
||||
return check_common_errors();
|
||||
}
|
||||
|
||||
bool FileWorker::read(void* buffer, uint16_t bytes_to_read) {
|
||||
if(!read_internal(buffer, bytes_to_read)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return check_common_errors();
|
||||
}
|
||||
|
||||
bool FileWorker::read_until(string_t str_result, char separator) {
|
||||
string_clean(str_result);
|
||||
const uint8_t buffer_size = 32;
|
||||
uint8_t buffer[buffer_size];
|
||||
|
||||
do {
|
||||
uint16_t read_count = fs_api.get()->file.read(&file, buffer, buffer_size);
|
||||
if(file.error_id != FSE_OK) {
|
||||
show_error_internal("Cannot read\nfile");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool result = false;
|
||||
for(uint16_t i = 0; i < read_count; i++) {
|
||||
if(buffer[i] == separator) {
|
||||
uint64_t position;
|
||||
if(!tell_internal(&position)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
position = position - read_count + i + 1;
|
||||
|
||||
if(!seek_internal(position, true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
result = true;
|
||||
break;
|
||||
} else {
|
||||
string_push_back(str_result, buffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if(result || read_count == 0) {
|
||||
break;
|
||||
}
|
||||
} while(true);
|
||||
|
||||
return check_common_errors();
|
||||
}
|
||||
|
||||
bool FileWorker::read_hex(uint8_t* buffer, uint16_t bytes_to_read) {
|
||||
uint8_t text[2];
|
||||
|
||||
for(uint8_t i = 0; i < bytes_to_read; i++) {
|
||||
if(i != 0) {
|
||||
// space
|
||||
if(!read_internal(text, 1)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// actual data
|
||||
if(!read_internal(text, 2)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// convert hex value to byte
|
||||
buffer[i] = strtol(reinterpret_cast<char*>(text), NULL, 16);
|
||||
}
|
||||
|
||||
return check_common_errors();
|
||||
}
|
||||
|
||||
bool FileWorker::tell(uint64_t* position) {
|
||||
if(!tell_internal(position)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return check_common_errors();
|
||||
}
|
||||
|
||||
bool FileWorker::seek(uint64_t position, bool from_start) {
|
||||
if(!seek_internal(position, from_start)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return check_common_errors();
|
||||
}
|
||||
|
||||
bool FileWorker::write(const void* buffer, uint16_t bytes_to_write) {
|
||||
if(!write_internal(buffer, bytes_to_write)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return check_common_errors();
|
||||
}
|
||||
|
||||
bool FileWorker::write_hex(const uint8_t* buffer, uint16_t bytes_to_write) {
|
||||
const uint8_t byte_text_size = 3;
|
||||
char byte_text[byte_text_size];
|
||||
|
||||
for(uint8_t i = 0; i < bytes_to_write; i++) {
|
||||
sniprintf(byte_text, byte_text_size, "%02X", buffer[i]);
|
||||
|
||||
if(i != 0) {
|
||||
// space
|
||||
const char* space = " ";
|
||||
if(!write_internal(space, 1)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if(!write_internal(byte_text, 2)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return check_common_errors();
|
||||
}
|
||||
|
||||
void FileWorker::show_error(const char* error_text) {
|
||||
sd_ex_api.get()->show_error(sd_ex_api.get()->context, error_text);
|
||||
}
|
||||
|
||||
bool FileWorker::file_select(
|
||||
const char* path,
|
||||
const char* extension,
|
||||
char* result,
|
||||
uint8_t result_size,
|
||||
char* selected_filename) {
|
||||
return sd_ex_api.get()->file_select(
|
||||
sd_ex_api.get()->context, path, extension, result, result_size, selected_filename);
|
||||
|
||||
}
|
||||
|
||||
bool FileWorker::check_common_errors() {
|
||||
sd_ex_api.get()->check_error(sd_ex_api.get()->context);
|
||||
return true;
|
||||
}
|
||||
|
||||
void FileWorker::show_error_message(const char* error_text) {
|
||||
string_set_str(error_string, error_text);
|
||||
sd_ex_api.get()->show_error(sd_ex_api.get()->context, string_get_cstr(error_string));
|
||||
void FileWorker::show_error_internal(const char* error_text) {
|
||||
if(!silent) {
|
||||
show_error(error_text);
|
||||
}
|
||||
}
|
||||
|
||||
bool FileWorker::read_internal(void* buffer, uint16_t bytes_to_read) {
|
||||
uint16_t read_count = fs_api.get()->file.read(&file, buffer, bytes_to_read);
|
||||
|
||||
if(file.error_id != FSE_OK || read_count != bytes_to_read) {
|
||||
show_error_internal("Cannot read\nfile");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FileWorker::write_internal(const void* buffer, uint16_t bytes_to_write) {
|
||||
uint16_t write_count = fs_api.get()->file.write(&file, buffer, bytes_to_write);
|
||||
|
||||
if(file.error_id != FSE_OK || write_count != bytes_to_write) {
|
||||
show_error_internal("Cannot write\nto file");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FileWorker::tell_internal(uint64_t* position) {
|
||||
*position = fs_api.get()->file.tell(&file);
|
||||
|
||||
if(file.error_id != FSE_OK) {
|
||||
show_error_internal("Cannot tell\nfile offset");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FileWorker::seek_internal(uint64_t position, bool from_start) {
|
||||
fs_api.get()->file.seek(&file, position, from_start);
|
||||
if(file.error_id != FSE_OK) {
|
||||
show_error_internal("Cannot seek\nfile");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
@@ -4,24 +4,148 @@
|
||||
#include <filesystem-api.h>
|
||||
#include <m-string.h>
|
||||
|
||||
/**
|
||||
* @brief File operations helper class.
|
||||
* Automatically opens API records, shows error message on error.
|
||||
*/
|
||||
class FileWorker {
|
||||
public:
|
||||
FileWorker();
|
||||
FileWorker(bool silent = false);
|
||||
~FileWorker();
|
||||
|
||||
RecordController<FS_Api> fs_api;
|
||||
RecordController<SdCard_Api> sd_ex_api;
|
||||
|
||||
/**
|
||||
* @brief Open file
|
||||
*
|
||||
* @param filename
|
||||
* @param access_mode
|
||||
* @param open_mode
|
||||
* @return true on success
|
||||
*/
|
||||
bool open(const char* filename, FS_AccessMode access_mode, FS_OpenMode open_mode);
|
||||
|
||||
/**
|
||||
* @brief Close file
|
||||
*
|
||||
* @return true on success
|
||||
*/
|
||||
bool close();
|
||||
|
||||
/**
|
||||
* @brief Creates a directory. Doesn't show error if directory exist.
|
||||
*
|
||||
* @param dirname
|
||||
* @return true on success
|
||||
*/
|
||||
bool mkdir(const char* dirname);
|
||||
|
||||
/**
|
||||
* @brief Removes the file. Doesn't show error if file doesn't exist.
|
||||
*
|
||||
* @param filename
|
||||
* @return true on success
|
||||
*/
|
||||
bool remove(const char* filename);
|
||||
|
||||
/**
|
||||
* @brief Reads data from a file.
|
||||
*
|
||||
* @param buffer
|
||||
* @param bytes_to_read
|
||||
* @return true on success
|
||||
*/
|
||||
bool read(void* buffer, uint16_t bytes_to_read = 1);
|
||||
|
||||
/**
|
||||
* @brief Reads data from a file until separator or EOF is found.
|
||||
* Moves seek pointer to the next symbol after the separator. The separator is not included in the result.
|
||||
*
|
||||
* @param result
|
||||
* @param separator
|
||||
* @return true on success
|
||||
*/
|
||||
bool read_until(string_t result, char separator = '\n');
|
||||
|
||||
/**
|
||||
* @brief Reads data in hexadecimal space-delimited format. For example "AF FF" in a file - [175, 255] in a read buffer.
|
||||
*
|
||||
* @param buffer
|
||||
* @param bytes_to_read
|
||||
* @return true on success
|
||||
*/
|
||||
bool read_hex(uint8_t* buffer, uint16_t bytes_to_read = 1);
|
||||
|
||||
/**
|
||||
* @brief Read seek pointer value
|
||||
*
|
||||
* @param position
|
||||
* @return true on success
|
||||
*/
|
||||
bool tell(uint64_t* position);
|
||||
|
||||
/**
|
||||
* @brief Set seek pointer value
|
||||
*
|
||||
* @param position
|
||||
* @param from_start
|
||||
* @return true on success
|
||||
*/
|
||||
bool seek(uint64_t position, bool from_start);
|
||||
|
||||
/**
|
||||
* @brief Write data to file.
|
||||
*
|
||||
* @param buffer
|
||||
* @param bytes_to_write
|
||||
* @return true on success
|
||||
*/
|
||||
bool write(const void* buffer, uint16_t bytes_to_write = 1);
|
||||
|
||||
/**
|
||||
* @brief Write data to file in hexadecimal space-delimited format. For example [175, 255] in a write buffer - "AF FF" in a file.
|
||||
*
|
||||
* @param buffer
|
||||
* @param bytes_to_write
|
||||
* @return true on success
|
||||
*/
|
||||
bool write_hex(const uint8_t* buffer, uint16_t bytes_to_write = 1);
|
||||
|
||||
/**
|
||||
* @brief Show system file error message
|
||||
*
|
||||
* @param error_text
|
||||
*/
|
||||
void show_error(const char* error_text);
|
||||
|
||||
/**
|
||||
* @brief Show system file select widget
|
||||
*
|
||||
* @param path
|
||||
* @param extension
|
||||
* @param result
|
||||
* @param result_size
|
||||
* @param selected_filename
|
||||
* @return true if file was selected
|
||||
*/
|
||||
bool file_select(
|
||||
const char* path,
|
||||
const char* extension,
|
||||
char* result,
|
||||
uint8_t result_size,
|
||||
char* selected_filename);
|
||||
|
||||
private:
|
||||
File file;
|
||||
bool silent;
|
||||
|
||||
bool check_common_errors();
|
||||
void show_error_message(const char* error_text);
|
||||
string_t error_string;
|
||||
|
||||
void show_error_internal(const char* error_text);
|
||||
|
||||
bool read_internal(void* buffer, uint16_t bytes_to_read = 1);
|
||||
bool write_internal(const void* buffer, uint16_t bytes_to_write = 1);
|
||||
bool tell_internal(uint64_t* position);
|
||||
bool seek_internal(uint64_t position, bool from_start);
|
||||
};
|
||||
|
Reference in New Issue
Block a user