From 96babf734c4122d9bd0b1c9892165ee7c2bc6c98 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Sun, 27 Aug 2023 11:50:11 +0200 Subject: [PATCH] defined why2_yml_read this definitely isn't code from chatgpt combined with my dumb-ass and some random stuff from stackoverflow, nah :)) --- src/chat/parser.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/src/chat/parser.c b/src/chat/parser.c index 15957e1..3db3cfb 100644 --- a/src/chat/parser.c +++ b/src/chat/parser.c @@ -14,4 +14,56 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . -*/ \ No newline at end of file +*/ + +#include +#include + +#include +#include + +#include + +char *why2_yml_read(char *path, char *key) +{ + FILE *file = fopen(path, "r"); + + yaml_parser_t parser; + yaml_token_t token; + int state = 0; // 0: looking for key, 1: looking for value + + if (!yaml_parser_initialize(&parser)) why2_die("Failed to initialize parser."); + + yaml_parser_set_input_file(&parser, file); + char *value = NULL; + + while (yaml_parser_scan(&parser, &token)) + { + switch (token.type) + { + case YAML_SCALAR_TOKEN: + if (state == 0 && strcmp((char*) token.data.scalar.value, key) == 0) + { + state = 1; + } else if (state == 1) + { + value = why2_strdup((char*) token.data.scalar.value); + state = 0; // Reset state for the next key-value pair + } + + break; + + case YAML_STREAM_END_TOKEN: + goto skip; + + default: + break; + } + + yaml_token_delete(&token); //TODO: Possible memory leak (or maybe not i have no fucking idea) + } + + skip: + + return value; +}