diff --git a/cpp/config.toml b/cpp/config.toml new file mode 100644 index 0000000..b88f474 --- /dev/null +++ b/cpp/config.toml @@ -0,0 +1,6 @@ +[main] +height = 720 +width = 1280 + +[others] +backgrounds = [ 'test1.png', 'test2.png', 'test3.png' ] \ No newline at end of file diff --git a/cpp/jsonfile_tests/json_object b/cpp/jsonfile_tests/json_object new file mode 100644 index 0000000..9c295d7 Binary files /dev/null and b/cpp/jsonfile_tests/json_object differ diff --git a/cpp/jsonfile_tests/json_object.cpp b/cpp/jsonfile_tests/json_object.cpp index e0e8d0e..913e881 100644 --- a/cpp/jsonfile_tests/json_object.cpp +++ b/cpp/jsonfile_tests/json_object.cpp @@ -19,22 +19,30 @@ void read_json_file() void create_json_file() { // Create json data - json data = json::parse(R"( - { - data1 : {}, - data2 : {} - } - )"); + json data = json::parse(R"({ + "error": { + "code": 101, + "message": "operation failed!" + }, + "result": { + "backgrounds": ["test1.png", "test2.png", "test3.png"] + }, + "data2": {} + })"); // Create json object for a key - // json data2 = json::parse(R"( - // { - // test1:2, - // test2:3 - // } - // )"); + json data2 = json::parse(R"({ + "error": { + "code": 101, + "message": "operation failed!" + }, + "result": { + "backgrounds": ["test1.png", "test2.png", "test3.png"] + }, + "data2": {} + })"); - // data["data2"] = data; + data["data2"] = data2; // auto data2 = data["data2"]; // std::cout << data2 << std::endl; diff --git a/cpp/jsonfile_tests/test.json b/cpp/jsonfile_tests/test.json new file mode 100644 index 0000000..b97f3f3 --- /dev/null +++ b/cpp/jsonfile_tests/test.json @@ -0,0 +1,27 @@ +{ + "data2": { + "data2": {}, + "error": { + "code": 101, + "message": "operation failed!" + }, + "result": { + "backgrounds": [ + "test1.png", + "test2.png", + "test3.png" + ] + } + }, + "error": { + "code": 101, + "message": "operation failed!" + }, + "result": { + "backgrounds": [ + "test1.png", + "test2.png", + "test3.png" + ] + } +} \ No newline at end of file diff --git a/cpp/toml_test b/cpp/toml_test new file mode 100644 index 0000000..7ae08ad Binary files /dev/null and b/cpp/toml_test differ diff --git a/cpp/toml_test.cpp b/cpp/toml_test.cpp index e69de29..8ed374a 100644 --- a/cpp/toml_test.cpp +++ b/cpp/toml_test.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include "../toml/toml.hpp" +using namespace std::string_view_literals; + +void save_toml_file() +{ + static constexpr std::string_view some_toml = R"( + [main] + width = 1280 + height = 720 + + [others] + backgrounds = ["test1.png", "test2.png", "test3.png"] + )"sv; + + try + { + // parse directly from a string view: + toml::table tbl = toml::parse(some_toml); + std::cout << tbl << "\n"; + + // Save data to a file + std::fstream outfile; + outfile.open("config.toml", std::ios_base::out); + if (outfile.is_open()) + { + outfile << tbl; + } + outfile.close(); + + // // parse from a string stream: + // { + // std::stringstream ss{ std::string{ some_toml } }; + // toml::table tbl = toml::parse(ss); + // std::cout << tbl << "\n"; + // } + } + catch (const toml::parse_error &err) + { + std::cerr << "Parsing failed:\n" + << err << "\n"; + return; + } +} + +int main() +{ + save_toml_file(); + + return 0; +} \ No newline at end of file