2012-12-14 04:36:33 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
|
|
|
*
|
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2 (b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
|
|
|
* $Id:$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2012-12-24 22:38:41 +00:00
|
|
|
#include <stdio.h> /* rename() */
|
|
|
|
#include <stdlib.h> /* strtoul(), strtod(), realloc(), qsort(), mkstemp() */
|
2012-12-14 04:36:33 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
#ifdef WIN32 /* tr_mkstemp() */
|
2012-12-14 04:36:33 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#define _S_IREAD 256
|
|
|
|
#define _S_IWRITE 128
|
|
|
|
#endif
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
#include <locale.h> /* setlocale() */
|
|
|
|
#include <unistd.h> /* write(), unlink() */
|
2012-12-14 04:36:33 +00:00
|
|
|
|
|
|
|
#include <event2/buffer.h>
|
|
|
|
|
|
|
|
#define __LIBTRANSMISSION_VARIANT_MODULE___
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "ConvertUTF.h"
|
2012-12-24 22:38:41 +00:00
|
|
|
#include "fdlimit.h" /* tr_close_file() */
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2012-12-14 04:36:33 +00:00
|
|
|
#include "platform.h" /* TR_PATH_MAX */
|
2012-12-24 22:38:41 +00:00
|
|
|
#include "utils.h" /* tr_new(), tr_free() */
|
2012-12-14 04:36:33 +00:00
|
|
|
#include "variant.h"
|
|
|
|
#include "variant-common.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
static bool
|
2012-12-14 04:36:33 +00:00
|
|
|
tr_variantIsContainer (const tr_variant * v)
|
|
|
|
{
|
|
|
|
return tr_variantIsList (v) || tr_variantIsDict (v);
|
|
|
|
}
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
static bool
|
2012-12-14 04:36:33 +00:00
|
|
|
tr_variantIsSomething (const tr_variant * v)
|
|
|
|
{
|
|
|
|
return tr_variantIsContainer (v)
|
|
|
|
|| tr_variantIsInt (v)
|
|
|
|
|| tr_variantIsString (v)
|
|
|
|
|| tr_variantIsReal (v)
|
|
|
|
|| tr_variantIsBool (v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_variantInit (tr_variant * v, char type)
|
|
|
|
{
|
|
|
|
v->type = type;
|
2012-12-24 22:38:41 +00:00
|
|
|
memset (&v->val, 0, sizeof(v->val));
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
static const struct tr_variant_string STRING_INIT =
|
|
|
|
{
|
|
|
|
.type = TR_STRING_TYPE_QUARK,
|
|
|
|
.quark = TR_KEY_NONE,
|
|
|
|
.len = 0,
|
|
|
|
.str.str = ""
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
tr_variant_string_clear (struct tr_variant_string * str)
|
|
|
|
{
|
|
|
|
if (str->type == TR_STRING_TYPE_HEAP)
|
|
|
|
tr_free ((char*)(str->str.str));
|
|
|
|
|
|
|
|
*str = STRING_INIT;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* returns a const pointer to the variant's string */
|
2012-12-22 20:35:19 +00:00
|
|
|
static const char *
|
|
|
|
tr_variant_string_get_string (const struct tr_variant_string * str)
|
|
|
|
{
|
|
|
|
const char * ret;
|
|
|
|
|
|
|
|
switch (str->type)
|
|
|
|
{
|
|
|
|
case TR_STRING_TYPE_BUF: ret = str->str.buf; break;
|
|
|
|
case TR_STRING_TYPE_HEAP: ret = str->str.str; break;
|
|
|
|
case TR_STRING_TYPE_QUARK: ret = str->str.str; break;
|
|
|
|
default: ret = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variant_string_set_quark (struct tr_variant_string * str,
|
|
|
|
const tr_quark quark)
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
|
|
|
tr_variant_string_clear (str);
|
|
|
|
|
|
|
|
str->type = TR_STRING_TYPE_QUARK;
|
|
|
|
str->quark = quark;
|
|
|
|
str->str.str = tr_quark_get_string (quark, &str->len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variant_string_set_string (struct tr_variant_string * str,
|
|
|
|
const char * bytes,
|
|
|
|
int len)
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
|
|
|
tr_variant_string_clear (str);
|
|
|
|
|
|
|
|
if (bytes == NULL)
|
|
|
|
len = 0;
|
|
|
|
else if (len < 0)
|
|
|
|
len = strlen (bytes);
|
|
|
|
|
2012-12-28 23:46:34 +00:00
|
|
|
if ((size_t)len < sizeof(str->str.buf))
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
|
|
|
str->type = TR_STRING_TYPE_BUF;
|
|
|
|
memcpy (str->str.buf, bytes, len);
|
|
|
|
str->str.buf[len] = '\0';
|
|
|
|
str->len = len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char * tmp = tr_new (char, len+1);
|
|
|
|
memcpy (tmp, bytes, len);
|
|
|
|
tmp[len] = '\0';
|
|
|
|
str->type = TR_STRING_TYPE_HEAP;
|
|
|
|
str->str.str = tmp;
|
|
|
|
str->len = len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static inline const char *
|
|
|
|
getStr (const tr_variant * v)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
assert (tr_variantIsString (v));
|
|
|
|
|
|
|
|
return tr_variant_string_get_string (&v->val.s);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-12-22 20:35:19 +00:00
|
|
|
dictIndexOf (const tr_variant * dict, const tr_quark key)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-14 16:04:44 +00:00
|
|
|
if (tr_variantIsDict (dict))
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-15 00:01:59 +00:00
|
|
|
const tr_variant * walk;
|
|
|
|
const tr_variant * const begin = dict->val.l.vals;
|
|
|
|
const tr_variant * const end = begin + dict->val.l.count;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
for (walk=begin; walk!=end; ++walk)
|
|
|
|
if (walk->key == key)
|
2012-12-15 00:01:59 +00:00
|
|
|
return walk - begin;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variant *
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantDictFind (tr_variant * dict, const tr_quark key)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-14 16:04:44 +00:00
|
|
|
const int i = dictIndexOf (dict, key);
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
return i < 0 ? NULL : dict->val.l.vals+i;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantDictFindType (tr_variant * dict,
|
|
|
|
const tr_quark key,
|
|
|
|
int type,
|
|
|
|
tr_variant ** setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
|
|
|
return tr_variantIsType (*setme = tr_variantDictFind (dict, key), type);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
tr_variantListSize (const tr_variant * list)
|
|
|
|
{
|
2012-12-14 16:04:44 +00:00
|
|
|
return tr_variantIsList (list) ? list->val.l.count : 0;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_variant*
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantListChild (tr_variant * v,
|
|
|
|
size_t i)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_variant * ret = NULL;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
if (tr_variantIsList (v) && (i < v->val.l.count))
|
|
|
|
ret = v->val.l.vals + i;
|
2012-12-14 16:04:44 +00:00
|
|
|
|
|
|
|
return ret;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
bool
|
2012-12-14 04:36:33 +00:00
|
|
|
tr_variantListRemove (tr_variant * list, size_t i)
|
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
bool removed = false;
|
|
|
|
|
2012-12-14 04:36:33 +00:00
|
|
|
if (tr_variantIsList (list) && (i < list->val.l.count))
|
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
removed = true;
|
2012-12-14 04:36:33 +00:00
|
|
|
tr_variantFree (&list->val.l.vals[i]);
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_removeElementFromArray (list->val.l.vals, i,
|
|
|
|
sizeof (tr_variant),
|
|
|
|
list->val.l.count--);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
return removed;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantGetInt (const tr_variant * v,
|
2012-12-14 04:36:33 +00:00
|
|
|
int64_t * setme)
|
|
|
|
{
|
|
|
|
bool success = false;
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
if (!success && ((success = tr_variantIsInt (v))))
|
2012-12-14 04:36:33 +00:00
|
|
|
if (setme)
|
2012-12-24 22:38:41 +00:00
|
|
|
*setme = v->val.i;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
if (!success && ((success = tr_variantIsBool (v))))
|
|
|
|
if (setme)
|
|
|
|
*setme = v->val.b ? 1 : 0;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantGetStr (const tr_variant * v,
|
2012-12-14 04:36:33 +00:00
|
|
|
const char ** setme,
|
|
|
|
size_t * len)
|
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
const bool success = tr_variantIsString (v);
|
2012-12-14 04:36:33 +00:00
|
|
|
|
|
|
|
if (success)
|
2012-12-24 22:38:41 +00:00
|
|
|
*setme = getStr (v);
|
2012-12-14 04:36:33 +00:00
|
|
|
|
|
|
|
if (len != NULL)
|
2012-12-24 22:38:41 +00:00
|
|
|
*len = success ? v->val.s.len : 0;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantGetRaw (const tr_variant * v,
|
2012-12-14 04:36:33 +00:00
|
|
|
const uint8_t ** setme_raw,
|
|
|
|
size_t * setme_len)
|
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
const bool success = tr_variantIsString (v);
|
2012-12-14 04:36:33 +00:00
|
|
|
|
|
|
|
if (success)
|
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
*setme_raw = (uint8_t*) getStr (v);
|
|
|
|
*setme_len = v->val.s.len;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantGetBool (const tr_variant * v, bool * setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
|
|
|
const char * str;
|
|
|
|
bool success = false;
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
if ((success = tr_variantIsBool (v)))
|
|
|
|
*setme = v->val.b;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
if (!success && tr_variantIsInt (v))
|
|
|
|
if ((success = (v->val.i==0 || v->val.i==1)))
|
|
|
|
*setme = v->val.i!=0;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
if (!success && tr_variantGetStr (v, &str, NULL))
|
2012-12-14 04:36:33 +00:00
|
|
|
if ((success = (!strcmp (str,"true") || !strcmp (str,"false"))))
|
|
|
|
*setme = !strcmp (str,"true");
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantGetReal (const tr_variant * v, double * setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
|
|
|
bool success = false;
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
if (!success && ((success = tr_variantIsReal (v))))
|
|
|
|
*setme = v->val.d;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
if (!success && ((success = tr_variantIsInt (v))))
|
|
|
|
*setme = v->val.i;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
if (!success && tr_variantIsString (v))
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
|
|
|
char * endptr;
|
|
|
|
char locale[128];
|
|
|
|
double d;
|
|
|
|
|
|
|
|
/* the json spec requires a '.' decimal point regardless of locale */
|
|
|
|
tr_strlcpy (locale, setlocale (LC_NUMERIC, NULL), sizeof (locale));
|
|
|
|
setlocale (LC_NUMERIC, "POSIX");
|
2012-12-24 22:38:41 +00:00
|
|
|
d = strtod (getStr (v), &endptr);
|
2012-12-14 04:36:33 +00:00
|
|
|
setlocale (LC_NUMERIC, locale);
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
if ((success = (getStr (v) != endptr) && !*endptr))
|
2012-12-14 04:36:33 +00:00
|
|
|
*setme = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantDictFindInt (tr_variant * dict,
|
|
|
|
const tr_quark key,
|
|
|
|
int64_t * setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
|
|
|
tr_variant * child = tr_variantDictFind (dict, key);
|
|
|
|
return tr_variantGetInt (child, setme);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantDictFindBool (tr_variant * dict,
|
|
|
|
const tr_quark key,
|
|
|
|
bool * setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
|
|
|
tr_variant * child = tr_variantDictFind (dict, key);
|
|
|
|
return tr_variantGetBool (child, setme);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantDictFindReal (tr_variant * dict,
|
|
|
|
const tr_quark key,
|
|
|
|
double * setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
|
|
|
tr_variant * child = tr_variantDictFind (dict, key);
|
|
|
|
return tr_variantGetReal (child, setme);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantDictFindStr (tr_variant * dict,
|
|
|
|
const tr_quark key,
|
|
|
|
const char ** setme,
|
|
|
|
size_t * len)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
|
|
|
tr_variant * child = tr_variantDictFind (dict, key);
|
|
|
|
return tr_variantGetStr (child, setme, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantDictFindList (tr_variant * dict,
|
|
|
|
const tr_quark key,
|
|
|
|
tr_variant ** setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
return tr_variantDictFindType (dict, key, TR_VARIANT_TYPE_LIST, setme);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantDictFindDict (tr_variant * dict,
|
|
|
|
const tr_quark key,
|
|
|
|
tr_variant ** setme)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
return tr_variantDictFindType (dict, key, TR_VARIANT_TYPE_DICT, setme);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
tr_variantDictFindRaw (tr_variant * dict,
|
2012-12-22 20:35:19 +00:00
|
|
|
const tr_quark key,
|
2012-12-14 04:36:33 +00:00
|
|
|
const uint8_t ** setme_raw,
|
|
|
|
size_t * setme_len)
|
|
|
|
{
|
|
|
|
tr_variant * child = tr_variantDictFind (dict, key);
|
|
|
|
return tr_variantGetRaw (child, setme_raw, setme_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantInitRaw (tr_variant * v, const void * src, size_t byteCount)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantInit (v, TR_VARIANT_TYPE_STR);
|
|
|
|
tr_variant_string_set_string (&v->val.s, src, byteCount);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantInitQuark (tr_variant * v, const tr_quark q)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantInit (v, TR_VARIANT_TYPE_STR);
|
|
|
|
tr_variant_string_set_quark (&v->val.s, q);
|
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
void
|
|
|
|
tr_variantInitStr (tr_variant * v, const void * str, int len)
|
|
|
|
{
|
|
|
|
tr_variantInit (v, TR_VARIANT_TYPE_STR);
|
|
|
|
tr_variant_string_set_string (&v->val.s, str, len);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantInitBool (tr_variant * v, bool value)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantInit (v, TR_VARIANT_TYPE_BOOL);
|
|
|
|
v->val.b = value != 0;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantInitReal (tr_variant * v, double value)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantInit (v, TR_VARIANT_TYPE_REAL);
|
|
|
|
v->val.d = value;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantInitInt (tr_variant * v, int64_t value)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantInit (v, TR_VARIANT_TYPE_INT);
|
|
|
|
v->val.i = value;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
void
|
|
|
|
tr_variantInitList (tr_variant * v, size_t reserve_count)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantInit (v, TR_VARIANT_TYPE_LIST);
|
|
|
|
tr_variantListReserve (v, reserve_count);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
static void
|
|
|
|
containerReserve (tr_variant * v, size_t count)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
const size_t needed = v->val.l.count + count;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
assert (tr_variantIsContainer (v));
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
if (needed > v->val.l.alloc)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
|
|
|
/* scale the alloc size in powers-of-2 */
|
2012-12-24 22:38:41 +00:00
|
|
|
size_t n = v->val.l.alloc ? v->val.l.alloc : 8;
|
2012-12-14 04:36:33 +00:00
|
|
|
while (n < needed)
|
|
|
|
n *= 2u;
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
v->val.l.vals = tr_renew (tr_variant, v->val.l.vals, n);
|
|
|
|
v->val.l.alloc = n;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
void
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_variantListReserve (tr_variant * list, size_t count)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-14 16:04:44 +00:00
|
|
|
assert (tr_variantIsList (list));
|
2012-12-24 22:38:41 +00:00
|
|
|
containerReserve (list, count);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
void
|
|
|
|
tr_variantInitDict (tr_variant * v, size_t reserve_count)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantInit (v, TR_VARIANT_TYPE_DICT);
|
|
|
|
tr_variantDictReserve (v, reserve_count);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
void
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_variantDictReserve (tr_variant * dict,
|
|
|
|
size_t reserve_count)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-14 16:04:44 +00:00
|
|
|
assert (tr_variantIsDict (dict));
|
2012-12-24 22:38:41 +00:00
|
|
|
containerReserve (dict, reserve_count);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_variant *
|
|
|
|
tr_variantListAdd (tr_variant * list)
|
|
|
|
{
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_variant * child;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
|
|
|
assert (tr_variantIsList (list));
|
|
|
|
|
2012-12-14 16:04:44 +00:00
|
|
|
containerReserve (list, 1);
|
|
|
|
child = &list->val.l.vals[list->val.l.count++];
|
2012-12-24 22:38:41 +00:00
|
|
|
child->key = 0;
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_variantInit (child, TR_VARIANT_TYPE_INT);
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2012-12-14 16:04:44 +00:00
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_variant *
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_variantListAddInt (tr_variant * list,
|
|
|
|
int64_t val)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_variant * child = tr_variantListAdd (list);
|
|
|
|
tr_variantInitInt (child, val);
|
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_variant *
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_variantListAddReal (tr_variant * list,
|
|
|
|
double val)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_variant * child = tr_variantListAdd (list);
|
|
|
|
tr_variantInitReal (child, val);
|
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_variant *
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_variantListAddBool (tr_variant * list,
|
|
|
|
bool val)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_variant * child = tr_variantListAdd (list);
|
|
|
|
tr_variantInitBool (child, val);
|
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_variant *
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_variantListAddStr (tr_variant * list,
|
|
|
|
const char * val)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_variant * child = tr_variantListAdd (list);
|
|
|
|
tr_variantInitStr (child, val, -1);
|
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variant *
|
|
|
|
tr_variantListAddQuark (tr_variant * list,
|
|
|
|
const tr_quark val)
|
|
|
|
{
|
|
|
|
tr_variant * child = tr_variantListAdd (list);
|
|
|
|
tr_variantInitQuark (child, val);
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
2012-12-14 04:36:33 +00:00
|
|
|
tr_variant *
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_variantListAddRaw (tr_variant * list,
|
|
|
|
const void * val,
|
|
|
|
size_t len)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_variant * child = tr_variantListAdd (list);
|
|
|
|
tr_variantInitRaw (child, val, len);
|
|
|
|
return child;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_variant*
|
|
|
|
tr_variantListAddList (tr_variant * list,
|
|
|
|
size_t reserve_count)
|
|
|
|
{
|
|
|
|
tr_variant * child = tr_variantListAdd (list);
|
|
|
|
tr_variantInitList (child, reserve_count);
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variant*
|
|
|
|
tr_variantListAddDict (tr_variant * list,
|
|
|
|
size_t reserve_count)
|
|
|
|
{
|
|
|
|
tr_variant * child = tr_variantListAdd (list);
|
|
|
|
tr_variantInitDict (child, reserve_count);
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variant *
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantDictAdd (tr_variant * dict,
|
|
|
|
const tr_quark key)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variant * val;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
|
|
|
assert (tr_variantIsDict (dict));
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
containerReserve (dict, 1);
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
val = dict->val.l.vals + dict->val.l.count++;
|
|
|
|
tr_variantInit (val, TR_VARIANT_TYPE_INT);
|
|
|
|
val->key = key;
|
|
|
|
return val;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static tr_variant*
|
2012-12-22 20:35:19 +00:00
|
|
|
dictFindOrAdd (tr_variant * dict, const tr_quark key, int type)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
|
|
|
tr_variant * child;
|
|
|
|
|
|
|
|
/* see if it already exists, and if so, try to reuse it */
|
|
|
|
if ((child = tr_variantDictFind (dict, key)))
|
|
|
|
{
|
|
|
|
if (!tr_variantIsType (child, type))
|
|
|
|
{
|
|
|
|
tr_variantDictRemove (dict, key);
|
|
|
|
child = NULL;
|
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
else if (child->type == TR_VARIANT_TYPE_STR)
|
|
|
|
{
|
|
|
|
tr_variant_string_clear (&child->val.s);
|
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if it doesn't exist, create it */
|
|
|
|
if (child == NULL)
|
2012-12-22 20:35:19 +00:00
|
|
|
child = tr_variantDictAdd (dict, key);
|
2012-12-14 04:36:33 +00:00
|
|
|
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variant*
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantDictAddInt (tr_variant * dict,
|
|
|
|
const tr_quark key,
|
|
|
|
int64_t val)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
|
|
|
tr_variant * child = dictFindOrAdd (dict, key, TR_VARIANT_TYPE_INT);
|
|
|
|
tr_variantInitInt (child, val);
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variant*
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantDictAddBool (tr_variant * dict,
|
|
|
|
const tr_quark key,
|
|
|
|
bool val)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
|
|
|
tr_variant * child = dictFindOrAdd (dict, key, TR_VARIANT_TYPE_BOOL);
|
|
|
|
tr_variantInitBool (child, val);
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variant*
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantDictAddReal (tr_variant * dict,
|
|
|
|
const tr_quark key,
|
|
|
|
double val)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
|
|
|
tr_variant * child = dictFindOrAdd (dict, key, TR_VARIANT_TYPE_REAL);
|
|
|
|
tr_variantInitReal (child, val);
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variant*
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantDictAddQuark (tr_variant * dict,
|
|
|
|
const tr_quark key,
|
|
|
|
const tr_quark val)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variant * child = dictFindOrAdd (dict, key, TR_VARIANT_TYPE_STR);
|
|
|
|
tr_variantInitQuark (child, val);
|
2012-12-14 16:04:44 +00:00
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variant*
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantDictAddStr (tr_variant * dict,
|
|
|
|
const tr_quark key,
|
|
|
|
const char * val)
|
2012-12-14 16:04:44 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variant * child = dictFindOrAdd (dict, key, TR_VARIANT_TYPE_STR);
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_variantInitStr (child, val, -1);
|
2012-12-14 04:36:33 +00:00
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variant*
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantDictAddRaw (tr_variant * dict,
|
|
|
|
const tr_quark key,
|
|
|
|
const void * src,
|
|
|
|
size_t len)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variant * child = dictFindOrAdd (dict, key, TR_VARIANT_TYPE_STR);
|
2012-12-14 04:36:33 +00:00
|
|
|
tr_variantInitRaw (child, src, len);
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variant*
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantDictAddList (tr_variant * dict,
|
|
|
|
const tr_quark key,
|
|
|
|
size_t reserve_count)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variant * child = tr_variantDictAdd (dict, key);
|
2012-12-14 04:36:33 +00:00
|
|
|
tr_variantInitList (child, reserve_count);
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variant*
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantDictAddDict (tr_variant * dict,
|
|
|
|
const tr_quark key,
|
|
|
|
size_t reserve_count)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variant * child = tr_variantDictAdd (dict, key);
|
2012-12-14 04:36:33 +00:00
|
|
|
tr_variantInitDict (child, reserve_count);
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
bool
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variantDictRemove (tr_variant * dict,
|
|
|
|
const tr_quark key)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
bool removed = false;
|
2012-12-14 04:36:33 +00:00
|
|
|
const int i = dictIndexOf (dict, key);
|
|
|
|
|
|
|
|
if (i >= 0)
|
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
const int last = dict->val.l.count - 1;
|
|
|
|
|
2012-12-14 04:36:33 +00:00
|
|
|
tr_variantFree (&dict->val.l.vals[i]);
|
2012-12-24 22:38:41 +00:00
|
|
|
|
|
|
|
if (i != last)
|
|
|
|
dict->val.l.vals[i] = dict->val.l.vals[last];
|
|
|
|
|
|
|
|
--dict->val.l.count;
|
|
|
|
|
|
|
|
removed = true;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
return removed;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** BENC WALKING
|
|
|
|
***/
|
|
|
|
|
|
|
|
struct KeyIndex
|
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
const char * keystr;
|
|
|
|
tr_variant * val;
|
2012-12-14 04:36:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
compareKeyIndex (const void * va, const void * vb)
|
|
|
|
{
|
|
|
|
const struct KeyIndex * a = va;
|
|
|
|
const struct KeyIndex * b = vb;
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
return strcmp (a->keystr, b->keystr);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct SaveNode
|
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
const tr_variant * v;
|
|
|
|
tr_variant sorted;
|
|
|
|
size_t childIndex;
|
|
|
|
bool isVisited;
|
2012-12-14 04:36:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2012-12-24 22:38:41 +00:00
|
|
|
nodeConstruct (struct SaveNode * node,
|
|
|
|
const tr_variant * v,
|
|
|
|
bool sort_dicts)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
node->isVisited = false;
|
|
|
|
node->childIndex = 0;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
if (sort_dicts && tr_variantIsDict(v))
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
/* make node->sorted a sorted version of this dictionary */
|
|
|
|
|
|
|
|
size_t i;
|
|
|
|
const size_t n = v->val.l.count;
|
|
|
|
struct KeyIndex * tmp = tr_new (struct KeyIndex, n);
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
for (i=0; i<n; i++)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
tmp[i].val = v->val.l.vals+i;
|
|
|
|
tmp[i].keystr = tr_quark_get_string (tmp[i].val->key, NULL);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
qsort (tmp, n, sizeof (struct KeyIndex), compareKeyIndex);
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantInitDict (&node->sorted, n);
|
|
|
|
for (i=0; i<n; ++i)
|
|
|
|
node->sorted.val.l.vals[i] = *tmp[i].val;
|
|
|
|
node->sorted.val.l.count = n;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_free (tmp);
|
|
|
|
|
|
|
|
node->v = &node->sorted;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
node->v = v;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-24 22:38:41 +00:00
|
|
|
nodeDestruct (struct SaveNode * node)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
if (node->v == &node->sorted)
|
|
|
|
tr_free (node->sorted.val.l.vals);
|
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)
|
|
|
|
*/
|
|
|
|
void
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantWalk (const tr_variant * v,
|
2012-12-14 04:36:33 +00:00
|
|
|
const struct VariantWalkFuncs * walkFuncs,
|
|
|
|
void * user_data,
|
|
|
|
bool sort_dicts)
|
|
|
|
{
|
|
|
|
int stackSize = 0;
|
|
|
|
int stackAlloc = 64;
|
|
|
|
struct SaveNode * stack = tr_new (struct SaveNode, stackAlloc);
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
nodeConstruct (&stack[stackSize++], v, sort_dicts);
|
2012-12-14 04:36:33 +00:00
|
|
|
|
|
|
|
while (stackSize > 0)
|
|
|
|
{
|
|
|
|
struct SaveNode * node = &stack[stackSize-1];
|
2012-12-24 22:38:41 +00:00
|
|
|
const tr_variant * v;
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
if (!node->isVisited)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
v = node->v;
|
|
|
|
node->isVisited = true;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
2012-12-24 22:38:41 +00:00
|
|
|
else if (tr_variantIsContainer(node->v) && (node->childIndex < node->v->val.l.count))
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
const int index = node->childIndex++;
|
|
|
|
v = node->v->val.l.vals + index;
|
|
|
|
|
|
|
|
if (tr_variantIsDict (node->v))
|
|
|
|
{
|
|
|
|
tr_variant tmp;
|
|
|
|
tr_variantInitQuark (&tmp, v->key);
|
|
|
|
walkFuncs->stringFunc (&tmp, user_data);
|
|
|
|
}
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
else /* done with this node */
|
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
if (tr_variantIsContainer (node->v))
|
|
|
|
walkFuncs->containerEndFunc (node->v, user_data);
|
|
|
|
--stackSize;
|
|
|
|
nodeDestruct (node);
|
|
|
|
continue;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
if (v) switch (v->type)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
|
|
|
case TR_VARIANT_TYPE_INT:
|
2012-12-24 22:38:41 +00:00
|
|
|
walkFuncs->intFunc (v, user_data);
|
2012-12-14 04:36:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_VARIANT_TYPE_BOOL:
|
2012-12-24 22:38:41 +00:00
|
|
|
walkFuncs->boolFunc (v, user_data);
|
2012-12-14 04:36:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_VARIANT_TYPE_REAL:
|
2012-12-24 22:38:41 +00:00
|
|
|
walkFuncs->realFunc (v, user_data);
|
2012-12-14 04:36:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_VARIANT_TYPE_STR:
|
2012-12-24 22:38:41 +00:00
|
|
|
walkFuncs->stringFunc (v, user_data);
|
2012-12-14 04:36:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_VARIANT_TYPE_LIST:
|
2012-12-24 22:38:41 +00:00
|
|
|
if (v == node->v)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
walkFuncs->listBeginFunc (v, user_data);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (stackAlloc == stackSize)
|
|
|
|
{
|
|
|
|
stackAlloc *= 2;
|
|
|
|
stack = tr_renew (struct SaveNode, stack, stackAlloc);
|
|
|
|
}
|
2012-12-24 22:38:41 +00:00
|
|
|
nodeConstruct (&stack[stackSize++], v, sort_dicts);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_VARIANT_TYPE_DICT:
|
2012-12-24 22:38:41 +00:00
|
|
|
if (v == node->v)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
walkFuncs->dictBeginFunc (v, user_data);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (stackAlloc == stackSize)
|
|
|
|
{
|
|
|
|
stackAlloc *= 2;
|
|
|
|
stack = tr_renew (struct SaveNode, stack, stackAlloc);
|
|
|
|
}
|
2012-12-24 22:38:41 +00:00
|
|
|
nodeConstruct (&stack[stackSize++], v, sort_dicts);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* did caller give us an uninitialized val? */
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddError ("%s", _("Invalid metadata"));
|
2012-12-14 04:36:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free (stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
static void
|
2012-12-22 20:35:19 +00:00
|
|
|
freeDummyFunc (const tr_variant * v UNUSED, void * buf UNUSED)
|
2012-12-14 04:36:33 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
static void
|
2012-12-22 20:35:19 +00:00
|
|
|
freeStringFunc (const tr_variant * v, void * unused UNUSED)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_variant_string_clear (&((tr_variant*)v)->val.s);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-22 20:35:19 +00:00
|
|
|
freeContainerEndFunc (const tr_variant * v, void * unused UNUSED)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_free (v->val.l.vals);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct VariantWalkFuncs freeWalkFuncs = { freeDummyFunc,
|
|
|
|
freeDummyFunc,
|
|
|
|
freeDummyFunc,
|
|
|
|
freeStringFunc,
|
|
|
|
freeDummyFunc,
|
|
|
|
freeDummyFunc,
|
|
|
|
freeContainerEndFunc };
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_variantFree (tr_variant * v)
|
|
|
|
{
|
|
|
|
if (tr_variantIsSomething (v))
|
|
|
|
tr_variantWalk (v, &freeWalkFuncs, NULL, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
|
|
|
tr_variantListCopy (tr_variant * target, const tr_variant * src)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
const tr_variant * val;
|
|
|
|
|
|
|
|
while ((val = tr_variantListChild ((tr_variant*)src, i++)))
|
|
|
|
{
|
|
|
|
if (tr_variantIsBool (val))
|
|
|
|
{
|
|
|
|
bool boolVal = 0;
|
|
|
|
tr_variantGetBool (val, &boolVal);
|
|
|
|
tr_variantListAddBool (target, boolVal);
|
|
|
|
}
|
|
|
|
else if (tr_variantIsReal (val))
|
|
|
|
{
|
|
|
|
double realVal = 0;
|
|
|
|
tr_variantGetReal (val, &realVal);
|
|
|
|
tr_variantListAddReal (target, realVal);
|
|
|
|
}
|
|
|
|
else if (tr_variantIsInt (val))
|
|
|
|
{
|
|
|
|
int64_t intVal = 0;
|
|
|
|
tr_variantGetInt (val, &intVal);
|
|
|
|
tr_variantListAddInt (target, intVal);
|
|
|
|
}
|
|
|
|
else if (tr_variantIsString (val))
|
|
|
|
{
|
2012-12-14 16:04:44 +00:00
|
|
|
size_t len;
|
|
|
|
const char * str;
|
|
|
|
tr_variantGetStr (val, &str, &len);
|
|
|
|
tr_variantListAddRaw (target, str, len);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
else if (tr_variantIsDict (val))
|
|
|
|
{
|
|
|
|
tr_variantMergeDicts (tr_variantListAddDict (target, 0), val);
|
|
|
|
}
|
|
|
|
else if (tr_variantIsList (val))
|
|
|
|
{
|
|
|
|
tr_variantListCopy (tr_variantListAddList (target, 0), val);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddError ("tr_variantListCopy skipping item");
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
tr_variantDictSize (const tr_variant * dict)
|
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
return tr_variantIsDict (dict) ? dict->val.l.count : 0;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_variantDictChild (tr_variant * dict,
|
|
|
|
size_t n,
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_quark * key,
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_variant ** val)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
|
|
|
bool success = 0;
|
|
|
|
|
|
|
|
assert (tr_variantIsDict (dict));
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
if (tr_variantIsDict (dict) && (n<dict->val.l.count))
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
*key = dict->val.l.vals[n].key;
|
|
|
|
*val = dict->val.l.vals+n;
|
|
|
|
success = true;
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_variantMergeDicts (tr_variant * target, const tr_variant * source)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
const size_t sourceCount = tr_variantDictSize (source);
|
|
|
|
|
|
|
|
assert (tr_variantIsDict (target));
|
|
|
|
assert (tr_variantIsDict (source));
|
|
|
|
|
|
|
|
tr_variantDictReserve (target, sourceCount + tr_variantDictSize(target));
|
|
|
|
|
|
|
|
for (i=0; i<sourceCount; ++i)
|
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_quark key;
|
2012-12-14 04:36:33 +00:00
|
|
|
tr_variant * val;
|
|
|
|
tr_variant * t;
|
|
|
|
|
|
|
|
if (tr_variantDictChild ((tr_variant*)source, i, &key, &val))
|
|
|
|
{
|
|
|
|
if (tr_variantIsBool (val))
|
|
|
|
{
|
|
|
|
bool boolVal;
|
|
|
|
tr_variantGetBool (val, &boolVal);
|
|
|
|
tr_variantDictAddBool (target, key, boolVal);
|
|
|
|
}
|
|
|
|
else if (tr_variantIsReal (val))
|
|
|
|
{
|
|
|
|
double realVal = 0;
|
|
|
|
tr_variantGetReal (val, &realVal);
|
|
|
|
tr_variantDictAddReal (target, key, realVal);
|
|
|
|
}
|
|
|
|
else if (tr_variantIsInt (val))
|
|
|
|
{
|
|
|
|
int64_t intVal = 0;
|
|
|
|
tr_variantGetInt (val, &intVal);
|
|
|
|
tr_variantDictAddInt (target, key, intVal);
|
|
|
|
}
|
|
|
|
else if (tr_variantIsString (val))
|
|
|
|
{
|
2012-12-14 16:04:44 +00:00
|
|
|
size_t len;
|
|
|
|
const char * str;
|
|
|
|
tr_variantGetStr (val, &str, &len);
|
|
|
|
tr_variantDictAddRaw (target, key, str, len);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
else if (tr_variantIsDict (val) && tr_variantDictFindDict (target, key, &t))
|
|
|
|
{
|
|
|
|
tr_variantMergeDicts (t, val);
|
|
|
|
}
|
|
|
|
else if (tr_variantIsList (val))
|
|
|
|
{
|
|
|
|
if (tr_variantDictFind (target, key) == NULL)
|
|
|
|
tr_variantListCopy (tr_variantDictAddList (target, key, tr_variantListSize (val)), val);
|
|
|
|
}
|
|
|
|
else if (tr_variantIsDict (val))
|
|
|
|
{
|
|
|
|
tr_variant * target_dict = tr_variantDictFind (target, key);
|
|
|
|
|
|
|
|
if (target_dict == NULL)
|
|
|
|
target_dict = tr_variantDictAddDict (target, key, tr_variantDictSize (val));
|
|
|
|
|
|
|
|
if (tr_variantIsDict (target_dict))
|
|
|
|
tr_variantMergeDicts (target_dict, val);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddDebug ("tr_variantMergeDicts skipping \"%s\"", tr_quark_get_string(key,NULL));
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
struct evbuffer *
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantToBuf (const tr_variant * v, tr_variant_fmt fmt)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2013-01-24 16:33:49 +00:00
|
|
|
char lc_numeric[128];
|
2012-12-24 22:38:41 +00:00
|
|
|
struct evbuffer * buf = evbuffer_new();
|
2012-12-14 04:36:33 +00:00
|
|
|
|
2013-01-24 16:33:49 +00:00
|
|
|
/* parse with LC_NUMERIC="C" to ensure a "." decimal separator */
|
|
|
|
tr_strlcpy (lc_numeric, setlocale (LC_NUMERIC, NULL), sizeof (lc_numeric));
|
|
|
|
setlocale (LC_NUMERIC, "C");
|
|
|
|
|
2012-12-14 04:36:33 +00:00
|
|
|
evbuffer_expand (buf, 4096); /* alloc a little memory to start off with */
|
|
|
|
|
|
|
|
switch (fmt)
|
|
|
|
{
|
|
|
|
case TR_VARIANT_FMT_BENC:
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantToBufBenc (v, buf);
|
2012-12-14 04:36:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_VARIANT_FMT_JSON:
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantToBufJson (v, buf, false);
|
2012-12-14 04:36:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_VARIANT_FMT_JSON_LEAN:
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantToBufJson (v, buf, true);
|
2012-12-14 04:36:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-01-24 16:33:49 +00:00
|
|
|
/* restore the previous locale */
|
|
|
|
setlocale (LC_NUMERIC, lc_numeric);
|
2012-12-14 04:36:33 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantToStr (const tr_variant * v, tr_variant_fmt fmt, int * len)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
struct evbuffer * buf = tr_variantToBuf (v, fmt);
|
2012-12-14 04:36:33 +00:00
|
|
|
const size_t n = evbuffer_get_length (buf);
|
|
|
|
char * ret = evbuffer_free_to_str (buf);
|
|
|
|
if (len != NULL)
|
|
|
|
*len = (int) n;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
/* portability wrapper for mkstemp(). */
|
2012-12-14 04:36:33 +00:00
|
|
|
static int
|
|
|
|
tr_mkstemp (char * template)
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
const int flags = O_RDWR | O_BINARY | O_CREAT | O_EXCL | _O_SHORT_LIVED;
|
|
|
|
const mode_t mode = _S_IREAD | _S_IWRITE;
|
|
|
|
mktemp (template);
|
|
|
|
return open (template, flags, mode);
|
|
|
|
#else
|
|
|
|
return mkstemp (template);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-24 22:38:41 +00:00
|
|
|
tr_variantToFile (const tr_variant * v,
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_variant_fmt fmt,
|
|
|
|
const char * filename)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
|
|
|
char * tmp;
|
|
|
|
int fd;
|
|
|
|
int err = 0;
|
|
|
|
char buf[TR_PATH_MAX];
|
|
|
|
|
|
|
|
/* follow symlinks to find the "real" file, to make sure the temporary
|
2012-12-24 22:38:41 +00:00
|
|
|
* we build with tr_mkstemp() is created on the right partition */
|
2012-12-14 04:36:33 +00:00
|
|
|
if (tr_realpath (filename, buf) != NULL)
|
|
|
|
filename = buf;
|
|
|
|
|
|
|
|
/* if the file already exists, try to move it out of the way & keep it as a backup */
|
|
|
|
tmp = tr_strdup_printf ("%s.tmp.XXXXXX", filename);
|
|
|
|
fd = tr_mkstemp (tmp);
|
|
|
|
tr_set_file_for_single_pass (fd);
|
|
|
|
if (fd >= 0)
|
|
|
|
{
|
|
|
|
int nleft;
|
|
|
|
|
|
|
|
/* save the variant to a temporary file */
|
|
|
|
{
|
2012-12-24 22:38:41 +00:00
|
|
|
struct evbuffer * buf = tr_variantToBuf (v, fmt);
|
2012-12-14 04:36:33 +00:00
|
|
|
const char * walk = (const char *) evbuffer_pullup (buf, -1);
|
|
|
|
nleft = evbuffer_get_length (buf);
|
|
|
|
|
|
|
|
while (nleft > 0)
|
|
|
|
{
|
|
|
|
const int n = write (fd, walk, nleft);
|
|
|
|
if (n >= 0)
|
|
|
|
{
|
|
|
|
nleft -= n;
|
|
|
|
walk += n;
|
|
|
|
}
|
|
|
|
else if (errno != EAGAIN)
|
|
|
|
{
|
|
|
|
err = errno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
evbuffer_free (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nleft > 0)
|
|
|
|
{
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddError (_("Couldn't save temporary file \"%1$s\": %2$s"), tmp, tr_strerror (err));
|
2012-12-14 04:36:33 +00:00
|
|
|
tr_close_file (fd);
|
|
|
|
unlink (tmp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_close_file (fd);
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
if (MoveFileEx (tmp, filename, MOVEFILE_REPLACE_EXISTING))
|
|
|
|
#else
|
|
|
|
if (!rename (tmp, filename))
|
|
|
|
#endif
|
|
|
|
{
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddInfo (_("Saved \"%s\""), filename);
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = errno;
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddError (_("Couldn't save file \"%1$s\": %2$s"), filename, tr_strerror (err));
|
2012-12-14 04:36:33 +00:00
|
|
|
unlink (tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = errno;
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddError (_("Couldn't save temporary file \"%1$s\": %2$s"), tmp, tr_strerror (err));
|
2012-12-14 04:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_free (tmp);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
int
|
2012-12-14 16:04:44 +00:00
|
|
|
tr_variantFromFile (tr_variant * setme,
|
|
|
|
tr_variant_fmt fmt,
|
|
|
|
const char * filename)
|
2012-12-14 04:36:33 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
size_t buflen;
|
|
|
|
uint8_t * buf;
|
|
|
|
const int old_errno = errno;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
buf = tr_loadFile (filename, &buflen);
|
|
|
|
|
|
|
|
if (errno)
|
|
|
|
err = errno;
|
|
|
|
else
|
|
|
|
err = tr_variantFromBuf (setme, fmt, buf, buflen, filename, NULL);
|
|
|
|
|
|
|
|
tr_free (buf);
|
|
|
|
errno = old_errno;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_variantFromBuf (tr_variant * setme,
|
|
|
|
tr_variant_fmt fmt,
|
|
|
|
const void * buf,
|
|
|
|
size_t buflen,
|
|
|
|
const char * optional_source,
|
|
|
|
const char ** setme_end)
|
|
|
|
{
|
|
|
|
int err;
|
2013-01-24 16:33:49 +00:00
|
|
|
char lc_numeric[128];
|
|
|
|
|
|
|
|
/* parse with LC_NUMERIC="C" to ensure a "." decimal separator */
|
|
|
|
tr_strlcpy (lc_numeric, setlocale (LC_NUMERIC, NULL), sizeof (lc_numeric));
|
|
|
|
setlocale (LC_NUMERIC, "C");
|
2012-12-14 04:36:33 +00:00
|
|
|
|
|
|
|
switch (fmt)
|
|
|
|
{
|
|
|
|
case TR_VARIANT_FMT_JSON:
|
|
|
|
case TR_VARIANT_FMT_JSON_LEAN:
|
|
|
|
err = tr_jsonParse (optional_source, buf, buflen, setme, setme_end);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_VARIANT_FMT_BENC:
|
|
|
|
err = tr_variantParseBenc (buf, ((const char*)buf)+buflen, setme, setme_end);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-01-24 16:33:49 +00:00
|
|
|
/* restore the previous locale */
|
|
|
|
setlocale (LC_NUMERIC, lc_numeric);
|
2012-12-14 04:36:33 +00:00
|
|
|
return err;
|
|
|
|
}
|