diff --git a/src/to-json/to_json.h b/include/to_json.h similarity index 100% rename from src/to-json/to_json.h rename to include/to_json.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 08534e5..afa07b3 100644 --- a/src/CMakeLists.txt +++ b/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) diff --git a/src/cleardns.c b/src/cleardns.c index 3ad366a..6439ea1 100644 --- a/src/cleardns.c +++ b/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; } diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 2e7dff9..d74b0be 100644 --- a/src/common/CMakeLists.txt +++ b/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) diff --git a/src/common/json.c b/src/common/json.c index 95f33e7..f9e6912 100644 --- a/src/common/json.c +++ b/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)