tcp-chat/src/config.c

35 lines
1.1 KiB
C

#include "config.h"
Result(void) config_findValue(str config_str, str key, str* value, bool throwNotFoundError){
u32 line_n = 0;
while(config_str.size > 0){
line_n++;
i32 line_end = str_seekChar(config_str, '\n', 0);
if(line_end < 0)
line_end = config_str.size - 1;
str line = str_sliceBefore(config_str, line_end);
config_str = str_sliceAfter(config_str, line_end + 1);
i32 sep_pos = str_seekChar(line, '=', 1);
if(sep_pos < 0){
//not a 'key = value' line
continue;
}
str line_key = str_sliceBefore(line, sep_pos - 1);
str_trim(&line_key, false);
if(str_equals(line_key, key)){
str line_value = str_sliceAfter(line, sep_pos + 1);
str_trim(&line_value, false);
*value = line_value;
return RESULT_VOID;
}
}
if(throwNotFoundError){
char* err_msg = sprintf_malloc("can't find key '"FMT_str"'", key.size, key.data);
return RESULT_ERROR(err_msg, true);
}
return RESULT_VOID;
}