Browse Source

update: interface of to_json function

dev
Dnomd343 2 years ago
parent
commit
04aacd0fd3
  1. 2
      include/common/json.h
  2. 10
      src/applet/adguard.c
  3. 21
      src/cleardns.c
  4. 29
      src/common/json.c
  5. 24
      src/demo.c
  6. 8
      src/loader/default.c
  7. 11
      src/loader/parser.c

2
include/common/json.h

@ -4,7 +4,7 @@
#include <stdint.h>
#include "cJSON.h"
char* to_json(const char *config_file);
char* to_json(const char *content);
uint8_t is_json_suffix(const char *file_name);
cJSON* json_field_get(cJSON *entry, const char *key);
void json_field_replace(cJSON *entry, const char *key, cJSON *content);

10
src/applet/adguard.c

@ -92,9 +92,13 @@ process* adguard_load(adguard *info, const char *dir) { // load adguard options
log_info("Create AdGuardHome configure");
adguard_config_ret = adguard_config(info, "{}"); // begin with empty json
} else { // configure exist -> modify
char *adguard_config_raw = to_json(adguard_config_file);
adguard_config_ret = adguard_config(info, adguard_config_raw);
free(adguard_config_raw);
char *adguard_config_content = read_file(adguard_config_file);
log_debug("AdGuardHome raw configure ->\n%s", adguard_config_content);
char *adguard_config_json = to_json(adguard_config_content);
log_debug("AdGuardHome json configure ->\n%s", adguard_config_json);
adguard_config_ret = adguard_config(info, adguard_config_json);
free(adguard_config_content);
free(adguard_config_json);
}
save_file(adguard_config_file, adguard_config_ret); // save modified configure
free(adguard_config_file);

21
src/cleardns.c

@ -114,24 +114,9 @@ void cleardns() { // cleardns service
process_list_daemon(); // daemon all process
}
#include "json.h"
int main(int argc, char *argv[]) {
char error_string[] = "\f233\a";
char yaml_string[] = "test: ok\narray:\n - 123\n - 234\n - 345\n";
log_info("Test yaml content ->\n%s", yaml_string);
char *json_result = to_json(yaml_string);
log_info("Return json content -> %s", json_result);
free(json_result);
log_info("Test error content");
char *ret = to_json(error_string);
log_info("Return -> %s", ret);
// init(argc, argv);
// log_info("ClearDNS server start (%s)", VERSION);
// cleardns();
init(argc, argv);
log_info("ClearDNS server start (%s)", VERSION);
cleardns();
return 0;
}

29
src/common/json.c

@ -3,7 +3,6 @@
#include "cJSON.h"
#include "logger.h"
#include "sundry.h"
#include "system.h"
#include "to_json.h"
#include "constant.h"
#include "structure.h"
@ -18,39 +17,15 @@ uint8_t is_json_suffix(const char *file_name) { // whether file name end with `.
return FALSE;
}
char* to_json(const char *raw) { // convert JSON / TOML / YAML to json format (if failed -> return NULL)
const char *json_string = to_json_rust(raw); // convert to json format
char* to_json(const char *content) { // convert JSON / TOML / YAML to json format (if failed -> return NULL)
const char *json_string = to_json_rust(content); // convert to json format
char *json_content = strdup(json_string); // load string into owner heap
free_rust_string(json_string); // free rust string
if (strlen(json_content) == 0) { // empty string -> convert error
free(json_content);
return NULL;
}
return json_content;
// char flag[9];
// for (int i = 0; i < 8; ++i) { // generate 8-bits flag
// flag[i] = (char)gen_rand_num(10);
// flag[i] += 48;
// }
// flag[8] = '\0';
//
// char *output_file = string_join("/tmp/to-json-", flag);
// char *to_json_cmd = string_load("toJSON %s > %s", file, output_file);
// int to_json_ret = run_command(to_json_cmd);
// free(to_json_cmd);
//
// char *json_content = NULL;
// if (!to_json_ret) { // toJSON return zero code (convert fine)
// json_content = read_file(output_file);
// }
// remove_file(output_file);
// free(output_file);
// return json_content;
}
cJSON* json_field_get(cJSON *entry, const char *key) { // fetch key from json map (create when key not exist)

24
src/demo.c

@ -1,24 +0,0 @@
#include <stdio.h>
#include <string.h>
#include "to_json.h"
int main() {
printf("start demo\n");
char yaml_string[] = "test: ok\narray:\n - 123\n - 234\n - 345\n";
printf("----------------\n");
printf("%s", yaml_string);
printf("----------------\n");
const char *raw_json_string = to_json_rust(yaml_string);
char *json_string = strdup(raw_json_string);
printf("----------------\n");
printf("%s\n", json_string);
printf("----------------\n");
free_rust_string(raw_json_string);
printf("rust string free complete\n");
return 0;
}

8
src/loader/default.c

@ -57,12 +57,8 @@ void load_default_config(const char *config_file) {
log_info("Loading default configure file");
char *config_content = NULL;
if (is_json_suffix(config_file)) { // convert to json format
char temp_file[] = "temp.yml";
save_file(temp_file, DEFAULT_CONFIG);
config_content = to_json(temp_file);
remove_file(temp_file);
}
if (config_content == NULL) {
config_content = to_json(DEFAULT_CONFIG);
} else {
config_content = strdup(DEFAULT_CONFIG);
}
save_file(config_file, config_content);

11
src/loader/parser.c

@ -177,17 +177,20 @@ void cleardns_parser(cleardns_config *config, const char *config_content) { // J
}
void config_parser(cleardns_config *config, const char *config_file) {
char *config_content;
char *config_content = read_file(config_file);
if (is_json_suffix(config_file)) { // JSON format
log_info("Start JSON configure parser");
config_content = read_file(config_file);
} else { // YAML or TOML format
log_info("Start configure parser");
config_content = to_json(config_file); // convert to json format
if (config_content == NULL) {
char *convert_ret = to_json(config_content);
if (convert_ret == NULL) { // convert failed
log_fatal("Configure parser error");
}
free(config_content);
config_content = convert_ret;
}
cleardns_parser(config, config_content); // configure parser
free(config_content);
log_info("Configure parser success");

Loading…
Cancel
Save