From 03da03d67983273231a3ccfe60849d40f80feb13 Mon Sep 17 00:00:00 2001 From: Luka Jankovic Date: Thu, 24 Jul 2025 11:55:03 +0200 Subject: [PATCH] add yaml library, example --- CMakeLists.txt | 6 +++++- data.yaml | 8 ++++++++ kubo_file.c | 26 ++++++++++++++++++++++++++ kubo_file.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ main.c | 9 ++++++++- 5 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 data.yaml create mode 100644 kubo_file.c create mode 100644 kubo_file.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ed5bf4..4dcc20e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ else() endif() set(SOURCES + kubo_file.c kubo_input.c kubo_command_bar.c kubo_bar.c @@ -46,4 +47,7 @@ else() target_compile_options(kubo PRIVATE -Wall -Wextra -Werror) endif() -target_link_libraries(kubo raylib) +target_include_directories(kubo PRIVATE ${CYAML_INCLUDE_DIR}) +target_link_libraries(kubo PRIVATE ${CYAML_LIBRARY}) + +target_link_libraries(kubo PRIVATE raylib) diff --git a/data.yaml b/data.yaml new file mode 100644 index 0000000..2d07c8d --- /dev/null +++ b/data.yaml @@ -0,0 +1,8 @@ +name: Fibonacci +data: + - 1 + - 1 + - 2 + - 3 + - 5 + - 8 diff --git a/kubo_file.c b/kubo_file.c new file mode 100644 index 0000000..c85ce4a --- /dev/null +++ b/kubo_file.c @@ -0,0 +1,26 @@ +#include "kubo_file.h" + +void kubo_file_parse(char *file_name) { + static const cyaml_config_t config = { + .log_fn = cyaml_log, + .mem_fn = cyaml_mem, + .log_level = CYAML_LOG_WARNING, + }; + + struct numbers *n; + + cyaml_err_t err = cyaml_load_file(file_name, &config, &top_schema, (cyaml_data_t **)&n, NULL); + if (err != CYAML_OK) { + printf("cyaml err\n"); + } + + printf("%s:\n", n->name); + for (unsigned i = 0; i < n->data_count; i++) { + printf(" - %i\n", n->data[i]); + } + + err = cyaml_free(&config, &top_schema, n, 0); + if (err != CYAML_OK) { + printf("cyaml free err\n"); + } +} diff --git a/kubo_file.h b/kubo_file.h new file mode 100644 index 0000000..497f95a --- /dev/null +++ b/kubo_file.h @@ -0,0 +1,49 @@ + +/* + * Copyright Luka Jankovic 2025 + * + * This file is part of Kubo. + * + * Kubo is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * Kubo is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * Kubo. If not, see . + */ + +#ifndef KUBO_FILE_H +#define KUBO_FILE_H + +#include +#include + +struct numbers { + char *name; + int *data; + unsigned data_count; +}; + +static const cyaml_schema_value_t data_entry = { + CYAML_VALUE_INT(CYAML_FLAG_DEFAULT, int), +}; + +static const cyaml_schema_field_t top_mapping_schema[] = { + CYAML_FIELD_STRING_PTR("name", CYAML_FLAG_POINTER, struct numbers, name, 0, + CYAML_UNLIMITED), + CYAML_FIELD_SEQUENCE("data", CYAML_FLAG_POINTER, struct numbers, data, + &data_entry, 0, CYAML_UNLIMITED), + CYAML_FIELD_END}; + +static const cyaml_schema_value_t top_schema = { + CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER, struct numbers, top_mapping_schema), +}; + +void kubo_file_parse(char *file_name); + +#endif diff --git a/main.c b/main.c index ef5b46d..d6f5566 100644 --- a/main.c +++ b/main.c @@ -4,7 +4,14 @@ #include "kubo_context.h" #include "kubo_window.h" -int main() { +#include "kubo_file.h" + +int main(int argc, char *argv[]) { + + if (argc == 2) { + kubo_file_parse(argv[1]); + } + struct kubo_context context; kubo_context_init(&context);