From b761fae3263ffb7f25eda6f1482f48fe53c5ddc2 Mon Sep 17 00:00:00 2001 From: shariq Date: Thu, 11 May 2017 01:37:39 +0500 Subject: [PATCH] Simplified config parsing --- assets/config.cfg | 24 +++++++++--------- src/config_vars.c | 20 +++++++-------- src/input.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++ src/input.h | 2 ++ 4 files changed, 85 insertions(+), 23 deletions(-) diff --git a/assets/config.cfg b/assets/config.cfg index 23f1e33..c9dbf22 100644 --- a/assets/config.cfg +++ b/assets/config.cfg @@ -1,13 +1,13 @@ msaa_levels: 4 -debug_draw_color: 1.000, 0.000, 0.000, 1.000 -render_width: 800 -fog_density: 0.0020 -render_height: 600 -fog_mode: 2 -fog_color: 0.500, 0.200, 0.200 -fog_max_dist: 150.0000 -msaa_enabled: true -fog_start_dist: 20.0000 -debug_draw_enabled: false -debug_draw_mode: 0 -ambient_light: 0.100, 0.100, 0.100 +debug_draw_color : 1.000, 0.000, 0.000, 1.000 +render_width : 800 +fog_density : 0.0020 +render_height : 600 +fog_mode : 2 +fog_color : 0.500, 0.200, 0.200 +fog_max_dist : 150.0000 +msaa_enabled : true +fog_start_dist : 20.0000 +debug_draw_enabled : false +debug_draw_mode : 0 +ambient_light : 0.100, 0.100, 0.100 \ No newline at end of file diff --git a/src/config_vars.c b/src/config_vars.c index d5d2dd1..2f0bc02 100644 --- a/src/config_vars.c +++ b/src/config_vars.c @@ -60,26 +60,25 @@ int config_vars_load(const char* filename) while(fgets(line_buffer, MAX_LINE_LEN - 1, config_file)) { current_line++; - line_buffer[strcspn(line_buffer, "\r\n")] = '\0'; + memset(key_str, '\0', HASH_MAX_KEY_LEN); if(line_buffer[0] == '#' || strlen(line_buffer) == 0) continue; - - log_message("Line : %s", line_buffer); - memset(key_str, '\0', HASH_MAX_KEY_LEN); + char* value_str = strstr(line_buffer, ":"); if(!value_str) { log_warning("Malformed value in config file %s, line %d", filename, current_line); continue; } - int key_str_len = value_str - line_buffer; - strncpy(key_str, line_buffer, key_str_len); - if(key_str_len >= HASH_MAX_KEY_LEN) - key_str[HASH_MAX_KEY_LEN - 1] = '\0'; - else - key_str[key_str_len] = '\0'; + value_str++; /* Ignore the colon(:) */ + + if(sscanf(line_buffer, " %1024[^: ] : %*s", key_str) != 1) + { + log_warning("Unable to read key in config file %s, line %d", filename, current_line); + continue; + } struct Variant* value = hashmap_value_get(cvars, key_str); if(!value) @@ -112,7 +111,6 @@ int config_vars_save(const char* filename) { memset(variant_str, '\0', MAX_VARIANT_STR_LEN); variant_to_str(value, variant_str, MAX_VARIANT_STR_LEN); - log_message("Writing : %s: %s", key, variant_str); fprintf(config_file, "%s: %s\n", key, variant_str); } log_message("Config file %s written.", filename); diff --git a/src/input.c b/src/input.c index 8dab9fc..9c8ff42 100644 --- a/src/input.c +++ b/src/input.c @@ -6,6 +6,7 @@ #include "platform.h" #include "log.h" #include "gui.h" +#include "file_io.h" struct Input_Map { @@ -42,6 +43,67 @@ void input_cleanup(void) array_free(input_map_list); } +int input_load(const char* filename) +{ + int success = 0; + /* const int MAX_KEYBIND_LEN = 128; */ + /* const int MAX_LINE_LEN = 512; */ + /* FILE* config_file = io_file_open(filename, "r"); */ + /* if(!config_file) */ + /* { */ + /* log_error("input:vars_load", "Could not open %s", filename); */ + /* return success; */ + /* } */ + + /* /\* Read line by line, ignore comments *\/ */ + /* char key_str[MAX_KEYBIND_LEN]; */ + /* char line_buffer[MAX_LINE_LEN]; */ + /* memset(key_str, '\0', MAX_KEYBIND_LEN); */ + /* memset(line_buffer, '\0', MAX_LINE_LEN); */ + /* int current_line = 0; */ + /* while(fgets(line_buffer, MAX_LINE_LEN - 1, config_file)) */ + /* { */ + /* current_line++; */ + /* line_buffer[strcspn(line_buffer, "\r\n")] = '\0'; */ + + /* if(line_buffer[0] == '#' || strlen(line_buffer) == 0) */ + /* continue; */ + + /* log_message("Line : %s", line_buffer); */ + /* memset(key_str, '\0', MAX_KEYBIND_LEN); */ + /* char* value_str = strstr(line_buffer, ":"); */ + /* if(!value_str) */ + /* { */ + /* log_warning("Malformed value in config file %s, line %d", filename, current_line); */ + /* continue; */ + /* } */ + /* int key_str_len = value_str - line_buffer; */ + /* strncpy(key_str, line_buffer, key_str_len); */ + /* if(key_str_len >= MAX_KEYBIND_LEN) */ + /* key_str[MAX_KEYBIND_LEN - 1] = '\0'; */ + /* else */ + /* key_str[key_str_len] = '\0'; */ + /* value_str++; /\* Ignore the colon(:) *\/ */ + + /* struct Variant* value = hashmap_value_get(cvars, key_str); */ + /* if(!value) */ + /* { */ + /* log_warning("Unknown value in config file %s, line %d", filename, current_line); */ + /* continue; */ + /* } */ + /* variant_from_str(value, value_str, value->type); */ + /* } */ + + /* success = 1; */ + /* fclose(config_file); */ + return success; +} + +int input_save(const char* filename) +{ + +} + void input_on_mousemotion(int x, int y, int xrel, int yrel) { /* TODO: This is temporary. After proper event loop is added this code should not be here */ diff --git a/src/input.h b/src/input.h index 0b89c92..66dc095 100644 --- a/src/input.h +++ b/src/input.h @@ -409,6 +409,8 @@ enum Keyboard_Scancode }; void input_init(void); +int input_load(const char* filename); +int input_save(const char* filename); void input_cleanup(void); int input_mousebutton_state_get(uint button, int state_type); int input_is_key_pressed(int key);