Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Lumengine committed Oct 12, 2023
2 parents b4edade + 2f99b2d commit 33e5923
Show file tree
Hide file tree
Showing 75 changed files with 23,861 additions and 15,475 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: build

on:
push:
pull_request:
workflow_run:
# Use a workflow as a trigger of scheduled builds. Forked repositories can disable scheduled builds by disabling
# "scheduled" workflow, while maintaining ability to perform local CI builds.
workflows:
- scheduled
branches:
- master
- develop
types:
- requested

env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
BUILD_TYPE: Release

jobs:
Windows:
runs-on: windows-2019
env:
VS_PATH: C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\
MSBUILD_PATH: C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\

steps:
- uses: actions/checkout@v2
- name: Configure CMake
run: cmake -S examples -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

macOS:
runs-on: macos-latest

steps:
- name: Install Dependencies
run: |
brew install glfw3
- uses: actions/checkout@v2
- name: Configure CMake
run: cmake -S examples -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

Linux:
runs-on: ubuntu-latest

steps:
- name: Install Dependencies
run: |
sudo apt-get update
sudo apt-get install -y libglfw3-dev
- uses: actions/checkout@v2
- name: Configure CMake
run: cmake -S examples -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

40 changes: 0 additions & 40 deletions .travis.yml

This file was deleted.

40 changes: 0 additions & 40 deletions appveyor.yml

This file was deleted.

78 changes: 77 additions & 1 deletion crude_json.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// 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
// license: you are granted a perpetual, irrevocable license to copy, modify,
Expand All @@ -14,7 +16,10 @@
# include <clocale>
# include <cmath>
# include <cstring>

# if CRUDE_JSON_IO
# include <stdio.h>
# include <memory>
# endif

namespace crude_json {

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
27 changes: 27 additions & 0 deletions crude_json.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// 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
// license: you are granted a perpetual, irrevocable license to copy, modify,
Expand All @@ -24,6 +26,10 @@
# define CRUDE_ASSERT(expr) assert(expr)
# endif

# ifndef CRUDE_JSON_IO
# define CRUDE_JSON_IO 1
# endif

namespace crude_json {

struct value;
Expand Down Expand Up @@ -92,6 +98,8 @@ struct value
void push_back(const value& value);
void push_back(value&& value);

size_t erase(const string& key);

bool is_primitive() const { return is_string() || is_number() || is_boolean() || is_null(); }
bool is_structured() const { return is_object() || is_array(); }
bool is_null() const { return m_Type == type_t::null; }
Expand All @@ -105,6 +113,9 @@ struct value
template <typename T> const T& get() const;
template <typename T> T& get();

template <typename T> const T* get_ptr() const;
template <typename T> T* get_ptr();

string dump(const int indent = -1, const char indent_char = ' ') const;

void swap(value& other);
Expand All @@ -114,6 +125,11 @@ struct value
// Returns discarded value for invalid inputs.
static value parse(const string& data);

# if CRUDE_JSON_IO
static std::pair<value, bool> load(const string& path);
bool save(const string& path, const int indent = -1, const char indent_char = ' ') const;
# endif

private:
struct parser;

Expand Down Expand Up @@ -217,6 +233,17 @@ template <> inline string& value::get<string>() { CRUDE_ASSERT(m_T
template <> inline boolean& value::get<boolean>() { CRUDE_ASSERT(m_Type == type_t::boolean); return *boolean_ptr(m_Storage); }
template <> inline number& value::get<number>() { CRUDE_ASSERT(m_Type == type_t::number); return *number_ptr(m_Storage); }

template <> inline const object* value::get_ptr<object>() const { if (m_Type == type_t::object) return object_ptr(m_Storage); else return nullptr; }
template <> inline const array* value::get_ptr<array>() const { if (m_Type == type_t::array) return array_ptr(m_Storage); else return nullptr; }
template <> inline const string* value::get_ptr<string>() const { if (m_Type == type_t::string) return string_ptr(m_Storage); else return nullptr; }
template <> inline const boolean* value::get_ptr<boolean>() const { if (m_Type == type_t::boolean) return boolean_ptr(m_Storage); else return nullptr; }
template <> inline const number* value::get_ptr<number>() const { if (m_Type == type_t::number) return number_ptr(m_Storage); else return nullptr; }

template <> inline object* value::get_ptr<object>() { if (m_Type == type_t::object) return object_ptr(m_Storage); else return nullptr; }
template <> inline array* value::get_ptr<array>() { if (m_Type == type_t::array) return array_ptr(m_Storage); else return nullptr; }
template <> inline string* value::get_ptr<string>() { if (m_Type == type_t::string) return string_ptr(m_Storage); else return nullptr; }
template <> inline boolean* value::get_ptr<boolean>() { if (m_Type == type_t::boolean) return boolean_ptr(m_Storage); else return nullptr; }
template <> inline number* value::get_ptr<number>() { if (m_Type == type_t::number) return number_ptr(m_Storage); else return nullptr; }

} // namespace crude_json

Expand Down
Loading

0 comments on commit 33e5923

Please sign in to comment.