2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2008-2023 Mnemosyne LLC.
|
2022-08-08 18:05:39 +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:34:42 +00:00
|
|
|
|
2016-03-29 16:37:21 +00:00
|
|
|
#pragma once
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-10-06 22:24:04 +00:00
|
|
|
#include <cstddef> // size_t
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <cstdint> // int64_t
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <string_view>
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2012-12-23 03:18:39 +00:00
|
|
|
#include "quark.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2015-04-11 10:51:59 +00:00
|
|
|
struct tr_error;
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
/**
|
|
|
|
* @addtogroup tr_variant Variant
|
|
|
|
*
|
|
|
|
* An object that acts like a union for
|
|
|
|
* integers, strings, lists, dictionaries, booleans, and floating-point numbers.
|
2023-01-23 16:26:11 +00:00
|
|
|
* The structure is named `tr_variant` due to the historical reason that it was
|
2012-12-14 04:34:42 +00:00
|
|
|
* originally tightly coupled with bencoded data. It currently supports
|
|
|
|
* being parsed from, and serialized to, both bencoded notation and json notation.
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
enum tr_string_type
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
TR_STRING_TYPE_QUARK,
|
|
|
|
TR_STRING_TYPE_HEAP,
|
2021-11-15 07:21:57 +00:00
|
|
|
TR_STRING_TYPE_BUF,
|
|
|
|
TR_STRING_TYPE_VIEW
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2012-12-22 20:35:19 +00:00
|
|
|
|
|
|
|
/* these are PRIVATE IMPLEMENTATION details that should not be touched.
|
|
|
|
* I'll probably change them just to break your code! HA HA HA!
|
|
|
|
* it's included in the header for inlining and composition */
|
|
|
|
struct tr_variant_string
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_string_type type;
|
|
|
|
size_t len;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
char buf[16];
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str;
|
2021-08-15 09:41:48 +00:00
|
|
|
} str;
|
2012-12-22 20:35:19 +00:00
|
|
|
};
|
2015-01-02 11:15:31 +00:00
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
/* these are PRIVATE IMPLEMENTATION details that should not be touched.
|
|
|
|
* I'll probably change them just to break your code! HA HA HA!
|
|
|
|
* it's included in the header for inlining and composition */
|
|
|
|
enum
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
TR_VARIANT_TYPE_INT = 1,
|
|
|
|
TR_VARIANT_TYPE_STR = 2,
|
|
|
|
TR_VARIANT_TYPE_LIST = 4,
|
|
|
|
TR_VARIANT_TYPE_DICT = 8,
|
|
|
|
TR_VARIANT_TYPE_BOOL = 16,
|
|
|
|
TR_VARIANT_TYPE_REAL = 32
|
2012-12-14 04:34:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* These are PRIVATE IMPLEMENTATION details that should not be touched.
|
|
|
|
* I'll probably change them just to break your code! HA HA HA!
|
|
|
|
* it's included in the header for inlining and composition */
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_variant
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-10-06 14:26:07 +00:00
|
|
|
char type = '\0';
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
tr_quark key = TR_KEY_NONE;
|
2012-12-24 22:38:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
union
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool b;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
double d;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t i;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_variant_string s;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
struct
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t alloc;
|
|
|
|
size_t count;
|
|
|
|
struct tr_variant* vals;
|
2012-12-14 04:34:42 +00:00
|
|
|
} l;
|
2021-10-06 14:26:07 +00:00
|
|
|
} val = {};
|
|
|
|
};
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-08-18 01:25:42 +00:00
|
|
|
/**
|
|
|
|
* @brief Clear the variant to an empty state.
|
|
|
|
*
|
|
|
|
* `tr_variantIsEmpty()` will return true after this is called.
|
|
|
|
*
|
|
|
|
* The variant itself is not freed, but any memory used by
|
|
|
|
* its *value* -- e.g. a string or child variants -- is freed.
|
|
|
|
*/
|
2023-01-28 23:58:20 +00:00
|
|
|
void tr_variantClear(tr_variant* clearme);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Serialization / Deserialization
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
enum tr_variant_fmt
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
|
|
|
TR_VARIANT_FMT_BENC,
|
|
|
|
TR_VARIANT_FMT_JSON,
|
|
|
|
TR_VARIANT_FMT_JSON_LEAN /* saves bandwidth by omitting all whitespace. */
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-03-26 03:39:08 +00:00
|
|
|
int tr_variantToFile(tr_variant const* variant, tr_variant_fmt fmt, std::string_view filename);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-10-19 16:42:08 +00:00
|
|
|
[[nodiscard]] std::string tr_variantToStr(tr_variant const* variant, tr_variant_fmt fmt);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-11-18 05:37:35 +00:00
|
|
|
enum tr_variant_parse_opts
|
|
|
|
{
|
|
|
|
TR_VARIANT_PARSE_BENC = (1 << 0),
|
|
|
|
TR_VARIANT_PARSE_JSON = (1 << 1),
|
|
|
|
TR_VARIANT_PARSE_INPLACE = (1 << 2)
|
|
|
|
};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-24 19:37:34 +00:00
|
|
|
bool tr_variantFromFile(
|
|
|
|
tr_variant* setme,
|
|
|
|
tr_variant_parse_opts opts,
|
2022-03-26 03:39:08 +00:00
|
|
|
std::string_view filename,
|
2021-12-24 19:37:34 +00:00
|
|
|
struct tr_error** error = nullptr);
|
2021-11-18 05:37:35 +00:00
|
|
|
|
|
|
|
bool tr_variantFromBuf(
|
|
|
|
tr_variant* setme,
|
|
|
|
int variant_parse_opts,
|
|
|
|
std::string_view buf,
|
|
|
|
char const** setme_end = nullptr,
|
|
|
|
tr_error** error = nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-05 21:12:45 +00:00
|
|
|
template<typename T>
|
|
|
|
bool tr_variantFromBuf(
|
|
|
|
tr_variant* setme,
|
|
|
|
int variant_parse_opts,
|
|
|
|
T const& buf,
|
|
|
|
char const** setme_end = nullptr,
|
|
|
|
tr_error** error = nullptr)
|
|
|
|
{
|
|
|
|
return tr_variantFromBuf(
|
|
|
|
setme,
|
|
|
|
variant_parse_opts,
|
|
|
|
std::string_view{ std::data(buf), static_cast<size_t>(std::size(buf)) },
|
|
|
|
setme_end,
|
|
|
|
error);
|
|
|
|
}
|
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] constexpr bool tr_variantIsType(tr_variant const* b, int type)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
return b != nullptr && b->type == type;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] constexpr bool tr_variantIsEmpty(tr_variant const* b)
|
2022-08-18 01:25:42 +00:00
|
|
|
{
|
|
|
|
return b == nullptr || b->type == '\0';
|
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Strings
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] constexpr bool tr_variantIsString(tr_variant const* b)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
return b != nullptr && b->type == TR_VARIANT_TYPE_STR;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 21:30:18 +00:00
|
|
|
bool tr_variantGetStrView(tr_variant const* variant, std::string_view* setme);
|
2015-01-02 11:15:31 +00:00
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
void tr_variantInitStr(tr_variant* initme, std::string_view value);
|
|
|
|
void tr_variantInitQuark(tr_variant* initme, tr_quark value);
|
|
|
|
void tr_variantInitRaw(tr_variant* initme, void const* value, size_t value_len);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-11-03 23:08:02 +00:00
|
|
|
constexpr void tr_variantInit(tr_variant* initme, char type)
|
|
|
|
{
|
|
|
|
initme->val = {};
|
|
|
|
initme->type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void tr_variantInitStrView(tr_variant* initme, std::string_view in)
|
|
|
|
{
|
|
|
|
tr_variantInit(initme, TR_VARIANT_TYPE_STR);
|
|
|
|
initme->val.s.type = TR_STRING_TYPE_VIEW;
|
|
|
|
initme->val.s.len = std::size(in);
|
|
|
|
initme->val.s.str.str = std::data(in);
|
|
|
|
}
|
|
|
|
|
2022-10-24 18:40:12 +00:00
|
|
|
bool tr_variantGetRaw(tr_variant const* variant, std::byte const** setme_raw, size_t* setme_len);
|
2022-08-03 06:15:37 +00:00
|
|
|
bool tr_variantGetRaw(tr_variant const* variant, uint8_t const** setme_raw, size_t* setme_len);
|
2012-12-22 20:35:19 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Real Numbers
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] constexpr bool tr_variantIsReal(tr_variant const* v)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
return v != nullptr && v->type == TR_VARIANT_TYPE_REAL;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_variantGetReal(tr_variant const* variant, double* value_setme);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-11-03 23:08:02 +00:00
|
|
|
constexpr void tr_variantInitReal(tr_variant* initme, double value)
|
|
|
|
{
|
|
|
|
tr_variantInit(initme, TR_VARIANT_TYPE_REAL);
|
|
|
|
initme->val.d = value;
|
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Booleans
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] constexpr bool tr_variantIsBool(tr_variant const* v)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
return v != nullptr && v->type == TR_VARIANT_TYPE_BOOL;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_variantGetBool(tr_variant const* variant, bool* setme);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-11-03 23:08:02 +00:00
|
|
|
constexpr void tr_variantInitBool(tr_variant* initme, bool value)
|
|
|
|
{
|
|
|
|
tr_variantInit(initme, TR_VARIANT_TYPE_BOOL);
|
|
|
|
initme->val.b = value;
|
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Ints
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] constexpr bool tr_variantIsInt(tr_variant const* v)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
return v != nullptr && v->type == TR_VARIANT_TYPE_INT;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_variantGetInt(tr_variant const* val, int64_t* setme);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-11-03 23:08:02 +00:00
|
|
|
constexpr void tr_variantInitInt(tr_variant* initme, int64_t value)
|
|
|
|
{
|
|
|
|
tr_variantInit(initme, TR_VARIANT_TYPE_INT);
|
|
|
|
initme->val.i = value;
|
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Lists
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] constexpr bool tr_variantIsList(tr_variant const* v)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
return v != nullptr && v->type == TR_VARIANT_TYPE_LIST;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_variantInitList(tr_variant* initme, size_t reserve_count);
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_variantListReserve(tr_variant* list, size_t reserve_count);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* tr_variantListAdd(tr_variant* list);
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variant* tr_variantListAddBool(tr_variant* list, bool value);
|
|
|
|
tr_variant* tr_variantListAddInt(tr_variant* list, int64_t value);
|
|
|
|
tr_variant* tr_variantListAddReal(tr_variant* list, double value);
|
|
|
|
tr_variant* tr_variantListAddStr(tr_variant* list, std::string_view value);
|
|
|
|
tr_variant* tr_variantListAddStrView(tr_variant* list, std::string_view value);
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variant* tr_variantListAddQuark(tr_variant* list, tr_quark value);
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variant* tr_variantListAddRaw(tr_variant* list, void const* value, size_t value_len);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* tr_variantListAddList(tr_variant* list, size_t reserve_count);
|
|
|
|
tr_variant* tr_variantListAddDict(tr_variant* list, size_t reserve_count);
|
|
|
|
tr_variant* tr_variantListChild(tr_variant* list, size_t pos);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_variantListRemove(tr_variant* list, size_t pos);
|
2022-11-03 23:08:02 +00:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr size_t tr_variantListSize(tr_variant const* list)
|
|
|
|
{
|
|
|
|
return tr_variantIsList(list) ? list->val.l.count : 0;
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Dictionaries
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] constexpr bool tr_variantIsDict(tr_variant const* v)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
return v != nullptr && v->type == TR_VARIANT_TYPE_DICT;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_variantInitDict(tr_variant* initme, size_t reserve_count);
|
|
|
|
void tr_variantDictReserve(tr_variant* dict, size_t reserve_count);
|
2023-01-28 23:58:20 +00:00
|
|
|
bool tr_variantDictRemove(tr_variant* dict, tr_quark key);
|
|
|
|
|
|
|
|
tr_variant* tr_variantDictAdd(tr_variant* dict, tr_quark key);
|
|
|
|
tr_variant* tr_variantDictAddReal(tr_variant* dict, tr_quark key, double value);
|
|
|
|
tr_variant* tr_variantDictAddInt(tr_variant* dict, tr_quark key, int64_t value);
|
|
|
|
tr_variant* tr_variantDictAddBool(tr_variant* dict, tr_quark key, bool value);
|
|
|
|
tr_variant* tr_variantDictAddStr(tr_variant* dict, tr_quark key, std::string_view value);
|
|
|
|
tr_variant* tr_variantDictAddStrView(tr_variant* dict, tr_quark key, std::string_view value);
|
|
|
|
tr_variant* tr_variantDictAddQuark(tr_variant* dict, tr_quark key, tr_quark val);
|
|
|
|
tr_variant* tr_variantDictAddList(tr_variant* dict, tr_quark key, size_t reserve_count);
|
|
|
|
tr_variant* tr_variantDictAddDict(tr_variant* dict, tr_quark key, size_t reserve_count);
|
|
|
|
tr_variant* tr_variantDictSteal(tr_variant* dict, tr_quark key, tr_variant* value);
|
|
|
|
tr_variant* tr_variantDictAddRaw(tr_variant* dict, tr_quark key, void const* value, size_t len);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
bool tr_variantDictChild(tr_variant* dict, size_t pos, tr_quark* setme_key, tr_variant** setme_value);
|
2023-01-28 23:58:20 +00:00
|
|
|
tr_variant* tr_variantDictFind(tr_variant* dict, tr_quark key);
|
|
|
|
bool tr_variantDictFindList(tr_variant* dict, tr_quark key, tr_variant** setme);
|
|
|
|
bool tr_variantDictFindDict(tr_variant* dict, tr_quark key, tr_variant** setme_value);
|
|
|
|
bool tr_variantDictFindInt(tr_variant* dict, tr_quark key, int64_t* setme);
|
|
|
|
bool tr_variantDictFindReal(tr_variant* dict, tr_quark key, double* setme);
|
|
|
|
bool tr_variantDictFindBool(tr_variant* dict, tr_quark key, bool* setme);
|
|
|
|
bool tr_variantDictFindStrView(tr_variant* dict, tr_quark key, std::string_view* setme);
|
|
|
|
bool tr_variantDictFindRaw(tr_variant* dict, tr_quark key, uint8_t const** setme_raw, size_t* setme_len);
|
|
|
|
bool tr_variantDictFindRaw(tr_variant* dict, tr_quark key, std::byte const** setme_raw, size_t* setme_len);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
|
|
|
/* this is only quasi-supported. don't rely on it too heavily outside of libT */
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_variantMergeDicts(tr_variant* dict_target, tr_variant const* dict_source);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
namespace libtransmission
|
|
|
|
{
|
|
|
|
|
|
|
|
struct VariantConverter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
template<typename T>
|
|
|
|
static std::optional<T> load(tr_variant* src);
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static void save(tr_variant* tgt, T const& val);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace libtransmission
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
/* @} */
|