~aleteoryx/muditaos

fd5f7f1821e5cafac6e9bf11df0e77f0189124b2 — Bartosz 3 years ago 6efaa60
[MOS-872] Extended json11 supported types

Moved statvfs to stack from heap
M module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp => module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp +6 -5
@@ 23,14 23,14 @@ namespace sdesktop::endpoints

        auto parseFileEntry(const std::filesystem::directory_entry &entry) -> json11::Json
        {
            int size = 0;
            uintmax_t size = 0;
            FileType type;

            if (entry.is_directory()) {
                type = FileType::directory;
            }
            else {
                size = static_cast<int>(entry.file_size());
                size = entry.file_size();
            }

            if (entry.is_regular_file()) {


@@ 370,14 370,15 @@ namespace sdesktop::endpoints
    auto FS_Helper::getFreeSpaceForUserFilesMiB() const -> unsigned long
    {
        const auto userDiskPath = purefs::dir::getUserDiskPath();
        struct statvfs vfstat
        {};

        auto vfstat = std::make_unique<struct statvfs>();
        if (statvfs(userDiskPath.c_str(), vfstat.get()) < 0) {
        if (statvfs(userDiskPath.c_str(), &vfstat) < 0) {
            return 0;
        }

        const auto freeBytes =
            (static_cast<std::uint64_t>(vfstat->f_bfree) * static_cast<std::uint64_t>(vfstat->f_bsize));
            (static_cast<std::uint64_t>(vfstat.f_bfree) * static_cast<std::uint64_t>(vfstat.f_bsize));
        const auto freeMiBs = freeBytes / bytesInMebibyte;

        return freeMiBs;

M third-party/json/CMakeLists.txt => third-party/json/CMakeLists.txt +5 -0
@@ 1,5 1,10 @@
add_library(json)
add_library(json::json ALIAS json)

if (${ENABLE_TESTS})
    add_subdirectory(test)
endif()

target_sources(json
    PRIVATE
        json11.cpp

M third-party/json/json11.cpp => third-party/json/json11.cpp +47 -0
@@ 25,6 25,7 @@
#include <cstdlib>
#include <cstdio>
#include <limits>
#include <cinttypes>

namespace json11 {



@@ 69,6 70,22 @@ static void dump(int value, string &out) {
    snprintf(buf, sizeof buf, "%d", value);
    out += buf;
}
static void dump(unsigned int value, string &out) {
    char buf[32];
    snprintf(buf, sizeof buf, "%u", value);
    out += buf;
}
static void dump(intmax_t value, string &out) {
    char buf[32];
    snprintf(buf, sizeof buf, "%" PRIiMAX, value);
    out += buf;
}
static void dump(uintmax_t value, string &out) {
    char buf[32];
    snprintf(buf, sizeof buf, "%" PRIuMAX, value);
    out += buf;
}


static void dump(bool value, string &out) {
    out += value ? "true" : "false";


@@ 187,6 204,30 @@ class JsonInt final : public Value<Json::NUMBER, int> {
public:
    explicit JsonInt(int value) : Value(value) {}
};
class JsonUnsignedInt final : public Value<Json::NUMBER, unsigned int> {
    double number_value() const override { return m_value; }
    unsigned int unsigned_int_value() const override { return m_value; }
    bool equals(const JsonValue * other) const override { return m_value == other->number_value(); }
    bool less(const JsonValue * other)   const override { return m_value <  other->number_value(); }
  public:
    explicit JsonUnsignedInt(unsigned int value) : Value(value) {}
};
class JsonIntmax_t final : public Value<Json::NUMBER, intmax_t> {
    double number_value() const override { return m_value; }
    intmax_t intmax_t_value() const override { return m_value; }
    bool equals(const JsonValue * other) const override { return m_value == other->number_value(); }
    bool less(const JsonValue * other)   const override { return m_value <  other->number_value(); }
  public:
    explicit JsonIntmax_t(intmax_t value) : Value(value) {}
};
class JsonUintmax_t final : public Value<Json::NUMBER, uintmax_t> {
    double number_value() const override { return m_value; }
    uintmax_t uintmax_t_value() const override { return m_value; }
    bool equals(const JsonValue * other) const override { return m_value == other->number_value(); }
    bool less(const JsonValue * other)   const override { return m_value <  other->number_value(); }
  public:
    explicit JsonUintmax_t(uintmax_t value) : Value(value) {}
};

class JsonBoolean final : public Value<Json::BOOL, bool> {
    bool bool_value() const override { return m_value; }


@@ 254,6 295,9 @@ Json::Json() noexcept                  : m_ptr(statics().null) {}
Json::Json(std::nullptr_t) noexcept    : m_ptr(statics().null) {}
Json::Json(double value)               : m_ptr(make_shared<JsonDouble>(value)) {}
Json::Json(int value)                  : m_ptr(make_shared<JsonInt>(value)) {}
Json::Json(unsigned int value)         : m_ptr(make_shared<JsonUnsignedInt>(value)) {}
Json::Json(intmax_t value)             : m_ptr(make_shared<JsonIntmax_t>(value)) {}
Json::Json(uintmax_t value)            : m_ptr(make_shared<JsonUintmax_t>(value)) {}
Json::Json(bool value)                 : m_ptr(value ? statics().t : statics().f) {}
Json::Json(const string &value)        : m_ptr(make_shared<JsonString>(value)) {}
Json::Json(string &&value)             : m_ptr(make_shared<JsonString>(move(value))) {}


@@ 279,6 323,9 @@ const Json & Json::operator[] (const string &key) const { return (*m_ptr)[key]; 

double                    JsonValue::number_value()              const { return 0; }
int                       JsonValue::int_value()                 const { return 0; }
unsigned int              JsonValue::unsigned_int_value()        const { return 0; }
intmax_t                  JsonValue::intmax_t_value()            const { return 0; }
uintmax_t                 JsonValue::uintmax_t_value()           const { return 0; }
bool                      JsonValue::bool_value()                const { return false; }
const string &            JsonValue::string_value()              const { return statics().empty_string; }
const vector<Json> &      JsonValue::array_items()               const { return statics().empty_vector; }

M third-party/json/json11.hpp => third-party/json/json11.hpp +9 -1
@@ 92,6 92,9 @@ public:
    Json(std::nullptr_t) noexcept;  // NUL
    Json(double value);             // NUMBER
    Json(int value);                // NUMBER
    Json(unsigned int value);       // NUMBER
    Json(intmax_t value);           // NUMBER
    Json(uintmax_t value);          // NUMBER
    Json(bool value);               // BOOL
    Json(const std::string &value); // STRING
    Json(std::string &&value);      // STRING


@@ 137,7 140,6 @@ public:
    // can both be applied to a NUMBER-typed object.
    double number_value() const;
    int int_value() const;

    // Return the enclosed value if this is a boolean, false otherwise.
    bool bool_value() const;
    // Return the enclosed string if this is a string, "" otherwise.


@@ 213,6 215,9 @@ class JsonValue {
protected:
    friend class Json;
    friend class JsonInt;
    friend class JsonUnsignedInt;
    friend class JsonIntmax_t;
    friend class JsonUintmax_t;
    friend class JsonDouble;
    virtual Json::Type type() const = 0;
    virtual bool equals(const JsonValue * other) const = 0;


@@ 220,6 225,9 @@ protected:
    virtual void dump(std::string &out) const = 0;
    virtual double number_value() const;
    virtual int int_value() const;
    virtual unsigned int unsigned_int_value() const;
    virtual intmax_t intmax_t_value() const;
    virtual uintmax_t uintmax_t_value() const;
    virtual bool bool_value() const;
    virtual const std::string &string_value() const;
    virtual const Json::array &array_items() const;

A third-party/json/test/CMakeLists.txt => third-party/json/test/CMakeLists.txt +8 -0
@@ 0,0 1,8 @@
add_catch2_executable(
    NAME
        json-test
    SRCS
        json11-test.cpp
    LIBS
        json
)

A third-party/json/test/json11-test.cpp => third-party/json/test/json11-test.cpp +224 -0
@@ 0,0 1,224 @@
#include <catch2/catch.hpp>
#include "json11.hpp"

TEST_CASE("json11 - custom types test")
{
    std::string err{};
    std::string dumpString{};

    SECTION("signed"){
        SECTION("intmax_t - max")
        {
            intmax_t value = INTMAX_MAX;
            auto json = json11::Json::object{
                     {"test", value}};
            REQUIRE(json["test"] == value);

            dumpString = json11::Json(json).dump();
            auto newJson = json11::Json::parse(dumpString,err);
            REQUIRE(err.empty());
            REQUIRE(newJson == json);
            REQUIRE(newJson["test"] == value);
        }
        SECTION("intmax_t - min")
        {
            intmax_t value = INTMAX_MIN;
            auto json = json11::Json::object{
                     {"test", value}};
            REQUIRE(json["test"] == value);

            dumpString = json11::Json(json).dump();
            auto newJson = json11::Json::parse(dumpString,err);
            REQUIRE(err.empty());
            REQUIRE(newJson == json);
            REQUIRE(newJson["test"] == value);
        }
        SECTION("int64_t - max")
        {
            int64_t value = INT64_MAX;
            auto json = json11::Json::object{
                    {"test", value}};
            REQUIRE(json["test"] == value);

            dumpString = json11::Json(json).dump();
            auto newJson = json11::Json::parse(dumpString,err);
            REQUIRE(err.empty());
            REQUIRE(newJson == json);
            REQUIRE(newJson["test"] == value);
        }
        SECTION("int64_t - min")
        {
            int64_t value = INT64_MIN;
            auto json = json11::Json::object{
                    {"test", value}};
            REQUIRE(json["test"] == value);

            dumpString = json11::Json(json).dump();
            auto newJson = json11::Json::parse(dumpString,err);
            REQUIRE(err.empty());
            REQUIRE(newJson == json);
            REQUIRE(newJson["test"] == value);
        }
        SECTION("int32_t - max")
        {
            int32_t value = INT32_MAX;
            auto json = json11::Json::object{
                    {"test", value}};
            REQUIRE(json["test"] == value);

            dumpString = json11::Json(json).dump();
            auto newJson = json11::Json::parse(dumpString,err);
            REQUIRE(err.empty());
            REQUIRE(newJson == json);
            REQUIRE(newJson["test"] == value);
        }
        SECTION("int32_t - min")
        {
            int32_t value = INT32_MIN;
            auto json = json11::Json::object{
                    {"test", value}};
            REQUIRE(json["test"] == value);

            dumpString = json11::Json(json).dump();
            auto newJson = json11::Json::parse(dumpString,err);
            REQUIRE(err.empty());
            REQUIRE(newJson == json);
            REQUIRE(newJson["test"] == value);
        }
        SECTION("int16_t - min")
        {
            int16_t value = INT16_MAX;
            auto json = json11::Json::object{
                    {"test", value}};
            REQUIRE(json["test"] == value);

            dumpString = json11::Json(json).dump();
            auto newJson = json11::Json::parse(dumpString,err);
            REQUIRE(err.empty());
            REQUIRE(newJson == json);
            REQUIRE(newJson["test"] == value);
        }
        SECTION("int16_t - max")
        {
            int16_t value = INT16_MIN;
            auto json = json11::Json::object{
                    {"test", value}};
            REQUIRE(json["test"] == value);

            dumpString = json11::Json(json).dump();
            auto newJson = json11::Json::parse(dumpString,err);
            REQUIRE(err.empty());
            REQUIRE(newJson == json);
            REQUIRE(newJson["test"] == value);
        }
        SECTION("int8_t - min")
        {
            int8_t value = INT8_MAX;
            auto json = json11::Json::object{
                    {"test", value}};
            REQUIRE(json["test"] == value);

            dumpString = json11::Json(json).dump();
            auto newJson = json11::Json::parse(dumpString,err);
            REQUIRE(err.empty());
            REQUIRE(newJson == json);
            REQUIRE(newJson["test"] == value);
        }
        SECTION("int8_t - max")
        {
            int8_t value = INT8_MIN;
            auto json = json11::Json::object{
                    {"test", value}};
            REQUIRE(json["test"] == value);

            dumpString = json11::Json(json).dump();
            auto newJson = json11::Json::parse(dumpString,err);
            REQUIRE(err.empty());
            REQUIRE(newJson == json);
            REQUIRE(newJson["test"] == value);
        }
    }

    SECTION("unsigned"){
        SECTION("uintmax_t")
        {
            uintmax_t value = UINTMAX_MAX;
            auto json = json11::Json::object{
                     {"test", value}};
            REQUIRE(json["test"] == value);

            dumpString = json11::Json(json).dump();
            auto newJson = json11::Json::parse(dumpString,err);
            REQUIRE(err.empty());
            REQUIRE(newJson == json);
            REQUIRE(newJson["test"] == value);
        }
        SECTION("uint64_t")
        {
            uint64_t value = UINT64_MAX;
            auto json = json11::Json::object{
                    {"test", value}};
            REQUIRE(json["test"] == value);

            dumpString = json11::Json(json).dump();
            auto newJson = json11::Json::parse(dumpString,err);
            REQUIRE(err.empty());
            REQUIRE(newJson == json);
            REQUIRE(newJson["test"] == value);
        }
        SECTION("uint32_t")
        {
            uint32_t value = UINT32_MAX;
            auto json = json11::Json::object{
                    {"test", value}};
            REQUIRE(json["test"] == value);

            dumpString = json11::Json(json).dump();
            auto newJson = json11::Json::parse(dumpString,err);
            REQUIRE(err.empty());
            REQUIRE(newJson == json);
            REQUIRE(newJson["test"] == value);
        }
        SECTION("uint16_t")
        {
            uint16_t value = UINT16_MAX;
            auto json = json11::Json::object{
                    {"test", value}};
            REQUIRE(json["test"] == value);

            dumpString = json11::Json(json).dump();
            auto newJson = json11::Json::parse(dumpString,err);
            REQUIRE(err.empty());
            REQUIRE(newJson == json);
            REQUIRE(newJson["test"] == value);
        }
        SECTION("uint8_t")
        {
            uint8_t value = UINT8_MAX;
            auto json = json11::Json::object{
                   {"test", value}};
            REQUIRE(json["test"] == value);

            dumpString = json11::Json(json).dump();
            auto newJson = json11::Json::parse(dumpString,err);
            REQUIRE(err.empty());
            REQUIRE(newJson == json);
            REQUIRE(newJson["test"] == value);
        }
        SECTION("size_t")
        {
            size_t value = SIZE_MAX;
            auto json = json11::Json::object{
                   {"test", value}};
            REQUIRE(json["test"] == value);

            dumpString = json11::Json(json).dump();
            auto newJson = json11::Json::parse(dumpString,err);
            REQUIRE(err.empty());
            REQUIRE(newJson == json);
            REQUIRE(newJson["test"] == value);
        }
    }

}