From 37954d9b20c2cd30a7635b367122bf1c9baf2511 Mon Sep 17 00:00:00 2001 From: CK Tan Date: Mon, 25 Oct 2021 23:21:48 -0700 Subject: [PATCH] add toml_key_exists() --- LICENSE | 2 +- toml.c | 21 ++++++++++++++++++++- toml.h | 4 +++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/LICENSE b/LICENSE index a3292b1..bb09e49 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2017 CK Tan +Copyright (c) CK Tan https://github.com/cktan/tomlc99 Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/toml.c b/toml.c index 8ec554e..89c0a26 100644 --- a/toml.c +++ b/toml.c @@ -2,7 +2,7 @@ MIT License - Copyright (c) 2017 - 2021 CK Tan + Copyright (c) CK Tan https://github.com/cktan/tomlc99 Permission is hereby granted, free of charge, to any person obtaining a copy @@ -1806,6 +1806,25 @@ const char* toml_key_in(const toml_table_t* tab, int keyidx) return 0; } +int toml_key_exists(const toml_table_t* tab, const char* key) +{ + int i; + for (i = 0; i < tab->nkval; i++) { + if (0 == strcmp(key, tab->kval[i]->key)) + return 1; + } + for (i = 0; i < tab->narr; i++) { + if (0 == strcmp(key, tab->arr[i]->key)) + return 1; + } + for (i = 0; i < tab->ntab; i++) { + if (0 == strcmp(key, tab->tab[i]->key)) + return 1; + } + return 0; +} + + toml_raw_t toml_raw_in(const toml_table_t* tab, const char* key) { int i; diff --git a/toml.h b/toml.h index b91ef89..03adcbe 100644 --- a/toml.h +++ b/toml.h @@ -1,7 +1,7 @@ /* MIT License - Copyright (c) 2017 - 2019 CK Tan + Copyright (c) CK Tan https://github.com/cktan/tomlc99 Permission is hereby granted, free of charge, to any person obtaining a copy @@ -109,6 +109,8 @@ TOML_EXTERN toml_table_t* toml_table_at(const toml_array_t* arr, int idx); /* on tables: */ /* ... retrieve the key in table at keyidx. Return 0 if out of range. */ TOML_EXTERN const char* toml_key_in(const toml_table_t* tab, int keyidx); +/* ... returns 1 if key exists in tab, 0 otherwise */ +TOML_EXTERN int toml_key_exists(const toml_table_t* tab, const char* key); /* ... retrieve values using key. */ TOML_EXTERN toml_datum_t toml_string_in(const toml_table_t* arr, const char* key); TOML_EXTERN toml_datum_t toml_bool_in(const toml_table_t* arr, const char* key);