2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2008-2023 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
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <string_view>
|
2021-09-15 18:51:19 +00:00
|
|
|
#include <vector>
|
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-common.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
|
|
|
|
{
|
|
|
|
constexpr bool tr_variantIsContainer(tr_variant const* v)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_variantIsList(v) || tr_variantIsDict(v);
|
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-01-05 04:16:22 +00:00
|
|
|
auto constexpr StringInit = tr_variant_string{
|
2021-09-12 17:41:49 +00:00
|
|
|
TR_STRING_TYPE_QUARK,
|
|
|
|
0,
|
|
|
|
{},
|
2012-12-22 20:35:19 +00:00
|
|
|
};
|
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
void tr_variant_string_clear(struct tr_variant_string* str)
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (str->type == TR_STRING_TYPE_HEAP)
|
|
|
|
{
|
2023-01-28 21:26:23 +00:00
|
|
|
delete[] const_cast<char*>(str->str.str);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
*str = StringInit;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* returns a const pointer to the variant's string */
|
2023-01-05 04:16:22 +00:00
|
|
|
constexpr char const* tr_variant_string_get_string(struct tr_variant_string const* str)
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (str->type)
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_STRING_TYPE_BUF:
|
2021-10-06 22:24:04 +00:00
|
|
|
return str->str.buf;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
case TR_STRING_TYPE_HEAP:
|
|
|
|
case TR_STRING_TYPE_QUARK:
|
2021-11-15 07:21:57 +00:00
|
|
|
case TR_STRING_TYPE_VIEW:
|
2021-10-06 22:24:04 +00:00
|
|
|
return str->str.str;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
default:
|
2021-10-06 22:24:04 +00:00
|
|
|
return nullptr;
|
2012-12-22 20:35:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
void tr_variant_string_set_quark(struct tr_variant_string* str, tr_quark quark)
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant_string_clear(str);
|
2012-12-22 20:35:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
str->type = TR_STRING_TYPE_QUARK;
|
2022-08-28 21:17:07 +00:00
|
|
|
auto const sv = tr_quark_get_string_view(quark);
|
|
|
|
str->str.str = std::data(sv);
|
|
|
|
str->len = std::size(sv);
|
2012-12-22 20:35:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
void tr_variant_string_set_string(struct tr_variant_string* str, std::string_view in)
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant_string_clear(str);
|
2012-12-22 20:35:19 +00:00
|
|
|
|
2021-10-20 02:30:50 +00:00
|
|
|
auto const* const bytes = std::data(in);
|
|
|
|
auto const len = std::size(in);
|
2012-12-22 20:35:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (len < sizeof(str->str.buf))
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
str->type = TR_STRING_TYPE_BUF;
|
2020-10-31 21:23:43 +00:00
|
|
|
if (len > 0)
|
|
|
|
{
|
2021-10-20 02:30:50 +00:00
|
|
|
std::copy_n(bytes, len, str->str.buf);
|
2020-10-31 21:23:43 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
str->str.buf[len] = '\0';
|
|
|
|
str->len = len;
|
2012-12-22 20:35:19 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2022-08-17 19:44:18 +00:00
|
|
|
auto* tmp = new char[len + 1];
|
2021-10-20 02:30:50 +00:00
|
|
|
std::copy_n(bytes, len, tmp);
|
2017-04-19 12:04:45 +00:00
|
|
|
tmp[len] = '\0';
|
|
|
|
str->type = TR_STRING_TYPE_HEAP;
|
|
|
|
str->str.str = tmp;
|
|
|
|
str->len = len;
|
2012-12-22 20:35:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2012-12-22 20:35:19 +00:00
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
constexpr char const* getStr(tr_variant const* v)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_variantIsString(v));
|
2012-12-22 20:35:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_variant_string_get_string(&v->val.s);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
constexpr int dictIndexOf(tr_variant const* dict, tr_quark key)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantIsDict(dict))
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = 0; i < dict->val.l.count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
if (dict->val.l.vals[i].key == key)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
return (int)i;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return -1;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
bool dictFindType(tr_variant* dict, tr_quark key, int type, tr_variant** setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-01-05 04:16:22 +00:00
|
|
|
*setme = tr_variantDictFind(dict, key);
|
|
|
|
return tr_variantIsType(*setme, type);
|
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
tr_variant* containerReserve(tr_variant* v, size_t count)
|
|
|
|
{
|
|
|
|
TR_ASSERT(tr_variantIsContainer(v));
|
|
|
|
|
|
|
|
if (size_t const needed = v->val.l.count + count; needed > v->val.l.alloc)
|
|
|
|
{
|
|
|
|
/* scale the alloc size in powers-of-2 */
|
|
|
|
size_t n = v->val.l.alloc != 0 ? v->val.l.alloc : 8;
|
|
|
|
|
|
|
|
while (n < needed)
|
|
|
|
{
|
|
|
|
n *= 2U;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* vals = new tr_variant[n];
|
|
|
|
std::copy_n(v->val.l.vals, v->val.l.count, vals);
|
|
|
|
delete[] v->val.l.vals;
|
|
|
|
v->val.l.vals = vals;
|
|
|
|
v->val.l.alloc = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return v->val.l.vals + v->val.l.count;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variant* dictFindOrAdd(tr_variant* dict, tr_quark key, int type)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-01-05 04:16:22 +00:00
|
|
|
/* see if it already exists, and if so, try to reuse it */
|
|
|
|
tr_variant* child = tr_variantDictFind(dict, key);
|
|
|
|
if (child != nullptr)
|
|
|
|
{
|
|
|
|
if (!tr_variantIsType(child, type))
|
|
|
|
{
|
|
|
|
tr_variantDictRemove(dict, key);
|
|
|
|
child = nullptr;
|
|
|
|
}
|
|
|
|
else if (child->type == TR_VARIANT_TYPE_STR)
|
|
|
|
{
|
|
|
|
tr_variant_string_clear(&child->val.s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if it doesn't exist, create it */
|
|
|
|
if (child == nullptr)
|
|
|
|
{
|
|
|
|
child = tr_variantDictAdd(dict, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variant* tr_variantDictFind(tr_variant* dict, tr_quark key)
|
2023-01-05 04:16:22 +00:00
|
|
|
{
|
|
|
|
auto const i = dictIndexOf(dict, key);
|
|
|
|
|
|
|
|
return i < 0 ? nullptr : dict->val.l.vals + i;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variant* tr_variantListChild(tr_variant* list, size_t pos)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
if (tr_variantIsList(list) && pos < list->val.l.count)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
return list->val.l.vals + pos;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 16:04:44 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
return {};
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
bool tr_variantListRemove(tr_variant* list, size_t pos)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2022-08-29 02:51:26 +00:00
|
|
|
if (!tr_variantIsList(list) && pos < list->val.l.count)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2022-08-29 02:51:26 +00:00
|
|
|
return false;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 02:51:26 +00:00
|
|
|
auto& vals = list->val.l.vals;
|
|
|
|
auto& count = list->val.l.count;
|
|
|
|
|
|
|
|
tr_variantClear(&vals[pos]);
|
|
|
|
std::move(vals + pos + 1, vals + count, vals + pos);
|
|
|
|
--count;
|
|
|
|
vals[count] = {};
|
|
|
|
|
|
|
|
return true;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_variantGetInt(tr_variant const* v, int64_t* setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool success = false;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2019-03-17 06:09:08 +00:00
|
|
|
if (tr_variantIsInt(v))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (setme != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
*setme = v->val.i;
|
|
|
|
}
|
2019-03-17 06:09:08 +00:00
|
|
|
|
|
|
|
success = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2019-03-17 06:09:08 +00:00
|
|
|
if (!success && tr_variantIsBool(v))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (setme != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
*setme = v->val.b ? 1 : 0;
|
|
|
|
}
|
2019-03-17 06:09:08 +00:00
|
|
|
|
|
|
|
success = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return success;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 21:30:18 +00:00
|
|
|
bool tr_variantGetStrView(tr_variant const* v, std::string_view* setme)
|
|
|
|
{
|
|
|
|
if (!tr_variantIsString(v))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char const* const str = tr_variant_string_get_string(&v->val.s);
|
|
|
|
size_t const len = v->val.s.len;
|
|
|
|
*setme = std::string_view{ str, len };
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-24 18:40:12 +00:00
|
|
|
bool tr_variantGetRaw(tr_variant const* v, std::byte const** setme_raw, size_t* setme_len)
|
|
|
|
{
|
|
|
|
bool const success = tr_variantIsString(v);
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
{
|
2023-01-28 21:26:23 +00:00
|
|
|
*setme_raw = reinterpret_cast<std::byte const*>(getStr(v));
|
2022-10-24 18:40:12 +00:00
|
|
|
*setme_len = v->val.s.len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const success = tr_variantIsString(v);
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (success)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-01-28 21:26:23 +00:00
|
|
|
*setme_raw = reinterpret_cast<uint8_t const*>(getStr(v));
|
2017-04-19 12:04:45 +00:00
|
|
|
*setme_len = v->val.s.len;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return success;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_variantGetBool(tr_variant const* v, bool* setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2019-03-17 06:09:08 +00:00
|
|
|
if (tr_variantIsBool(v))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
*setme = v->val.b;
|
2021-11-01 21:30:18 +00:00
|
|
|
return true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2021-11-01 21:30:18 +00:00
|
|
|
if (tr_variantIsInt(v) && (v->val.i == 0 || v->val.i == 1))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-11-01 21:47:57 +00:00
|
|
|
*setme = v->val.i != 0;
|
2021-11-01 21:30:18 +00:00
|
|
|
return true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2022-02-08 05:44:31 +00:00
|
|
|
if (auto sv = std::string_view{}; tr_variantGetStrView(v, &sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-01 21:30:18 +00:00
|
|
|
if (sv == "true"sv)
|
|
|
|
{
|
|
|
|
*setme = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sv == "false"sv)
|
|
|
|
{
|
|
|
|
*setme = false;
|
|
|
|
return true;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_variantGetReal(tr_variant const* v, double* setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool success = false;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2019-03-17 06:09:08 +00:00
|
|
|
if (tr_variantIsReal(v))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
*setme = v->val.d;
|
2019-03-17 06:09:08 +00:00
|
|
|
success = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2019-03-17 06:09:08 +00:00
|
|
|
if (!success && tr_variantIsInt(v))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
*setme = (double)v->val.i;
|
2019-03-17 06:09:08 +00:00
|
|
|
success = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!success && tr_variantIsString(v))
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2022-05-16 05:06:17 +00:00
|
|
|
if (auto sv = std::string_view{}; tr_variantGetStrView(v, &sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-06-30 14:49:58 +00:00
|
|
|
if (auto d = tr_num_parse<double>(sv); d)
|
2022-05-16 05:06:17 +00:00
|
|
|
{
|
|
|
|
*setme = *d;
|
|
|
|
success = true;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return success;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
bool tr_variantDictFindInt(tr_variant* dict, tr_quark key, int64_t* setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
tr_variant const* child = tr_variantDictFind(dict, key);
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_variantGetInt(child, setme);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
bool tr_variantDictFindBool(tr_variant* dict, tr_quark key, bool* setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
tr_variant const* child = tr_variantDictFind(dict, key);
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_variantGetBool(child, setme);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
bool tr_variantDictFindReal(tr_variant* dict, tr_quark key, double* setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
tr_variant const* child = tr_variantDictFind(dict, key);
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_variantGetReal(child, setme);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
bool tr_variantDictFindStrView(tr_variant* dict, tr_quark key, std::string_view* setme)
|
2021-11-01 21:30:18 +00:00
|
|
|
{
|
|
|
|
tr_variant const* const child = tr_variantDictFind(dict, key);
|
|
|
|
return tr_variantGetStrView(child, setme);
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
bool tr_variantDictFindList(tr_variant* dict, tr_quark key, tr_variant** setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-01-05 04:16:22 +00:00
|
|
|
return dictFindType(dict, key, TR_VARIANT_TYPE_LIST, setme);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
bool tr_variantDictFindDict(tr_variant* dict, tr_quark key, tr_variant** setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-01-05 04:16:22 +00:00
|
|
|
return dictFindType(dict, key, TR_VARIANT_TYPE_DICT, setme);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
bool tr_variantDictFindRaw(tr_variant* dict, tr_quark key, uint8_t const** setme_raw, size_t* setme_len)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2022-10-24 18:40:12 +00:00
|
|
|
auto const* child = tr_variantDictFind(dict, key);
|
|
|
|
return tr_variantGetRaw(child, setme_raw, setme_len);
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
bool tr_variantDictFindRaw(tr_variant* dict, tr_quark key, std::byte const** setme_raw, size_t* setme_len)
|
2022-10-24 18:40:12 +00:00
|
|
|
{
|
|
|
|
auto const* child = tr_variantDictFind(dict, 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-01-28 23:58:20 +00:00
|
|
|
void tr_variantInitRaw(tr_variant* initme, void const* value, size_t value_len)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variantInit(initme, TR_VARIANT_TYPE_STR);
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variant_string_set_string(&initme->val.s, { static_cast<char const*>(value), value_len });
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
void tr_variantInitQuark(tr_variant* initme, tr_quark value)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variantInit(initme, TR_VARIANT_TYPE_STR);
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variant_string_set_quark(&initme->val.s, value);
|
2012-12-22 20:35:19 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
void tr_variantInitStr(tr_variant* initme, std::string_view value)
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variantInit(initme, TR_VARIANT_TYPE_STR);
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variant_string_set_string(&initme->val.s, value);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_variantInitList(tr_variant* initme, size_t reserve_count)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variantInit(initme, TR_VARIANT_TYPE_LIST);
|
|
|
|
tr_variantListReserve(initme, reserve_count);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_variantListReserve(tr_variant* list, size_t count)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_variantIsList(list));
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
containerReserve(list, count);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_variantInitDict(tr_variant* initme, size_t reserve_count)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variantInit(initme, TR_VARIANT_TYPE_DICT);
|
|
|
|
tr_variantDictReserve(initme, reserve_count);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_variantDictReserve(tr_variant* dict, size_t reserve_count)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_variantIsDict(dict));
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
containerReserve(dict, reserve_count);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* tr_variantListAdd(tr_variant* list)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_variantIsList(list));
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2021-10-28 01:16:24 +00:00
|
|
|
tr_variant* child = containerReserve(list, 1);
|
|
|
|
++list->val.l.count;
|
2017-04-19 12:04:45 +00:00
|
|
|
child->key = 0;
|
|
|
|
tr_variantInit(child, TR_VARIANT_TYPE_INT);
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variant* tr_variantListAddInt(tr_variant* list, int64_t value)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* child = tr_variantListAdd(list);
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variantInitInt(child, value);
|
2017-04-19 12:04:45 +00:00
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variant* tr_variantListAddReal(tr_variant* list, double value)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* child = tr_variantListAdd(list);
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variantInitReal(child, value);
|
2017-04-19 12:04:45 +00:00
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variant* tr_variantListAddBool(tr_variant* list, bool value)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* child = tr_variantListAdd(list);
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variantInitBool(child, value);
|
2017-04-19 12:04:45 +00:00
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variant* tr_variantListAddStr(tr_variant* list, std::string_view value)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* child = tr_variantListAdd(list);
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variantInitStr(child, value);
|
2017-04-19 12:04:45 +00:00
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variant* tr_variantListAddStrView(tr_variant* list, std::string_view value)
|
2021-11-15 07:21:57 +00:00
|
|
|
{
|
|
|
|
tr_variant* child = tr_variantListAdd(list);
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variantInitStrView(child, value);
|
2021-11-15 07:21:57 +00:00
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variant* tr_variantListAddQuark(tr_variant* list, tr_quark value)
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* child = tr_variantListAdd(list);
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variantInitQuark(child, value);
|
2017-04-19 12:04:45 +00:00
|
|
|
return child;
|
2012-12-22 20:35:19 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variant* tr_variantListAddRaw(tr_variant* list, void const* value, size_t value_len)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* child = tr_variantListAdd(list);
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variantInitRaw(child, value, value_len);
|
2017-04-19 12:04:45 +00:00
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* tr_variantListAddList(tr_variant* list, size_t reserve_count)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* child = tr_variantListAdd(list);
|
|
|
|
tr_variantInitList(child, reserve_count);
|
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* tr_variantListAddDict(tr_variant* list, size_t reserve_count)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* child = tr_variantListAdd(list);
|
|
|
|
tr_variantInitDict(child, reserve_count);
|
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variant* tr_variantDictAdd(tr_variant* dict, tr_quark key)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_variantIsDict(dict));
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2021-10-28 01:16:24 +00:00
|
|
|
tr_variant* val = containerReserve(dict, 1);
|
|
|
|
++dict->val.l.count;
|
2017-04-19 12:04:45 +00:00
|
|
|
val->key = key;
|
2021-10-28 01:16:24 +00:00
|
|
|
tr_variantInit(val, TR_VARIANT_TYPE_INT);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return val;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variant* tr_variantDictAddInt(tr_variant* dict, tr_quark key, int64_t val)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* child = dictFindOrAdd(dict, key, TR_VARIANT_TYPE_INT);
|
|
|
|
tr_variantInitInt(child, val);
|
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variant* tr_variantDictAddBool(tr_variant* dict, tr_quark key, bool val)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* child = dictFindOrAdd(dict, key, TR_VARIANT_TYPE_BOOL);
|
|
|
|
tr_variantInitBool(child, val);
|
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variant* tr_variantDictAddReal(tr_variant* dict, tr_quark key, double val)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* child = dictFindOrAdd(dict, key, TR_VARIANT_TYPE_REAL);
|
|
|
|
tr_variantInitReal(child, val);
|
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variant* tr_variantDictAddQuark(tr_variant* dict, tr_quark key, tr_quark const val)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* child = dictFindOrAdd(dict, key, TR_VARIANT_TYPE_STR);
|
|
|
|
tr_variantInitQuark(child, val);
|
|
|
|
return child;
|
2012-12-14 16:04:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variant* tr_variantDictAddStr(tr_variant* dict, tr_quark key, std::string_view val)
|
2012-12-14 16:04:44 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* child = dictFindOrAdd(dict, key, TR_VARIANT_TYPE_STR);
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variantInitStr(child, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variant* tr_variantDictAddStrView(tr_variant* dict, tr_quark key, std::string_view val)
|
2021-11-15 07:21:57 +00:00
|
|
|
{
|
|
|
|
tr_variant* child = dictFindOrAdd(dict, key, TR_VARIANT_TYPE_STR);
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variantInitStrView(child, val);
|
2021-11-15 07:21:57 +00:00
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variant* tr_variantDictAddRaw(tr_variant* dict, tr_quark key, void const* value, size_t len)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* child = dictFindOrAdd(dict, key, TR_VARIANT_TYPE_STR);
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variantInitRaw(child, value, len);
|
2017-04-19 12:04:45 +00:00
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variant* tr_variantDictAddList(tr_variant* dict, tr_quark key, size_t reserve_count)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* child = tr_variantDictAdd(dict, key);
|
|
|
|
tr_variantInitList(child, reserve_count);
|
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variant* tr_variantDictAddDict(tr_variant* dict, tr_quark key, size_t reserve_count)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* child = tr_variantDictAdd(dict, key);
|
|
|
|
tr_variantInitDict(child, reserve_count);
|
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variant* tr_variantDictSteal(tr_variant* dict, tr_quark key, tr_variant* value)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* child = tr_variantDictAdd(dict, key);
|
|
|
|
*child = *value;
|
|
|
|
child->key = key;
|
|
|
|
tr_variantInit(value, value->type);
|
|
|
|
return child;
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
bool tr_variantDictRemove(tr_variant* dict, tr_quark key)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool removed = false;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2022-02-08 05:44:31 +00:00
|
|
|
if (int const i = dictIndexOf(dict, key); i >= 0)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
int const last = (int)dict->val.l.count - 1;
|
2012-12-24 22:38:41 +00:00
|
|
|
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&dict->val.l.vals[i]);
|
2012-12-24 22:38:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (i != last)
|
|
|
|
{
|
|
|
|
dict->val.l.vals[i] = dict->val.l.vals[last];
|
|
|
|
}
|
2012-12-24 22:38:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
--dict->val.l.count;
|
2012-12-24 22:38:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
removed = true;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return removed;
|
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;
|
|
|
|
|
2021-11-05 14:54:44 +00:00
|
|
|
explicit WalkNode(tr_variant const* v_in)
|
2021-09-15 22:11:00 +00:00
|
|
|
{
|
2021-11-05 14:54:44 +00:00
|
|
|
assign(v_in);
|
2021-09-15 22:11:00 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2021-09-15 22:11:00 +00:00
|
|
|
tr_variant const* nextChild()
|
|
|
|
{
|
|
|
|
if (!tr_variantIsContainer(&v) || (child_index >= v.val.l.count))
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2021-09-15 22:11:00 +00:00
|
|
|
auto idx = child_index++;
|
|
|
|
if (!sorted.empty())
|
|
|
|
{
|
|
|
|
idx = sorted[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.val.l.vals + idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_visited = false;
|
|
|
|
|
2021-11-05 14:54:44 +00:00
|
|
|
// shallow bitwise copy of the variant passed to the constructor
|
|
|
|
tr_variant v = {};
|
2021-09-15 22:11:00 +00:00
|
|
|
|
2021-11-05 14:54:44 +00:00
|
|
|
protected:
|
|
|
|
friend class VariantWalker;
|
|
|
|
|
|
|
|
void assign(tr_variant const* v_in)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2021-11-05 14:54:44 +00:00
|
|
|
is_visited = false;
|
|
|
|
v = *v_in;
|
|
|
|
child_index = 0;
|
|
|
|
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
|
|
|
{
|
|
|
|
if (!tr_variantIsDict(&v))
|
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
|
|
|
|
2021-11-05 14:54:44 +00:00
|
|
|
auto const n = v.val.l.count;
|
2021-09-15 18:51:19 +00:00
|
|
|
auto const* children = v.val.l.vals;
|
2021-11-05 14:54:44 +00:00
|
|
|
|
|
|
|
sortbuf.resize(n);
|
2021-09-15 18:51:19 +00:00
|
|
|
for (size_t i = 0; i < n; ++i)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2022-08-28 21:17:07 +00:00
|
|
|
sortbuf[i] = { tr_quark_get_string_view(children[i].key), i };
|
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
|
|
|
|
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-12 12:36:16 +00:00
|
|
|
small::vector<size_t, 512> sorted;
|
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)
|
|
|
|
{
|
|
|
|
if (size == std::size(stack))
|
|
|
|
{
|
|
|
|
stack.emplace_back(v_in);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stack[size].assign(v_in);
|
|
|
|
}
|
|
|
|
|
|
|
|
++size;
|
|
|
|
|
|
|
|
if (sort_dicts)
|
|
|
|
{
|
|
|
|
top().sort(sortbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pop()
|
|
|
|
{
|
|
|
|
TR_ASSERT(size > 0);
|
|
|
|
if (size > 0)
|
|
|
|
{
|
|
|
|
--size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-23 22:47:41 +00:00
|
|
|
[[nodiscard]] bool empty() const
|
2021-11-05 14:54:44 +00:00
|
|
|
{
|
|
|
|
return size == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
WalkNode& top()
|
|
|
|
{
|
|
|
|
TR_ASSERT(size > 0);
|
|
|
|
return stack[size - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t size = 0;
|
2023-06-21 03:50:36 +00:00
|
|
|
|
|
|
|
static auto constexpr InitialCapacity = size_t{ 32U };
|
|
|
|
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)
|
|
|
|
*/
|
2022-09-07 16:04:28 +00:00
|
|
|
void tr_variantWalk(tr_variant const* top, struct VariantWalkFuncs 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{};
|
2022-08-03 06:15:37 +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
|
|
|
|
2021-09-15 18:51:19 +00:00
|
|
|
if (!node.is_visited)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2021-09-15 18:51:19 +00:00
|
|
|
v = &node.v;
|
2022-08-02 23:34:53 +00:00
|
|
|
|
2021-09-15 18:51:19 +00:00
|
|
|
node.is_visited = true;
|
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
|
|
|
{
|
2022-08-02 23:34:53 +00:00
|
|
|
v = node.nextChild();
|
|
|
|
|
|
|
|
if (v != nullptr)
|
2012-12-24 22:38:41 +00:00
|
|
|
{
|
2022-08-02 23:34:53 +00:00
|
|
|
if (tr_variantIsDict(&node.v))
|
|
|
|
{
|
|
|
|
auto tmp = tr_variant{};
|
|
|
|
tr_variantInitQuark(&tmp, v->key);
|
2022-09-07 16:04:28 +00:00
|
|
|
walk_funcs->stringFunc(&tmp, 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
|
|
|
{
|
2022-08-02 23:34:53 +00:00
|
|
|
if (tr_variantIsContainer(&node.v))
|
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
walk_funcs->containerEndFunc(&node.v, 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
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (v != nullptr)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (v->type)
|
|
|
|
{
|
|
|
|
case TR_VARIANT_TYPE_INT:
|
2022-09-07 16:04:28 +00:00
|
|
|
walk_funcs->intFunc(v, user_data);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_VARIANT_TYPE_BOOL:
|
2022-09-07 16:04:28 +00:00
|
|
|
walk_funcs->boolFunc(v, user_data);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_VARIANT_TYPE_REAL:
|
2022-09-07 16:04:28 +00:00
|
|
|
walk_funcs->realFunc(v, user_data);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_VARIANT_TYPE_STR:
|
2022-09-07 16:04:28 +00:00
|
|
|
walk_funcs->stringFunc(v, user_data);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_VARIANT_TYPE_LIST:
|
2021-09-15 18:51:19 +00:00
|
|
|
if (v == &node.v)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
walk_funcs->listBeginFunc(v, user_data);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-15 22:11:00 +00:00
|
|
|
stack.emplace(v, sort_dicts);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_VARIANT_TYPE_DICT:
|
2021-09-15 18:51:19 +00:00
|
|
|
if (v == &node.v)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
walk_funcs->dictBeginFunc(v, user_data);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-15 22:11:00 +00:00
|
|
|
stack.emplace(v, sort_dicts);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* did caller give us an uninitialized val? */
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddError(_("Invalid metadata"));
|
2017-04-19 12:04:45 +00:00
|
|
|
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-01-05 04:16:22 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
namespace clear_helpers
|
|
|
|
{
|
|
|
|
void freeDummyFunc(tr_variant const* /*v*/, void* /*buf*/)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
void freeStringFunc(tr_variant const* v, void* /*user_data*/)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-01-28 21:26:23 +00:00
|
|
|
tr_variant_string_clear(&const_cast<tr_variant*>(v)->val.s);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
void freeContainerEndFunc(tr_variant const* v, void* /*user_data*/)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2022-08-17 19:44:18 +00:00
|
|
|
delete[] v->val.l.vals;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
VariantWalkFuncs constexpr FreeWalkFuncs = {
|
2021-08-15 09:41:48 +00:00
|
|
|
freeDummyFunc, //
|
|
|
|
freeDummyFunc, //
|
|
|
|
freeDummyFunc, //
|
|
|
|
freeStringFunc, //
|
|
|
|
freeDummyFunc, //
|
|
|
|
freeDummyFunc, //
|
|
|
|
freeContainerEndFunc, //
|
2017-04-19 12:04:45 +00:00
|
|
|
};
|
2023-01-05 04:16:22 +00:00
|
|
|
} // namespace clear_helpers
|
|
|
|
} // namespace
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
void tr_variantClear(tr_variant* clearme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-01-05 04:16:22 +00:00
|
|
|
using namespace clear_helpers;
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
if (!tr_variantIsEmpty(clearme))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variantWalk(clearme, &FreeWalkFuncs, nullptr, false);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-08-18 01:25:42 +00:00
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
*clearme = {};
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2023-01-05 04:16:22 +00:00
|
|
|
|
|
|
|
bool tr_variantDictChild(tr_variant* dict, size_t pos, tr_quark* key, tr_variant** setme_value)
|
|
|
|
{
|
|
|
|
TR_ASSERT(tr_variantIsDict(dict));
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
if (tr_variantIsDict(dict) && pos < dict->val.l.count)
|
|
|
|
{
|
|
|
|
*key = dict->val.l.vals[pos].key;
|
|
|
|
*setme_value = dict->val.l.vals + pos;
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
namespace merge_helpers
|
|
|
|
{
|
|
|
|
void tr_variantListCopy(tr_variant* target, tr_variant const* src)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
for (size_t i = 0;; ++i)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto const* const child = tr_variantListChild(const_cast<tr_variant*>(src), i);
|
|
|
|
if (child == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-09-07 16:04:28 +00:00
|
|
|
|
|
|
|
if (tr_variantIsBool(child))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto val = bool{};
|
|
|
|
tr_variantGetBool(child, &val);
|
|
|
|
tr_variantListAddBool(target, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-09-07 16:04:28 +00:00
|
|
|
else if (tr_variantIsReal(child))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto val = double{};
|
|
|
|
tr_variantGetReal(child, &val);
|
|
|
|
tr_variantListAddReal(target, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-09-07 16:04:28 +00:00
|
|
|
else if (tr_variantIsInt(child))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto val = int64_t{};
|
|
|
|
tr_variantGetInt(child, &val);
|
|
|
|
tr_variantListAddInt(target, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-09-07 16:04:28 +00:00
|
|
|
else if (tr_variantIsString(child))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto val = std::string_view{};
|
|
|
|
(void)tr_variantGetStrView(child, &val);
|
|
|
|
tr_variantListAddRaw(target, std::data(val), std::size(val));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-09-07 16:04:28 +00:00
|
|
|
else if (tr_variantIsDict(child))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_variantMergeDicts(tr_variantListAddDict(target, 0), child);
|
|
|
|
}
|
|
|
|
else if (tr_variantIsList(child))
|
|
|
|
{
|
|
|
|
tr_variantListCopy(tr_variantListAddList(target, 0), child);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddWarn("tr_variantListCopy skipping item");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
constexpr size_t tr_variantDictSize(tr_variant const* dict)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return tr_variantIsDict(dict) ? dict->val.l.count : 0;
|
|
|
|
}
|
2023-01-05 04:16:22 +00:00
|
|
|
} // namespace merge_helpers
|
|
|
|
} // namespace
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_variantMergeDicts(tr_variant* target, tr_variant const* source)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2023-01-05 04:16:22 +00:00
|
|
|
using namespace merge_helpers;
|
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_variantIsDict(target));
|
|
|
|
TR_ASSERT(tr_variantIsDict(source));
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
size_t const source_count = tr_variantDictSize(source);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_variantDictReserve(target, source_count + tr_variantDictSize(target));
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
for (size_t i = 0; i < source_count; ++i)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2021-10-15 17:27:12 +00:00
|
|
|
auto key = tr_quark{};
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_variant* child = nullptr;
|
|
|
|
if (tr_variantDictChild(const_cast<tr_variant*>(source), i, &key, &child))
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2021-10-15 17:27:12 +00:00
|
|
|
tr_variant* t = nullptr;
|
|
|
|
|
2021-05-25 16:21:41 +00:00
|
|
|
// if types differ, ensure that target will overwrite source
|
2021-12-17 05:47:51 +00:00
|
|
|
auto const* const target_child = tr_variantDictFind(target, key);
|
2022-09-07 16:04:28 +00:00
|
|
|
if ((target_child != nullptr) && !tr_variantIsType(target_child, child->type))
|
2021-05-25 16:21:41 +00:00
|
|
|
{
|
|
|
|
tr_variantDictRemove(target, key);
|
|
|
|
}
|
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (tr_variantIsBool(child))
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto val = bool{};
|
|
|
|
tr_variantGetBool(child, &val);
|
|
|
|
tr_variantDictAddBool(target, key, val);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
2022-09-07 16:04:28 +00:00
|
|
|
else if (tr_variantIsReal(child))
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto val = double{};
|
|
|
|
tr_variantGetReal(child, &val);
|
|
|
|
tr_variantDictAddReal(target, key, val);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
2022-09-07 16:04:28 +00:00
|
|
|
else if (tr_variantIsInt(child))
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto val = int64_t{};
|
|
|
|
tr_variantGetInt(child, &val);
|
|
|
|
tr_variantDictAddInt(target, key, val);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
2022-09-07 16:04:28 +00:00
|
|
|
else if (tr_variantIsString(child))
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto val = std::string_view{};
|
|
|
|
(void)tr_variantGetStrView(child, &val);
|
|
|
|
tr_variantDictAddRaw(target, key, std::data(val), std::size(val));
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
2022-09-07 16:04:28 +00:00
|
|
|
else if (tr_variantIsDict(child) && tr_variantDictFindDict(target, key, &t))
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_variantMergeDicts(t, child);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
2022-09-07 16:04:28 +00:00
|
|
|
else if (tr_variantIsList(child))
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tr_variantDictFind(target, key) == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_variantListCopy(tr_variantDictAddList(target, key, tr_variantListSize(child)), child);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
2022-09-07 16:04:28 +00:00
|
|
|
else if (tr_variantIsDict(child))
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* target_dict = tr_variantDictFind(target, key);
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (target_dict == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
target_dict = tr_variantDictAddDict(target, key, tr_variantDictSize(child));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantIsDict(target_dict))
|
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_variantMergeDicts(target_dict, child);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2022-08-28 21:17:07 +00:00
|
|
|
tr_logAddDebug(fmt::format("tr_variantMergeDicts skipping '{}'", tr_quark_get_string_view(key)));
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
std::string tr_variantToStr(tr_variant const* v, tr_variant_fmt fmt)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (fmt)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_VARIANT_FMT_JSON:
|
2022-10-19 16:42:08 +00:00
|
|
|
return tr_variantToStrJson(v, false);
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_VARIANT_FMT_JSON_LEAN:
|
2022-10-19 16:42:08 +00:00
|
|
|
return tr_variantToStrJson(v, true);
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
default: // TR_VARIANT_FMT_BENC:
|
|
|
|
return tr_variantToStrBenc(v);
|
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 03:39:08 +00:00
|
|
|
int tr_variantToFile(tr_variant const* v, tr_variant_fmt fmt, std::string_view filename)
|
2020-11-05 22:46:21 +00:00
|
|
|
{
|
2021-12-06 03:12:21 +00:00
|
|
|
auto error_code = int{ 0 };
|
2021-12-27 22:47:25 +00:00
|
|
|
auto const contents = tr_variantToStr(v, fmt);
|
2021-12-10 00:49:30 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_error* error = nullptr;
|
2023-06-30 14:49:58 +00:00
|
|
|
tr_file_save(filename, contents, &error);
|
2021-12-05 01:32:35 +00:00
|
|
|
if (error != nullptr)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-03-16 00:51:36 +00:00
|
|
|
_("Couldn't save '{path}': {error} ({error_code})"),
|
2022-03-14 04:43:35 +00:00
|
|
|
fmt::arg("path", filename),
|
2022-03-16 00:51:36 +00:00
|
|
|
fmt::arg("error", error->message),
|
|
|
|
fmt::arg("error_code", error->code)));
|
2021-12-06 03:12:21 +00:00
|
|
|
error_code = error->code;
|
2021-12-05 01:32:35 +00:00
|
|
|
tr_error_clear(&error);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
2021-12-10 00:49:30 +00:00
|
|
|
|
2021-12-06 03:12:21 +00:00
|
|
|
return error_code;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2021-11-18 05:37:35 +00:00
|
|
|
bool tr_variantFromBuf(tr_variant* setme, int opts, std::string_view buf, char const** setme_end, tr_error** error)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2021-11-18 05:37:35 +00:00
|
|
|
// supported formats: benc, json
|
|
|
|
TR_ASSERT((opts & (TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_JSON)) != 0);
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2022-04-06 21:39:39 +00:00
|
|
|
*setme = {};
|
|
|
|
|
|
|
|
auto const success = ((opts & TR_VARIANT_PARSE_BENC) != 0) ? tr_variantParseBenc(*setme, opts, buf, setme_end, error) :
|
|
|
|
tr_variantParseJson(*setme, opts, buf, setme_end, error);
|
|
|
|
|
|
|
|
if (!success)
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(setme);
|
2022-01-24 06:30:00 +00:00
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
return success;
|
2021-11-15 07:21:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 03:39:08 +00:00
|
|
|
bool tr_variantFromFile(tr_variant* setme, tr_variant_parse_opts opts, std::string_view filename, tr_error** error)
|
2021-11-15 07:21:57 +00:00
|
|
|
{
|
2021-11-18 05:37:35 +00:00
|
|
|
// can't do inplace when this function is allocating & freeing the memory...
|
|
|
|
TR_ASSERT((opts & TR_VARIANT_PARSE_INPLACE) == 0);
|
|
|
|
|
2023-06-30 14:49:58 +00:00
|
|
|
if (auto buf = std::vector<char>{}; tr_file_read(filename, buf, error))
|
2021-11-15 07:21:57 +00:00
|
|
|
{
|
2022-08-05 21:12:45 +00:00
|
|
|
return tr_variantFromBuf(setme, opts, buf, nullptr, error);
|
2021-11-17 03:29:30 +00:00
|
|
|
}
|
2021-11-15 07:21:57 +00:00
|
|
|
|
2022-08-05 21:12:45 +00:00
|
|
|
return false;
|
2021-11-15 07:21:57 +00:00
|
|
|
}
|