[FL-1489] IRDA: move to FileWorker (#594)
* [FL-1489] IRDA: move to FileWorker, fixes * Use FileWorker * Use file_select to select remotes * Fix some crashes * Add RAW parsing restrictions * Remove excess scene (LearnDoneAfter) * Move all file system logic to standalone object
This commit is contained in:
@@ -62,7 +62,24 @@ bool FileWorkerCpp::file_select(
|
||||
const char* extension,
|
||||
char* result,
|
||||
uint8_t result_size,
|
||||
char* selected_filename) {
|
||||
const char* selected_filename) {
|
||||
return file_worker_file_select(
|
||||
file_worker, path, extension, result, result_size, selected_filename);
|
||||
}
|
||||
}
|
||||
|
||||
bool FileWorkerCpp::read_until_buffered(string_t str_result, char* file_buf, size_t* file_buf_cnt, size_t max_length, char separator) {
|
||||
return file_worker_read_until_buffered(file_worker, str_result, file_buf, file_buf_cnt, max_length, separator);
|
||||
}
|
||||
|
||||
bool FileWorkerCpp::is_file_exist(const char* filename, bool* exist) {
|
||||
return file_worker_is_file_exist(file_worker, filename, exist);
|
||||
}
|
||||
|
||||
bool FileWorkerCpp::rename(const char* old_path, const char* new_path) {
|
||||
return file_worker_rename(file_worker, old_path, new_path);
|
||||
}
|
||||
|
||||
bool FileWorkerCpp::check_errors() {
|
||||
return file_worker_check_errors(file_worker);
|
||||
}
|
||||
|
||||
|
@@ -128,7 +128,50 @@ public:
|
||||
const char* extension,
|
||||
char* result,
|
||||
uint8_t result_size,
|
||||
char* selected_filename);
|
||||
const char* selected_filename);
|
||||
|
||||
/**
|
||||
* @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 included in the result.
|
||||
*
|
||||
* @param result
|
||||
* @param file_buf
|
||||
* @param file_buf_cnt
|
||||
* @param max_length
|
||||
* @param separator
|
||||
* @return true on success
|
||||
*/
|
||||
bool read_until_buffered(string_t str_result, char* file_buf, size_t* file_buf_cnt, size_t max_length, char separator = '\n');
|
||||
|
||||
/**
|
||||
* @brief Check whether file exist or not
|
||||
*
|
||||
* @param file_worker FileWorker instance
|
||||
* @param filename
|
||||
* @param exist - flag to show file exist
|
||||
* @return true on success
|
||||
*/
|
||||
bool is_file_exist(
|
||||
const char* filename,
|
||||
bool* exist);
|
||||
|
||||
/**
|
||||
* @brief Rename file or directory
|
||||
*
|
||||
* @param old_filename
|
||||
* @param new_filename
|
||||
* @return true on success
|
||||
*/
|
||||
bool rename(
|
||||
const char* old_path,
|
||||
const char* new_path);
|
||||
|
||||
/**
|
||||
* @brief Check errors
|
||||
*
|
||||
* @return true if no errors
|
||||
*/
|
||||
bool check_errors();
|
||||
|
||||
private:
|
||||
FileWorker* file_worker;
|
||||
|
@@ -1,4 +1,5 @@
|
||||
#include "file-worker.h"
|
||||
#include "m-string.h"
|
||||
#include <hex.h>
|
||||
#include <sd-card-api.h>
|
||||
#include <furi.h>
|
||||
@@ -8,6 +9,8 @@ struct FileWorker {
|
||||
SdCard_Api* sd_ex_api;
|
||||
bool silent;
|
||||
File file;
|
||||
char file_buf[48];
|
||||
size_t file_buf_cnt;
|
||||
};
|
||||
|
||||
bool file_worker_check_common_errors(FileWorker* file_worker);
|
||||
@@ -25,6 +28,7 @@ FileWorker* file_worker_alloc(bool _silent) {
|
||||
file_worker->silent = _silent;
|
||||
file_worker->fs_api = furi_record_open("sdcard");
|
||||
file_worker->sd_ex_api = furi_record_open("sdcard-ex");
|
||||
file_worker->file_buf_cnt = 0;
|
||||
|
||||
return file_worker;
|
||||
}
|
||||
@@ -215,14 +219,17 @@ bool file_worker_file_select(
|
||||
const char* extension,
|
||||
char* result,
|
||||
uint8_t result_size,
|
||||
char* selected_filename) {
|
||||
const char* selected_filename) {
|
||||
return file_worker->sd_ex_api->file_select(
|
||||
file_worker->sd_ex_api->context, path, extension, result, result_size, selected_filename);
|
||||
}
|
||||
|
||||
bool file_worker_check_common_errors(FileWorker* file_worker) {
|
||||
file_worker->sd_ex_api->check_error(file_worker->sd_ex_api->context);
|
||||
return true;
|
||||
/* TODO: [FL-1431] Add return value to file_parser.get_sd_api().check_error() and replace get_fs_info(). */
|
||||
FS_Error fs_err = file_worker->fs_api->common.get_fs_info(NULL, NULL);
|
||||
if(fs_err != FSE_OK)
|
||||
file_worker->sd_ex_api->show_error(file_worker->sd_ex_api->context, "SD card not found");
|
||||
return fs_err == FSE_OK;
|
||||
}
|
||||
|
||||
void file_worker_show_error_internal(FileWorker* file_worker, const char* error_text) {
|
||||
@@ -277,4 +284,91 @@ bool file_worker_seek_internal(FileWorker* file_worker, uint64_t position, bool
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool file_worker_read_until_buffered(FileWorker* file_worker, string_t str_result, char* file_buf, size_t* file_buf_cnt, size_t file_buf_size, char separator) {
|
||||
furi_assert(string_capacity(str_result) > 0);
|
||||
furi_assert(file_buf_size <= 512); /* fs_api->file.read now supports up to 512 bytes reading at a time */
|
||||
|
||||
string_clean(str_result);
|
||||
size_t newline_index = 0;
|
||||
bool found_eol = false;
|
||||
bool max_length_exceeded = false;
|
||||
size_t max_length = string_capacity(str_result) - 1;
|
||||
|
||||
while(1) {
|
||||
if(*file_buf_cnt > 0) {
|
||||
size_t end_index = 0;
|
||||
char* endline_ptr = (char*)memchr(file_buf, separator, *file_buf_cnt);
|
||||
newline_index = endline_ptr - file_buf;
|
||||
|
||||
if(endline_ptr == 0) {
|
||||
end_index = *file_buf_cnt;
|
||||
} else if(newline_index < *file_buf_cnt) {
|
||||
end_index = newline_index + 1;
|
||||
found_eol = true;
|
||||
} else {
|
||||
furi_assert(0);
|
||||
}
|
||||
|
||||
if (max_length && (string_size(str_result) + end_index > max_length))
|
||||
max_length_exceeded = true;
|
||||
|
||||
if (!max_length_exceeded) {
|
||||
for (size_t i = 0; i < end_index; ++i) {
|
||||
string_push_back(str_result, file_buf[i]);
|
||||
}
|
||||
}
|
||||
|
||||
memmove(file_buf, &file_buf[end_index], *file_buf_cnt - end_index);
|
||||
*file_buf_cnt = *file_buf_cnt - end_index;
|
||||
if(found_eol) break;
|
||||
}
|
||||
|
||||
*file_buf_cnt +=
|
||||
file_worker->fs_api->file.read(&file_worker->file, &file_buf[*file_buf_cnt], file_buf_size - *file_buf_cnt);
|
||||
if(file_worker->file.error_id != FSE_OK) {
|
||||
file_worker_show_error_internal(file_worker, "Cannot read\nfile");
|
||||
string_clear(str_result);
|
||||
*file_buf_cnt = 0;
|
||||
break;
|
||||
}
|
||||
if(*file_buf_cnt == 0) {
|
||||
break; // end of reading
|
||||
}
|
||||
}
|
||||
|
||||
if (max_length_exceeded)
|
||||
string_clear(str_result);
|
||||
|
||||
return string_size(str_result) || *file_buf_cnt;
|
||||
}
|
||||
|
||||
bool file_worker_rename(FileWorker* file_worker, const char* old_path, const char* new_path) {
|
||||
FS_Error fs_result = file_worker->fs_api->common.rename(old_path, new_path);
|
||||
|
||||
if(fs_result != FSE_OK && fs_result != FSE_EXIST) {
|
||||
file_worker_show_error_internal(file_worker, "Cannot rename\n file/directory");
|
||||
return false;
|
||||
}
|
||||
|
||||
return file_worker_check_common_errors(file_worker);
|
||||
}
|
||||
|
||||
bool file_worker_check_errors(FileWorker* file_worker) {
|
||||
return file_worker_check_common_errors(file_worker);
|
||||
}
|
||||
|
||||
bool file_worker_is_file_exist(
|
||||
FileWorker* file_worker,
|
||||
const char* filename,
|
||||
bool* exist) {
|
||||
|
||||
File file;
|
||||
*exist = file_worker->fs_api->file.open(&file, filename, FSAM_READ, FSOM_OPEN_EXISTING);
|
||||
if (*exist)
|
||||
file_worker->fs_api->file.close(&file);
|
||||
|
||||
return file_worker_check_common_errors(file_worker);
|
||||
}
|
||||
|
||||
|
@@ -163,8 +163,55 @@ bool file_worker_file_select(
|
||||
const char* extension,
|
||||
char* result,
|
||||
uint8_t result_size,
|
||||
char* selected_filename);
|
||||
const char* selected_filename);
|
||||
|
||||
/**
|
||||
* @brief Reads data from a file until separator or EOF is found.
|
||||
* The separator is included in the result.
|
||||
*
|
||||
* @param file_worker FileWorker instance
|
||||
* @param str_result
|
||||
* @param file_buf
|
||||
* @param file_buf_cnt
|
||||
* @param max_length
|
||||
* @param separator
|
||||
* @return true on success
|
||||
*/
|
||||
bool file_worker_read_until_buffered(FileWorker* file_worker, string_t str_result, char* file_buf, size_t* file_buf_cnt, size_t max_length, char separator);
|
||||
|
||||
/**
|
||||
* @brief Check whether file exist or not
|
||||
*
|
||||
* @param file_worker FileWorker instance
|
||||
* @param filename
|
||||
* @param exist - flag to show file exist
|
||||
* @return true on success
|
||||
*/
|
||||
bool file_worker_is_file_exist(
|
||||
FileWorker* file_worker,
|
||||
const char* filename,
|
||||
bool* exist);
|
||||
|
||||
/**
|
||||
* @brief Rename file or directory
|
||||
*
|
||||
* @param file_worker FileWorker instance
|
||||
* @param old_filename
|
||||
* @param new_filename
|
||||
* @return true on success
|
||||
*/
|
||||
bool file_worker_rename(FileWorker* file_worker,
|
||||
const char* old_path,
|
||||
const char* new_path);
|
||||
|
||||
/**
|
||||
* @brief Check errors
|
||||
*
|
||||
* @param file_worker FileWorker instance
|
||||
* @return true on success
|
||||
*/
|
||||
bool file_worker_check_errors(FileWorker* file_worker);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user