Made minor changes to parser code to make it a bit simpler

dev
shariq 8 years ago
parent 4f214da550
commit ce88b828d7
  1. 2
      README.md
  2. 18
      src/common/parser.c

@ -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

@ -6,6 +6,7 @@
#include <ctype.h>
#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;
}

Loading…
Cancel
Save