diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2021-03-02 12:57:16 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2021-03-02 12:57:16 -0500 |
commit | 64321c2a6f08cad8ebafafe1b09a913d16a1b0e2 (patch) | |
tree | 437fecdf02946559ad1aa1f4a8deb226bac3832e | |
parent | b2a19334ef6906cefe1b3ca0e3d5d726d24bf1d3 (diff) |
gracefully handle config errors
-rw-r--r-- | main.c | 39 | ||||
-rw-r--r-- | util.c | 20 |
2 files changed, 44 insertions, 15 deletions
@@ -395,7 +395,7 @@ int main(int argc, char **argv) { if (fs_path_type(ted->local_data_dir) == FS_NON_EXISTENT) fs_mkdir(ted->local_data_dir); - + } FILE *log = NULL; @@ -437,25 +437,32 @@ int main(int argc, char **argv) { #endif Settings *settings = &ted->settings; - + char config_err[sizeof ted->error] = {0}; + { - // read global configuration file first to establish defaults + // copy global config to local config + char local_config_filename[TED_PATH_MAX]; + strbuf_printf(local_config_filename, "%s" PATH_SEPARATOR_STR "ted.cfg", ted->local_data_dir); char global_config_filename[TED_PATH_MAX]; strbuf_printf(global_config_filename, "%s" PATH_SEPARATOR_STR "ted.cfg", ted->global_data_dir); - if (fs_file_exists(global_config_filename)) + if (!fs_file_exists(local_config_filename)) { + if (fs_file_exists(global_config_filename)) { + if (!copy_file(global_config_filename, local_config_filename)) { + die("Couldn't copy config %s to %s.", global_config_filename, local_config_filename); + } + } else { + die("ted's backup config file, %s, does not exist. Try reinstalling ted?", global_config_filename); + } + } + config_read(ted, local_config_filename); + if (ted_haserr(ted)) { + // if there's an error in the local config, read the global config to make sure everything's ok config_read(ted, global_config_filename); + strcpy(config_err, ted->error); + ted_clearerr(ted); // clear the error so later things (e.g. loading font) don't detect an error + } } - { - // read local configuration file - char config_filename[TED_PATH_MAX]; - if (ted_get_file(ted, "ted.cfg", config_filename, sizeof config_filename)) - config_read(ted, config_filename); - else - ted_seterr(ted, "Couldn't find config file (ted.cfg), not even the backup one that should have come with ted."); - } - if (ted_haserr(ted)) { - die("Error reading config: %s", ted_geterr(ted)); - } + SDL_Window *window = SDL_CreateWindow("ted", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 720, SDL_WINDOW_SHOWN|SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE); @@ -553,6 +560,8 @@ int main(int argc, char **argv) { Uint32 time_at_last_frame = SDL_GetTicks(); + strbuf_cpy(ted->error, config_err); + while (!ted->quit) { #if DEBUG //printf("\033[H\033[2J"); @@ -300,3 +300,23 @@ static void change_directory(char const *path) { #endif } +// returns true on success +static bool copy_file(char const *src, char const *dst) { + bool success = false; + FILE *src_file = fopen(src, "rb"); + if (src_file) { + FILE *dst_file = fopen(dst, "wb"); + if (dst_file) { + char buf[1024]; + while (1) { + size_t count = fread(buf, 1, sizeof buf, src_file); + fwrite(buf, 1, count, dst_file); + if (count < sizeof buf) break; + } + success = !ferror(src_file) && !ferror(dst_file); + fclose(dst_file); + } + fclose(src_file); + } + return success; +} |