2024-02-07 16:14:47 +00:00
|
|
|
// 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.
|
|
|
|
|
2024-02-15 17:31:09 +00:00
|
|
|
#include <array>
|
2024-03-16 04:27:55 +00:00
|
|
|
#include <chrono>
|
2024-02-15 17:31:09 +00:00
|
|
|
#include <cstddef> // size_t
|
|
|
|
#include <cstdint> // int64_t, uint32_t
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
#include <utility>
|
2024-02-07 16:14:47 +00:00
|
|
|
|
2024-02-15 17:31:09 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
|
|
|
#include "libtransmission/transmission.h"
|
|
|
|
|
|
|
|
#include "libtransmission/log.h" // for tr_log_level
|
|
|
|
#include "libtransmission/net.h" // for tr_port
|
|
|
|
#include "libtransmission/open-files.h" // for tr_open_files::Preallocation
|
|
|
|
#include "libtransmission/peer-io.h" // tr_preferred_transport
|
2024-02-07 16:14:47 +00:00
|
|
|
#include "libtransmission/settings.h"
|
2024-02-15 17:31:09 +00:00
|
|
|
#include "libtransmission/utils.h" // for tr_strv_strip(), tr_strlower()
|
2024-02-07 16:14:47 +00:00
|
|
|
#include "libtransmission/variant.h"
|
2024-03-16 04:27:55 +00:00
|
|
|
#include "libtransmission/tr-assert.h"
|
2024-02-07 16:14:47 +00:00
|
|
|
|
2024-02-15 17:31:09 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2024-02-07 16:14:47 +00:00
|
|
|
namespace libtransmission
|
|
|
|
{
|
|
|
|
namespace
|
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
template<typename T, size_t N>
|
|
|
|
using Lookup = std::array<std::pair<std::string_view, T>, N>;
|
2024-02-15 17:31:09 +00:00
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
bool load_bool(tr_variant const& src, bool* tgt)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
2024-03-31 19:06:36 +00:00
|
|
|
if (auto val = src.value_if<bool>())
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
*tgt = *val;
|
|
|
|
return true;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
return false;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
tr_variant save_bool(bool const& val)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
return val;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
bool load_double(tr_variant const& src, double* tgt)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
2024-03-31 19:06:36 +00:00
|
|
|
if (auto val = src.value_if<double>())
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
*tgt = *val;
|
|
|
|
return true;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
return false;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
tr_variant save_double(double const& val)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
auto constexpr EncryptionKeys = Lookup<tr_encryption_mode, 3U>{ {
|
|
|
|
{ "required", TR_ENCRYPTION_REQUIRED },
|
|
|
|
{ "preferred", TR_ENCRYPTION_PREFERRED },
|
|
|
|
{ "allowed", TR_CLEAR_PREFERRED },
|
|
|
|
} };
|
|
|
|
|
|
|
|
bool load_encryption_mode(tr_variant const& src, tr_encryption_mode* tgt)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
static constexpr auto& Keys = EncryptionKeys;
|
2024-02-15 17:31:09 +00:00
|
|
|
|
2024-03-31 19:06:36 +00:00
|
|
|
if (auto const val = src.value_if<std::string_view>())
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
auto const needle = tr_strlower(tr_strv_strip(*val));
|
|
|
|
|
|
|
|
for (auto const& [key, encryption] : Keys)
|
|
|
|
{
|
|
|
|
if (key == needle)
|
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
*tgt = encryption;
|
|
|
|
return true;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 19:06:36 +00:00
|
|
|
if (auto const val = src.value_if<int64_t>())
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
for (auto const& [key, encryption] : Keys)
|
|
|
|
{
|
|
|
|
if (encryption == *val)
|
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
*tgt = encryption;
|
|
|
|
return true;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
return false;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
tr_variant save_encryption_mode(tr_encryption_mode const& val)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
return static_cast<int64_t>(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
auto constexpr LogKeys = Lookup<tr_log_level, 7U>{ {
|
|
|
|
{ "critical", TR_LOG_CRITICAL },
|
|
|
|
{ "debug", TR_LOG_DEBUG },
|
|
|
|
{ "error", TR_LOG_ERROR },
|
|
|
|
{ "info", TR_LOG_INFO },
|
|
|
|
{ "off", TR_LOG_OFF },
|
|
|
|
{ "trace", TR_LOG_TRACE },
|
|
|
|
{ "warn", TR_LOG_WARN },
|
|
|
|
} };
|
|
|
|
|
|
|
|
bool load_log_level(tr_variant const& src, tr_log_level* tgt)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
static constexpr auto& Keys = LogKeys;
|
2024-02-15 17:31:09 +00:00
|
|
|
|
2024-03-31 19:06:36 +00:00
|
|
|
if (auto const val = src.value_if<std::string_view>())
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
auto const needle = tr_strlower(tr_strv_strip(*val));
|
|
|
|
|
|
|
|
for (auto const& [name, log_level] : Keys)
|
|
|
|
{
|
|
|
|
if (needle == name)
|
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
*tgt = log_level;
|
|
|
|
return true;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 19:06:36 +00:00
|
|
|
if (auto const val = src.value_if<int64_t>())
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
for (auto const& [name, log_level] : Keys)
|
|
|
|
{
|
|
|
|
if (log_level == *val)
|
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
*tgt = log_level;
|
|
|
|
return true;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
return false;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
tr_variant save_log_level(tr_log_level const& val)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
return static_cast<int64_t>(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
bool load_mode_t(tr_variant const& src, tr_mode_t* tgt)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
2024-03-31 19:06:36 +00:00
|
|
|
if (auto const val = src.value_if<std::string_view>())
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
if (auto const mode = tr_num_parse<uint32_t>(*val, nullptr, 8); mode)
|
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
*tgt = static_cast<tr_mode_t>(*mode);
|
|
|
|
return true;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 19:06:36 +00:00
|
|
|
if (auto const val = src.value_if<int64_t>())
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
*tgt = static_cast<tr_mode_t>(*val);
|
|
|
|
return true;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
return false;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
tr_variant save_mode_t(tr_mode_t const& val)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
return fmt::format("{:#03o}", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
bool load_msec(tr_variant const& src, std::chrono::milliseconds* tgt)
|
|
|
|
{
|
2024-03-31 19:06:36 +00:00
|
|
|
if (auto val = src.value_if<int64_t>())
|
2024-03-16 04:27:55 +00:00
|
|
|
{
|
|
|
|
*tgt = std::chrono::milliseconds(*val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variant save_msec(std::chrono::milliseconds const& src)
|
|
|
|
{
|
|
|
|
return src.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
bool load_port(tr_variant const& src, tr_port* tgt)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
2024-03-31 19:06:36 +00:00
|
|
|
if (auto const val = src.value_if<int64_t>())
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
*tgt = tr_port::from_host(*val);
|
|
|
|
return true;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
return false;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
tr_variant save_port(tr_port const& val)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
return int64_t{ val.host() };
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
auto constexpr PreallocationKeys = Lookup<tr_open_files::Preallocation, 5U>{ {
|
|
|
|
{ "off", tr_open_files::Preallocation::None },
|
|
|
|
{ "none", tr_open_files::Preallocation::None },
|
|
|
|
{ "fast", tr_open_files::Preallocation::Sparse },
|
|
|
|
{ "sparse", tr_open_files::Preallocation::Sparse },
|
|
|
|
{ "full", tr_open_files::Preallocation::Full },
|
|
|
|
} };
|
|
|
|
|
|
|
|
bool load_preallocation_mode(tr_variant const& src, tr_open_files::Preallocation* tgt)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
static constexpr auto& Keys = PreallocationKeys;
|
2024-02-15 17:31:09 +00:00
|
|
|
|
2024-03-31 19:06:36 +00:00
|
|
|
if (auto const val = src.value_if<std::string_view>())
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
auto const needle = tr_strlower(tr_strv_strip(*val));
|
|
|
|
|
|
|
|
for (auto const& [name, value] : Keys)
|
|
|
|
{
|
|
|
|
if (name == needle)
|
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
*tgt = value;
|
|
|
|
return true;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 19:06:36 +00:00
|
|
|
if (auto const val = src.value_if<int64_t>())
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
for (auto const& [name, value] : Keys)
|
|
|
|
{
|
|
|
|
if (value == static_cast<tr_open_files::Preallocation>(*val))
|
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
*tgt = value;
|
|
|
|
return true;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
return false;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
tr_variant save_preallocation_mode(tr_open_files::Preallocation const& val)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
return static_cast<int64_t>(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
auto constexpr PreferredTransportKeys = Lookup<tr_preferred_transport, TR_NUM_PREFERRED_TRANSPORT>{ {
|
|
|
|
{ "utp", TR_PREFER_UTP },
|
|
|
|
{ "tcp", TR_PREFER_TCP },
|
|
|
|
} };
|
|
|
|
|
|
|
|
bool load_preferred_transport(tr_variant const& src, tr_preferred_transport* tgt)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
static constexpr auto& Keys = PreferredTransportKeys;
|
2024-02-15 17:31:09 +00:00
|
|
|
|
2024-03-31 19:06:36 +00:00
|
|
|
if (auto const val = src.value_if<std::string_view>())
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
auto const needle = tr_strlower(tr_strv_strip(*val));
|
|
|
|
|
|
|
|
for (auto const& [name, value] : Keys)
|
|
|
|
{
|
|
|
|
if (name == needle)
|
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
*tgt = value;
|
|
|
|
return true;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 19:06:36 +00:00
|
|
|
if (auto const val = src.value_if<int64_t>())
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
for (auto const& [name, value] : Keys)
|
|
|
|
{
|
|
|
|
if (value == *val)
|
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
*tgt = value;
|
|
|
|
return true;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
return false;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
tr_variant save_preferred_transport(tr_preferred_transport const& val)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
for (auto const& [key, value] : PreferredTransportKeys)
|
|
|
|
{
|
|
|
|
if (value == val)
|
|
|
|
{
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<int64_t>(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
bool load_size_t(tr_variant const& src, size_t* tgt)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
2024-03-31 19:06:36 +00:00
|
|
|
if (auto const val = src.value_if<int64_t>())
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
*tgt = static_cast<size_t>(*val);
|
|
|
|
return true;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
return false;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
tr_variant save_size_t(size_t const& val)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
return uint64_t{ val };
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
bool load_string(tr_variant const& src, std::string* tgt)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
2024-03-31 19:06:36 +00:00
|
|
|
if (auto const val = src.value_if<std::string_view>())
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
*tgt = std::string{ *val };
|
|
|
|
return true;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
return false;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
tr_variant save_string(std::string const& val)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
bool load_tos_t(tr_variant const& src, tr_tos_t* tgt)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
2024-03-31 19:06:36 +00:00
|
|
|
if (auto const val = src.value_if<std::string_view>())
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
if (auto const tos = tr_tos_t::from_string(*val); tos)
|
|
|
|
{
|
|
|
|
*tgt = *tos;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-31 19:06:36 +00:00
|
|
|
if (auto const val = src.value_if<int64_t>())
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
*tgt = tr_tos_t{ static_cast<int>(*val) };
|
|
|
|
return true;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
return false;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
tr_variant save_tos_t(tr_tos_t const& val)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
return val.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
auto constexpr VerifyModeKeys = Lookup<tr_verify_added_mode, 2U>{ {
|
|
|
|
{ "fast", TR_VERIFY_ADDED_FAST },
|
|
|
|
{ "full", TR_VERIFY_ADDED_FULL },
|
|
|
|
} };
|
|
|
|
|
|
|
|
bool load_verify_added_mode(tr_variant const& src, tr_verify_added_mode* tgt)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
static constexpr auto& Keys = VerifyModeKeys;
|
|
|
|
|
2024-03-31 19:06:36 +00:00
|
|
|
if (auto const val = src.value_if<std::string_view>())
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
auto const needle = tr_strlower(tr_strv_strip(*val));
|
|
|
|
|
|
|
|
for (auto const& [name, value] : Keys)
|
|
|
|
{
|
|
|
|
if (name == needle)
|
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
*tgt = value;
|
|
|
|
return true;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 19:06:36 +00:00
|
|
|
if (auto const val = src.value_if<int64_t>())
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
for (auto const& [name, value] : Keys)
|
|
|
|
{
|
|
|
|
if (value == *val)
|
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
*tgt = value;
|
|
|
|
return true;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
return false;
|
2024-02-15 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
tr_variant save_verify_added_mode(tr_verify_added_mode const& val)
|
2024-02-15 17:31:09 +00:00
|
|
|
{
|
|
|
|
for (auto const& [key, value] : VerifyModeKeys)
|
|
|
|
{
|
|
|
|
if (value == val)
|
|
|
|
{
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<int64_t>(val);
|
|
|
|
}
|
2024-02-07 16:14:47 +00:00
|
|
|
} // unnamed namespace
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
Settings::Settings()
|
|
|
|
{
|
|
|
|
add_type_handler(load_bool, save_bool);
|
|
|
|
add_type_handler(load_double, save_double);
|
|
|
|
add_type_handler(load_encryption_mode, save_encryption_mode);
|
|
|
|
add_type_handler(load_log_level, save_log_level);
|
|
|
|
add_type_handler(load_mode_t, save_mode_t);
|
|
|
|
add_type_handler(load_msec, save_msec);
|
|
|
|
add_type_handler(load_port, save_port);
|
|
|
|
add_type_handler(load_preallocation_mode, save_preallocation_mode);
|
|
|
|
add_type_handler(load_preferred_transport, save_preferred_transport);
|
|
|
|
add_type_handler(load_size_t, save_size_t);
|
|
|
|
add_type_handler(load_string, save_string);
|
|
|
|
add_type_handler(load_tos_t, save_tos_t);
|
|
|
|
add_type_handler(load_verify_added_mode, save_verify_added_mode);
|
|
|
|
}
|
|
|
|
|
2024-02-07 16:14:47 +00:00
|
|
|
void Settings::load(tr_variant const& src)
|
|
|
|
{
|
|
|
|
auto const* map = src.get_if<tr_variant::Map>();
|
|
|
|
if (map == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
for (auto& field : fields())
|
2024-02-07 16:14:47 +00:00
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
if (auto const iter = map->find(field.key); iter != std::end(*map))
|
2024-02-07 16:14:47 +00:00
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
auto const type_index = std::type_index{ field.type };
|
|
|
|
TR_ASSERT(load_.count(type_index) == 1U);
|
|
|
|
load_.at(type_index)(iter->second, field.ptr);
|
2024-02-07 16:14:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variant Settings::save() const
|
|
|
|
{
|
|
|
|
auto const fields = const_cast<Settings*>(this)->fields();
|
|
|
|
|
2024-02-15 17:31:09 +00:00
|
|
|
auto map = tr_variant::Map{ std::size(fields) };
|
2024-02-07 16:14:47 +00:00
|
|
|
|
2024-03-16 04:27:55 +00:00
|
|
|
for (auto const& field : fields)
|
2024-02-07 16:14:47 +00:00
|
|
|
{
|
2024-03-16 04:27:55 +00:00
|
|
|
auto const type_index = std::type_index{ field.type };
|
|
|
|
TR_ASSERT(save_.count(type_index) == 1U);
|
|
|
|
map.try_emplace(field.key, save_.at(type_index)(field.ptr));
|
2024-02-07 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
} // namespace libtransmission
|