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