1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/* hb_dict.h
Copyright (c) 2003-2018 HandBrake Team
This file is part of the HandBrake source code
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License v2.
For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
*/
#if !defined(HB_DICT_H)
#define HB_DICT_H
#include "hbtypes.h"
#include <jansson.h>
#define HB_VALUE_TYPE_DICT JSON_OBJECT
#define HB_VALUE_TYPE_ARRAY JSON_ARRAY
#define HB_VALUE_TYPE_STRING JSON_STRING
#define HB_VALUE_TYPE_INT JSON_INTEGER
#define HB_VALUE_TYPE_DOUBLE JSON_REAL
#define HB_VALUE_TYPE_NULL JSON_NULL
#define HB_VALUE_TYPE_BOOL 0xff
#define HB_DICT_ITER_DONE NULL
typedef int hb_value_type_t;
typedef json_t hb_value_t;
typedef hb_value_t hb_dict_t;
typedef hb_value_t hb_value_array_t;
typedef void* hb_dict_iter_t;
/* A dictionary implementation.
*
* an hb_dict_t must be initialized with hb_dict_init() before use.
*
* "key" must be a string with non-zero length (NULL and "" are invalid keys).
* "value" must be an hb_value_t*
*/
hb_dict_t * hb_dict_init(void);
/* free dictionary and release references to all values it contains */
void hb_dict_free(hb_dict_t ** dict_ptr);
/* return number of member elements in the dictionary */
int hb_dict_elements(hb_dict_t * dict);
/* add value to dictionary. dictionary takes ownership of value */
void hb_dict_set(hb_dict_t * dict, const char * key,
hb_value_t * value);
void hb_dict_case_set(hb_dict_t * dict, const char *key,
hb_value_t *value);
/* remove value from dictionary. releases reference to value */
int hb_dict_remove(hb_dict_t * dict, const char * key);
/* get value from dictionary. value has borrowed reference */
hb_value_t * hb_dict_get(const hb_dict_t * dict, const char * key);
int hb_dict_extract_int(int *dst,
const hb_dict_t * dict,
const char * key);
int hb_dict_extract_double(double *dst,
const hb_dict_t * dict,
const char * key);
int hb_dict_extract_bool(int *dst,
const hb_dict_t * dict,
const char * key);
int hb_dict_extract_string(char **dst,
const hb_dict_t * dict,
const char * key);
int hb_dict_extract_rational(hb_rational_t *dst,
const hb_dict_t * dict,
const char * key);
int hb_dict_extract_int_array(int *dst, int count,
const hb_dict_t * dict,
const char * key);
/* dict iterator
* hb_dict_iter_init(dict) returns an iter to the first key/value in the dict
* hb_dict_iter_next(dict, iter) returns an iter to the next key/value
* HB_DICT_ITER_DONE if the end of the dictionary was reached.
*/
hb_dict_iter_t hb_dict_iter_init(const hb_dict_t *dict);
hb_dict_iter_t hb_dict_iter_next(const hb_dict_t *dict, hb_dict_iter_t iter);
int hb_dict_iter_next_ex(const hb_dict_t *dict,
hb_dict_iter_t *iter,
const char **key, hb_value_t **val);
/* get key from iter */
const char * hb_dict_iter_key(const hb_dict_iter_t iter);
/* get value from iter. value has borrowed reference */
hb_value_t * hb_dict_iter_value(const hb_dict_iter_t iter);
/* hb_value_array_t */
hb_value_array_t * hb_value_array_init(void);
/* remove all elements of array */
void hb_value_array_clear(hb_value_array_t *array);
/* get value from array. value has borrowed reference */
hb_value_t * hb_value_array_get(const hb_value_array_t *array, int index);
/* replace value at index in array. array takes ownership of new value */
void hb_value_array_set(hb_value_array_t *array, int index,
hb_value_t *value);
/* insert value at index in array. values move up.
* array takes ownership of new value */
void hb_value_array_insert(hb_value_array_t *array, int index,
hb_value_t *value);
/* append value to array. array takes ownership of new value */
void hb_value_array_append(hb_value_array_t *array,
hb_value_t *value);
/* remove value from array. releases reference to value */
void hb_value_array_remove(hb_value_array_t *array, int index);
/* clears dst and performs a deep copy */
void hb_value_array_copy(hb_value_array_t *dst,
const hb_value_array_t *src, int count);
/* appends copy of value to array. if value is an array, appends a copy of
* each element to array */
void hb_value_array_concat(hb_value_array_t *array,
hb_value_t *value);
size_t hb_value_array_len(const hb_value_array_t *array);
/* hb_value_t */
int hb_value_type(const hb_value_t *value);
int hb_value_is_number(const hb_value_t *value);
hb_value_t * hb_value_dup(const hb_value_t *value);
hb_value_t * hb_value_incref(hb_value_t *value);
void hb_value_decref(hb_value_t *value);
void hb_value_free(hb_value_t **value);
/* Create new hb_value_t */
hb_value_t * hb_value_null(void);
hb_value_t * hb_value_string(const char *value);
hb_value_t * hb_value_int(json_int_t value);
hb_value_t * hb_value_double(double value);
hb_value_t * hb_value_bool(int value);
hb_value_t * hb_value_json(const char *json);
hb_value_t * hb_value_read_json(const char *path);
/* Transform hb_value_t from one type to another */
hb_value_t * hb_value_xform(const hb_value_t *value, int type);
/* Extract values */
/* hb_value_t must be of type HB_VALUE_TYPE_STRING */
const char * hb_value_get_string(const hb_value_t *value);
/* hb_value_t may be of any type, automatic conversion performed */
json_int_t hb_value_get_int(const hb_value_t *value);
double hb_value_get_double(const hb_value_t *value);
int hb_value_get_bool(const hb_value_t *value);
/* converts value type and returns an allocated string representation.
* caller must free the returned string */
char * hb_value_get_string_xform(const hb_value_t *value);
/* converts value to json string */
char * hb_value_get_json(const hb_value_t *value);
/* write json representation to a file */
int hb_value_write_file_json(hb_value_t *value, FILE *file);
int hb_value_write_json(hb_value_t *value, const char *path);
/* specialized dict functions */
/*
* hb_encopts_to_dict() converts an op1=val1:opt2=val2:opt3=val3 type string to
* an hb_dict_t dictionary. */
hb_dict_t * hb_encopts_to_dict(const char * encopts, int encoder);
char * hb_dict_to_encopts(const hb_dict_t * dict);
/* convenience macros */
#define hb_dict_get_string(dict, key) hb_value_get_string(hb_dict_get(dict, key))
#define hb_dict_get_int(dict, key) hb_value_get_int(hb_dict_get(dict, key))
#define hb_dict_get_double(dict, key) hb_value_get_double(hb_dict_get(dict, key))
#define hb_dict_get_bool(dict, key) hb_value_get_bool(hb_dict_get(dict, key))
#define hb_dict_set_string(dict, key, val) hb_dict_set(dict, key, hb_value_string(val))
#define hb_dict_set_int(dict, key, val) hb_dict_set(dict, key, hb_value_int(val))
#define hb_dict_set_double(dict, key, val) hb_dict_set(dict, key, hb_value_double(val))
#define hb_dict_set_bool(dict, key, val) hb_dict_set(dict, key, hb_value_bool(val))
#endif // !defined(HB_DICT_H)
|