defined why2_yml_read

this definitely isn't code from chatgpt combined with my dumb-ass and some random stuff from stackoverflow, nah :))
This commit is contained in:
Václav Šmejkal 2023-08-27 11:50:11 +02:00
parent 4016478492
commit 96babf734c
Signed by: ENGO150
GPG Key ID: 4A57E86482968843

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
*/
#include <stdio.h>
#include <string.h>
#include <why2/memory.h>
#include <why2/misc.h>
#include <yaml.h>
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;
}