Browse Source

feat: json parser framework

dev
dnomd343 2 years ago
parent
commit
fb888d7c12
  1. 3
      include/common.h
  2. 3
      include/utils/structure.h
  3. 10
      src/common.c
  4. 53
      src/load/json.c
  5. 9
      src/utils/structure.c

3
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 string_list_debug(char *describe, char **string_list);
void uint32_list_debug(char *describe, uint32_t **uint32_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 #endif

3
include/utils/structure.h

@ -3,9 +3,6 @@
#include "common.h" #include "common.h"
char* string_init(const char *str);
char* string_join(const char *base, const char *add);
uint32_t** uint32_list_init(); uint32_t** uint32_list_init();
char* uint32_list_dump(uint32_t **int_list); char* uint32_list_dump(uint32_t **int_list);
void uint32_list_free(uint32_t **uint32_list); void uint32_list_free(uint32_t **uint32_list);

10
src/common.c

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "common.h" #include "common.h"
#include "logger.h" #include "logger.h"
#include "structure.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) { void string_list_debug(char *describe, char **string_list) {
char *string_ret = string_list_dump(string_list); char *string_ret = string_list_dump(string_list);
log_debug("%s -> %s", describe, string_ret); log_debug("%s -> %s", describe, string_ret);

53
src/load/json.c

@ -1,21 +1,70 @@
#include <string.h> #include <string.h>
#include <stdlib.h>
#include "load.h" #include "load.h"
#include "cJSON.h" #include "cJSON.h"
#include "common.h" #include "common.h"
#include "logger.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 void json_config_parser(cleardns_config *config, const char *config_file) { // JSON format configure
char *config_content = read_file(config_file); char *config_content = read_file(config_file);
cJSON *json = cJSON_Parse(config_content); cJSON *json = cJSON_Parse(config_content);
if (json == NULL) { if (json == NULL) {
log_fatal("JSON format error"); log_fatal("JSON configure `%s` format error", config_file);
} }
json = json->child; json = json->child;
while (json != NULL) { while (json != NULL) {
if (!strcmp(json->string, "port")) { 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 json = json->next; // next field
} }
cJSON_free(json); // free JSON struct cJSON_free(json); // free JSON struct

9
src/utils/structure.c

@ -3,15 +3,6 @@
#include <string.h> #include <string.h>
#include "structure.h" #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* uint32_init(uint32_t number) { // new uint32 (by malloc)
uint32_t *data = (uint32_t *)malloc(sizeof(uint32_t)); uint32_t *data = (uint32_t *)malloc(sizeof(uint32_t));
*data = number; *data = number;

Loading…
Cancel
Save