From 06d8fa45fbe41b01be2810a847007e867d512114 Mon Sep 17 00:00:00 2001 From: xtruan Date: Mon, 17 Jul 2023 14:15:40 -0600 Subject: [PATCH] fix: more robust file handling, bump version --- flipchess.h | 2 +- helpers/flipchess_file.c | 13 +++++++++++-- helpers/flipchess_file.h | 3 ++- scenes/flipchess_scene_scene_1.c | 2 +- views/flipchess_scene_1.c | 23 ++++++++++++++--------- 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/flipchess.h b/flipchess.h index 2d17c393870..4df1475418d 100644 --- a/flipchess.h +++ b/flipchess.h @@ -16,7 +16,7 @@ #include "views/flipchess_startscreen.h" #include "views/flipchess_scene_1.h" -#define FLIPCHESS_VERSION "v0.1.3" +#define FLIPCHESS_VERSION "v0.1.4" #define TEXT_BUFFER_SIZE 96 #define TEXT_SIZE (TEXT_BUFFER_SIZE - 1) diff --git a/helpers/flipchess_file.c b/helpers/flipchess_file.c index 284267109aa..257883c9397 100644 --- a/helpers/flipchess_file.c +++ b/helpers/flipchess_file.c @@ -58,7 +58,7 @@ bool flipchess_load_file(char* contents, const FlipChessFile file_type, const ch char chr; int i = 0; while((storage_file_read(settings_file, &chr, 1) == 1) && - !storage_file_eof(settings_file) && !isspace(chr)) { + !storage_file_eof(settings_file)) { if(i < FILE_MAX_CHARS) { contents[i] = chr; } @@ -95,7 +95,8 @@ bool flipchess_save_file( const char* settings, const FlipChessFile file_type, const char* file_name, - const bool append) { + const bool append, + const bool overwrite) { bool ret = false; const char* path; const char* path_bak; @@ -120,6 +121,10 @@ bool flipchess_save_file( // try to create the folder storage_simply_mkdir(fs_api, FLIPCHESS_APP_BASE_FOLDER); + if(overwrite) { + storage_simply_remove(fs_api, path); + } + File* settings_file = storage_file_alloc(fs_api); if(storage_file_open(settings_file, path, FSAM_WRITE, open_mode)) { storage_file_write(settings_file, settings, strlen(settings)); @@ -130,6 +135,10 @@ bool flipchess_save_file( storage_file_free(settings_file); if(path_bak != NULL) { + if(overwrite) { + storage_simply_remove(fs_api, path_bak); + } + File* settings_file_bak = storage_file_alloc(fs_api); if(storage_file_open(settings_file_bak, path_bak, FSAM_WRITE, open_mode)) { storage_file_write(settings_file_bak, settings, strlen(settings)); diff --git a/helpers/flipchess_file.h b/helpers/flipchess_file.h index 25d73849df5..1550b2e2d43 100644 --- a/helpers/flipchess_file.h +++ b/helpers/flipchess_file.h @@ -11,4 +11,5 @@ bool flipchess_save_file( const char* contents, const FlipChessFile file_type, const char* file_name, - const bool append); \ No newline at end of file + const bool append, + const bool overwrite); \ No newline at end of file diff --git a/scenes/flipchess_scene_scene_1.c b/scenes/flipchess_scene_scene_1.c index 3a8756f8142..11e96b5827b 100644 --- a/scenes/flipchess_scene_scene_1.c +++ b/scenes/flipchess_scene_scene_1.c @@ -50,6 +50,6 @@ void flipchess_scene_scene_1_on_exit(void* context) { FlipChess* app = context; if(app->import_game == 1 && strlen(app->import_game_text) > 0) { - flipchess_save_file(app->import_game_text, FlipChessFileBoard, NULL, false); + flipchess_save_file(app->import_game_text, FlipChessFileBoard, NULL, false, true); } } \ No newline at end of file diff --git a/views/flipchess_scene_1.c b/views/flipchess_scene_1.c index bc6b364f3a7..404e405ccd9 100644 --- a/views/flipchess_scene_1.c +++ b/views/flipchess_scene_1.c @@ -160,6 +160,17 @@ void flipchess_drawBoard(FlipChessScene1Model* model) { model->paramFlipBoard); } +uint8_t flipchess_saveState(FlipChess* app, FlipChessScene1Model* model) { + for(uint8_t i = 0; i < SCL_FEN_MAX_LENGTH; i++) { + app->import_game_text[i] = '\0'; + } + const uint8_t res = SCL_boardToFEN(model->game.board, app->import_game_text); + if(res > 0) { + app->import_game = 1; + } + return res; +} + uint8_t flipchess_turn(FlipChessScene1Model* model) { // 0: none, 1: player, 2: AI, 3: undo uint8_t moveType = FlipChessStatusNone; @@ -171,9 +182,6 @@ uint8_t flipchess_turn(FlipChessScene1Model* model) { // printf("ply number: %d\n", model->game.ply); - // SCL_boardToFEN(model->game.board, string); - // printf("FEN: %s\n", string); - // int16_t eval = SCL_boardEvaluateStatic(model->game.board); // printf( // "board static evaluation: %lf (%d)\n", @@ -591,8 +599,7 @@ bool flipchess_scene_1_input(InputEvent* event, void* context) { if(app->sound == 1) flipchess_voice_a_strange_game(); flipchess_play_long_bump(app); } - SCL_boardToFEN(model->game.board, app->import_game_text); - app->import_game = 1; + flipchess_saveState(app, model); flipchess_drawBoard(model); }, true); @@ -618,8 +625,7 @@ bool flipchess_scene_1_input(InputEvent* event, void* context) { if(app->sound == 1) flipchess_voice_a_strange_game(); flipchess_play_long_bump(app); } - SCL_boardToFEN(model->game.board, app->import_game_text); - app->import_game = 1; + flipchess_saveState(app, model); flipchess_drawBoard(model); } }, @@ -669,8 +675,7 @@ void flipchess_scene_1_enter(void* context) { if(turn == FlipChessStatusReturn) { init = turn; } else { - SCL_boardToFEN(model->game.board, app->import_game_text); - app->import_game = 1; + flipchess_saveState(app, model); flipchess_drawBoard(model); } }