Browse Source

feat: add to-json for cleardns

dev
Dnomd343 2 years ago
parent
commit
2eed5d674b
  1. 0
      include/to_json.h
  2. 2
      src/CMakeLists.txt
  3. 21
      src/cleardns.c
  4. 1
      src/common/CMakeLists.txt
  5. 46
      src/common/json.c

0
src/to-json/to_json.h → include/to_json.h

2
src/CMakeLists.txt

@ -7,6 +7,8 @@ include_directories(${PROJECT_SOURCE_DIR}/include/bcrypt)
include_directories(${PROJECT_SOURCE_DIR}/include/common)
include_directories(${PROJECT_SOURCE_DIR}/include/loader)
link_directories(${PROJECT_SOURCE_DIR}/src/to-json/target/release)
add_subdirectory(utils)
add_subdirectory(applet)
add_subdirectory(bcrypt)

21
src/cleardns.c

@ -114,9 +114,24 @@ void cleardns() { // cleardns service
process_list_daemon(); // daemon all process
}
#include "json.h"
int main(int argc, char *argv[]) {
init(argc, argv);
log_info("ClearDNS server start (%s)", VERSION);
cleardns();
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();
return 0;
}

1
src/common/CMakeLists.txt

@ -1,3 +1,4 @@
cmake_minimum_required(VERSION 2.8.12)
add_library(common json.c sundry.c system.c structure.c)
target_link_libraries(common to_json)

46
src/common/json.c

@ -4,6 +4,7 @@
#include "logger.h"
#include "sundry.h"
#include "system.h"
#include "to_json.h"
#include "constant.h"
#include "structure.h"
@ -17,26 +18,39 @@ uint8_t is_json_suffix(const char *file_name) { // whether file name end with `.
return FALSE;
}
char* to_json(const char *file) { // convert JSON / TOML / YAML to json format (if failed -> return NULL)
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* 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 *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 = strdup(json_string); // load string into owner heap
char *json_content = NULL;
if (!to_json_ret) { // toJSON return zero code (convert fine)
json_content = read_file(output_file);
free_rust_string(json_string); // free rust string
if (strlen(json_content) == 0) { // empty string -> convert error
free(json_content);
return NULL;
}
remove_file(output_file);
free(output_file);
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)

Loading…
Cancel
Save