diff options
Diffstat (limited to '3rdparty/imgui-node-editor/crude_json.cpp')
-rw-r--r-- | 3rdparty/imgui-node-editor/crude_json.cpp | 80 |
1 files changed, 78 insertions, 2 deletions
diff --git a/3rdparty/imgui-node-editor/crude_json.cpp b/3rdparty/imgui-node-editor/crude_json.cpp index 34f117f..a9f7c70 100644 --- a/3rdparty/imgui-node-editor/crude_json.cpp +++ b/3rdparty/imgui-node-editor/crude_json.cpp @@ -1,4 +1,6 @@ -// Crude implementation of JSON value object and parser. +// Crude implementation of JSON value object and parser. +// +// VERSION 0.1 // // LICENSE // This software is dual-licensed to the public domain and under the following @@ -14,7 +16,10 @@ # include <clocale> # include <cmath> # include <cstring> - +# if CRUDE_JSON_IO +# include <stdio.h> +# include <memory> +# endif namespace crude_json { @@ -147,6 +152,22 @@ void value::push_back(value&& value) } } +size_t value::erase(const string& key) +{ + if (!is_object()) + return 0; + + auto& o = *object_ptr(m_Storage); + auto it = o.find(key); + + if (it == o.end()) + return 0; + + o.erase(it); + + return 1; +} + void value::swap(value& other) { using std::swap; @@ -811,4 +832,59 @@ value value::parse(const string& data) return v; } +# if CRUDE_JSON_IO +std::pair<value, bool> value::load(const string& path) +{ + // Modern C++, so beautiful... + std::unique_ptr<FILE, void(*)(FILE*)> file{nullptr, [](FILE* file) { if (file) fclose(file); }}; +# if defined(_MSC_VER) || (defined(__STDC_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__) + FILE* handle = nullptr; + if (fopen_s(&handle, path.c_str(), "rb") != 0) + return {value{}, false}; + file.reset(handle); +# else + file.reset(fopen(path.c_str(), "rb")); +# endif + + if (!file) + return {value{}, false}; + + fseek(file.get(), 0, SEEK_END); + auto size = static_cast<size_t>(ftell(file.get())); + fseek(file.get(), 0, SEEK_SET); + + string data; + data.resize(size); + if (fread(const_cast<char*>(data.data()), size, 1, file.get()) != 1) + return {value{}, false}; + + return {parse(data), true}; +} + +bool value::save(const string& path, const int indent, const char indent_char) const +{ + // Modern C++, so beautiful... + std::unique_ptr<FILE, void(*)(FILE*)> file{nullptr, [](FILE* file) { if (file) fclose(file); }}; +# if defined(_MSC_VER) || (defined(__STDC_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__) + FILE* handle = nullptr; + if (fopen_s(&handle, path.c_str(), "wb") != 0) + return false; + file.reset(handle); +# else + file.reset(fopen(path.c_str(), "wb")); +# endif + + if (!file) + return false; + + auto data = dump(indent, indent_char); + + if (fwrite(data.data(), data.size(), 1, file.get()) != 1) + return false; + + return true; +} + +# endif + } // namespace crude_json |