From ce88b828d79399178d73b4b2a6c3a51ecda25fff Mon Sep 17 00:00:00 2001 From: shariq Date: Wed, 20 Sep 2017 20:55:25 +0500 Subject: [PATCH] Made minor changes to parser code to make it a bit simpler --- README.md | 2 +- src/common/parser.c | 18 +++++++----------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 17e11e6..19ae4cd 100644 --- a/README.md +++ b/README.md @@ -149,7 +149,7 @@ - ## TODO - - Modify entity loading logic to use the new parsing code + - Modify entity loading logic to use the new parsing code by parsing all entity properties into a hashmap first then recreating entity from that - Implement sound/listener loading from scene file - Finish loading scene from file - Update makefiles to be able to compile the code in it's current state diff --git a/src/common/parser.c b/src/common/parser.c index b156a33..486c96c 100644 --- a/src/common/parser.c +++ b/src/common/parser.c @@ -6,6 +6,7 @@ #include #define MAX_LINE_LEN 512 +#define MAX_VALUE_LEN 512 #define QUOTE(str) #str #define KEY_LEN_STR(LEN) QUOTE(LEN) @@ -24,15 +25,19 @@ bool parser_load(FILE* file, const char* filename, Parser_Assign_Func assign_fun } /* Read line by line, ignore comments */ + char format_str[64]; char key_str[HASH_MAX_KEY_LEN]; + char value_str[MAX_VALUE_LEN]; char line_buffer[MAX_LINE_LEN]; memset(key_str, '\0', HASH_MAX_KEY_LEN); memset(line_buffer, '\0', MAX_LINE_LEN); + snprintf(format_str, 64, " %%%d[^: ] : %%%d[^\n]", HASH_MAX_KEY_LEN, MAX_VALUE_LEN); while(fgets(line_buffer, MAX_LINE_LEN - 1, file)) { current_line++; memset(key_str, '\0', HASH_MAX_KEY_LEN); + memset(value_str, '\0', HASH_MAX_KEY_LEN); if(strlen(line_buffer) == 0 || isspace(line_buffer[0]) != 0) { @@ -44,19 +49,10 @@ bool parser_load(FILE* file, const char* filename, Parser_Assign_Func assign_fun if(line_buffer[0] == '#') continue; - - char* value_str = strstr(line_buffer, ":"); - if(!value_str) - { - log_warning("Malformed value in config file %s, line %d", filename, current_line); - continue; - } - - value_str++; /* Ignore the colon(:) and set the pointer after it */ - if(sscanf(line_buffer, " %" KEY_LEN_STR(HASH_MAX_KEY_LEN) "[^: ] : %*s", key_str) != 1) + if(sscanf(line_buffer, format_str, key_str, value_str) != 2) { - log_warning("Parser unable to read key in file %s, line %d", filename, current_line); + log_warning("Malformed value in config file %s, line %d", filename, current_line); continue; }