2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2021-09-15 18:51:19 +00:00
|
|
|
#include <algorithm> // std::sort
|
2023-11-21 15:02:03 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iterator>
|
|
|
|
#include <optional>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <string_view>
|
2023-11-21 15:02:03 +00:00
|
|
|
#include <utility>
|
2021-09-15 18:51:19 +00:00
|
|
|
#include <vector>
|
2023-11-21 15:02:03 +00:00
|
|
|
#include <variant>
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2014-07-28 04:13:38 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <share.h>
|
2012-12-14 04:36:33 +00:00
|
|
|
#endif
|
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2023-06-21 03:50:36 +00:00
|
|
|
#include <small/vector.hpp>
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#define LIBTRANSMISSION_VARIANT_MODULE
|
2017-11-14 20:21:28 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/error.h"
|
|
|
|
#include "libtransmission/log.h"
|
|
|
|
#include "libtransmission/quark.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
|
|
|
#include "libtransmission/utils.h"
|
|
|
|
#include "libtransmission/variant.h"
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2021-11-01 21:30:18 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
namespace
|
|
|
|
{
|
2023-12-17 18:49:39 +00:00
|
|
|
[[nodiscard]] constexpr bool variant_is_container(tr_variant const* const var)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-21 21:16:54 +00:00
|
|
|
return var != nullptr && (var->holds_alternative<tr_variant::Vector>() || var->holds_alternative<tr_variant::Map>());
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-12-17 18:49:39 +00:00
|
|
|
[[nodiscard]] constexpr int variant_index(tr_variant const* const var)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
if (var != nullptr)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
return var->index();
|
2023-08-21 04:15:23 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
return tr_variant::NoneIndex;
|
|
|
|
}
|
2023-12-17 18:49:39 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
[[nodiscard]] tr_variant* dict_set(tr_variant* const var, tr_quark const key, T&& val)
|
|
|
|
{
|
|
|
|
TR_ASSERT(var != nullptr);
|
|
|
|
TR_ASSERT(var->holds_alternative<tr_variant::Map>());
|
|
|
|
|
|
|
|
if (auto* const map = var != nullptr ? var->get_if<tr_variant::MapIndex>() : nullptr; map != nullptr)
|
|
|
|
{
|
|
|
|
map->erase(key);
|
|
|
|
return &map->try_emplace(key, std::forward<T>(val)).first;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
[[nodiscard]] tr_variant* vec_add(tr_variant* const var, T&& val)
|
|
|
|
{
|
|
|
|
TR_ASSERT(var != nullptr);
|
|
|
|
TR_ASSERT(var->holds_alternative<tr_variant::Vector>());
|
|
|
|
|
|
|
|
if (auto* const vec = var != nullptr ? var->get_if<tr_variant::VectorIndex>() : nullptr; vec != nullptr)
|
|
|
|
{
|
|
|
|
return &vec->emplace_back(std::forward<T>(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
2023-08-23 17:57:58 +00:00
|
|
|
} // namespace
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
// ---
|
|
|
|
|
|
|
|
tr_variant::StringHolder::StringHolder(std::string&& str) noexcept
|
|
|
|
: str_{ std::move(str) }
|
|
|
|
{
|
|
|
|
sv_ = str_;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
tr_variant::StringHolder::StringHolder(StringHolder&& that) noexcept
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
*this = std::move(that);
|
2023-01-05 04:16:22 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
void tr_variant::StringHolder::set_unmanaged(std::string_view sv)
|
2023-01-05 04:16:22 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
str_.clear();
|
|
|
|
sv_ = sv;
|
|
|
|
}
|
2023-01-05 04:16:22 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
tr_variant::StringHolder& tr_variant::StringHolder::operator=(StringHolder&& that) noexcept
|
|
|
|
{
|
|
|
|
auto const managed = std::data(that.sv_) == std::data(that.str_);
|
|
|
|
std::swap(str_, that.str_);
|
|
|
|
sv_ = managed ? str_ : that.sv_;
|
|
|
|
return *this;
|
|
|
|
}
|
2023-01-05 04:16:22 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
// ---
|
2023-01-05 04:16:22 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
tr_variant::Merge::Merge(tr_variant& tgt)
|
|
|
|
: tgt_{ tgt }
|
|
|
|
{
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
void tr_variant::Merge::operator()(std::monostate const& src)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
tgt_ = src;
|
|
|
|
}
|
|
|
|
void tr_variant::Merge::operator()(bool const& src)
|
|
|
|
{
|
|
|
|
tgt_ = src;
|
|
|
|
}
|
|
|
|
void tr_variant::Merge::operator()(int64_t const& src)
|
|
|
|
{
|
|
|
|
tgt_ = src;
|
|
|
|
}
|
|
|
|
void tr_variant::Merge::operator()(double const& src)
|
|
|
|
{
|
|
|
|
tgt_ = src;
|
|
|
|
}
|
|
|
|
void tr_variant::Merge::operator()(tr_variant::StringHolder const& src)
|
|
|
|
{
|
|
|
|
tgt_ = src.sv_;
|
|
|
|
}
|
2023-01-05 04:16:22 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
void tr_variant::Merge::operator()(tr_variant::Vector const& src)
|
|
|
|
{
|
|
|
|
auto const n_items = std::size(src);
|
|
|
|
auto& tgt = tgt_.val_.emplace<Vector>();
|
|
|
|
tgt.resize(n_items);
|
|
|
|
for (size_t i = 0; i < n_items; ++i)
|
2023-01-05 04:16:22 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
std::visit(Merge{ tgt[i] }, src[i].val_);
|
2023-01-05 04:16:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
void tr_variant::Merge::operator()(tr_variant::Map const& src)
|
2023-01-05 04:16:22 +00:00
|
|
|
{
|
2023-12-17 18:49:39 +00:00
|
|
|
// if tgt_ isn't already a map, make it one
|
2023-08-23 17:57:58 +00:00
|
|
|
if (tgt_.index() != tr_variant::MapIndex)
|
2023-08-21 04:15:23 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
tgt_.val_.emplace<tr_variant::Map>();
|
2023-08-21 04:15:23 +00:00
|
|
|
}
|
2023-12-17 18:49:39 +00:00
|
|
|
|
|
|
|
if (auto* tgt = tgt_.get_if<tr_variant::MapIndex>(); tgt != nullptr)
|
2023-08-21 04:15:23 +00:00
|
|
|
{
|
2023-12-17 18:49:39 +00:00
|
|
|
tgt->reserve(std::size(*tgt) + std::size(src));
|
|
|
|
for (auto const& [key, val] : src)
|
|
|
|
{
|
|
|
|
std::visit(Merge{ (*tgt)[key] }, val.val_);
|
|
|
|
}
|
2023-08-21 04:15:23 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
// ---
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
tr_variant* tr_variantDictFind(tr_variant* const var, tr_quark key)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
if (auto* const map = var != nullptr ? var->get_if<tr_variant::MapIndex>() : nullptr; map != nullptr)
|
|
|
|
{
|
|
|
|
if (auto iter = map->find(key); iter != std::end(*map))
|
|
|
|
{
|
|
|
|
return &iter->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
tr_variant* tr_variantListChild(tr_variant* const var, size_t pos)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
if (auto* const vec = var != nullptr ? var->get_if<tr_variant::VectorIndex>() : nullptr; vec != nullptr)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
if (pos < std::size(*vec))
|
2023-08-21 21:16:54 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
return &vec->at(pos);
|
2023-08-21 21:16:54 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
return {};
|
|
|
|
}
|
2022-08-29 02:51:26 +00:00
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
bool tr_variantListRemove(tr_variant* const var, size_t pos)
|
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
if (auto* const vec = var != nullptr ? var->get_if<tr_variant::VectorIndex>() : nullptr;
|
|
|
|
vec != nullptr && pos < std::size(*vec))
|
|
|
|
{
|
|
|
|
vec->erase(std::begin(*vec) + pos);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
bool tr_variantGetInt(tr_variant const* const var, int64_t* setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
switch (variant_index(var))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
case tr_variant::IntIndex:
|
2023-09-01 22:23:44 +00:00
|
|
|
if (setme != nullptr)
|
|
|
|
{
|
|
|
|
*setme = *var->get_if<tr_variant::IntIndex>();
|
|
|
|
}
|
2023-08-21 04:15:23 +00:00
|
|
|
return true;
|
2019-03-17 06:09:08 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
case tr_variant::BoolIndex:
|
2023-09-01 22:23:44 +00:00
|
|
|
if (setme != nullptr)
|
|
|
|
{
|
|
|
|
*setme = *var->get_if<tr_variant::BoolIndex>() ? 1 : 0;
|
|
|
|
}
|
2023-08-21 04:15:23 +00:00
|
|
|
return true;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
bool tr_variantGetStrView(tr_variant const* const var, std::string_view* setme)
|
2021-11-01 21:30:18 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
switch (variant_index(var))
|
2021-11-01 21:30:18 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
case tr_variant::StringIndex:
|
|
|
|
*setme = *var->get_if<tr_variant::StringIndex>();
|
2023-08-21 04:15:23 +00:00
|
|
|
return true;
|
2021-11-01 21:30:18 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2021-11-01 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 18:40:12 +00:00
|
|
|
bool tr_variantGetRaw(tr_variant const* v, std::byte const** setme_raw, size_t* setme_len)
|
|
|
|
{
|
2023-07-28 21:14:31 +00:00
|
|
|
if (auto sv = std::string_view{}; tr_variantGetStrView(v, &sv))
|
2022-10-24 18:40:12 +00:00
|
|
|
{
|
2023-07-28 21:14:31 +00:00
|
|
|
*setme_raw = reinterpret_cast<std::byte const*>(std::data(sv));
|
|
|
|
*setme_len = std::size(sv);
|
|
|
|
return true;
|
2022-10-24 18:40:12 +00:00
|
|
|
}
|
|
|
|
|
2023-07-28 21:14:31 +00:00
|
|
|
return false;
|
2022-10-24 18:40:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_variantGetRaw(tr_variant const* v, uint8_t const** setme_raw, size_t* setme_len)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-07-28 21:14:31 +00:00
|
|
|
if (auto sv = std::string_view{}; tr_variantGetStrView(v, &sv))
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-07-28 21:14:31 +00:00
|
|
|
*setme_raw = reinterpret_cast<uint8_t const*>(std::data(sv));
|
|
|
|
*setme_len = std::size(sv);
|
|
|
|
return true;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-07-28 21:14:31 +00:00
|
|
|
return false;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
bool tr_variantGetBool(tr_variant const* const var, bool* setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
switch (variant_index(var))
|
2023-08-21 04:15:23 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
case tr_variant::BoolIndex:
|
|
|
|
*setme = *var->get_if<tr_variant::BoolIndex>();
|
2021-11-01 21:30:18 +00:00
|
|
|
return true;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
case tr_variant::IntIndex:
|
|
|
|
if (auto const val = *var->get_if<tr_variant::IntIndex>(); val == 0 || val == 1)
|
|
|
|
{
|
|
|
|
*setme = val != 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
case tr_variant::StringIndex:
|
|
|
|
if (auto const val = *var->get_if<tr_variant::StringIndex>(); val == "true"sv)
|
2021-11-01 21:30:18 +00:00
|
|
|
{
|
|
|
|
*setme = true;
|
|
|
|
return true;
|
|
|
|
}
|
2023-08-23 17:57:58 +00:00
|
|
|
else if (val == "false"sv)
|
2021-11-01 21:30:18 +00:00
|
|
|
{
|
|
|
|
*setme = false;
|
|
|
|
return true;
|
|
|
|
}
|
2023-08-23 17:57:58 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2021-11-01 21:30:18 +00:00
|
|
|
return false;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
bool tr_variantGetReal(tr_variant const* const var, double* setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
switch (variant_index(var))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
case tr_variant::DoubleIndex:
|
|
|
|
*setme = *var->get_if<tr_variant::DoubleIndex>();
|
2023-08-21 04:15:23 +00:00
|
|
|
return true;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
case tr_variant::IntIndex:
|
|
|
|
*setme = static_cast<double>(*var->get_if<tr_variant::IntIndex>());
|
2023-08-21 04:15:23 +00:00
|
|
|
return true;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
case tr_variant::StringIndex:
|
|
|
|
if (auto const val = tr_num_parse<double>(*var->get_if<tr_variant::StringIndex>()); val)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-08-21 04:15:23 +00:00
|
|
|
*setme = *val;
|
|
|
|
return true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2023-08-23 17:57:58 +00:00
|
|
|
[[fallthrough]];
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
bool tr_variantDictFindInt(tr_variant* const var, tr_quark key, int64_t* setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-21 04:15:23 +00:00
|
|
|
auto const* const child = tr_variantDictFind(var, key);
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_variantGetInt(child, setme);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
bool tr_variantDictFindBool(tr_variant* const var, tr_quark key, bool* setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-21 04:15:23 +00:00
|
|
|
auto const* const child = tr_variantDictFind(var, key);
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_variantGetBool(child, setme);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
bool tr_variantDictFindReal(tr_variant* const var, tr_quark key, double* setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-21 04:15:23 +00:00
|
|
|
auto const* const child = tr_variantDictFind(var, key);
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_variantGetReal(child, setme);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
bool tr_variantDictFindStrView(tr_variant* const var, tr_quark key, std::string_view* setme)
|
2021-11-01 21:30:18 +00:00
|
|
|
{
|
2023-08-21 04:15:23 +00:00
|
|
|
auto const* const child = tr_variantDictFind(var, key);
|
2021-11-01 21:30:18 +00:00
|
|
|
return tr_variantGetStrView(child, setme);
|
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
bool tr_variantDictFindList(tr_variant* const var, tr_quark key, tr_variant** setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
if (auto* const res = tr_variantDictFind(var, key); res != nullptr && res->holds_alternative<tr_variant::Vector>())
|
|
|
|
{
|
|
|
|
*setme = res;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
bool tr_variantDictFindDict(tr_variant* const var, tr_quark key, tr_variant** setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
if (auto* const res = tr_variantDictFind(var, key); res != nullptr && res->holds_alternative<tr_variant::Map>())
|
|
|
|
{
|
|
|
|
*setme = res;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
bool tr_variantDictFindRaw(tr_variant* const var, tr_quark key, uint8_t const** setme_raw, size_t* setme_len)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-21 04:15:23 +00:00
|
|
|
auto const* const child = tr_variantDictFind(var, key);
|
2022-10-24 18:40:12 +00:00
|
|
|
return tr_variantGetRaw(child, setme_raw, setme_len);
|
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
bool tr_variantDictFindRaw(tr_variant* const var, tr_quark key, std::byte const** setme_raw, size_t* setme_len)
|
2022-10-24 18:40:12 +00:00
|
|
|
{
|
2023-08-21 04:15:23 +00:00
|
|
|
auto const* const child = tr_variantDictFind(var, key);
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_variantGetRaw(child, setme_raw, setme_len);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-09-13 13:49:17 +00:00
|
|
|
void tr_variantInitList(tr_variant* initme, size_t n_reserve)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
auto vec = tr_variant::Vector{};
|
2023-09-13 13:49:17 +00:00
|
|
|
vec.reserve(n_reserve);
|
2023-08-23 17:57:58 +00:00
|
|
|
*initme = std::move(vec);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 13:49:17 +00:00
|
|
|
void tr_variantListReserve(tr_variant* const var, size_t n_reserve)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-21 21:16:54 +00:00
|
|
|
TR_ASSERT(var != nullptr);
|
|
|
|
TR_ASSERT(var->holds_alternative<tr_variant::Vector>());
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
if (auto* const vec = var != nullptr ? var->get_if<tr_variant::VectorIndex>() : nullptr; vec != nullptr)
|
|
|
|
{
|
2023-09-13 13:49:17 +00:00
|
|
|
vec->reserve(std::size(*vec) + n_reserve);
|
2023-08-23 17:57:58 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 13:49:17 +00:00
|
|
|
void tr_variantInitDict(tr_variant* initme, size_t n_reserve)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-09-13 13:49:17 +00:00
|
|
|
*initme = tr_variant::Map{ n_reserve };
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 13:49:17 +00:00
|
|
|
void tr_variantDictReserve(tr_variant* const var, size_t n_reserve)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-09-13 13:49:17 +00:00
|
|
|
TR_ASSERT(var != nullptr);
|
|
|
|
TR_ASSERT(var->holds_alternative<tr_variant::Map>());
|
|
|
|
|
|
|
|
if (auto* const map = var != nullptr ? var->get_if<tr_variant::MapIndex>() : nullptr; map != nullptr)
|
|
|
|
{
|
|
|
|
map->reserve(std::size(*map) + n_reserve);
|
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
tr_variant* tr_variantListAdd(tr_variant* const var)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-21 21:16:54 +00:00
|
|
|
TR_ASSERT(var != nullptr);
|
|
|
|
TR_ASSERT(var->holds_alternative<tr_variant::Vector>());
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
if (auto* const vec = var != nullptr ? var->get_if<tr_variant::VectorIndex>() : nullptr; vec != nullptr)
|
|
|
|
{
|
|
|
|
return &vec->emplace_back();
|
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
return nullptr;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-12-17 18:49:39 +00:00
|
|
|
tr_variant* tr_variantListAddInt(tr_variant* const var, int64_t const value)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-12-17 18:49:39 +00:00
|
|
|
return vec_add(var, value);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-12-17 18:49:39 +00:00
|
|
|
tr_variant* tr_variantListAddReal(tr_variant* const var, double const value)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-12-17 18:49:39 +00:00
|
|
|
return vec_add(var, value);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-12-17 18:49:39 +00:00
|
|
|
tr_variant* tr_variantListAddBool(tr_variant* const var, bool const value)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-12-17 18:49:39 +00:00
|
|
|
return vec_add(var, value);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-12-17 18:49:39 +00:00
|
|
|
tr_variant* tr_variantListAddStr(tr_variant* const var, std::string_view const value)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-12-17 18:49:39 +00:00
|
|
|
return vec_add(var, std::string{ value });
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
tr_variant* tr_variantListAddStrView(tr_variant* const var, std::string_view value)
|
2021-11-15 07:21:57 +00:00
|
|
|
{
|
2023-12-17 18:49:39 +00:00
|
|
|
return vec_add(var, tr_variant::unmanaged_string(value));
|
2021-11-15 07:21:57 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 13:49:17 +00:00
|
|
|
tr_variant* tr_variantListAddRaw(tr_variant* const var, void const* value, size_t n_bytes)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-12-17 18:49:39 +00:00
|
|
|
return vec_add(var, tr_variant::make_raw(value, n_bytes));
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-12-17 18:49:39 +00:00
|
|
|
tr_variant* tr_variantListAddList(tr_variant* const var, size_t const n_reserve)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-12-17 18:49:39 +00:00
|
|
|
return vec_add(var, tr_variant::make_vector(n_reserve));
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-12-17 18:49:39 +00:00
|
|
|
tr_variant* tr_variantListAddDict(tr_variant* const var, size_t const n_reserve)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-12-17 18:49:39 +00:00
|
|
|
return vec_add(var, tr_variant::make_map(n_reserve));
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
tr_variant* tr_variantDictAdd(tr_variant* const var, tr_quark key)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-21 21:16:54 +00:00
|
|
|
TR_ASSERT(var != nullptr);
|
|
|
|
TR_ASSERT(var->holds_alternative<tr_variant::Map>());
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
if (auto* const map = var != nullptr ? var->get_if<tr_variant::MapIndex>() : nullptr; map != nullptr)
|
|
|
|
{
|
|
|
|
return &(*map)[key];
|
|
|
|
}
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
return {};
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-12-17 18:49:39 +00:00
|
|
|
tr_variant* tr_variantDictAddInt(tr_variant* const var, tr_quark const key, int64_t const val)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-12-17 18:49:39 +00:00
|
|
|
return dict_set(var, key, val);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
tr_variant* tr_variantDictAddBool(tr_variant* const var, tr_quark key, bool val)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-12-17 18:49:39 +00:00
|
|
|
return dict_set(var, key, val);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-12-17 18:49:39 +00:00
|
|
|
tr_variant* tr_variantDictAddReal(tr_variant* const var, tr_quark const key, double const val)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-12-17 18:49:39 +00:00
|
|
|
return dict_set(var, key, val);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-12-17 18:49:39 +00:00
|
|
|
tr_variant* tr_variantDictAddStr(tr_variant* const var, tr_quark const key, std::string_view const val)
|
2012-12-14 16:04:44 +00:00
|
|
|
{
|
2023-12-17 18:49:39 +00:00
|
|
|
return dict_set(var, key, val);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-12-17 18:49:39 +00:00
|
|
|
tr_variant* tr_variantDictAddRaw(tr_variant* const var, tr_quark const key, void const* const value, size_t const n_bytes)
|
2021-11-15 07:21:57 +00:00
|
|
|
{
|
2023-12-17 18:49:39 +00:00
|
|
|
return dict_set(var, key, std::string{ static_cast<char const*>(value), n_bytes });
|
2021-11-15 07:21:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-17 18:49:39 +00:00
|
|
|
tr_variant* tr_variantDictAddList(tr_variant* const var, tr_quark const key, size_t const n_reserve)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-12-17 18:49:39 +00:00
|
|
|
return dict_set(var, key, tr_variant::make_vector(n_reserve));
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-12-17 18:49:39 +00:00
|
|
|
tr_variant* tr_variantDictAddStrView(tr_variant* const var, tr_quark const key, std::string_view const val)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-12-17 18:49:39 +00:00
|
|
|
return dict_set(var, key, tr_variant::unmanaged_string(val));
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 13:49:17 +00:00
|
|
|
tr_variant* tr_variantDictAddDict(tr_variant* const var, tr_quark key, size_t n_reserve)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-12-17 18:49:39 +00:00
|
|
|
return dict_set(var, key, tr_variant::make_map(n_reserve));
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
bool tr_variantDictRemove(tr_variant* const var, tr_quark key)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
if (auto* const map = var != nullptr ? var->get_if<tr_variant::MapIndex>() : nullptr; map != nullptr)
|
|
|
|
{
|
|
|
|
return map->erase(key) != 0U;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// --- BENC WALKING
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2021-09-15 18:51:19 +00:00
|
|
|
class WalkNode
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2021-09-15 22:11:00 +00:00
|
|
|
public:
|
2023-06-21 03:50:36 +00:00
|
|
|
WalkNode() = default;
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
explicit WalkNode(tr_variant const* const var)
|
|
|
|
: var_{ var }
|
2021-09-15 22:11:00 +00:00
|
|
|
{
|
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
std::pair<tr_quark, tr_variant const*> next_child()
|
2021-09-15 22:11:00 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
if (var_ == nullptr)
|
2021-09-15 22:11:00 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
return {};
|
2021-09-15 22:11:00 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
if (auto const* const map = var_->get_if<tr_variant::MapIndex>(); map != nullptr)
|
2021-09-15 22:11:00 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
if (auto idx = next_index(); idx < std::size(*map))
|
|
|
|
{
|
|
|
|
auto iter = std::cbegin(*map);
|
|
|
|
std::advance(iter, idx);
|
|
|
|
return { iter->first, &iter->second };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (auto const* const vec = var_->get_if<tr_variant::VectorIndex>(); vec != nullptr)
|
|
|
|
{
|
|
|
|
if (auto idx = next_index(); idx < std::size(*vec))
|
|
|
|
{
|
|
|
|
return { {}, &vec->at(idx) };
|
|
|
|
}
|
2021-09-15 22:11:00 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
return {};
|
2021-09-15 22:11:00 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
[[nodiscard]] constexpr auto is_visited() const noexcept
|
|
|
|
{
|
|
|
|
return is_visited_;
|
|
|
|
}
|
2021-09-15 22:11:00 +00:00
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
constexpr void set_visited() noexcept
|
|
|
|
{
|
|
|
|
is_visited_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] tr_variant const* current() const noexcept
|
|
|
|
{
|
|
|
|
return var_;
|
|
|
|
}
|
2021-09-15 22:11:00 +00:00
|
|
|
|
2021-11-05 14:54:44 +00:00
|
|
|
protected:
|
|
|
|
friend class VariantWalker;
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
tr_variant const* var_ = nullptr;
|
|
|
|
|
|
|
|
bool is_visited_ = false;
|
|
|
|
|
2021-11-05 14:54:44 +00:00
|
|
|
void assign(tr_variant const* v_in)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-21 04:15:23 +00:00
|
|
|
var_ = v_in;
|
|
|
|
is_visited_ = false;
|
|
|
|
child_index_ = 0;
|
2021-11-05 14:54:44 +00:00
|
|
|
sorted.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ByKey
|
|
|
|
{
|
|
|
|
std::string_view key;
|
2022-09-23 05:51:15 +00:00
|
|
|
size_t idx = {};
|
2021-11-05 14:54:44 +00:00
|
|
|
};
|
2012-12-24 22:38:41 +00:00
|
|
|
|
2023-06-21 03:50:36 +00:00
|
|
|
template<typename Container>
|
|
|
|
void sort(Container& sortbuf)
|
2021-11-05 14:54:44 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
auto const* const map = var_ != nullptr ? var_->get_if<tr_variant::MapIndex>() : nullptr;
|
|
|
|
if (map == nullptr)
|
2021-09-15 18:51:19 +00:00
|
|
|
{
|
2021-11-05 14:54:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
auto idx = size_t{};
|
|
|
|
auto const n = std::size(*map);
|
2021-11-05 14:54:44 +00:00
|
|
|
sortbuf.resize(n);
|
2023-08-23 17:57:58 +00:00
|
|
|
for (auto const& [key, val] : *map)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
sortbuf[idx] = { tr_quark_get_string_view(key), idx };
|
|
|
|
++idx;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 14:54:44 +00:00
|
|
|
std::sort(std::begin(sortbuf), std::end(sortbuf), [](ByKey const& a, ByKey const& b) { return a.key < b.key; });
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-15 18:51:19 +00:00
|
|
|
// keep the sorted indices
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-15 18:51:19 +00:00
|
|
|
sorted.resize(n);
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = 0; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-05 14:54:44 +00:00
|
|
|
sorted[i] = sortbuf[i].idx;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-09-15 18:51:19 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2022-01-24 00:53:35 +00:00
|
|
|
private:
|
2021-09-15 22:11:00 +00:00
|
|
|
// When walking `v`'s children, this is the index of the next child
|
2023-08-21 04:15:23 +00:00
|
|
|
size_t child_index_ = 0;
|
2019-04-28 08:27:33 +00:00
|
|
|
|
2021-09-15 22:11:00 +00:00
|
|
|
// When `v` is a dict, this is its children's indices sorted by key.
|
|
|
|
// Bencoded dicts must be sorted, so this is useful when writing benc.
|
2023-07-28 14:53:10 +00:00
|
|
|
small::vector<size_t, 128U> sorted;
|
2023-08-23 17:57:58 +00:00
|
|
|
|
|
|
|
[[nodiscard]] size_t next_index()
|
|
|
|
{
|
|
|
|
auto idx = child_index_++;
|
|
|
|
|
|
|
|
if (idx < std::size(sorted))
|
|
|
|
{
|
|
|
|
idx = sorted[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx;
|
|
|
|
}
|
2021-09-15 18:51:19 +00:00
|
|
|
};
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2021-11-05 14:54:44 +00:00
|
|
|
class VariantWalker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void emplace(tr_variant const* v_in, bool sort_dicts)
|
|
|
|
{
|
2023-08-21 04:15:23 +00:00
|
|
|
stack_.emplace_back(v_in);
|
2021-11-05 14:54:44 +00:00
|
|
|
|
|
|
|
if (sort_dicts)
|
|
|
|
{
|
2023-08-21 04:15:23 +00:00
|
|
|
top().sort(sortbuf_);
|
2021-11-05 14:54:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pop()
|
|
|
|
{
|
2023-08-21 04:15:23 +00:00
|
|
|
TR_ASSERT(!std::empty(stack_));
|
2023-12-17 18:49:39 +00:00
|
|
|
|
|
|
|
if (auto const size = std::size(stack_); size != 0U)
|
|
|
|
{
|
|
|
|
stack_.resize(size - 1U);
|
|
|
|
}
|
2021-11-05 14:54:44 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
[[nodiscard]] bool empty() const noexcept
|
2021-11-05 14:54:44 +00:00
|
|
|
{
|
2023-08-21 04:15:23 +00:00
|
|
|
return std::empty(stack_);
|
2021-11-05 14:54:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WalkNode& top()
|
|
|
|
{
|
2023-08-21 04:15:23 +00:00
|
|
|
TR_ASSERT(!std::empty(stack_));
|
|
|
|
return stack_.back();
|
2021-11-05 14:54:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-07-28 14:53:10 +00:00
|
|
|
static auto constexpr InitialCapacity = size_t{ 24U };
|
2023-08-21 04:15:23 +00:00
|
|
|
small::vector<WalkNode, InitialCapacity> stack_;
|
|
|
|
small::vector<WalkNode::ByKey, InitialCapacity> sortbuf_;
|
2021-11-05 14:54:44 +00:00
|
|
|
};
|
|
|
|
|
2012-12-14 04:36:33 +00:00
|
|
|
/**
|
|
|
|
* This function's previous recursive implementation was
|
|
|
|
* easier to read, but was vulnerable to a smash-stacking
|
|
|
|
* attack via maliciously-crafted data. (#667)
|
|
|
|
*/
|
2023-08-17 16:02:45 +00:00
|
|
|
void tr_variant_serde::walk(tr_variant const& top, WalkFuncs const& walk_funcs, void* user_data, bool sort_dicts)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2021-11-05 14:54:44 +00:00
|
|
|
auto stack = VariantWalker{};
|
2023-08-17 16:02:45 +00:00
|
|
|
stack.emplace(&top, sort_dicts);
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2021-09-15 18:51:19 +00:00
|
|
|
while (!stack.empty())
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2021-09-15 22:11:00 +00:00
|
|
|
auto& node = stack.top();
|
2021-10-15 17:27:12 +00:00
|
|
|
tr_variant const* v = nullptr;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
if (!node.is_visited())
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-21 04:15:23 +00:00
|
|
|
v = node.current();
|
|
|
|
node.set_visited();
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
2022-08-02 23:34:53 +00:00
|
|
|
else
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
auto [key, child] = node.next_child();
|
|
|
|
|
|
|
|
v = child;
|
2022-08-02 23:34:53 +00:00
|
|
|
|
|
|
|
if (v != nullptr)
|
2012-12-24 22:38:41 +00:00
|
|
|
{
|
2023-08-21 21:16:54 +00:00
|
|
|
if (node.current()->holds_alternative<tr_variant::Map>())
|
2022-08-02 23:34:53 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
auto const keystr = tr_quark_get_string_view(key);
|
2023-11-10 21:41:32 +00:00
|
|
|
walk_funcs.string_func(tr_variant::unmanaged_string(keystr), keystr, user_data);
|
2022-08-02 23:34:53 +00:00
|
|
|
}
|
2012-12-24 22:38:41 +00:00
|
|
|
}
|
2022-08-02 23:34:53 +00:00
|
|
|
else // finished with this node
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-08-21 04:15:23 +00:00
|
|
|
if (variant_is_container(node.current()))
|
2022-08-02 23:34:53 +00:00
|
|
|
{
|
2023-08-21 04:15:23 +00:00
|
|
|
walk_funcs.container_end_func(*node.current(), user_data);
|
2022-08-02 23:34:53 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-02 23:34:53 +00:00
|
|
|
stack.pop();
|
|
|
|
continue;
|
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
switch (variant_index(v))
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
case tr_variant::BoolIndex:
|
|
|
|
walk_funcs.bool_func(*v, *v->get_if<tr_variant::BoolIndex>(), user_data);
|
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
case tr_variant::IntIndex:
|
|
|
|
walk_funcs.int_func(*v, *v->get_if<tr_variant::IntIndex>(), user_data);
|
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
case tr_variant::DoubleIndex:
|
|
|
|
walk_funcs.double_func(*v, *v->get_if<tr_variant::DoubleIndex>(), user_data);
|
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
case tr_variant::StringIndex:
|
|
|
|
walk_funcs.string_func(*v, *v->get_if<tr_variant::StringIndex>(), user_data);
|
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
case tr_variant::VectorIndex:
|
|
|
|
if (v == node.current())
|
|
|
|
{
|
|
|
|
walk_funcs.list_begin_func(*v, user_data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stack.emplace(v, sort_dicts);
|
|
|
|
}
|
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
case tr_variant::MapIndex:
|
|
|
|
if (v == node.current())
|
|
|
|
{
|
|
|
|
walk_funcs.dict_begin_func(*v, user_data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stack.emplace(v, sort_dicts);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2023-08-23 17:57:58 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: // NoneIndex:
|
|
|
|
break;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
bool tr_variantDictChild(tr_variant* const var, size_t pos, tr_quark* key, tr_variant** setme_value)
|
2023-01-05 04:16:22 +00:00
|
|
|
{
|
2023-08-21 21:16:54 +00:00
|
|
|
TR_ASSERT(var != nullptr);
|
|
|
|
TR_ASSERT(var->holds_alternative<tr_variant::Map>());
|
2023-01-05 04:16:22 +00:00
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
if (auto* const map = var != nullptr ? var->get_if<tr_variant::MapIndex>() : nullptr;
|
|
|
|
map != nullptr && pos < std::size(*map))
|
2023-01-05 04:16:22 +00:00
|
|
|
{
|
2023-08-23 17:57:58 +00:00
|
|
|
auto iter = std::begin(*map);
|
|
|
|
std::advance(iter, pos);
|
|
|
|
*key = iter->first;
|
|
|
|
*setme_value = &iter->second;
|
|
|
|
return true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 17:57:58 +00:00
|
|
|
return false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-08-21 04:15:23 +00:00
|
|
|
void tr_variantMergeDicts(tr_variant* const tgt, tr_variant const* const src)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-21 21:16:54 +00:00
|
|
|
TR_ASSERT(tgt != nullptr);
|
|
|
|
TR_ASSERT(src != nullptr);
|
2023-08-23 17:57:58 +00:00
|
|
|
tgt->merge(*src);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
std::optional<tr_variant> tr_variant_serde::parse(std::string_view input)
|
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
error_ = {};
|
2023-08-17 16:02:45 +00:00
|
|
|
return type_ == Type::Json ? parse_json(input) : parse_benc(input);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
[[nodiscard]] std::optional<tr_variant> tr_variant_serde::parse_file(std::string_view filename)
|
2020-11-05 22:46:21 +00:00
|
|
|
{
|
2023-08-17 16:02:45 +00:00
|
|
|
TR_ASSERT_MSG(!parse_inplace_, "not supported in from_file()");
|
|
|
|
parse_inplace_ = false;
|
2021-12-10 00:49:30 +00:00
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
if (auto buf = std::vector<char>{}; tr_file_read(filename, buf, &error_))
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-17 16:02:45 +00:00
|
|
|
return parse(buf);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
2021-12-10 00:49:30 +00:00
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
return {};
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
std::string tr_variant_serde::to_string(tr_variant const& var) const
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-08-17 16:02:45 +00:00
|
|
|
return type_ == Type::Json ? to_json_string(var) : to_benc_string(var);
|
2021-11-15 07:21:57 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
bool tr_variant_serde::to_file(tr_variant const& var, std::string_view filename)
|
2021-11-15 07:21:57 +00:00
|
|
|
{
|
2023-08-17 16:02:45 +00:00
|
|
|
tr_file_save(filename, to_string(var), &error_);
|
2021-11-18 05:37:35 +00:00
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
if (error_)
|
2021-11-15 07:21:57 +00:00
|
|
|
{
|
2023-08-17 16:02:45 +00:00
|
|
|
tr_logAddError(fmt::format(
|
|
|
|
_("Couldn't save '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", filename),
|
2023-11-04 16:39:41 +00:00
|
|
|
fmt::arg("error", error_.message()),
|
|
|
|
fmt::arg("error_code", error_.code())));
|
2023-08-17 16:02:45 +00:00
|
|
|
return false;
|
2021-11-17 03:29:30 +00:00
|
|
|
}
|
2021-11-15 07:21:57 +00:00
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
return true;
|
2021-11-15 07:21:57 +00:00
|
|
|
}
|