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