transmission/libtransmission/variant.cc

864 lines
21 KiB
C++
Raw Normal View History

// This file Copyright © Mnemosyne LLC.
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
// 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
#include <algorithm> // std::sort
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include <variant>
2012-12-14 04:36:33 +00:00
#ifdef _WIN32
#include <share.h>
2012-12-14 04:36:33 +00:00
#endif
#include <fmt/core.h>
#include <small/vector.hpp>
#define LIBTRANSMISSION_VARIANT_MODULE
#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
using namespace std::literals;
namespace
{
[[nodiscard]] constexpr bool variant_is_container(tr_variant const* const var)
2012-12-14 04:36:33 +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
}
[[nodiscard]] constexpr size_t variant_index(tr_variant const* const var)
2012-12-14 04:36:33 +00:00
{
if (var != nullptr)
2012-12-14 04:36:33 +00:00
{
return var->index();
}
return tr_variant::NoneIndex;
}
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 {};
}
} // namespace
2012-12-14 04:36:33 +00:00
// ---
tr_variant::StringHolder::StringHolder(std::string&& str) noexcept
: str_{ std::move(str) }
{
sv_ = str_;
2012-12-14 04:36:33 +00:00
}
tr_variant::StringHolder::StringHolder(StringHolder&& that) noexcept
2012-12-14 04:36:33 +00:00
{
*this = std::move(that);
}
2012-12-14 04:36:33 +00:00
void tr_variant::StringHolder::set_unmanaged(std::string_view sv)
{
str_.clear();
sv_ = sv;
}
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;
}
// ---
tr_variant::Merge::Merge(tr_variant& tgt)
: tgt_{ tgt }
{
2012-12-14 04:36:33 +00:00
}
void tr_variant::Merge::operator()(std::monostate const& src)
2012-12-14 04:36:33 +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_;
}
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)
{
std::visit(Merge{ tgt[i] }, src[i].val_);
}
}
void tr_variant::Merge::operator()(tr_variant::Map const& src)
{
// if tgt_ isn't already a map, make it one
if (tgt_.index() != tr_variant::MapIndex)
{
tgt_.val_.emplace<tr_variant::Map>();
}
if (auto* tgt = tgt_.get_if<tr_variant::MapIndex>(); tgt != nullptr)
{
tgt->reserve(std::size(*tgt) + std::size(src));
for (auto const& [key, val] : src)
{
std::visit(Merge{ (*tgt)[key] }, val.val_);
}
}
2012-12-14 04:36:33 +00:00
}
// ---
tr_variant* tr_variantDictFind(tr_variant* const var, tr_quark key)
2012-12-14 04:36:33 +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
}
tr_variant* tr_variantListChild(tr_variant* const var, size_t pos)
2012-12-14 04:36:33 +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
{
if (pos < std::size(*vec))
{
return &vec->at(pos);
}
2012-12-14 04:36:33 +00:00
}
return {};
}
bool tr_variantListRemove(tr_variant* const var, size_t pos)
{
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
}
bool tr_variantGetInt(tr_variant const* const var, int64_t* setme)
2012-12-14 04:36:33 +00:00
{
switch (variant_index(var))
{
case tr_variant::IntIndex:
if (setme != nullptr)
{
*setme = *var->get_if<tr_variant::IntIndex>();
}
return true;
case tr_variant::BoolIndex:
if (setme != nullptr)
{
*setme = *var->get_if<tr_variant::BoolIndex>() ? 1 : 0;
}
return true;
2012-12-14 04:36:33 +00:00
default:
return false;
}
2012-12-14 04:36:33 +00:00
}
bool tr_variantGetStrView(tr_variant const* const var, std::string_view* setme)
{
switch (variant_index(var))
{
case tr_variant::StringIndex:
*setme = *var->get_if<tr_variant::StringIndex>();
return true;
default:
return false;
}
}
bool tr_variantGetRaw(tr_variant const* v, std::byte const** setme_raw, size_t* setme_len)
{
if (auto sv = std::string_view{}; tr_variantGetStrView(v, &sv))
{
*setme_raw = reinterpret_cast<std::byte const*>(std::data(sv));
*setme_len = std::size(sv);
return true;
}
return false;
}
bool tr_variantGetRaw(tr_variant const* v, uint8_t const** setme_raw, size_t* setme_len)
2012-12-14 04:36:33 +00:00
{
if (auto sv = std::string_view{}; tr_variantGetStrView(v, &sv))
2012-12-14 04:36:33 +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
}
return false;
2012-12-14 04:36:33 +00:00
}
bool tr_variantGetBool(tr_variant const* const var, bool* setme)
2012-12-14 04:36:33 +00:00
{
switch (variant_index(var))
{
case tr_variant::BoolIndex:
*setme = *var->get_if<tr_variant::BoolIndex>();
return true;
2012-12-14 04:36:33 +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
case tr_variant::StringIndex:
if (auto const val = *var->get_if<tr_variant::StringIndex>(); val == "true"sv)
{
*setme = true;
return true;
}
else if (val == "false"sv)
{
*setme = false;
return true;
}
break;
default:
break;
}
2012-12-14 04:36:33 +00:00
return false;
2012-12-14 04:36:33 +00:00
}
bool tr_variantGetReal(tr_variant const* const var, double* setme)
2012-12-14 04:36:33 +00:00
{
switch (variant_index(var))
{
case tr_variant::DoubleIndex:
*setme = *var->get_if<tr_variant::DoubleIndex>();
return true;
2012-12-14 04:36:33 +00:00
case tr_variant::IntIndex:
*setme = static_cast<double>(*var->get_if<tr_variant::IntIndex>());
return true;
2012-12-14 04:36:33 +00:00
case tr_variant::StringIndex:
if (auto const val = tr_num_parse<double>(*var->get_if<tr_variant::StringIndex>()); val)
{
*setme = *val;
return true;
}
[[fallthrough]];
2012-12-14 04:36:33 +00:00
default:
return false;
}
2012-12-14 04:36:33 +00:00
}
bool tr_variantDictFindInt(tr_variant* const var, tr_quark key, int64_t* setme)
2012-12-14 04:36:33 +00:00
{
auto const* const child = tr_variantDictFind(var, key);
return tr_variantGetInt(child, setme);
2012-12-14 04:36:33 +00:00
}
bool tr_variantDictFindBool(tr_variant* const var, tr_quark key, bool* setme)
2012-12-14 04:36:33 +00:00
{
auto const* const child = tr_variantDictFind(var, key);
return tr_variantGetBool(child, setme);
2012-12-14 04:36:33 +00:00
}
bool tr_variantDictFindReal(tr_variant* const var, tr_quark key, double* setme)
2012-12-14 04:36:33 +00:00
{
auto const* const child = tr_variantDictFind(var, key);
return tr_variantGetReal(child, setme);
2012-12-14 04:36:33 +00:00
}
bool tr_variantDictFindStrView(tr_variant* const var, tr_quark key, std::string_view* setme)
{
auto const* const child = tr_variantDictFind(var, key);
return tr_variantGetStrView(child, setme);
}
bool tr_variantDictFindList(tr_variant* const var, tr_quark key, tr_variant** setme)
2012-12-14 04:36:33 +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
}
bool tr_variantDictFindDict(tr_variant* const var, tr_quark key, tr_variant** setme)
2012-12-14 04:36:33 +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
}
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
{
auto const* const child = tr_variantDictFind(var, key);
return tr_variantGetRaw(child, setme_raw, setme_len);
}
bool tr_variantDictFindRaw(tr_variant* const var, tr_quark key, std::byte const** setme_raw, size_t* setme_len)
{
auto const* const child = tr_variantDictFind(var, key);
return tr_variantGetRaw(child, setme_raw, setme_len);
2012-12-14 04:36:33 +00:00
}
// ---
2012-12-14 04:36:33 +00:00
void tr_variantInitList(tr_variant* initme, size_t n_reserve)
2012-12-14 04:36:33 +00:00
{
auto vec = tr_variant::Vector{};
vec.reserve(n_reserve);
*initme = std::move(vec);
2012-12-14 04:36:33 +00:00
}
void tr_variantListReserve(tr_variant* const var, size_t n_reserve)
2012-12-14 04:36:33 +00:00
{
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)
{
vec->reserve(std::size(*vec) + n_reserve);
}
2012-12-14 04:36:33 +00:00
}
void tr_variantInitDict(tr_variant* initme, size_t n_reserve)
2012-12-14 04:36:33 +00:00
{
*initme = tr_variant::Map{ n_reserve };
2012-12-14 04:36:33 +00:00
}
void tr_variantDictReserve(tr_variant* const var, size_t n_reserve)
2012-12-14 04:36:33 +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
}
tr_variant* tr_variantListAdd(tr_variant* const var)
2012-12-14 04:36:33 +00:00
{
TR_ASSERT(var != nullptr);
TR_ASSERT(var->holds_alternative<tr_variant::Vector>());
2012-12-14 04:36:33 +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
return nullptr;
2012-12-14 04:36:33 +00:00
}
tr_variant* tr_variantListAddInt(tr_variant* const var, int64_t const value)
2012-12-14 04:36:33 +00:00
{
return vec_add(var, value);
2012-12-14 04:36:33 +00:00
}
tr_variant* tr_variantListAddReal(tr_variant* const var, double const value)
2012-12-14 04:36:33 +00:00
{
return vec_add(var, value);
2012-12-14 04:36:33 +00:00
}
tr_variant* tr_variantListAddBool(tr_variant* const var, bool const value)
2012-12-14 04:36:33 +00:00
{
return vec_add(var, value);
2012-12-14 04:36:33 +00:00
}
tr_variant* tr_variantListAddStr(tr_variant* const var, std::string_view const value)
2012-12-14 04:36:33 +00:00
{
return vec_add(var, std::string{ value });
2012-12-14 04:36:33 +00:00
}
tr_variant* tr_variantListAddStrView(tr_variant* const var, std::string_view value)
{
return vec_add(var, tr_variant::unmanaged_string(value));
}
tr_variant* tr_variantListAddRaw(tr_variant* const var, void const* value, size_t n_bytes)
2012-12-14 04:36:33 +00:00
{
return vec_add(var, tr_variant::make_raw(value, n_bytes));
2012-12-14 04:36:33 +00:00
}
tr_variant* tr_variantListAddList(tr_variant* const var, size_t const n_reserve)
2012-12-14 04:36:33 +00:00
{
return vec_add(var, tr_variant::make_vector(n_reserve));
2012-12-14 04:36:33 +00:00
}
tr_variant* tr_variantListAddDict(tr_variant* const var, size_t const n_reserve)
2012-12-14 04:36:33 +00:00
{
return vec_add(var, tr_variant::make_map(n_reserve));
2012-12-14 04:36:33 +00:00
}
tr_variant* tr_variantDictAdd(tr_variant* const var, tr_quark key)
2012-12-14 04:36:33 +00:00
{
TR_ASSERT(var != nullptr);
TR_ASSERT(var->holds_alternative<tr_variant::Map>());
2012-12-14 04:36:33 +00:00
if (auto* const map = var != nullptr ? var->get_if<tr_variant::MapIndex>() : nullptr; map != nullptr)
{
return &(*map)[key];
}
return {};
2012-12-14 04:36:33 +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
{
return dict_set(var, key, val);
2012-12-14 04:36:33 +00:00
}
tr_variant* tr_variantDictAddBool(tr_variant* const var, tr_quark key, bool val)
2012-12-14 04:36:33 +00:00
{
return dict_set(var, key, val);
2012-12-14 04:36:33 +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
{
return dict_set(var, key, val);
2012-12-14 04:36:33 +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
{
return dict_set(var, key, val);
2012-12-14 04:36:33 +00:00
}
tr_variant* tr_variantDictAddRaw(tr_variant* const var, tr_quark const key, void const* const value, size_t const n_bytes)
{
return dict_set(var, key, std::string{ static_cast<char const*>(value), n_bytes });
}
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
{
return dict_set(var, key, tr_variant::make_vector(n_reserve));
2012-12-14 04:36:33 +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
{
return dict_set(var, key, tr_variant::unmanaged_string(val));
2012-12-14 04:36:33 +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
{
return dict_set(var, key, tr_variant::make_map(n_reserve));
2012-12-14 04:36:33 +00:00
}
bool tr_variantDictRemove(tr_variant* const var, tr_quark key)
{
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
}
// --- BENC WALKING
2012-12-14 04:36:33 +00:00
class WalkNode
2012-12-14 04:36:33 +00:00
{
public:
WalkNode() = default;
explicit WalkNode(tr_variant const* const var)
: var_{ var }
{
}
2012-12-14 04:36:33 +00:00
std::pair<tr_quark, tr_variant const*> next_child()
{
if (var_ == nullptr)
{
return {};
}
2012-12-14 04:36:33 +00:00
if (auto const* const map = var_->get_if<tr_variant::MapIndex>(); map != nullptr)
{
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) };
}
}
return {};
}
[[nodiscard]] constexpr auto is_visited() const noexcept
{
return is_visited_;
}
constexpr void set_visited() noexcept
{
is_visited_ = true;
}
[[nodiscard]] tr_variant const* current() const noexcept
{
return var_;
}
protected:
friend class VariantWalker;
tr_variant const* var_ = nullptr;
bool is_visited_ = false;
void assign(tr_variant const* v_in)
2012-12-14 04:36:33 +00:00
{
var_ = v_in;
is_visited_ = false;
child_index_ = 0;
sorted.clear();
}
struct ByKey
{
std::string_view key;
size_t idx = {};
};
template<typename Container>
void sort(Container& sortbuf)
{
auto const* const map = var_ != nullptr ? var_->get_if<tr_variant::MapIndex>() : nullptr;
if (map == nullptr)
{
return;
}
2012-12-14 04:36:33 +00:00
auto idx = size_t{};
auto const n = std::size(*map);
sortbuf.resize(n);
for (auto const& [key, val] : *map)
2012-12-14 04:36:33 +00:00
{
sortbuf[idx] = { tr_quark_get_string_view(key), idx };
++idx;
2012-12-14 04:36:33 +00:00
}
std::sort(std::begin(sortbuf), std::end(sortbuf), [](ByKey const& a, ByKey const& b) { return a.key < b.key; });
// keep the sorted indices
sorted.resize(n);
2017-05-13 22:38:31 +00:00
for (size_t i = 0; i < n; ++i)
{
sorted[i] = sortbuf[i].idx;
}
}
2012-12-14 04:36:33 +00:00
2022-01-24 00:53:35 +00:00
private:
// When walking `v`'s children, this is the index of the next child
size_t child_index_ = 0;
// 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.
small::vector<size_t, 128U> sorted;
[[nodiscard]] size_t next_index()
{
auto idx = child_index_++;
if (idx < std::size(sorted))
{
idx = sorted[idx];
}
return idx;
}
};
2012-12-14 04:36:33 +00:00
class VariantWalker
{
public:
void emplace(tr_variant const* v_in, bool sort_dicts)
{
stack_.emplace_back(v_in);
if (sort_dicts)
{
top().sort(sortbuf_);
}
}
void pop()
{
TR_ASSERT(!std::empty(stack_));
if (auto const size = std::size(stack_); size != 0U)
{
stack_.resize(size - 1U);
}
}
[[nodiscard]] bool empty() const noexcept
{
return std::empty(stack_);
}
WalkNode& top()
{
TR_ASSERT(!std::empty(stack_));
return stack_.back();
}
private:
static auto constexpr InitialCapacity = size_t{ 24U };
small::vector<WalkNode, InitialCapacity> stack_;
small::vector<WalkNode::ByKey, InitialCapacity> sortbuf_;
};
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
{
auto stack = VariantWalker{};
2023-08-17 16:02:45 +00:00
stack.emplace(&top, sort_dicts);
2012-12-14 04:36:33 +00:00
while (!stack.empty())
2012-12-14 04:36:33 +00:00
{
auto& node = stack.top();
tr_variant const* v = nullptr;
2012-12-14 04:36:33 +00:00
if (!node.is_visited())
2012-12-14 04:36:33 +00:00
{
v = node.current();
node.set_visited();
2012-12-14 04:36:33 +00:00
}
else
2012-12-14 04:36:33 +00:00
{
auto [key, child] = node.next_child();
v = child;
if (v != nullptr)
{
if (node.current()->holds_alternative<tr_variant::Map>())
{
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);
}
}
else // finished with this node
{
if (variant_is_container(node.current()))
{
walk_funcs.container_end_func(*node.current(), user_data);
}
stack.pop();
continue;
}
2012-12-14 04:36:33 +00:00
}
switch (variant_index(v))
2012-12-14 04:36:33 +00:00
{
case tr_variant::BoolIndex:
walk_funcs.bool_func(*v, *v->get_if<tr_variant::BoolIndex>(), user_data);
break;
case tr_variant::IntIndex:
walk_funcs.int_func(*v, *v->get_if<tr_variant::IntIndex>(), user_data);
break;
case tr_variant::DoubleIndex:
walk_funcs.double_func(*v, *v->get_if<tr_variant::DoubleIndex>(), user_data);
break;
case tr_variant::StringIndex:
walk_funcs.string_func(*v, *v->get_if<tr_variant::StringIndex>(), user_data);
break;
case tr_variant::VectorIndex:
if (v == node.current())
{
walk_funcs.list_begin_func(*v, user_data);
}
else
{
stack.emplace(v, sort_dicts);
}
break;
case tr_variant::MapIndex:
if (v == node.current())
{
walk_funcs.dict_begin_func(*v, user_data);
}
else
{
stack.emplace(v, sort_dicts);
}
break;
default: // NoneIndex:
break;
2012-12-14 04:36:33 +00:00
}
}
}
// ---
2012-12-14 04:36:33 +00:00
bool tr_variantDictChild(tr_variant* const var, size_t pos, tr_quark* key, tr_variant** setme_value)
{
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 && pos < std::size(*map))
{
auto iter = std::begin(*map);
std::advance(iter, pos);
*key = iter->first;
*setme_value = &iter->second;
return true;
}
return false;
}
2012-12-14 04:36:33 +00:00
void tr_variantMergeDicts(tr_variant* const tgt, tr_variant const* const src)
2012-12-14 04:36:33 +00:00
{
TR_ASSERT(tgt != nullptr);
TR_ASSERT(src != nullptr);
tgt->merge(*src);
2012-12-14 04:36:33 +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)
{
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)
{
2023-08-17 16:02:45 +00:00
TR_ASSERT_MSG(!parse_inplace_, "not supported in from_file()");
parse_inplace_ = false;
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
}
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);
}
2023-08-17 16:02:45 +00:00
bool tr_variant_serde::to_file(tr_variant const& var, std::string_view filename)
{
2023-08-17 16:02:45 +00:00
tr_file_save(filename, to_string(var), &error_);
if (error_)
{
2023-08-17 16:02:45 +00:00
tr_logAddError(fmt::format(
_("Couldn't save '{path}': {error} ({error_code})"),
fmt::arg("path", filename),
fmt::arg("error", error_.message()),
fmt::arg("error_code", error_.code())));
2023-08-17 16:02:45 +00:00
return false;
}
2023-08-17 16:02:45 +00:00
return true;
}