From fb888d7c1244eaf8e80bfa7f1977ecdbdffd5d07 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Mon, 12 Sep 2022 18:43:45 +0800 Subject: [PATCH] feat: json parser framework --- include/common.h | 3 +++ include/utils/structure.h | 3 --- src/common.c | 10 ++++++++ src/load/json.c | 53 +++++++++++++++++++++++++++++++++++++-- src/utils/structure.c | 9 ------- 5 files changed, 64 insertions(+), 14 deletions(-) diff --git a/include/common.h b/include/common.h index f4882d7..a513baa 100644 --- a/include/common.h +++ b/include/common.h @@ -41,4 +41,7 @@ void save_file(const char *file, const char *content); void string_list_debug(char *describe, char **string_list); void uint32_list_debug(char *describe, uint32_t **uint32_list); +char* string_init(const char *str); +char* string_join(const char *base, const char *add); + #endif diff --git a/include/utils/structure.h b/include/utils/structure.h index 8010d71..2ae345e 100644 --- a/include/utils/structure.h +++ b/include/utils/structure.h @@ -3,9 +3,6 @@ #include "common.h" -char* string_init(const char *str); -char* string_join(const char *base, const char *add); - uint32_t** uint32_list_init(); char* uint32_list_dump(uint32_t **int_list); void uint32_list_free(uint32_t **uint32_list); diff --git a/src/common.c b/src/common.c index 5d30b6f..d4c5466 100644 --- a/src/common.c +++ b/src/common.c @@ -1,5 +1,6 @@ #include #include +#include #include "common.h" #include "logger.h" #include "structure.h" @@ -12,6 +13,15 @@ char* show_bool(uint8_t value) { // return `true` or `false` } } +char* string_init(const char *str) { // new string + return strcpy((char *)malloc(strlen(str) + 1), str); +} + +char* string_join(const char *base, const char *add) { // combine string + char *ret = (char *)malloc(strlen(base) + strlen(add) + 1); + return strcat(strcpy(ret, base), add); +} + void string_list_debug(char *describe, char **string_list) { char *string_ret = string_list_dump(string_list); log_debug("%s -> %s", describe, string_ret); diff --git a/src/load/json.c b/src/load/json.c index 7095216..4371bf3 100644 --- a/src/load/json.c +++ b/src/load/json.c @@ -1,21 +1,70 @@ #include +#include #include "load.h" #include "cJSON.h" #include "common.h" #include "logger.h" +int json_int_value(char *key, cJSON *json) { // json int or string value -> int + if (cJSON_IsNumber(json)) { + return json->valueint; + } else if (cJSON_IsString(json)) { + char *p; + return (int)strtol(json->valuestring, &p, 10); + } else { + log_fatal("`%s` must be number or string", key); + } + return 0; // never reach +} + +uint8_t json_bool_value(char *key, cJSON *json) { // json bool value -> bool + if (cJSON_IsBool(json)) { + return json->valueint; + } else { + log_fatal("`%s` must be boolean", key); + } + return FALSE; +} + +void json_cache_parser(cache_config *config, cJSON *json) { // cache options parser + while (json != NULL) { + if (!strcmp(json->string, "size")) { + config->size = json_int_value("cache.size", json); + } + if (!strcmp(json->string, "enable")) { + config->enable = json_bool_value("cache.enable", json); + } + if (!strcmp(json->string, "optimistic")) { + config->optimistic = json_bool_value("cache.optimistic", json); + } + json = json->next; // next field + } +} + void json_config_parser(cleardns_config *config, const char *config_file) { // JSON format configure char *config_content = read_file(config_file); cJSON *json = cJSON_Parse(config_content); if (json == NULL) { - log_fatal("JSON format error"); + log_fatal("JSON configure `%s` format error", config_file); } json = json->child; while (json != NULL) { if (!strcmp(json->string, "port")) { - // get dns port + config->port = json_int_value("port", json); + } + if (!strcmp(json->string, "cache")) { + json_cache_parser(&config->cache, json->child); } + // domestic + // foreign + + // diverter + // adguard + // reject + // hosts + // ttl + json = json->next; // next field } cJSON_free(json); // free JSON struct diff --git a/src/utils/structure.c b/src/utils/structure.c index 8c7d0c6..fa1d8e8 100644 --- a/src/utils/structure.c +++ b/src/utils/structure.c @@ -3,15 +3,6 @@ #include #include "structure.h" -char* string_init(const char *str) { // new string - return strcpy((char *)malloc(strlen(str) + 1), str); -} - -char* string_join(const char *base, const char *add) { // combine string - char *ret = (char *)malloc(strlen(base) + strlen(add) + 1); - return strcat(strcpy(ret, base), add); -} - uint32_t* uint32_init(uint32_t number) { // new uint32 (by malloc) uint32_t *data = (uint32_t *)malloc(sizeof(uint32_t)); *data = number;