2014-01-27 22:14:58 -08:00
|
|
|
/******************************************************************************
|
|
|
|
Copyright (C) 2014 by Hugh Bailey <obs.jim@gmail.com>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "util/bmem.h"
|
|
|
|
#include "util/darray.h"
|
|
|
|
#include "obs-data.h"
|
|
|
|
|
|
|
|
struct obs_data_item {
|
|
|
|
volatile int ref;
|
|
|
|
struct obs_data *parent;
|
|
|
|
struct obs_data_item *next;
|
|
|
|
enum obs_data_type type;
|
|
|
|
size_t name_len;
|
|
|
|
size_t data_len;
|
|
|
|
size_t capacity;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct obs_data {
|
|
|
|
volatile int ref;
|
|
|
|
char *json;
|
|
|
|
struct obs_data_item *first_item;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct obs_data_array {
|
|
|
|
volatile int ref;
|
|
|
|
DARRAY(obs_data_t) objects;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
/* Item structure, designed to be one allocation only */
|
|
|
|
|
2014-02-13 04:27:35 -08:00
|
|
|
/* ensures data after the name has alignment (in case of SSE) */
|
2014-01-27 22:14:58 -08:00
|
|
|
static inline size_t get_name_align_size(const char *name)
|
|
|
|
{
|
|
|
|
size_t name_size = strlen(name) + 1;
|
2014-02-13 04:27:35 -08:00
|
|
|
size_t alignment = base_get_alignment();
|
|
|
|
size_t total_size;
|
|
|
|
|
|
|
|
total_size = sizeof(struct obs_data_item) + (name_size + alignment-1);
|
|
|
|
total_size &= ~(alignment-1);
|
|
|
|
|
|
|
|
return total_size - sizeof(struct obs_data_item);
|
2014-01-27 22:14:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline char *get_item_name(struct obs_data_item *item)
|
|
|
|
{
|
|
|
|
return (char*)item + sizeof(struct obs_data_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *get_item_data(struct obs_data_item *item)
|
|
|
|
{
|
|
|
|
return (uint8_t*)get_item_name(item) + item->name_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t obs_data_item_total_size(struct obs_data_item *item)
|
|
|
|
{
|
|
|
|
return sizeof(struct obs_data_item) + item->data_len + item->name_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline obs_data_t get_item_obj(struct obs_data_item *item)
|
|
|
|
{
|
2014-01-28 14:45:30 -08:00
|
|
|
if (!item)
|
|
|
|
return NULL;
|
|
|
|
|
2014-01-27 22:14:58 -08:00
|
|
|
return *(obs_data_t*)get_item_data(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline obs_data_array_t get_item_array(struct obs_data_item *item)
|
|
|
|
{
|
2014-01-28 14:45:30 -08:00
|
|
|
if (!item)
|
|
|
|
return NULL;
|
|
|
|
|
2014-01-27 22:14:58 -08:00
|
|
|
return *(obs_data_array_t*)get_item_data(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void item_data_release(struct obs_data_item *item)
|
|
|
|
{
|
|
|
|
if (item->type == OBS_DATA_OBJECT) {
|
|
|
|
obs_data_t obj = get_item_obj(item);
|
|
|
|
obs_data_release(obj);
|
|
|
|
|
|
|
|
} else if (item->type == OBS_DATA_ARRAY) {
|
|
|
|
obs_data_array_t array = get_item_array(item);
|
|
|
|
obs_data_array_release(array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void item_data_addref(struct obs_data_item *item)
|
|
|
|
{
|
|
|
|
if (item->type == OBS_DATA_OBJECT) {
|
|
|
|
obs_data_t obj = get_item_obj(item);
|
|
|
|
obs_data_addref(obj);
|
|
|
|
|
|
|
|
} else if (item->type == OBS_DATA_ARRAY) {
|
|
|
|
obs_data_array_t array = get_item_array(item);
|
|
|
|
obs_data_array_addref(array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct obs_data_item *obs_data_item_create(const char *name,
|
|
|
|
const void *data, size_t size, enum obs_data_type type)
|
|
|
|
{
|
|
|
|
struct obs_data_item *item;
|
|
|
|
size_t name_size, total_size;
|
|
|
|
|
|
|
|
if (!name || !data)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
name_size = get_name_align_size(name);
|
|
|
|
total_size = name_size + sizeof(struct obs_data_item) + size;
|
|
|
|
|
2014-02-09 11:34:07 -08:00
|
|
|
item = bzalloc(total_size);
|
2014-01-27 22:14:58 -08:00
|
|
|
|
|
|
|
item->capacity = total_size;
|
|
|
|
item->type = type;
|
|
|
|
item->name_len = name_size;
|
|
|
|
item->data_len = size;
|
|
|
|
item->ref = 1;
|
|
|
|
|
|
|
|
strcpy(get_item_name(item), name);
|
|
|
|
memcpy(get_item_data(item), data, size);
|
|
|
|
|
|
|
|
item_data_addref(item);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct obs_data_item **get_item_prev_next(struct obs_data *data,
|
|
|
|
struct obs_data_item *current)
|
|
|
|
{
|
|
|
|
if (!current || !data)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
struct obs_data_item **prev_next = &data->first_item;
|
|
|
|
struct obs_data_item *item = data->first_item;
|
|
|
|
|
|
|
|
while (item) {
|
|
|
|
if (item == current)
|
|
|
|
return prev_next;
|
|
|
|
|
|
|
|
prev_next = &item->next;
|
|
|
|
item = item->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void obs_data_item_detach(struct obs_data_item *item)
|
|
|
|
{
|
|
|
|
struct obs_data_item **prev_next = get_item_prev_next(item->parent,
|
|
|
|
item);
|
|
|
|
|
|
|
|
if (prev_next) {
|
|
|
|
*prev_next = item->next;
|
|
|
|
item->next = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void obs_data_item_reattach(struct obs_data_item *old_ptr,
|
|
|
|
struct obs_data_item *new_ptr)
|
|
|
|
{
|
|
|
|
struct obs_data_item **prev_next = get_item_prev_next(new_ptr->parent,
|
|
|
|
old_ptr);
|
|
|
|
|
|
|
|
if (prev_next)
|
|
|
|
*prev_next = new_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct obs_data_item *obs_data_item_ensure_capacity(
|
|
|
|
struct obs_data_item *item)
|
|
|
|
{
|
|
|
|
size_t new_size = obs_data_item_total_size(item);
|
|
|
|
struct obs_data_item *new_item;
|
|
|
|
|
|
|
|
if (item->capacity >= new_size)
|
|
|
|
return item;
|
|
|
|
|
|
|
|
new_item = brealloc(item, new_size);
|
|
|
|
new_item->capacity = new_size;
|
|
|
|
|
|
|
|
obs_data_item_reattach(item, new_item);
|
|
|
|
return new_item;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void obs_data_item_destroy(struct obs_data_item *item)
|
|
|
|
{
|
|
|
|
item_data_release(item);
|
|
|
|
obs_data_item_detach(item);
|
|
|
|
bfree(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void obs_data_item_setdata(
|
|
|
|
struct obs_data_item **p_item, const void *data, size_t size,
|
|
|
|
enum obs_data_type type)
|
|
|
|
{
|
|
|
|
if (!p_item || !*p_item)
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct obs_data_item *item = *p_item;
|
|
|
|
item_data_release(item);
|
|
|
|
|
|
|
|
item->data_len = size;
|
|
|
|
item->type = type;
|
|
|
|
item = obs_data_item_ensure_capacity(item);
|
|
|
|
|
|
|
|
if (size) {
|
|
|
|
memcpy(get_item_data(item), data, size);
|
|
|
|
item_data_addref(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
*p_item = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
obs_data_t obs_data_create()
|
|
|
|
{
|
2014-02-09 11:34:07 -08:00
|
|
|
struct obs_data *data = bzalloc(sizeof(struct obs_data));
|
2014-01-27 22:14:58 -08:00
|
|
|
data->ref = 1;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
obs_data_t obs_data_create_from_json(const char *json_string)
|
|
|
|
{
|
|
|
|
/* TODO */
|
2014-02-14 14:13:36 -08:00
|
|
|
#pragma message ("TODO: implement obs_data_create_from_json")
|
|
|
|
UNUSED_PARAMETER(json_string);
|
2014-01-27 22:14:58 -08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-02-02 20:34:17 -08:00
|
|
|
void obs_data_addref(obs_data_t data)
|
2014-01-27 22:14:58 -08:00
|
|
|
{
|
2014-02-02 20:34:17 -08:00
|
|
|
if (data)
|
|
|
|
++data->ref;
|
2014-01-27 22:14:58 -08:00
|
|
|
}
|
|
|
|
|
2014-02-02 20:33:02 -08:00
|
|
|
static inline void obs_data_destroy(struct obs_data *data)
|
2014-01-27 22:14:58 -08:00
|
|
|
{
|
|
|
|
struct obs_data_item *item = data->first_item;
|
|
|
|
|
|
|
|
while (item) {
|
|
|
|
struct obs_data_item *next = item->next;
|
|
|
|
obs_data_item_release(&item);
|
|
|
|
item = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
bfree(data->json);
|
|
|
|
bfree(data);
|
|
|
|
}
|
|
|
|
|
2014-02-02 20:34:17 -08:00
|
|
|
void obs_data_release(obs_data_t data)
|
2014-01-27 22:14:58 -08:00
|
|
|
{
|
|
|
|
if (!data)
|
2014-02-02 20:34:17 -08:00
|
|
|
return;
|
2014-01-27 22:14:58 -08:00
|
|
|
|
2014-02-02 20:34:17 -08:00
|
|
|
if (--data->ref == 0)
|
2014-01-27 22:14:58 -08:00
|
|
|
obs_data_destroy(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *obs_data_getjson(obs_data_t data)
|
|
|
|
{
|
|
|
|
/* TODO */
|
|
|
|
return data->json;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct obs_data_item *get_item(struct obs_data *data, const char *name)
|
|
|
|
{
|
2014-01-28 14:45:30 -08:00
|
|
|
if (!data)
|
|
|
|
return NULL;
|
|
|
|
|
2014-01-27 22:14:58 -08:00
|
|
|
struct obs_data_item *item = data->first_item;
|
|
|
|
|
|
|
|
while (item) {
|
|
|
|
if (strcmp(get_item_name(item), name) == 0)
|
|
|
|
return item;
|
|
|
|
|
|
|
|
item = item->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct obs_data_item *get_item_of(struct obs_data *data,
|
|
|
|
const char *name, enum obs_data_type type)
|
|
|
|
{
|
|
|
|
struct obs_data_item *item = get_item(data, name);
|
2014-01-28 14:45:30 -08:00
|
|
|
return (item && item->type == type) ? item : NULL;
|
2014-01-27 22:14:58 -08:00
|
|
|
}
|
|
|
|
|
2014-01-28 17:41:24 -08:00
|
|
|
static void set_item_data(struct obs_data *data, struct obs_data_item *item,
|
|
|
|
const char *name, const void *ptr, size_t size,
|
|
|
|
enum obs_data_type type)
|
2014-01-27 22:14:58 -08:00
|
|
|
{
|
|
|
|
if (!item) {
|
|
|
|
item = obs_data_item_create(name, ptr, size, type);
|
|
|
|
item->next = data->first_item;
|
|
|
|
item->parent = data;
|
|
|
|
|
|
|
|
data->first_item = item;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
obs_data_item_setdata(&item, ptr, size, type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-28 17:41:24 -08:00
|
|
|
static inline void set_item(struct obs_data *data, const char *name,
|
|
|
|
const void *ptr, size_t size, enum obs_data_type type)
|
|
|
|
{
|
|
|
|
if (!data)
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct obs_data_item *item = get_item(data, name);
|
|
|
|
set_item_data(data, item, name, ptr, size, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void set_item_def(struct obs_data *data, const char *name,
|
|
|
|
const void *ptr, size_t size, enum obs_data_type type)
|
|
|
|
{
|
|
|
|
if (!data)
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct obs_data_item *item = get_item(data, name);
|
|
|
|
if (item && item->type == type)
|
|
|
|
return;
|
|
|
|
|
|
|
|
set_item_data(data, item, name, ptr, size, type);
|
|
|
|
}
|
|
|
|
|
2014-01-27 22:14:58 -08:00
|
|
|
void obs_data_erase(obs_data_t data, const char *name)
|
|
|
|
{
|
|
|
|
struct obs_data_item *item = get_item(data, name);
|
|
|
|
|
2014-01-28 00:51:25 -08:00
|
|
|
if (item) {
|
|
|
|
obs_data_item_detach(item);
|
|
|
|
obs_data_item_release(&item);
|
|
|
|
}
|
2014-01-27 22:14:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void obs_data_setstring(obs_data_t data, const char *name, const char *val)
|
|
|
|
{
|
|
|
|
if (!val) val = "";
|
|
|
|
set_item(data, name, val, strlen(val)+1, OBS_DATA_STRING);
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_data_setint(obs_data_t data, const char *name, long long val)
|
|
|
|
{
|
|
|
|
double f_val = (double)val;
|
|
|
|
set_item(data, name, &f_val, sizeof(double), OBS_DATA_NUMBER);
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_data_setdouble(obs_data_t data, const char *name, double val)
|
|
|
|
{
|
|
|
|
set_item(data, name, &val, sizeof(double), OBS_DATA_NUMBER);
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_data_setbool(obs_data_t data, const char *name, bool val)
|
|
|
|
{
|
|
|
|
set_item(data, name, &val, sizeof(bool), OBS_DATA_BOOLEAN);
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_data_setobj(obs_data_t data, const char *name, obs_data_t obj)
|
|
|
|
{
|
|
|
|
set_item(data, name, &obj, sizeof(obs_data_t), OBS_DATA_OBJECT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_data_setarray(obs_data_t data, const char *name,
|
|
|
|
obs_data_array_t array)
|
|
|
|
{
|
|
|
|
set_item(data, name, &array, sizeof(obs_data_t), OBS_DATA_ARRAY);
|
|
|
|
}
|
|
|
|
|
2014-01-28 17:41:24 -08:00
|
|
|
void obs_data_set_default_string(obs_data_t data, const char *name,
|
|
|
|
const char *val)
|
|
|
|
{
|
|
|
|
if (!val) val = "";
|
|
|
|
set_item_def(data, name, val, strlen(val)+1, OBS_DATA_STRING);
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_data_set_default_int(obs_data_t data, const char *name, long long val)
|
|
|
|
{
|
|
|
|
double f_val = (double)val;
|
|
|
|
set_item_def(data, name, &f_val, sizeof(double), OBS_DATA_NUMBER);
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_data_set_default_double(obs_data_t data, const char *name, double val)
|
|
|
|
{
|
|
|
|
set_item_def(data, name, &val, sizeof(double), OBS_DATA_NUMBER);
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_data_set_default_bool(obs_data_t data, const char *name, bool val)
|
|
|
|
{
|
|
|
|
set_item_def(data, name, &val, sizeof(bool), OBS_DATA_BOOLEAN);
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_data_set_default_obj(obs_data_t data, const char *name, obs_data_t obj)
|
|
|
|
{
|
|
|
|
set_item_def(data, name, &obj, sizeof(obs_data_t), OBS_DATA_OBJECT);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *obs_data_getstring(obs_data_t data, const char *name)
|
2014-01-27 22:14:58 -08:00
|
|
|
{
|
|
|
|
struct obs_data_item *item = get_item_of(data, name, OBS_DATA_STRING);
|
2014-01-28 17:41:24 -08:00
|
|
|
return item ? get_item_data(item) : "";
|
2014-01-27 22:14:58 -08:00
|
|
|
}
|
|
|
|
|
2014-01-28 17:41:24 -08:00
|
|
|
long long obs_data_getint(obs_data_t data, const char *name)
|
2014-01-27 22:14:58 -08:00
|
|
|
{
|
|
|
|
struct obs_data_item *item = get_item_of(data, name, OBS_DATA_NUMBER);
|
2014-01-28 17:41:24 -08:00
|
|
|
return item ? (long long)*(double*)get_item_data(item) : 0;
|
2014-01-27 22:14:58 -08:00
|
|
|
}
|
|
|
|
|
2014-01-28 17:41:24 -08:00
|
|
|
double obs_data_getdouble(obs_data_t data, const char *name)
|
2014-01-27 22:14:58 -08:00
|
|
|
{
|
|
|
|
struct obs_data_item *item = get_item_of(data, name, OBS_DATA_NUMBER);
|
2014-01-28 17:41:24 -08:00
|
|
|
return item ? *(double*)get_item_data(item) : 0.0;
|
2014-01-27 22:14:58 -08:00
|
|
|
}
|
|
|
|
|
2014-01-28 17:41:24 -08:00
|
|
|
bool obs_data_getbool(obs_data_t data, const char *name)
|
2014-01-27 22:14:58 -08:00
|
|
|
{
|
|
|
|
struct obs_data_item *item = get_item_of(data, name, OBS_DATA_BOOLEAN);
|
2014-01-28 17:41:24 -08:00
|
|
|
return item ? *(bool*)get_item_data(item) : false;
|
2014-01-27 22:14:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
obs_data_t obs_data_getobj(obs_data_t data, const char *name)
|
|
|
|
{
|
|
|
|
struct obs_data_item *item = get_item_of(data, name, OBS_DATA_OBJECT);
|
|
|
|
obs_data_t obj = get_item_obj(item);
|
|
|
|
|
|
|
|
if (obj)
|
|
|
|
obj->ref++;
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
obs_data_array_t obs_data_getarray(obs_data_t data, const char *name)
|
|
|
|
{
|
|
|
|
struct obs_data_item *item = get_item_of(data, name, OBS_DATA_ARRAY);
|
|
|
|
obs_data_array_t array = get_item_array(item);
|
|
|
|
|
|
|
|
if (array)
|
|
|
|
array->ref++;
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
obs_data_array_t obs_data_array_create()
|
|
|
|
{
|
2014-02-09 11:34:07 -08:00
|
|
|
struct obs_data_array *array = bzalloc(sizeof(struct obs_data_array));
|
2014-01-27 22:14:58 -08:00
|
|
|
array->ref = 1;
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2014-02-02 20:34:17 -08:00
|
|
|
void obs_data_array_addref(obs_data_array_t array)
|
2014-01-27 22:14:58 -08:00
|
|
|
{
|
2014-02-02 20:34:17 -08:00
|
|
|
if (array)
|
|
|
|
++array->ref;
|
2014-01-27 22:14:58 -08:00
|
|
|
}
|
|
|
|
|
2014-01-28 00:51:25 -08:00
|
|
|
static inline void obs_data_array_destroy(obs_data_array_t array)
|
2014-01-27 22:14:58 -08:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < array->objects.num; i++)
|
|
|
|
obs_data_release(array->objects.array[i]);
|
|
|
|
da_free(array->objects);
|
|
|
|
bfree(array);
|
|
|
|
}
|
|
|
|
|
2014-02-02 20:34:17 -08:00
|
|
|
void obs_data_array_release(obs_data_array_t array)
|
2014-01-27 22:14:58 -08:00
|
|
|
{
|
2014-02-02 20:34:17 -08:00
|
|
|
if (!array)
|
|
|
|
return;
|
2014-01-27 22:14:58 -08:00
|
|
|
|
2014-02-02 20:34:17 -08:00
|
|
|
if (--array->ref == 0)
|
|
|
|
obs_data_array_destroy(array);
|
2014-01-27 22:14:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t obs_data_array_count(obs_data_array_t array)
|
|
|
|
{
|
|
|
|
return array->objects.num;
|
|
|
|
}
|
|
|
|
|
|
|
|
obs_data_t obs_data_array_item(obs_data_array_t array, size_t idx)
|
|
|
|
{
|
|
|
|
obs_data_t data;
|
|
|
|
data = (idx < array->objects.num) ? array->objects.array[idx] : NULL;
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
data->ref++;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t obs_data_array_push_back(obs_data_array_t array, obs_data_t obj)
|
|
|
|
{
|
|
|
|
obj->ref++;
|
|
|
|
return da_push_back(array->objects, &obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_data_array_insert(obs_data_array_t array, size_t idx, obs_data_t obj)
|
|
|
|
{
|
|
|
|
obj->ref++;
|
|
|
|
da_insert(array->objects, idx, &obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_data_array_erase(obs_data_array_t array, size_t idx)
|
|
|
|
{
|
|
|
|
obs_data_release(array->objects.array[idx]);
|
|
|
|
da_erase(array->objects, idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
/* Item iteration */
|
|
|
|
|
|
|
|
obs_data_item_t obs_data_first(obs_data_t data)
|
|
|
|
{
|
|
|
|
if (data->first_item)
|
|
|
|
data->first_item->ref++;
|
|
|
|
return data->first_item;
|
|
|
|
}
|
|
|
|
|
|
|
|
obs_data_item_t obs_data_item_byname(obs_data_t data, const char *name)
|
|
|
|
{
|
|
|
|
struct obs_data_item *item = get_item(data, name);
|
|
|
|
if (item)
|
|
|
|
item->ref++;
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool obs_data_item_next(obs_data_item_t *item)
|
|
|
|
{
|
|
|
|
if (item && *item) {
|
|
|
|
(*item)->ref--;
|
|
|
|
*item = (*item)->next;
|
|
|
|
|
|
|
|
if (*item) {
|
|
|
|
(*item)->ref++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_data_item_release(obs_data_item_t *item)
|
|
|
|
{
|
|
|
|
if (item && *item) {
|
|
|
|
int ref = --(*item)->ref;
|
|
|
|
if (!ref) {
|
|
|
|
obs_data_item_destroy(*item);
|
|
|
|
*item = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_data_item_remove(obs_data_item_t *item)
|
|
|
|
{
|
|
|
|
if (item && *item) {
|
|
|
|
obs_data_item_detach(*item);
|
|
|
|
obs_data_item_release(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum obs_data_type obs_data_item_gettype(obs_data_item_t item)
|
|
|
|
{
|
|
|
|
return item->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_data_item_setstring(obs_data_item_t *item, const char *val)
|
|
|
|
{
|
2014-01-28 00:51:25 -08:00
|
|
|
if (!val) val = "";
|
2014-01-27 22:14:58 -08:00
|
|
|
obs_data_item_setdata(item, val, strlen(val)+1, OBS_DATA_STRING);
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_data_item_setint(obs_data_item_t *item, long long val)
|
|
|
|
{
|
|
|
|
double f_val = (double)val;
|
|
|
|
obs_data_item_setdata(item, &f_val, sizeof(double), OBS_DATA_NUMBER);
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_data_item_setdouble(obs_data_item_t *item, double val)
|
|
|
|
{
|
|
|
|
obs_data_item_setdata(item, &val, sizeof(double), OBS_DATA_NUMBER);
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_data_item_setbool(obs_data_item_t *item, bool val)
|
|
|
|
{
|
|
|
|
obs_data_item_setdata(item, &val, sizeof(bool), OBS_DATA_BOOLEAN);
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_data_item_setobj(obs_data_item_t *item, obs_data_t val)
|
|
|
|
{
|
|
|
|
obs_data_item_setdata(item, &val, sizeof(obs_data_t), OBS_DATA_OBJECT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_data_item_setarray(obs_data_item_t *item, obs_data_array_t val)
|
|
|
|
{
|
|
|
|
obs_data_item_setdata(item, &val, sizeof(obs_data_array_t),
|
|
|
|
OBS_DATA_ARRAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool item_valid(struct obs_data_item *item,
|
|
|
|
enum obs_data_type type)
|
|
|
|
{
|
|
|
|
return item && item->type == type;
|
|
|
|
}
|
|
|
|
|
2014-01-28 17:41:24 -08:00
|
|
|
const char *obs_data_item_getstring(obs_data_item_t item)
|
2014-01-27 22:14:58 -08:00
|
|
|
{
|
2014-01-28 17:41:24 -08:00
|
|
|
return item_valid(item, OBS_DATA_STRING) ? get_item_data(item) : "";
|
2014-01-27 22:14:58 -08:00
|
|
|
}
|
|
|
|
|
2014-01-28 17:41:24 -08:00
|
|
|
long long obs_data_item_getint(obs_data_item_t item)
|
2014-01-27 22:14:58 -08:00
|
|
|
{
|
|
|
|
return item_valid(item, OBS_DATA_NUMBER) ?
|
2014-01-28 17:41:24 -08:00
|
|
|
(long long)*(double*)get_item_data(item) : 0;
|
2014-01-27 22:14:58 -08:00
|
|
|
}
|
|
|
|
|
2014-01-28 17:41:24 -08:00
|
|
|
double obs_data_item_getdouble(obs_data_item_t item)
|
2014-01-27 22:14:58 -08:00
|
|
|
{
|
|
|
|
return item_valid(item, OBS_DATA_NUMBER) ?
|
2014-01-28 17:41:24 -08:00
|
|
|
*(double*)get_item_data(item) : 0.0;
|
2014-01-27 22:14:58 -08:00
|
|
|
}
|
|
|
|
|
2014-01-28 17:41:24 -08:00
|
|
|
bool obs_data_item_getbool(obs_data_item_t item)
|
2014-01-27 22:14:58 -08:00
|
|
|
{
|
|
|
|
return item_valid(item, OBS_DATA_BOOLEAN) ?
|
2014-01-28 17:41:24 -08:00
|
|
|
*(bool*)get_item_data(item) : false;
|
2014-01-27 22:14:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
obs_data_t obs_data_item_getobj(obs_data_item_t item)
|
|
|
|
{
|
|
|
|
obs_data_t obj = item_valid(item, OBS_DATA_OBJECT) ?
|
|
|
|
get_item_obj(item) : NULL;
|
|
|
|
|
|
|
|
if (obj)
|
|
|
|
obj->ref++;
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
obs_data_array_t obs_data_item_getarray(obs_data_item_t item)
|
|
|
|
{
|
|
|
|
obs_data_array_t array = item_valid(item, OBS_DATA_ARRAY) ?
|
|
|
|
get_item_array(item) : NULL;
|
|
|
|
|
|
|
|
if (array)
|
|
|
|
array->ref++;
|
|
|
|
return array;
|
|
|
|
}
|