/src/glib/glib/gvariant.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright © 2007, 2008 Ryan Lortie |
3 | | * Copyright © 2010 Codethink Limited |
4 | | * |
5 | | * This library is free software; you can redistribute it and/or |
6 | | * modify it under the terms of the GNU Lesser General Public |
7 | | * License as published by the Free Software Foundation; either |
8 | | * version 2.1 of the License, or (at your option) any later version. |
9 | | * |
10 | | * This library is distributed in the hope that it will be useful, |
11 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | | * Lesser General Public License for more details. |
14 | | * |
15 | | * You should have received a copy of the GNU Lesser General Public |
16 | | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
17 | | * |
18 | | * Author: Ryan Lortie <desrt@desrt.ca> |
19 | | */ |
20 | | |
21 | | /* Prologue {{{1 */ |
22 | | |
23 | | #include "config.h" |
24 | | |
25 | | #include <glib/gvariant-serialiser.h> |
26 | | #include "gvariant-internal.h" |
27 | | #include <glib/gvariant-core.h> |
28 | | #include <glib/gtestutils.h> |
29 | | #include <glib/gstrfuncs.h> |
30 | | #include <glib/gslice.h> |
31 | | #include <glib/ghash.h> |
32 | | #include <glib/gmem.h> |
33 | | |
34 | | #include <string.h> |
35 | | |
36 | | |
37 | | /** |
38 | | * SECTION:gvariant |
39 | | * @title: GVariant |
40 | | * @short_description: strongly typed value datatype |
41 | | * @see_also: GVariantType |
42 | | * |
43 | | * #GVariant is a variant datatype; it can contain one or more values |
44 | | * along with information about the type of the values. |
45 | | * |
46 | | * A #GVariant may contain simple types, like an integer, or a boolean value; |
47 | | * or complex types, like an array of two strings, or a dictionary of key |
48 | | * value pairs. A #GVariant is also immutable: once it's been created neither |
49 | | * its type nor its content can be modified further. |
50 | | * |
51 | | * GVariant is useful whenever data needs to be serialized, for example when |
52 | | * sending method parameters in D-Bus, or when saving settings using GSettings. |
53 | | * |
54 | | * When creating a new #GVariant, you pass the data you want to store in it |
55 | | * along with a string representing the type of data you wish to pass to it. |
56 | | * |
57 | | * For instance, if you want to create a #GVariant holding an integer value you |
58 | | * can use: |
59 | | * |
60 | | * |[<!-- language="C" --> |
61 | | * GVariant *v = g_variant_new ("u", 40); |
62 | | * ]| |
63 | | * |
64 | | * The string "u" in the first argument tells #GVariant that the data passed to |
65 | | * the constructor (40) is going to be an unsigned integer. |
66 | | * |
67 | | * More advanced examples of #GVariant in use can be found in documentation for |
68 | | * [GVariant format strings][gvariant-format-strings-pointers]. |
69 | | * |
70 | | * The range of possible values is determined by the type. |
71 | | * |
72 | | * The type system used by #GVariant is #GVariantType. |
73 | | * |
74 | | * #GVariant instances always have a type and a value (which are given |
75 | | * at construction time). The type and value of a #GVariant instance |
76 | | * can never change other than by the #GVariant itself being |
77 | | * destroyed. A #GVariant cannot contain a pointer. |
78 | | * |
79 | | * #GVariant is reference counted using g_variant_ref() and |
80 | | * g_variant_unref(). #GVariant also has floating reference counts -- |
81 | | * see g_variant_ref_sink(). |
82 | | * |
83 | | * #GVariant is completely threadsafe. A #GVariant instance can be |
84 | | * concurrently accessed in any way from any number of threads without |
85 | | * problems. |
86 | | * |
87 | | * #GVariant is heavily optimised for dealing with data in serialised |
88 | | * form. It works particularly well with data located in memory-mapped |
89 | | * files. It can perform nearly all deserialisation operations in a |
90 | | * small constant time, usually touching only a single memory page. |
91 | | * Serialised #GVariant data can also be sent over the network. |
92 | | * |
93 | | * #GVariant is largely compatible with D-Bus. Almost all types of |
94 | | * #GVariant instances can be sent over D-Bus. See #GVariantType for |
95 | | * exceptions. (However, #GVariant's serialisation format is not the same |
96 | | * as the serialisation format of a D-Bus message body: use #GDBusMessage, |
97 | | * in the gio library, for those.) |
98 | | * |
99 | | * For space-efficiency, the #GVariant serialisation format does not |
100 | | * automatically include the variant's length, type or endianness, |
101 | | * which must either be implied from context (such as knowledge that a |
102 | | * particular file format always contains a little-endian |
103 | | * %G_VARIANT_TYPE_VARIANT which occupies the whole length of the file) |
104 | | * or supplied out-of-band (for instance, a length, type and/or endianness |
105 | | * indicator could be placed at the beginning of a file, network message |
106 | | * or network stream). |
107 | | * |
108 | | * A #GVariant's size is limited mainly by any lower level operating |
109 | | * system constraints, such as the number of bits in #gsize. For |
110 | | * example, it is reasonable to have a 2GB file mapped into memory |
111 | | * with #GMappedFile, and call g_variant_new_from_data() on it. |
112 | | * |
113 | | * For convenience to C programmers, #GVariant features powerful |
114 | | * varargs-based value construction and destruction. This feature is |
115 | | * designed to be embedded in other libraries. |
116 | | * |
117 | | * There is a Python-inspired text language for describing #GVariant |
118 | | * values. #GVariant includes a printer for this language and a parser |
119 | | * with type inferencing. |
120 | | * |
121 | | * ## Memory Use |
122 | | * |
123 | | * #GVariant tries to be quite efficient with respect to memory use. |
124 | | * This section gives a rough idea of how much memory is used by the |
125 | | * current implementation. The information here is subject to change |
126 | | * in the future. |
127 | | * |
128 | | * The memory allocated by #GVariant can be grouped into 4 broad |
129 | | * purposes: memory for serialised data, memory for the type |
130 | | * information cache, buffer management memory and memory for the |
131 | | * #GVariant structure itself. |
132 | | * |
133 | | * ## Serialised Data Memory |
134 | | * |
135 | | * This is the memory that is used for storing GVariant data in |
136 | | * serialised form. This is what would be sent over the network or |
137 | | * what would end up on disk, not counting any indicator of the |
138 | | * endianness, or of the length or type of the top-level variant. |
139 | | * |
140 | | * The amount of memory required to store a boolean is 1 byte. 16, |
141 | | * 32 and 64 bit integers and double precision floating point numbers |
142 | | * use their "natural" size. Strings (including object path and |
143 | | * signature strings) are stored with a nul terminator, and as such |
144 | | * use the length of the string plus 1 byte. |
145 | | * |
146 | | * Maybe types use no space at all to represent the null value and |
147 | | * use the same amount of space (sometimes plus one byte) as the |
148 | | * equivalent non-maybe-typed value to represent the non-null case. |
149 | | * |
150 | | * Arrays use the amount of space required to store each of their |
151 | | * members, concatenated. Additionally, if the items stored in an |
152 | | * array are not of a fixed-size (ie: strings, other arrays, etc) |
153 | | * then an additional framing offset is stored for each item. The |
154 | | * size of this offset is either 1, 2 or 4 bytes depending on the |
155 | | * overall size of the container. Additionally, extra padding bytes |
156 | | * are added as required for alignment of child values. |
157 | | * |
158 | | * Tuples (including dictionary entries) use the amount of space |
159 | | * required to store each of their members, concatenated, plus one |
160 | | * framing offset (as per arrays) for each non-fixed-sized item in |
161 | | * the tuple, except for the last one. Additionally, extra padding |
162 | | * bytes are added as required for alignment of child values. |
163 | | * |
164 | | * Variants use the same amount of space as the item inside of the |
165 | | * variant, plus 1 byte, plus the length of the type string for the |
166 | | * item inside the variant. |
167 | | * |
168 | | * As an example, consider a dictionary mapping strings to variants. |
169 | | * In the case that the dictionary is empty, 0 bytes are required for |
170 | | * the serialisation. |
171 | | * |
172 | | * If we add an item "width" that maps to the int32 value of 500 then |
173 | | * we will use 4 byte to store the int32 (so 6 for the variant |
174 | | * containing it) and 6 bytes for the string. The variant must be |
175 | | * aligned to 8 after the 6 bytes of the string, so that's 2 extra |
176 | | * bytes. 6 (string) + 2 (padding) + 6 (variant) is 14 bytes used |
177 | | * for the dictionary entry. An additional 1 byte is added to the |
178 | | * array as a framing offset making a total of 15 bytes. |
179 | | * |
180 | | * If we add another entry, "title" that maps to a nullable string |
181 | | * that happens to have a value of null, then we use 0 bytes for the |
182 | | * null value (and 3 bytes for the variant to contain it along with |
183 | | * its type string) plus 6 bytes for the string. Again, we need 2 |
184 | | * padding bytes. That makes a total of 6 + 2 + 3 = 11 bytes. |
185 | | * |
186 | | * We now require extra padding between the two items in the array. |
187 | | * After the 14 bytes of the first item, that's 2 bytes required. |
188 | | * We now require 2 framing offsets for an extra two |
189 | | * bytes. 14 + 2 + 11 + 2 = 29 bytes to encode the entire two-item |
190 | | * dictionary. |
191 | | * |
192 | | * ## Type Information Cache |
193 | | * |
194 | | * For each GVariant type that currently exists in the program a type |
195 | | * information structure is kept in the type information cache. The |
196 | | * type information structure is required for rapid deserialisation. |
197 | | * |
198 | | * Continuing with the above example, if a #GVariant exists with the |
199 | | * type "a{sv}" then a type information struct will exist for |
200 | | * "a{sv}", "{sv}", "s", and "v". Multiple uses of the same type |
201 | | * will share the same type information. Additionally, all |
202 | | * single-digit types are stored in read-only static memory and do |
203 | | * not contribute to the writable memory footprint of a program using |
204 | | * #GVariant. |
205 | | * |
206 | | * Aside from the type information structures stored in read-only |
207 | | * memory, there are two forms of type information. One is used for |
208 | | * container types where there is a single element type: arrays and |
209 | | * maybe types. The other is used for container types where there |
210 | | * are multiple element types: tuples and dictionary entries. |
211 | | * |
212 | | * Array type info structures are 6 * sizeof (void *), plus the |
213 | | * memory required to store the type string itself. This means that |
214 | | * on 32-bit systems, the cache entry for "a{sv}" would require 30 |
215 | | * bytes of memory (plus malloc overhead). |
216 | | * |
217 | | * Tuple type info structures are 6 * sizeof (void *), plus 4 * |
218 | | * sizeof (void *) for each item in the tuple, plus the memory |
219 | | * required to store the type string itself. A 2-item tuple, for |
220 | | * example, would have a type information structure that consumed |
221 | | * writable memory in the size of 14 * sizeof (void *) (plus type |
222 | | * string) This means that on 32-bit systems, the cache entry for |
223 | | * "{sv}" would require 61 bytes of memory (plus malloc overhead). |
224 | | * |
225 | | * This means that in total, for our "a{sv}" example, 91 bytes of |
226 | | * type information would be allocated. |
227 | | * |
228 | | * The type information cache, additionally, uses a #GHashTable to |
229 | | * store and look up the cached items and stores a pointer to this |
230 | | * hash table in static storage. The hash table is freed when there |
231 | | * are zero items in the type cache. |
232 | | * |
233 | | * Although these sizes may seem large it is important to remember |
234 | | * that a program will probably only have a very small number of |
235 | | * different types of values in it and that only one type information |
236 | | * structure is required for many different values of the same type. |
237 | | * |
238 | | * ## Buffer Management Memory |
239 | | * |
240 | | * #GVariant uses an internal buffer management structure to deal |
241 | | * with the various different possible sources of serialised data |
242 | | * that it uses. The buffer is responsible for ensuring that the |
243 | | * correct call is made when the data is no longer in use by |
244 | | * #GVariant. This may involve a g_free() or a g_slice_free() or |
245 | | * even g_mapped_file_unref(). |
246 | | * |
247 | | * One buffer management structure is used for each chunk of |
248 | | * serialised data. The size of the buffer management structure |
249 | | * is 4 * (void *). On 32-bit systems, that's 16 bytes. |
250 | | * |
251 | | * ## GVariant structure |
252 | | * |
253 | | * The size of a #GVariant structure is 6 * (void *). On 32-bit |
254 | | * systems, that's 24 bytes. |
255 | | * |
256 | | * #GVariant structures only exist if they are explicitly created |
257 | | * with API calls. For example, if a #GVariant is constructed out of |
258 | | * serialised data for the example given above (with the dictionary) |
259 | | * then although there are 9 individual values that comprise the |
260 | | * entire dictionary (two keys, two values, two variants containing |
261 | | * the values, two dictionary entries, plus the dictionary itself), |
262 | | * only 1 #GVariant instance exists -- the one referring to the |
263 | | * dictionary. |
264 | | * |
265 | | * If calls are made to start accessing the other values then |
266 | | * #GVariant instances will exist for those values only for as long |
267 | | * as they are in use (ie: until you call g_variant_unref()). The |
268 | | * type information is shared. The serialised data and the buffer |
269 | | * management structure for that serialised data is shared by the |
270 | | * child. |
271 | | * |
272 | | * ## Summary |
273 | | * |
274 | | * To put the entire example together, for our dictionary mapping |
275 | | * strings to variants (with two entries, as given above), we are |
276 | | * using 91 bytes of memory for type information, 29 bytes of memory |
277 | | * for the serialised data, 16 bytes for buffer management and 24 |
278 | | * bytes for the #GVariant instance, or a total of 160 bytes, plus |
279 | | * malloc overhead. If we were to use g_variant_get_child_value() to |
280 | | * access the two dictionary entries, we would use an additional 48 |
281 | | * bytes. If we were to have other dictionaries of the same type, we |
282 | | * would use more memory for the serialised data and buffer |
283 | | * management for those dictionaries, but the type information would |
284 | | * be shared. |
285 | | */ |
286 | | |
287 | | /* definition of GVariant structure is in gvariant-core.c */ |
288 | | |
289 | | /* this is a g_return_val_if_fail() for making |
290 | | * sure a (GVariant *) has the required type. |
291 | | */ |
292 | | #define TYPE_CHECK(value, TYPE, val) \ |
293 | 0 | if G_UNLIKELY (!g_variant_is_of_type (value, TYPE)) { \ |
294 | 0 | g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, \ |
295 | 0 | "g_variant_is_of_type (" #value \ |
296 | 0 | ", " #TYPE ")"); \ |
297 | 0 | return val; \ |
298 | 0 | } |
299 | | |
300 | | /* Numeric Type Constructor/Getters {{{1 */ |
301 | | /* < private > |
302 | | * g_variant_new_from_trusted: |
303 | | * @type: the #GVariantType |
304 | | * @data: the data to use |
305 | | * @size: the size of @data |
306 | | * |
307 | | * Constructs a new trusted #GVariant instance from the provided data. |
308 | | * This is used to implement g_variant_new_* for all the basic types. |
309 | | * |
310 | | * Note: @data must be backed by memory that is aligned appropriately for the |
311 | | * @type being loaded. Otherwise this function will internally create a copy of |
312 | | * the memory (since GLib 2.60) or (in older versions) fail and exit the |
313 | | * process. |
314 | | * |
315 | | * Returns: a new floating #GVariant |
316 | | */ |
317 | | static GVariant * |
318 | | g_variant_new_from_trusted (const GVariantType *type, |
319 | | gconstpointer data, |
320 | | gsize size) |
321 | 0 | { |
322 | 0 | GVariant *value; |
323 | 0 | GBytes *bytes; |
324 | |
|
325 | 0 | bytes = g_bytes_new (data, size); |
326 | 0 | value = g_variant_new_from_bytes (type, bytes, TRUE); |
327 | 0 | g_bytes_unref (bytes); |
328 | |
|
329 | 0 | return value; |
330 | 0 | } |
331 | | |
332 | | /** |
333 | | * g_variant_new_boolean: |
334 | | * @value: a #gboolean value |
335 | | * |
336 | | * Creates a new boolean #GVariant instance -- either %TRUE or %FALSE. |
337 | | * |
338 | | * Returns: (transfer none): a floating reference to a new boolean #GVariant instance |
339 | | * |
340 | | * Since: 2.24 |
341 | | **/ |
342 | | GVariant * |
343 | | g_variant_new_boolean (gboolean value) |
344 | 0 | { |
345 | 0 | guchar v = value; |
346 | |
|
347 | 0 | return g_variant_new_from_trusted (G_VARIANT_TYPE_BOOLEAN, &v, 1); |
348 | 0 | } |
349 | | |
350 | | /** |
351 | | * g_variant_get_boolean: |
352 | | * @value: a boolean #GVariant instance |
353 | | * |
354 | | * Returns the boolean value of @value. |
355 | | * |
356 | | * It is an error to call this function with a @value of any type |
357 | | * other than %G_VARIANT_TYPE_BOOLEAN. |
358 | | * |
359 | | * Returns: %TRUE or %FALSE |
360 | | * |
361 | | * Since: 2.24 |
362 | | **/ |
363 | | gboolean |
364 | | g_variant_get_boolean (GVariant *value) |
365 | 0 | { |
366 | 0 | const guchar *data; |
367 | |
|
368 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_BOOLEAN, FALSE); |
369 | |
|
370 | 0 | data = g_variant_get_data (value); |
371 | |
|
372 | 0 | return data != NULL ? *data != 0 : FALSE; |
373 | 0 | } |
374 | | |
375 | | /* the constructors and accessors for byte, int{16,32,64}, handles and |
376 | | * doubles all look pretty much exactly the same, so we reduce |
377 | | * copy/pasting here. |
378 | | */ |
379 | | #define NUMERIC_TYPE(TYPE, type, ctype) \ |
380 | 0 | GVariant *g_variant_new_##type (ctype value) { \ |
381 | 0 | return g_variant_new_from_trusted (G_VARIANT_TYPE_##TYPE, \ |
382 | 0 | &value, sizeof value); \ |
383 | 0 | } \ Unexecuted instantiation: g_variant_new_byte Unexecuted instantiation: g_variant_new_int16 Unexecuted instantiation: g_variant_new_uint16 Unexecuted instantiation: g_variant_new_int32 Unexecuted instantiation: g_variant_new_uint32 Unexecuted instantiation: g_variant_new_int64 Unexecuted instantiation: g_variant_new_uint64 Unexecuted instantiation: g_variant_new_handle Unexecuted instantiation: g_variant_new_double |
384 | 0 | ctype g_variant_get_##type (GVariant *value) { \ |
385 | 0 | const ctype *data; \ |
386 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_ ## TYPE, 0); \ |
387 | 0 | data = g_variant_get_data (value); \ |
388 | 0 | return data != NULL ? *data : 0; \ |
389 | 0 | } Unexecuted instantiation: g_variant_get_byte Unexecuted instantiation: g_variant_get_int16 Unexecuted instantiation: g_variant_get_uint16 Unexecuted instantiation: g_variant_get_int32 Unexecuted instantiation: g_variant_get_uint32 Unexecuted instantiation: g_variant_get_int64 Unexecuted instantiation: g_variant_get_uint64 Unexecuted instantiation: g_variant_get_handle Unexecuted instantiation: g_variant_get_double |
390 | | |
391 | | |
392 | | /** |
393 | | * g_variant_new_byte: |
394 | | * @value: a #guint8 value |
395 | | * |
396 | | * Creates a new byte #GVariant instance. |
397 | | * |
398 | | * Returns: (transfer none): a floating reference to a new byte #GVariant instance |
399 | | * |
400 | | * Since: 2.24 |
401 | | **/ |
402 | | /** |
403 | | * g_variant_get_byte: |
404 | | * @value: a byte #GVariant instance |
405 | | * |
406 | | * Returns the byte value of @value. |
407 | | * |
408 | | * It is an error to call this function with a @value of any type |
409 | | * other than %G_VARIANT_TYPE_BYTE. |
410 | | * |
411 | | * Returns: a #guint8 |
412 | | * |
413 | | * Since: 2.24 |
414 | | **/ |
415 | | NUMERIC_TYPE (BYTE, byte, guint8) |
416 | | |
417 | | /** |
418 | | * g_variant_new_int16: |
419 | | * @value: a #gint16 value |
420 | | * |
421 | | * Creates a new int16 #GVariant instance. |
422 | | * |
423 | | * Returns: (transfer none): a floating reference to a new int16 #GVariant instance |
424 | | * |
425 | | * Since: 2.24 |
426 | | **/ |
427 | | /** |
428 | | * g_variant_get_int16: |
429 | | * @value: an int16 #GVariant instance |
430 | | * |
431 | | * Returns the 16-bit signed integer value of @value. |
432 | | * |
433 | | * It is an error to call this function with a @value of any type |
434 | | * other than %G_VARIANT_TYPE_INT16. |
435 | | * |
436 | | * Returns: a #gint16 |
437 | | * |
438 | | * Since: 2.24 |
439 | | **/ |
440 | | NUMERIC_TYPE (INT16, int16, gint16) |
441 | | |
442 | | /** |
443 | | * g_variant_new_uint16: |
444 | | * @value: a #guint16 value |
445 | | * |
446 | | * Creates a new uint16 #GVariant instance. |
447 | | * |
448 | | * Returns: (transfer none): a floating reference to a new uint16 #GVariant instance |
449 | | * |
450 | | * Since: 2.24 |
451 | | **/ |
452 | | /** |
453 | | * g_variant_get_uint16: |
454 | | * @value: a uint16 #GVariant instance |
455 | | * |
456 | | * Returns the 16-bit unsigned integer value of @value. |
457 | | * |
458 | | * It is an error to call this function with a @value of any type |
459 | | * other than %G_VARIANT_TYPE_UINT16. |
460 | | * |
461 | | * Returns: a #guint16 |
462 | | * |
463 | | * Since: 2.24 |
464 | | **/ |
465 | | NUMERIC_TYPE (UINT16, uint16, guint16) |
466 | | |
467 | | /** |
468 | | * g_variant_new_int32: |
469 | | * @value: a #gint32 value |
470 | | * |
471 | | * Creates a new int32 #GVariant instance. |
472 | | * |
473 | | * Returns: (transfer none): a floating reference to a new int32 #GVariant instance |
474 | | * |
475 | | * Since: 2.24 |
476 | | **/ |
477 | | /** |
478 | | * g_variant_get_int32: |
479 | | * @value: an int32 #GVariant instance |
480 | | * |
481 | | * Returns the 32-bit signed integer value of @value. |
482 | | * |
483 | | * It is an error to call this function with a @value of any type |
484 | | * other than %G_VARIANT_TYPE_INT32. |
485 | | * |
486 | | * Returns: a #gint32 |
487 | | * |
488 | | * Since: 2.24 |
489 | | **/ |
490 | | NUMERIC_TYPE (INT32, int32, gint32) |
491 | | |
492 | | /** |
493 | | * g_variant_new_uint32: |
494 | | * @value: a #guint32 value |
495 | | * |
496 | | * Creates a new uint32 #GVariant instance. |
497 | | * |
498 | | * Returns: (transfer none): a floating reference to a new uint32 #GVariant instance |
499 | | * |
500 | | * Since: 2.24 |
501 | | **/ |
502 | | /** |
503 | | * g_variant_get_uint32: |
504 | | * @value: a uint32 #GVariant instance |
505 | | * |
506 | | * Returns the 32-bit unsigned integer value of @value. |
507 | | * |
508 | | * It is an error to call this function with a @value of any type |
509 | | * other than %G_VARIANT_TYPE_UINT32. |
510 | | * |
511 | | * Returns: a #guint32 |
512 | | * |
513 | | * Since: 2.24 |
514 | | **/ |
515 | | NUMERIC_TYPE (UINT32, uint32, guint32) |
516 | | |
517 | | /** |
518 | | * g_variant_new_int64: |
519 | | * @value: a #gint64 value |
520 | | * |
521 | | * Creates a new int64 #GVariant instance. |
522 | | * |
523 | | * Returns: (transfer none): a floating reference to a new int64 #GVariant instance |
524 | | * |
525 | | * Since: 2.24 |
526 | | **/ |
527 | | /** |
528 | | * g_variant_get_int64: |
529 | | * @value: an int64 #GVariant instance |
530 | | * |
531 | | * Returns the 64-bit signed integer value of @value. |
532 | | * |
533 | | * It is an error to call this function with a @value of any type |
534 | | * other than %G_VARIANT_TYPE_INT64. |
535 | | * |
536 | | * Returns: a #gint64 |
537 | | * |
538 | | * Since: 2.24 |
539 | | **/ |
540 | | NUMERIC_TYPE (INT64, int64, gint64) |
541 | | |
542 | | /** |
543 | | * g_variant_new_uint64: |
544 | | * @value: a #guint64 value |
545 | | * |
546 | | * Creates a new uint64 #GVariant instance. |
547 | | * |
548 | | * Returns: (transfer none): a floating reference to a new uint64 #GVariant instance |
549 | | * |
550 | | * Since: 2.24 |
551 | | **/ |
552 | | /** |
553 | | * g_variant_get_uint64: |
554 | | * @value: a uint64 #GVariant instance |
555 | | * |
556 | | * Returns the 64-bit unsigned integer value of @value. |
557 | | * |
558 | | * It is an error to call this function with a @value of any type |
559 | | * other than %G_VARIANT_TYPE_UINT64. |
560 | | * |
561 | | * Returns: a #guint64 |
562 | | * |
563 | | * Since: 2.24 |
564 | | **/ |
565 | | NUMERIC_TYPE (UINT64, uint64, guint64) |
566 | | |
567 | | /** |
568 | | * g_variant_new_handle: |
569 | | * @value: a #gint32 value |
570 | | * |
571 | | * Creates a new handle #GVariant instance. |
572 | | * |
573 | | * By convention, handles are indexes into an array of file descriptors |
574 | | * that are sent alongside a D-Bus message. If you're not interacting |
575 | | * with D-Bus, you probably don't need them. |
576 | | * |
577 | | * Returns: (transfer none): a floating reference to a new handle #GVariant instance |
578 | | * |
579 | | * Since: 2.24 |
580 | | **/ |
581 | | /** |
582 | | * g_variant_get_handle: |
583 | | * @value: a handle #GVariant instance |
584 | | * |
585 | | * Returns the 32-bit signed integer value of @value. |
586 | | * |
587 | | * It is an error to call this function with a @value of any type other |
588 | | * than %G_VARIANT_TYPE_HANDLE. |
589 | | * |
590 | | * By convention, handles are indexes into an array of file descriptors |
591 | | * that are sent alongside a D-Bus message. If you're not interacting |
592 | | * with D-Bus, you probably don't need them. |
593 | | * |
594 | | * Returns: a #gint32 |
595 | | * |
596 | | * Since: 2.24 |
597 | | **/ |
598 | | NUMERIC_TYPE (HANDLE, handle, gint32) |
599 | | |
600 | | /** |
601 | | * g_variant_new_double: |
602 | | * @value: a #gdouble floating point value |
603 | | * |
604 | | * Creates a new double #GVariant instance. |
605 | | * |
606 | | * Returns: (transfer none): a floating reference to a new double #GVariant instance |
607 | | * |
608 | | * Since: 2.24 |
609 | | **/ |
610 | | /** |
611 | | * g_variant_get_double: |
612 | | * @value: a double #GVariant instance |
613 | | * |
614 | | * Returns the double precision floating point value of @value. |
615 | | * |
616 | | * It is an error to call this function with a @value of any type |
617 | | * other than %G_VARIANT_TYPE_DOUBLE. |
618 | | * |
619 | | * Returns: a #gdouble |
620 | | * |
621 | | * Since: 2.24 |
622 | | **/ |
623 | | NUMERIC_TYPE (DOUBLE, double, gdouble) |
624 | | |
625 | | /* Container type Constructor / Deconstructors {{{1 */ |
626 | | /** |
627 | | * g_variant_new_maybe: |
628 | | * @child_type: (nullable): the #GVariantType of the child, or %NULL |
629 | | * @child: (nullable): the child value, or %NULL |
630 | | * |
631 | | * Depending on if @child is %NULL, either wraps @child inside of a |
632 | | * maybe container or creates a Nothing instance for the given @type. |
633 | | * |
634 | | * At least one of @child_type and @child must be non-%NULL. |
635 | | * If @child_type is non-%NULL then it must be a definite type. |
636 | | * If they are both non-%NULL then @child_type must be the type |
637 | | * of @child. |
638 | | * |
639 | | * If @child is a floating reference (see g_variant_ref_sink()), the new |
640 | | * instance takes ownership of @child. |
641 | | * |
642 | | * Returns: (transfer none): a floating reference to a new #GVariant maybe instance |
643 | | * |
644 | | * Since: 2.24 |
645 | | **/ |
646 | | GVariant * |
647 | | g_variant_new_maybe (const GVariantType *child_type, |
648 | | GVariant *child) |
649 | 0 | { |
650 | 0 | GVariantType *maybe_type; |
651 | 0 | GVariant *value; |
652 | |
|
653 | 0 | g_return_val_if_fail (child_type == NULL || g_variant_type_is_definite |
654 | 0 | (child_type), 0); |
655 | 0 | g_return_val_if_fail (child_type != NULL || child != NULL, NULL); |
656 | 0 | g_return_val_if_fail (child_type == NULL || child == NULL || |
657 | 0 | g_variant_is_of_type (child, child_type), |
658 | 0 | NULL); |
659 | | |
660 | 0 | if (child_type == NULL) |
661 | 0 | child_type = g_variant_get_type (child); |
662 | |
|
663 | 0 | maybe_type = g_variant_type_new_maybe (child_type); |
664 | |
|
665 | 0 | if (child != NULL) |
666 | 0 | { |
667 | 0 | GVariant **children; |
668 | 0 | gboolean trusted; |
669 | |
|
670 | 0 | children = g_new (GVariant *, 1); |
671 | 0 | children[0] = g_variant_ref_sink (child); |
672 | 0 | trusted = g_variant_is_trusted (children[0]); |
673 | |
|
674 | 0 | value = g_variant_new_from_children (maybe_type, children, 1, trusted); |
675 | 0 | } |
676 | 0 | else |
677 | 0 | value = g_variant_new_from_children (maybe_type, NULL, 0, TRUE); |
678 | |
|
679 | 0 | g_variant_type_free (maybe_type); |
680 | |
|
681 | 0 | return value; |
682 | 0 | } |
683 | | |
684 | | /** |
685 | | * g_variant_get_maybe: |
686 | | * @value: a maybe-typed value |
687 | | * |
688 | | * Given a maybe-typed #GVariant instance, extract its value. If the |
689 | | * value is Nothing, then this function returns %NULL. |
690 | | * |
691 | | * Returns: (nullable) (transfer full): the contents of @value, or %NULL |
692 | | * |
693 | | * Since: 2.24 |
694 | | **/ |
695 | | GVariant * |
696 | | g_variant_get_maybe (GVariant *value) |
697 | 0 | { |
698 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_MAYBE, NULL); |
699 | |
|
700 | 0 | if (g_variant_n_children (value)) |
701 | 0 | return g_variant_get_child_value (value, 0); |
702 | | |
703 | 0 | return NULL; |
704 | 0 | } |
705 | | |
706 | | /** |
707 | | * g_variant_new_variant: (constructor) |
708 | | * @value: a #GVariant instance |
709 | | * |
710 | | * Boxes @value. The result is a #GVariant instance representing a |
711 | | * variant containing the original value. |
712 | | * |
713 | | * If @child is a floating reference (see g_variant_ref_sink()), the new |
714 | | * instance takes ownership of @child. |
715 | | * |
716 | | * Returns: (transfer none): a floating reference to a new variant #GVariant instance |
717 | | * |
718 | | * Since: 2.24 |
719 | | **/ |
720 | | GVariant * |
721 | | g_variant_new_variant (GVariant *value) |
722 | 0 | { |
723 | 0 | g_return_val_if_fail (value != NULL, NULL); |
724 | | |
725 | 0 | g_variant_ref_sink (value); |
726 | |
|
727 | 0 | return g_variant_new_from_children (G_VARIANT_TYPE_VARIANT, |
728 | 0 | g_memdup2 (&value, sizeof value), |
729 | 0 | 1, g_variant_is_trusted (value)); |
730 | 0 | } |
731 | | |
732 | | /** |
733 | | * g_variant_get_variant: |
734 | | * @value: a variant #GVariant instance |
735 | | * |
736 | | * Unboxes @value. The result is the #GVariant instance that was |
737 | | * contained in @value. |
738 | | * |
739 | | * Returns: (transfer full): the item contained in the variant |
740 | | * |
741 | | * Since: 2.24 |
742 | | **/ |
743 | | GVariant * |
744 | | g_variant_get_variant (GVariant *value) |
745 | 0 | { |
746 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_VARIANT, NULL); |
747 | |
|
748 | 0 | return g_variant_get_child_value (value, 0); |
749 | 0 | } |
750 | | |
751 | | /** |
752 | | * g_variant_new_array: |
753 | | * @child_type: (nullable): the element type of the new array |
754 | | * @children: (nullable) (array length=n_children): an array of |
755 | | * #GVariant pointers, the children |
756 | | * @n_children: the length of @children |
757 | | * |
758 | | * Creates a new #GVariant array from @children. |
759 | | * |
760 | | * @child_type must be non-%NULL if @n_children is zero. Otherwise, the |
761 | | * child type is determined by inspecting the first element of the |
762 | | * @children array. If @child_type is non-%NULL then it must be a |
763 | | * definite type. |
764 | | * |
765 | | * The items of the array are taken from the @children array. No entry |
766 | | * in the @children array may be %NULL. |
767 | | * |
768 | | * All items in the array must have the same type, which must be the |
769 | | * same as @child_type, if given. |
770 | | * |
771 | | * If the @children are floating references (see g_variant_ref_sink()), the |
772 | | * new instance takes ownership of them as if via g_variant_ref_sink(). |
773 | | * |
774 | | * Returns: (transfer none): a floating reference to a new #GVariant array |
775 | | * |
776 | | * Since: 2.24 |
777 | | **/ |
778 | | GVariant * |
779 | | g_variant_new_array (const GVariantType *child_type, |
780 | | GVariant * const *children, |
781 | | gsize n_children) |
782 | 0 | { |
783 | 0 | GVariantType *array_type; |
784 | 0 | GVariant **my_children; |
785 | 0 | gboolean trusted; |
786 | 0 | GVariant *value; |
787 | 0 | gsize i; |
788 | |
|
789 | 0 | g_return_val_if_fail (n_children > 0 || child_type != NULL, NULL); |
790 | 0 | g_return_val_if_fail (n_children == 0 || children != NULL, NULL); |
791 | 0 | g_return_val_if_fail (child_type == NULL || |
792 | 0 | g_variant_type_is_definite (child_type), NULL); |
793 | | |
794 | 0 | my_children = g_new (GVariant *, n_children); |
795 | 0 | trusted = TRUE; |
796 | |
|
797 | 0 | if (child_type == NULL) |
798 | 0 | child_type = g_variant_get_type (children[0]); |
799 | 0 | array_type = g_variant_type_new_array (child_type); |
800 | |
|
801 | 0 | for (i = 0; i < n_children; i++) |
802 | 0 | { |
803 | 0 | TYPE_CHECK (children[i], child_type, NULL); |
804 | 0 | my_children[i] = g_variant_ref_sink (children[i]); |
805 | 0 | trusted &= g_variant_is_trusted (children[i]); |
806 | 0 | } |
807 | | |
808 | 0 | value = g_variant_new_from_children (array_type, my_children, |
809 | 0 | n_children, trusted); |
810 | 0 | g_variant_type_free (array_type); |
811 | |
|
812 | 0 | return value; |
813 | 0 | } |
814 | | |
815 | | /*< private > |
816 | | * g_variant_make_tuple_type: |
817 | | * @children: (array length=n_children): an array of GVariant * |
818 | | * @n_children: the length of @children |
819 | | * |
820 | | * Return the type of a tuple containing @children as its items. |
821 | | **/ |
822 | | static GVariantType * |
823 | | g_variant_make_tuple_type (GVariant * const *children, |
824 | | gsize n_children) |
825 | 0 | { |
826 | 0 | const GVariantType **types; |
827 | 0 | GVariantType *type; |
828 | 0 | gsize i; |
829 | |
|
830 | 0 | types = g_new (const GVariantType *, n_children); |
831 | |
|
832 | 0 | for (i = 0; i < n_children; i++) |
833 | 0 | types[i] = g_variant_get_type (children[i]); |
834 | |
|
835 | 0 | type = g_variant_type_new_tuple (types, n_children); |
836 | 0 | g_free (types); |
837 | |
|
838 | 0 | return type; |
839 | 0 | } |
840 | | |
841 | | /** |
842 | | * g_variant_new_tuple: |
843 | | * @children: (array length=n_children): the items to make the tuple out of |
844 | | * @n_children: the length of @children |
845 | | * |
846 | | * Creates a new tuple #GVariant out of the items in @children. The |
847 | | * type is determined from the types of @children. No entry in the |
848 | | * @children array may be %NULL. |
849 | | * |
850 | | * If @n_children is 0 then the unit tuple is constructed. |
851 | | * |
852 | | * If the @children are floating references (see g_variant_ref_sink()), the |
853 | | * new instance takes ownership of them as if via g_variant_ref_sink(). |
854 | | * |
855 | | * Returns: (transfer none): a floating reference to a new #GVariant tuple |
856 | | * |
857 | | * Since: 2.24 |
858 | | **/ |
859 | | GVariant * |
860 | | g_variant_new_tuple (GVariant * const *children, |
861 | | gsize n_children) |
862 | 0 | { |
863 | 0 | GVariantType *tuple_type; |
864 | 0 | GVariant **my_children; |
865 | 0 | gboolean trusted; |
866 | 0 | GVariant *value; |
867 | 0 | gsize i; |
868 | |
|
869 | 0 | g_return_val_if_fail (n_children == 0 || children != NULL, NULL); |
870 | | |
871 | 0 | my_children = g_new (GVariant *, n_children); |
872 | 0 | trusted = TRUE; |
873 | |
|
874 | 0 | for (i = 0; i < n_children; i++) |
875 | 0 | { |
876 | 0 | my_children[i] = g_variant_ref_sink (children[i]); |
877 | 0 | trusted &= g_variant_is_trusted (children[i]); |
878 | 0 | } |
879 | |
|
880 | 0 | tuple_type = g_variant_make_tuple_type (children, n_children); |
881 | 0 | value = g_variant_new_from_children (tuple_type, my_children, |
882 | 0 | n_children, trusted); |
883 | 0 | g_variant_type_free (tuple_type); |
884 | |
|
885 | 0 | return value; |
886 | 0 | } |
887 | | |
888 | | /*< private > |
889 | | * g_variant_make_dict_entry_type: |
890 | | * @key: a #GVariant, the key |
891 | | * @val: a #GVariant, the value |
892 | | * |
893 | | * Return the type of a dictionary entry containing @key and @val as its |
894 | | * children. |
895 | | **/ |
896 | | static GVariantType * |
897 | | g_variant_make_dict_entry_type (GVariant *key, |
898 | | GVariant *val) |
899 | 0 | { |
900 | 0 | return g_variant_type_new_dict_entry (g_variant_get_type (key), |
901 | 0 | g_variant_get_type (val)); |
902 | 0 | } |
903 | | |
904 | | /** |
905 | | * g_variant_new_dict_entry: (constructor) |
906 | | * @key: a basic #GVariant, the key |
907 | | * @value: a #GVariant, the value |
908 | | * |
909 | | * Creates a new dictionary entry #GVariant. @key and @value must be |
910 | | * non-%NULL. @key must be a value of a basic type (ie: not a container). |
911 | | * |
912 | | * If the @key or @value are floating references (see g_variant_ref_sink()), |
913 | | * the new instance takes ownership of them as if via g_variant_ref_sink(). |
914 | | * |
915 | | * Returns: (transfer none): a floating reference to a new dictionary entry #GVariant |
916 | | * |
917 | | * Since: 2.24 |
918 | | **/ |
919 | | GVariant * |
920 | | g_variant_new_dict_entry (GVariant *key, |
921 | | GVariant *value) |
922 | 0 | { |
923 | 0 | GVariantType *dict_type; |
924 | 0 | GVariant **children; |
925 | 0 | gboolean trusted; |
926 | |
|
927 | 0 | g_return_val_if_fail (key != NULL && value != NULL, NULL); |
928 | 0 | g_return_val_if_fail (!g_variant_is_container (key), NULL); |
929 | | |
930 | 0 | children = g_new (GVariant *, 2); |
931 | 0 | children[0] = g_variant_ref_sink (key); |
932 | 0 | children[1] = g_variant_ref_sink (value); |
933 | 0 | trusted = g_variant_is_trusted (key) && g_variant_is_trusted (value); |
934 | |
|
935 | 0 | dict_type = g_variant_make_dict_entry_type (key, value); |
936 | 0 | value = g_variant_new_from_children (dict_type, children, 2, trusted); |
937 | 0 | g_variant_type_free (dict_type); |
938 | |
|
939 | 0 | return value; |
940 | 0 | } |
941 | | |
942 | | /** |
943 | | * g_variant_lookup: (skip) |
944 | | * @dictionary: a dictionary #GVariant |
945 | | * @key: the key to look up in the dictionary |
946 | | * @format_string: a GVariant format string |
947 | | * @...: the arguments to unpack the value into |
948 | | * |
949 | | * Looks up a value in a dictionary #GVariant. |
950 | | * |
951 | | * This function is a wrapper around g_variant_lookup_value() and |
952 | | * g_variant_get(). In the case that %NULL would have been returned, |
953 | | * this function returns %FALSE. Otherwise, it unpacks the returned |
954 | | * value and returns %TRUE. |
955 | | * |
956 | | * @format_string determines the C types that are used for unpacking |
957 | | * the values and also determines if the values are copied or borrowed, |
958 | | * see the section on |
959 | | * [GVariant format strings][gvariant-format-strings-pointers]. |
960 | | * |
961 | | * This function is currently implemented with a linear scan. If you |
962 | | * plan to do many lookups then #GVariantDict may be more efficient. |
963 | | * |
964 | | * Returns: %TRUE if a value was unpacked |
965 | | * |
966 | | * Since: 2.28 |
967 | | */ |
968 | | gboolean |
969 | | g_variant_lookup (GVariant *dictionary, |
970 | | const gchar *key, |
971 | | const gchar *format_string, |
972 | | ...) |
973 | 0 | { |
974 | 0 | GVariantType *type; |
975 | 0 | GVariant *value; |
976 | | |
977 | | /* flatten */ |
978 | 0 | g_variant_get_data (dictionary); |
979 | |
|
980 | 0 | type = g_variant_format_string_scan_type (format_string, NULL, NULL); |
981 | 0 | value = g_variant_lookup_value (dictionary, key, type); |
982 | 0 | g_variant_type_free (type); |
983 | |
|
984 | 0 | if (value) |
985 | 0 | { |
986 | 0 | va_list ap; |
987 | |
|
988 | 0 | va_start (ap, format_string); |
989 | 0 | g_variant_get_va (value, format_string, NULL, &ap); |
990 | 0 | g_variant_unref (value); |
991 | 0 | va_end (ap); |
992 | |
|
993 | 0 | return TRUE; |
994 | 0 | } |
995 | | |
996 | 0 | else |
997 | 0 | return FALSE; |
998 | 0 | } |
999 | | |
1000 | | /** |
1001 | | * g_variant_lookup_value: |
1002 | | * @dictionary: a dictionary #GVariant |
1003 | | * @key: the key to look up in the dictionary |
1004 | | * @expected_type: (nullable): a #GVariantType, or %NULL |
1005 | | * |
1006 | | * Looks up a value in a dictionary #GVariant. |
1007 | | * |
1008 | | * This function works with dictionaries of the type a{s*} (and equally |
1009 | | * well with type a{o*}, but we only further discuss the string case |
1010 | | * for sake of clarity). |
1011 | | * |
1012 | | * In the event that @dictionary has the type a{sv}, the @expected_type |
1013 | | * string specifies what type of value is expected to be inside of the |
1014 | | * variant. If the value inside the variant has a different type then |
1015 | | * %NULL is returned. In the event that @dictionary has a value type other |
1016 | | * than v then @expected_type must directly match the value type and it is |
1017 | | * used to unpack the value directly or an error occurs. |
1018 | | * |
1019 | | * In either case, if @key is not found in @dictionary, %NULL is returned. |
1020 | | * |
1021 | | * If the key is found and the value has the correct type, it is |
1022 | | * returned. If @expected_type was specified then any non-%NULL return |
1023 | | * value will have this type. |
1024 | | * |
1025 | | * This function is currently implemented with a linear scan. If you |
1026 | | * plan to do many lookups then #GVariantDict may be more efficient. |
1027 | | * |
1028 | | * Returns: (transfer full): the value of the dictionary key, or %NULL |
1029 | | * |
1030 | | * Since: 2.28 |
1031 | | */ |
1032 | | GVariant * |
1033 | | g_variant_lookup_value (GVariant *dictionary, |
1034 | | const gchar *key, |
1035 | | const GVariantType *expected_type) |
1036 | 0 | { |
1037 | 0 | GVariantIter iter; |
1038 | 0 | GVariant *entry; |
1039 | 0 | GVariant *value; |
1040 | |
|
1041 | 0 | g_return_val_if_fail (g_variant_is_of_type (dictionary, |
1042 | 0 | G_VARIANT_TYPE ("a{s*}")) || |
1043 | 0 | g_variant_is_of_type (dictionary, |
1044 | 0 | G_VARIANT_TYPE ("a{o*}")), |
1045 | 0 | NULL); |
1046 | | |
1047 | 0 | g_variant_iter_init (&iter, dictionary); |
1048 | |
|
1049 | 0 | while ((entry = g_variant_iter_next_value (&iter))) |
1050 | 0 | { |
1051 | 0 | GVariant *entry_key; |
1052 | 0 | gboolean matches; |
1053 | |
|
1054 | 0 | entry_key = g_variant_get_child_value (entry, 0); |
1055 | 0 | matches = strcmp (g_variant_get_string (entry_key, NULL), key) == 0; |
1056 | 0 | g_variant_unref (entry_key); |
1057 | |
|
1058 | 0 | if (matches) |
1059 | 0 | break; |
1060 | | |
1061 | 0 | g_variant_unref (entry); |
1062 | 0 | } |
1063 | |
|
1064 | 0 | if (entry == NULL) |
1065 | 0 | return NULL; |
1066 | | |
1067 | 0 | value = g_variant_get_child_value (entry, 1); |
1068 | 0 | g_variant_unref (entry); |
1069 | |
|
1070 | 0 | if (g_variant_is_of_type (value, G_VARIANT_TYPE_VARIANT)) |
1071 | 0 | { |
1072 | 0 | GVariant *tmp; |
1073 | |
|
1074 | 0 | tmp = g_variant_get_variant (value); |
1075 | 0 | g_variant_unref (value); |
1076 | |
|
1077 | 0 | if (expected_type && !g_variant_is_of_type (tmp, expected_type)) |
1078 | 0 | { |
1079 | 0 | g_variant_unref (tmp); |
1080 | 0 | tmp = NULL; |
1081 | 0 | } |
1082 | |
|
1083 | 0 | value = tmp; |
1084 | 0 | } |
1085 | |
|
1086 | 0 | g_return_val_if_fail (expected_type == NULL || value == NULL || |
1087 | 0 | g_variant_is_of_type (value, expected_type), NULL); |
1088 | | |
1089 | 0 | return value; |
1090 | 0 | } |
1091 | | |
1092 | | /** |
1093 | | * g_variant_get_fixed_array: |
1094 | | * @value: a #GVariant array with fixed-sized elements |
1095 | | * @n_elements: (out): a pointer to the location to store the number of items |
1096 | | * @element_size: the size of each element |
1097 | | * |
1098 | | * Provides access to the serialised data for an array of fixed-sized |
1099 | | * items. |
1100 | | * |
1101 | | * @value must be an array with fixed-sized elements. Numeric types are |
1102 | | * fixed-size, as are tuples containing only other fixed-sized types. |
1103 | | * |
1104 | | * @element_size must be the size of a single element in the array, |
1105 | | * as given by the section on |
1106 | | * [serialized data memory][gvariant-serialised-data-memory]. |
1107 | | * |
1108 | | * In particular, arrays of these fixed-sized types can be interpreted |
1109 | | * as an array of the given C type, with @element_size set to the size |
1110 | | * the appropriate type: |
1111 | | * - %G_VARIANT_TYPE_INT16 (etc.): #gint16 (etc.) |
1112 | | * - %G_VARIANT_TYPE_BOOLEAN: #guchar (not #gboolean!) |
1113 | | * - %G_VARIANT_TYPE_BYTE: #guint8 |
1114 | | * - %G_VARIANT_TYPE_HANDLE: #guint32 |
1115 | | * - %G_VARIANT_TYPE_DOUBLE: #gdouble |
1116 | | * |
1117 | | * For example, if calling this function for an array of 32-bit integers, |
1118 | | * you might say `sizeof(gint32)`. This value isn't used except for the purpose |
1119 | | * of a double-check that the form of the serialised data matches the caller's |
1120 | | * expectation. |
1121 | | * |
1122 | | * @n_elements, which must be non-%NULL, is set equal to the number of |
1123 | | * items in the array. |
1124 | | * |
1125 | | * Returns: (array length=n_elements) (transfer none): a pointer to |
1126 | | * the fixed array |
1127 | | * |
1128 | | * Since: 2.24 |
1129 | | **/ |
1130 | | gconstpointer |
1131 | | g_variant_get_fixed_array (GVariant *value, |
1132 | | gsize *n_elements, |
1133 | | gsize element_size) |
1134 | 0 | { |
1135 | 0 | GVariantTypeInfo *array_info; |
1136 | 0 | gsize array_element_size; |
1137 | 0 | gconstpointer data; |
1138 | 0 | gsize size; |
1139 | |
|
1140 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_ARRAY, NULL); |
1141 | |
|
1142 | 0 | g_return_val_if_fail (n_elements != NULL, NULL); |
1143 | 0 | g_return_val_if_fail (element_size > 0, NULL); |
1144 | | |
1145 | 0 | array_info = g_variant_get_type_info (value); |
1146 | 0 | g_variant_type_info_query_element (array_info, NULL, &array_element_size); |
1147 | |
|
1148 | 0 | g_return_val_if_fail (array_element_size, NULL); |
1149 | | |
1150 | 0 | if G_UNLIKELY (array_element_size != element_size) |
1151 | 0 | { |
1152 | 0 | if (array_element_size) |
1153 | 0 | g_critical ("g_variant_get_fixed_array: assertion " |
1154 | 0 | "'g_variant_array_has_fixed_size (value, element_size)' " |
1155 | 0 | "failed: array size %"G_GSIZE_FORMAT" does not match " |
1156 | 0 | "given element_size %"G_GSIZE_FORMAT".", |
1157 | 0 | array_element_size, element_size); |
1158 | 0 | else |
1159 | 0 | g_critical ("g_variant_get_fixed_array: assertion " |
1160 | 0 | "'g_variant_array_has_fixed_size (value, element_size)' " |
1161 | 0 | "failed: array does not have fixed size."); |
1162 | 0 | } |
1163 | |
|
1164 | 0 | data = g_variant_get_data (value); |
1165 | 0 | size = g_variant_get_size (value); |
1166 | |
|
1167 | 0 | if (size % element_size) |
1168 | 0 | *n_elements = 0; |
1169 | 0 | else |
1170 | 0 | *n_elements = size / element_size; |
1171 | |
|
1172 | 0 | if (*n_elements) |
1173 | 0 | return data; |
1174 | | |
1175 | 0 | return NULL; |
1176 | 0 | } |
1177 | | |
1178 | | /** |
1179 | | * g_variant_new_fixed_array: |
1180 | | * @element_type: the #GVariantType of each element |
1181 | | * @elements: a pointer to the fixed array of contiguous elements |
1182 | | * @n_elements: the number of elements |
1183 | | * @element_size: the size of each element |
1184 | | * |
1185 | | * Constructs a new array #GVariant instance, where the elements are |
1186 | | * of @element_type type. |
1187 | | * |
1188 | | * @elements must be an array with fixed-sized elements. Numeric types are |
1189 | | * fixed-size as are tuples containing only other fixed-sized types. |
1190 | | * |
1191 | | * @element_size must be the size of a single element in the array. |
1192 | | * For example, if calling this function for an array of 32-bit integers, |
1193 | | * you might say sizeof(gint32). This value isn't used except for the purpose |
1194 | | * of a double-check that the form of the serialised data matches the caller's |
1195 | | * expectation. |
1196 | | * |
1197 | | * @n_elements must be the length of the @elements array. |
1198 | | * |
1199 | | * Returns: (transfer none): a floating reference to a new array #GVariant instance |
1200 | | * |
1201 | | * Since: 2.32 |
1202 | | **/ |
1203 | | GVariant * |
1204 | | g_variant_new_fixed_array (const GVariantType *element_type, |
1205 | | gconstpointer elements, |
1206 | | gsize n_elements, |
1207 | | gsize element_size) |
1208 | 0 | { |
1209 | 0 | GVariantType *array_type; |
1210 | 0 | gsize array_element_size; |
1211 | 0 | GVariantTypeInfo *array_info; |
1212 | 0 | GVariant *value; |
1213 | 0 | gpointer data; |
1214 | |
|
1215 | 0 | g_return_val_if_fail (g_variant_type_is_definite (element_type), NULL); |
1216 | 0 | g_return_val_if_fail (element_size > 0, NULL); |
1217 | | |
1218 | 0 | array_type = g_variant_type_new_array (element_type); |
1219 | 0 | array_info = g_variant_type_info_get (array_type); |
1220 | 0 | g_variant_type_info_query_element (array_info, NULL, &array_element_size); |
1221 | 0 | if G_UNLIKELY (array_element_size != element_size) |
1222 | 0 | { |
1223 | 0 | if (array_element_size) |
1224 | 0 | g_critical ("g_variant_new_fixed_array: array size %" G_GSIZE_FORMAT |
1225 | 0 | " does not match given element_size %" G_GSIZE_FORMAT ".", |
1226 | 0 | array_element_size, element_size); |
1227 | 0 | else |
1228 | 0 | g_critical ("g_variant_get_fixed_array: array does not have fixed size."); |
1229 | 0 | return NULL; |
1230 | 0 | } |
1231 | | |
1232 | 0 | data = g_memdup2 (elements, n_elements * element_size); |
1233 | 0 | value = g_variant_new_from_data (array_type, data, |
1234 | 0 | n_elements * element_size, |
1235 | 0 | FALSE, g_free, data); |
1236 | |
|
1237 | 0 | g_variant_type_free (array_type); |
1238 | 0 | g_variant_type_info_unref (array_info); |
1239 | |
|
1240 | 0 | return value; |
1241 | 0 | } |
1242 | | |
1243 | | /* String type constructor/getters/validation {{{1 */ |
1244 | | /** |
1245 | | * g_variant_new_string: |
1246 | | * @string: a normal UTF-8 nul-terminated string |
1247 | | * |
1248 | | * Creates a string #GVariant with the contents of @string. |
1249 | | * |
1250 | | * @string must be valid UTF-8, and must not be %NULL. To encode |
1251 | | * potentially-%NULL strings, use g_variant_new() with `ms` as the |
1252 | | * [format string][gvariant-format-strings-maybe-types]. |
1253 | | * |
1254 | | * Returns: (transfer none): a floating reference to a new string #GVariant instance |
1255 | | * |
1256 | | * Since: 2.24 |
1257 | | **/ |
1258 | | GVariant * |
1259 | | g_variant_new_string (const gchar *string) |
1260 | 0 | { |
1261 | 0 | g_return_val_if_fail (string != NULL, NULL); |
1262 | 0 | g_return_val_if_fail (g_utf8_validate (string, -1, NULL), NULL); |
1263 | | |
1264 | 0 | return g_variant_new_from_trusted (G_VARIANT_TYPE_STRING, |
1265 | 0 | string, strlen (string) + 1); |
1266 | 0 | } |
1267 | | |
1268 | | /** |
1269 | | * g_variant_new_take_string: (skip) |
1270 | | * @string: a normal UTF-8 nul-terminated string |
1271 | | * |
1272 | | * Creates a string #GVariant with the contents of @string. |
1273 | | * |
1274 | | * @string must be valid UTF-8, and must not be %NULL. To encode |
1275 | | * potentially-%NULL strings, use this with g_variant_new_maybe(). |
1276 | | * |
1277 | | * This function consumes @string. g_free() will be called on @string |
1278 | | * when it is no longer required. |
1279 | | * |
1280 | | * You must not modify or access @string in any other way after passing |
1281 | | * it to this function. It is even possible that @string is immediately |
1282 | | * freed. |
1283 | | * |
1284 | | * Returns: (transfer none): a floating reference to a new string |
1285 | | * #GVariant instance |
1286 | | * |
1287 | | * Since: 2.38 |
1288 | | **/ |
1289 | | GVariant * |
1290 | | g_variant_new_take_string (gchar *string) |
1291 | 0 | { |
1292 | 0 | GVariant *value; |
1293 | 0 | GBytes *bytes; |
1294 | |
|
1295 | 0 | g_return_val_if_fail (string != NULL, NULL); |
1296 | 0 | g_return_val_if_fail (g_utf8_validate (string, -1, NULL), NULL); |
1297 | | |
1298 | 0 | bytes = g_bytes_new_take (string, strlen (string) + 1); |
1299 | 0 | value = g_variant_new_from_bytes (G_VARIANT_TYPE_STRING, bytes, TRUE); |
1300 | 0 | g_bytes_unref (bytes); |
1301 | |
|
1302 | 0 | return value; |
1303 | 0 | } |
1304 | | |
1305 | | /** |
1306 | | * g_variant_new_printf: (skip) |
1307 | | * @format_string: a printf-style format string |
1308 | | * @...: arguments for @format_string |
1309 | | * |
1310 | | * Creates a string-type GVariant using printf formatting. |
1311 | | * |
1312 | | * This is similar to calling g_strdup_printf() and then |
1313 | | * g_variant_new_string() but it saves a temporary variable and an |
1314 | | * unnecessary copy. |
1315 | | * |
1316 | | * Returns: (transfer none): a floating reference to a new string |
1317 | | * #GVariant instance |
1318 | | * |
1319 | | * Since: 2.38 |
1320 | | **/ |
1321 | | GVariant * |
1322 | | g_variant_new_printf (const gchar *format_string, |
1323 | | ...) |
1324 | 0 | { |
1325 | 0 | GVariant *value; |
1326 | 0 | GBytes *bytes; |
1327 | 0 | gchar *string; |
1328 | 0 | va_list ap; |
1329 | |
|
1330 | 0 | g_return_val_if_fail (format_string != NULL, NULL); |
1331 | | |
1332 | 0 | va_start (ap, format_string); |
1333 | 0 | string = g_strdup_vprintf (format_string, ap); |
1334 | 0 | va_end (ap); |
1335 | |
|
1336 | 0 | bytes = g_bytes_new_take (string, strlen (string) + 1); |
1337 | 0 | value = g_variant_new_from_bytes (G_VARIANT_TYPE_STRING, bytes, TRUE); |
1338 | 0 | g_bytes_unref (bytes); |
1339 | |
|
1340 | 0 | return value; |
1341 | 0 | } |
1342 | | |
1343 | | /** |
1344 | | * g_variant_new_object_path: |
1345 | | * @object_path: a normal C nul-terminated string |
1346 | | * |
1347 | | * Creates a D-Bus object path #GVariant with the contents of @string. |
1348 | | * @string must be a valid D-Bus object path. Use |
1349 | | * g_variant_is_object_path() if you're not sure. |
1350 | | * |
1351 | | * Returns: (transfer none): a floating reference to a new object path #GVariant instance |
1352 | | * |
1353 | | * Since: 2.24 |
1354 | | **/ |
1355 | | GVariant * |
1356 | | g_variant_new_object_path (const gchar *object_path) |
1357 | 0 | { |
1358 | 0 | g_return_val_if_fail (g_variant_is_object_path (object_path), NULL); |
1359 | | |
1360 | 0 | return g_variant_new_from_trusted (G_VARIANT_TYPE_OBJECT_PATH, |
1361 | 0 | object_path, strlen (object_path) + 1); |
1362 | 0 | } |
1363 | | |
1364 | | /** |
1365 | | * g_variant_is_object_path: |
1366 | | * @string: a normal C nul-terminated string |
1367 | | * |
1368 | | * Determines if a given string is a valid D-Bus object path. You |
1369 | | * should ensure that a string is a valid D-Bus object path before |
1370 | | * passing it to g_variant_new_object_path(). |
1371 | | * |
1372 | | * A valid object path starts with `/` followed by zero or more |
1373 | | * sequences of characters separated by `/` characters. Each sequence |
1374 | | * must contain only the characters `[A-Z][a-z][0-9]_`. No sequence |
1375 | | * (including the one following the final `/` character) may be empty. |
1376 | | * |
1377 | | * Returns: %TRUE if @string is a D-Bus object path |
1378 | | * |
1379 | | * Since: 2.24 |
1380 | | **/ |
1381 | | gboolean |
1382 | | g_variant_is_object_path (const gchar *string) |
1383 | 0 | { |
1384 | 0 | g_return_val_if_fail (string != NULL, FALSE); |
1385 | | |
1386 | 0 | return g_variant_serialiser_is_object_path (string, strlen (string) + 1); |
1387 | 0 | } |
1388 | | |
1389 | | /** |
1390 | | * g_variant_new_signature: |
1391 | | * @signature: a normal C nul-terminated string |
1392 | | * |
1393 | | * Creates a D-Bus type signature #GVariant with the contents of |
1394 | | * @string. @string must be a valid D-Bus type signature. Use |
1395 | | * g_variant_is_signature() if you're not sure. |
1396 | | * |
1397 | | * Returns: (transfer none): a floating reference to a new signature #GVariant instance |
1398 | | * |
1399 | | * Since: 2.24 |
1400 | | **/ |
1401 | | GVariant * |
1402 | | g_variant_new_signature (const gchar *signature) |
1403 | 0 | { |
1404 | 0 | g_return_val_if_fail (g_variant_is_signature (signature), NULL); |
1405 | | |
1406 | 0 | return g_variant_new_from_trusted (G_VARIANT_TYPE_SIGNATURE, |
1407 | 0 | signature, strlen (signature) + 1); |
1408 | 0 | } |
1409 | | |
1410 | | /** |
1411 | | * g_variant_is_signature: |
1412 | | * @string: a normal C nul-terminated string |
1413 | | * |
1414 | | * Determines if a given string is a valid D-Bus type signature. You |
1415 | | * should ensure that a string is a valid D-Bus type signature before |
1416 | | * passing it to g_variant_new_signature(). |
1417 | | * |
1418 | | * D-Bus type signatures consist of zero or more definite #GVariantType |
1419 | | * strings in sequence. |
1420 | | * |
1421 | | * Returns: %TRUE if @string is a D-Bus type signature |
1422 | | * |
1423 | | * Since: 2.24 |
1424 | | **/ |
1425 | | gboolean |
1426 | | g_variant_is_signature (const gchar *string) |
1427 | 0 | { |
1428 | 0 | g_return_val_if_fail (string != NULL, FALSE); |
1429 | | |
1430 | 0 | return g_variant_serialiser_is_signature (string, strlen (string) + 1); |
1431 | 0 | } |
1432 | | |
1433 | | /** |
1434 | | * g_variant_get_string: |
1435 | | * @value: a string #GVariant instance |
1436 | | * @length: (optional) (default 0) (out): a pointer to a #gsize, |
1437 | | * to store the length |
1438 | | * |
1439 | | * Returns the string value of a #GVariant instance with a string |
1440 | | * type. This includes the types %G_VARIANT_TYPE_STRING, |
1441 | | * %G_VARIANT_TYPE_OBJECT_PATH and %G_VARIANT_TYPE_SIGNATURE. |
1442 | | * |
1443 | | * The string will always be UTF-8 encoded, will never be %NULL, and will never |
1444 | | * contain nul bytes. |
1445 | | * |
1446 | | * If @length is non-%NULL then the length of the string (in bytes) is |
1447 | | * returned there. For trusted values, this information is already |
1448 | | * known. Untrusted values will be validated and, if valid, a strlen() will be |
1449 | | * performed. If invalid, a default value will be returned — for |
1450 | | * %G_VARIANT_TYPE_OBJECT_PATH, this is `"/"`, and for other types it is the |
1451 | | * empty string. |
1452 | | * |
1453 | | * It is an error to call this function with a @value of any type |
1454 | | * other than those three. |
1455 | | * |
1456 | | * The return value remains valid as long as @value exists. |
1457 | | * |
1458 | | * Returns: (transfer none): the constant string, UTF-8 encoded |
1459 | | * |
1460 | | * Since: 2.24 |
1461 | | **/ |
1462 | | const gchar * |
1463 | | g_variant_get_string (GVariant *value, |
1464 | | gsize *length) |
1465 | 0 | { |
1466 | 0 | gconstpointer data; |
1467 | 0 | gsize size; |
1468 | |
|
1469 | 0 | g_return_val_if_fail (value != NULL, NULL); |
1470 | 0 | g_return_val_if_fail ( |
1471 | 0 | g_variant_is_of_type (value, G_VARIANT_TYPE_STRING) || |
1472 | 0 | g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH) || |
1473 | 0 | g_variant_is_of_type (value, G_VARIANT_TYPE_SIGNATURE), NULL); |
1474 | | |
1475 | 0 | data = g_variant_get_data (value); |
1476 | 0 | size = g_variant_get_size (value); |
1477 | |
|
1478 | 0 | if (!g_variant_is_trusted (value)) |
1479 | 0 | { |
1480 | 0 | switch (g_variant_classify (value)) |
1481 | 0 | { |
1482 | 0 | case G_VARIANT_CLASS_STRING: |
1483 | 0 | if (g_variant_serialiser_is_string (data, size)) |
1484 | 0 | break; |
1485 | | |
1486 | 0 | data = ""; |
1487 | 0 | size = 1; |
1488 | 0 | break; |
1489 | | |
1490 | 0 | case G_VARIANT_CLASS_OBJECT_PATH: |
1491 | 0 | if (g_variant_serialiser_is_object_path (data, size)) |
1492 | 0 | break; |
1493 | | |
1494 | 0 | data = "/"; |
1495 | 0 | size = 2; |
1496 | 0 | break; |
1497 | | |
1498 | 0 | case G_VARIANT_CLASS_SIGNATURE: |
1499 | 0 | if (g_variant_serialiser_is_signature (data, size)) |
1500 | 0 | break; |
1501 | | |
1502 | 0 | data = ""; |
1503 | 0 | size = 1; |
1504 | 0 | break; |
1505 | | |
1506 | 0 | default: |
1507 | 0 | g_assert_not_reached (); |
1508 | 0 | } |
1509 | 0 | } |
1510 | | |
1511 | 0 | if (length) |
1512 | 0 | *length = size - 1; |
1513 | |
|
1514 | 0 | return data; |
1515 | 0 | } |
1516 | | |
1517 | | /** |
1518 | | * g_variant_dup_string: |
1519 | | * @value: a string #GVariant instance |
1520 | | * @length: (out): a pointer to a #gsize, to store the length |
1521 | | * |
1522 | | * Similar to g_variant_get_string() except that instead of returning |
1523 | | * a constant string, the string is duplicated. |
1524 | | * |
1525 | | * The string will always be UTF-8 encoded. |
1526 | | * |
1527 | | * The return value must be freed using g_free(). |
1528 | | * |
1529 | | * Returns: (transfer full): a newly allocated string, UTF-8 encoded |
1530 | | * |
1531 | | * Since: 2.24 |
1532 | | **/ |
1533 | | gchar * |
1534 | | g_variant_dup_string (GVariant *value, |
1535 | | gsize *length) |
1536 | 0 | { |
1537 | 0 | return g_strdup (g_variant_get_string (value, length)); |
1538 | 0 | } |
1539 | | |
1540 | | /** |
1541 | | * g_variant_new_strv: |
1542 | | * @strv: (array length=length) (element-type utf8): an array of strings |
1543 | | * @length: the length of @strv, or -1 |
1544 | | * |
1545 | | * Constructs an array of strings #GVariant from the given array of |
1546 | | * strings. |
1547 | | * |
1548 | | * If @length is -1 then @strv is %NULL-terminated. |
1549 | | * |
1550 | | * Returns: (transfer none): a new floating #GVariant instance |
1551 | | * |
1552 | | * Since: 2.24 |
1553 | | **/ |
1554 | | GVariant * |
1555 | | g_variant_new_strv (const gchar * const *strv, |
1556 | | gssize length) |
1557 | 0 | { |
1558 | 0 | GVariant **strings; |
1559 | 0 | gsize i, length_unsigned; |
1560 | |
|
1561 | 0 | g_return_val_if_fail (length == 0 || strv != NULL, NULL); |
1562 | | |
1563 | 0 | if (length < 0) |
1564 | 0 | length = g_strv_length ((gchar **) strv); |
1565 | 0 | length_unsigned = length; |
1566 | |
|
1567 | 0 | strings = g_new (GVariant *, length_unsigned); |
1568 | 0 | for (i = 0; i < length_unsigned; i++) |
1569 | 0 | strings[i] = g_variant_ref_sink (g_variant_new_string (strv[i])); |
1570 | |
|
1571 | 0 | return g_variant_new_from_children (G_VARIANT_TYPE_STRING_ARRAY, |
1572 | 0 | strings, length_unsigned, TRUE); |
1573 | 0 | } |
1574 | | |
1575 | | /** |
1576 | | * g_variant_get_strv: |
1577 | | * @value: an array of strings #GVariant |
1578 | | * @length: (out) (optional): the length of the result, or %NULL |
1579 | | * |
1580 | | * Gets the contents of an array of strings #GVariant. This call |
1581 | | * makes a shallow copy; the return result should be released with |
1582 | | * g_free(), but the individual strings must not be modified. |
1583 | | * |
1584 | | * If @length is non-%NULL then the number of elements in the result |
1585 | | * is stored there. In any case, the resulting array will be |
1586 | | * %NULL-terminated. |
1587 | | * |
1588 | | * For an empty array, @length will be set to 0 and a pointer to a |
1589 | | * %NULL pointer will be returned. |
1590 | | * |
1591 | | * Returns: (array length=length zero-terminated=1) (transfer container): an array of constant strings |
1592 | | * |
1593 | | * Since: 2.24 |
1594 | | **/ |
1595 | | const gchar ** |
1596 | | g_variant_get_strv (GVariant *value, |
1597 | | gsize *length) |
1598 | 0 | { |
1599 | 0 | const gchar **strv; |
1600 | 0 | gsize n; |
1601 | 0 | gsize i; |
1602 | |
|
1603 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_STRING_ARRAY, NULL); |
1604 | |
|
1605 | 0 | g_variant_get_data (value); |
1606 | 0 | n = g_variant_n_children (value); |
1607 | 0 | strv = g_new (const gchar *, n + 1); |
1608 | |
|
1609 | 0 | for (i = 0; i < n; i++) |
1610 | 0 | { |
1611 | 0 | GVariant *string; |
1612 | |
|
1613 | 0 | string = g_variant_get_child_value (value, i); |
1614 | 0 | strv[i] = g_variant_get_string (string, NULL); |
1615 | 0 | g_variant_unref (string); |
1616 | 0 | } |
1617 | 0 | strv[i] = NULL; |
1618 | |
|
1619 | 0 | if (length) |
1620 | 0 | *length = n; |
1621 | |
|
1622 | 0 | return strv; |
1623 | 0 | } |
1624 | | |
1625 | | /** |
1626 | | * g_variant_dup_strv: |
1627 | | * @value: an array of strings #GVariant |
1628 | | * @length: (out) (optional): the length of the result, or %NULL |
1629 | | * |
1630 | | * Gets the contents of an array of strings #GVariant. This call |
1631 | | * makes a deep copy; the return result should be released with |
1632 | | * g_strfreev(). |
1633 | | * |
1634 | | * If @length is non-%NULL then the number of elements in the result |
1635 | | * is stored there. In any case, the resulting array will be |
1636 | | * %NULL-terminated. |
1637 | | * |
1638 | | * For an empty array, @length will be set to 0 and a pointer to a |
1639 | | * %NULL pointer will be returned. |
1640 | | * |
1641 | | * Returns: (array length=length zero-terminated=1) (transfer full): an array of strings |
1642 | | * |
1643 | | * Since: 2.24 |
1644 | | **/ |
1645 | | gchar ** |
1646 | | g_variant_dup_strv (GVariant *value, |
1647 | | gsize *length) |
1648 | 0 | { |
1649 | 0 | gchar **strv; |
1650 | 0 | gsize n; |
1651 | 0 | gsize i; |
1652 | |
|
1653 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_STRING_ARRAY, NULL); |
1654 | |
|
1655 | 0 | n = g_variant_n_children (value); |
1656 | 0 | strv = g_new (gchar *, n + 1); |
1657 | |
|
1658 | 0 | for (i = 0; i < n; i++) |
1659 | 0 | { |
1660 | 0 | GVariant *string; |
1661 | |
|
1662 | 0 | string = g_variant_get_child_value (value, i); |
1663 | 0 | strv[i] = g_variant_dup_string (string, NULL); |
1664 | 0 | g_variant_unref (string); |
1665 | 0 | } |
1666 | 0 | strv[i] = NULL; |
1667 | |
|
1668 | 0 | if (length) |
1669 | 0 | *length = n; |
1670 | |
|
1671 | 0 | return strv; |
1672 | 0 | } |
1673 | | |
1674 | | /** |
1675 | | * g_variant_new_objv: |
1676 | | * @strv: (array length=length) (element-type utf8): an array of strings |
1677 | | * @length: the length of @strv, or -1 |
1678 | | * |
1679 | | * Constructs an array of object paths #GVariant from the given array of |
1680 | | * strings. |
1681 | | * |
1682 | | * Each string must be a valid #GVariant object path; see |
1683 | | * g_variant_is_object_path(). |
1684 | | * |
1685 | | * If @length is -1 then @strv is %NULL-terminated. |
1686 | | * |
1687 | | * Returns: (transfer none): a new floating #GVariant instance |
1688 | | * |
1689 | | * Since: 2.30 |
1690 | | **/ |
1691 | | GVariant * |
1692 | | g_variant_new_objv (const gchar * const *strv, |
1693 | | gssize length) |
1694 | 0 | { |
1695 | 0 | GVariant **strings; |
1696 | 0 | gsize i, length_unsigned; |
1697 | |
|
1698 | 0 | g_return_val_if_fail (length == 0 || strv != NULL, NULL); |
1699 | | |
1700 | 0 | if (length < 0) |
1701 | 0 | length = g_strv_length ((gchar **) strv); |
1702 | 0 | length_unsigned = length; |
1703 | |
|
1704 | 0 | strings = g_new (GVariant *, length_unsigned); |
1705 | 0 | for (i = 0; i < length_unsigned; i++) |
1706 | 0 | strings[i] = g_variant_ref_sink (g_variant_new_object_path (strv[i])); |
1707 | |
|
1708 | 0 | return g_variant_new_from_children (G_VARIANT_TYPE_OBJECT_PATH_ARRAY, |
1709 | 0 | strings, length_unsigned, TRUE); |
1710 | 0 | } |
1711 | | |
1712 | | /** |
1713 | | * g_variant_get_objv: |
1714 | | * @value: an array of object paths #GVariant |
1715 | | * @length: (out) (optional): the length of the result, or %NULL |
1716 | | * |
1717 | | * Gets the contents of an array of object paths #GVariant. This call |
1718 | | * makes a shallow copy; the return result should be released with |
1719 | | * g_free(), but the individual strings must not be modified. |
1720 | | * |
1721 | | * If @length is non-%NULL then the number of elements in the result |
1722 | | * is stored there. In any case, the resulting array will be |
1723 | | * %NULL-terminated. |
1724 | | * |
1725 | | * For an empty array, @length will be set to 0 and a pointer to a |
1726 | | * %NULL pointer will be returned. |
1727 | | * |
1728 | | * Returns: (array length=length zero-terminated=1) (transfer container): an array of constant strings |
1729 | | * |
1730 | | * Since: 2.30 |
1731 | | **/ |
1732 | | const gchar ** |
1733 | | g_variant_get_objv (GVariant *value, |
1734 | | gsize *length) |
1735 | 0 | { |
1736 | 0 | const gchar **strv; |
1737 | 0 | gsize n; |
1738 | 0 | gsize i; |
1739 | |
|
1740 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_OBJECT_PATH_ARRAY, NULL); |
1741 | |
|
1742 | 0 | g_variant_get_data (value); |
1743 | 0 | n = g_variant_n_children (value); |
1744 | 0 | strv = g_new (const gchar *, n + 1); |
1745 | |
|
1746 | 0 | for (i = 0; i < n; i++) |
1747 | 0 | { |
1748 | 0 | GVariant *string; |
1749 | |
|
1750 | 0 | string = g_variant_get_child_value (value, i); |
1751 | 0 | strv[i] = g_variant_get_string (string, NULL); |
1752 | 0 | g_variant_unref (string); |
1753 | 0 | } |
1754 | 0 | strv[i] = NULL; |
1755 | |
|
1756 | 0 | if (length) |
1757 | 0 | *length = n; |
1758 | |
|
1759 | 0 | return strv; |
1760 | 0 | } |
1761 | | |
1762 | | /** |
1763 | | * g_variant_dup_objv: |
1764 | | * @value: an array of object paths #GVariant |
1765 | | * @length: (out) (optional): the length of the result, or %NULL |
1766 | | * |
1767 | | * Gets the contents of an array of object paths #GVariant. This call |
1768 | | * makes a deep copy; the return result should be released with |
1769 | | * g_strfreev(). |
1770 | | * |
1771 | | * If @length is non-%NULL then the number of elements in the result |
1772 | | * is stored there. In any case, the resulting array will be |
1773 | | * %NULL-terminated. |
1774 | | * |
1775 | | * For an empty array, @length will be set to 0 and a pointer to a |
1776 | | * %NULL pointer will be returned. |
1777 | | * |
1778 | | * Returns: (array length=length zero-terminated=1) (transfer full): an array of strings |
1779 | | * |
1780 | | * Since: 2.30 |
1781 | | **/ |
1782 | | gchar ** |
1783 | | g_variant_dup_objv (GVariant *value, |
1784 | | gsize *length) |
1785 | 0 | { |
1786 | 0 | gchar **strv; |
1787 | 0 | gsize n; |
1788 | 0 | gsize i; |
1789 | |
|
1790 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_OBJECT_PATH_ARRAY, NULL); |
1791 | |
|
1792 | 0 | n = g_variant_n_children (value); |
1793 | 0 | strv = g_new (gchar *, n + 1); |
1794 | |
|
1795 | 0 | for (i = 0; i < n; i++) |
1796 | 0 | { |
1797 | 0 | GVariant *string; |
1798 | |
|
1799 | 0 | string = g_variant_get_child_value (value, i); |
1800 | 0 | strv[i] = g_variant_dup_string (string, NULL); |
1801 | 0 | g_variant_unref (string); |
1802 | 0 | } |
1803 | 0 | strv[i] = NULL; |
1804 | |
|
1805 | 0 | if (length) |
1806 | 0 | *length = n; |
1807 | |
|
1808 | 0 | return strv; |
1809 | 0 | } |
1810 | | |
1811 | | |
1812 | | /** |
1813 | | * g_variant_new_bytestring: |
1814 | | * @string: (array zero-terminated=1) (element-type guint8): a normal |
1815 | | * nul-terminated string in no particular encoding |
1816 | | * |
1817 | | * Creates an array-of-bytes #GVariant with the contents of @string. |
1818 | | * This function is just like g_variant_new_string() except that the |
1819 | | * string need not be valid UTF-8. |
1820 | | * |
1821 | | * The nul terminator character at the end of the string is stored in |
1822 | | * the array. |
1823 | | * |
1824 | | * Returns: (transfer none): a floating reference to a new bytestring #GVariant instance |
1825 | | * |
1826 | | * Since: 2.26 |
1827 | | **/ |
1828 | | GVariant * |
1829 | | g_variant_new_bytestring (const gchar *string) |
1830 | 0 | { |
1831 | 0 | g_return_val_if_fail (string != NULL, NULL); |
1832 | | |
1833 | 0 | return g_variant_new_from_trusted (G_VARIANT_TYPE_BYTESTRING, |
1834 | 0 | string, strlen (string) + 1); |
1835 | 0 | } |
1836 | | |
1837 | | /** |
1838 | | * g_variant_get_bytestring: |
1839 | | * @value: an array-of-bytes #GVariant instance |
1840 | | * |
1841 | | * Returns the string value of a #GVariant instance with an |
1842 | | * array-of-bytes type. The string has no particular encoding. |
1843 | | * |
1844 | | * If the array does not end with a nul terminator character, the empty |
1845 | | * string is returned. For this reason, you can always trust that a |
1846 | | * non-%NULL nul-terminated string will be returned by this function. |
1847 | | * |
1848 | | * If the array contains a nul terminator character somewhere other than |
1849 | | * the last byte then the returned string is the string, up to the first |
1850 | | * such nul character. |
1851 | | * |
1852 | | * g_variant_get_fixed_array() should be used instead if the array contains |
1853 | | * arbitrary data that could not be nul-terminated or could contain nul bytes. |
1854 | | * |
1855 | | * It is an error to call this function with a @value that is not an |
1856 | | * array of bytes. |
1857 | | * |
1858 | | * The return value remains valid as long as @value exists. |
1859 | | * |
1860 | | * Returns: (transfer none) (array zero-terminated=1) (element-type guint8): |
1861 | | * the constant string |
1862 | | * |
1863 | | * Since: 2.26 |
1864 | | **/ |
1865 | | const gchar * |
1866 | | g_variant_get_bytestring (GVariant *value) |
1867 | 0 | { |
1868 | 0 | const gchar *string; |
1869 | 0 | gsize size; |
1870 | |
|
1871 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_BYTESTRING, NULL); |
1872 | | |
1873 | | /* Won't be NULL since this is an array type */ |
1874 | 0 | string = g_variant_get_data (value); |
1875 | 0 | size = g_variant_get_size (value); |
1876 | |
|
1877 | 0 | if (size && string[size - 1] == '\0') |
1878 | 0 | return string; |
1879 | 0 | else |
1880 | 0 | return ""; |
1881 | 0 | } |
1882 | | |
1883 | | /** |
1884 | | * g_variant_dup_bytestring: |
1885 | | * @value: an array-of-bytes #GVariant instance |
1886 | | * @length: (out) (optional) (default NULL): a pointer to a #gsize, to store |
1887 | | * the length (not including the nul terminator) |
1888 | | * |
1889 | | * Similar to g_variant_get_bytestring() except that instead of |
1890 | | * returning a constant string, the string is duplicated. |
1891 | | * |
1892 | | * The return value must be freed using g_free(). |
1893 | | * |
1894 | | * Returns: (transfer full) (array zero-terminated=1 length=length) (element-type guint8): |
1895 | | * a newly allocated string |
1896 | | * |
1897 | | * Since: 2.26 |
1898 | | **/ |
1899 | | gchar * |
1900 | | g_variant_dup_bytestring (GVariant *value, |
1901 | | gsize *length) |
1902 | 0 | { |
1903 | 0 | const gchar *original = g_variant_get_bytestring (value); |
1904 | 0 | gsize size; |
1905 | | |
1906 | | /* don't crash in case get_bytestring() had an assert failure */ |
1907 | 0 | if (original == NULL) |
1908 | 0 | return NULL; |
1909 | | |
1910 | 0 | size = strlen (original); |
1911 | |
|
1912 | 0 | if (length) |
1913 | 0 | *length = size; |
1914 | |
|
1915 | 0 | return g_memdup2 (original, size + 1); |
1916 | 0 | } |
1917 | | |
1918 | | /** |
1919 | | * g_variant_new_bytestring_array: |
1920 | | * @strv: (array length=length): an array of strings |
1921 | | * @length: the length of @strv, or -1 |
1922 | | * |
1923 | | * Constructs an array of bytestring #GVariant from the given array of |
1924 | | * strings. |
1925 | | * |
1926 | | * If @length is -1 then @strv is %NULL-terminated. |
1927 | | * |
1928 | | * Returns: (transfer none): a new floating #GVariant instance |
1929 | | * |
1930 | | * Since: 2.26 |
1931 | | **/ |
1932 | | GVariant * |
1933 | | g_variant_new_bytestring_array (const gchar * const *strv, |
1934 | | gssize length) |
1935 | 0 | { |
1936 | 0 | GVariant **strings; |
1937 | 0 | gsize i, length_unsigned; |
1938 | |
|
1939 | 0 | g_return_val_if_fail (length == 0 || strv != NULL, NULL); |
1940 | | |
1941 | 0 | if (length < 0) |
1942 | 0 | length = g_strv_length ((gchar **) strv); |
1943 | 0 | length_unsigned = length; |
1944 | |
|
1945 | 0 | strings = g_new (GVariant *, length_unsigned); |
1946 | 0 | for (i = 0; i < length_unsigned; i++) |
1947 | 0 | strings[i] = g_variant_ref_sink (g_variant_new_bytestring (strv[i])); |
1948 | |
|
1949 | 0 | return g_variant_new_from_children (G_VARIANT_TYPE_BYTESTRING_ARRAY, |
1950 | 0 | strings, length_unsigned, TRUE); |
1951 | 0 | } |
1952 | | |
1953 | | /** |
1954 | | * g_variant_get_bytestring_array: |
1955 | | * @value: an array of array of bytes #GVariant ('aay') |
1956 | | * @length: (out) (optional): the length of the result, or %NULL |
1957 | | * |
1958 | | * Gets the contents of an array of array of bytes #GVariant. This call |
1959 | | * makes a shallow copy; the return result should be released with |
1960 | | * g_free(), but the individual strings must not be modified. |
1961 | | * |
1962 | | * If @length is non-%NULL then the number of elements in the result is |
1963 | | * stored there. In any case, the resulting array will be |
1964 | | * %NULL-terminated. |
1965 | | * |
1966 | | * For an empty array, @length will be set to 0 and a pointer to a |
1967 | | * %NULL pointer will be returned. |
1968 | | * |
1969 | | * Returns: (array length=length) (transfer container): an array of constant strings |
1970 | | * |
1971 | | * Since: 2.26 |
1972 | | **/ |
1973 | | const gchar ** |
1974 | | g_variant_get_bytestring_array (GVariant *value, |
1975 | | gsize *length) |
1976 | 0 | { |
1977 | 0 | const gchar **strv; |
1978 | 0 | gsize n; |
1979 | 0 | gsize i; |
1980 | |
|
1981 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL); |
1982 | |
|
1983 | 0 | g_variant_get_data (value); |
1984 | 0 | n = g_variant_n_children (value); |
1985 | 0 | strv = g_new (const gchar *, n + 1); |
1986 | |
|
1987 | 0 | for (i = 0; i < n; i++) |
1988 | 0 | { |
1989 | 0 | GVariant *string; |
1990 | |
|
1991 | 0 | string = g_variant_get_child_value (value, i); |
1992 | 0 | strv[i] = g_variant_get_bytestring (string); |
1993 | 0 | g_variant_unref (string); |
1994 | 0 | } |
1995 | 0 | strv[i] = NULL; |
1996 | |
|
1997 | 0 | if (length) |
1998 | 0 | *length = n; |
1999 | |
|
2000 | 0 | return strv; |
2001 | 0 | } |
2002 | | |
2003 | | /** |
2004 | | * g_variant_dup_bytestring_array: |
2005 | | * @value: an array of array of bytes #GVariant ('aay') |
2006 | | * @length: (out) (optional): the length of the result, or %NULL |
2007 | | * |
2008 | | * Gets the contents of an array of array of bytes #GVariant. This call |
2009 | | * makes a deep copy; the return result should be released with |
2010 | | * g_strfreev(). |
2011 | | * |
2012 | | * If @length is non-%NULL then the number of elements in the result is |
2013 | | * stored there. In any case, the resulting array will be |
2014 | | * %NULL-terminated. |
2015 | | * |
2016 | | * For an empty array, @length will be set to 0 and a pointer to a |
2017 | | * %NULL pointer will be returned. |
2018 | | * |
2019 | | * Returns: (array length=length) (transfer full): an array of strings |
2020 | | * |
2021 | | * Since: 2.26 |
2022 | | **/ |
2023 | | gchar ** |
2024 | | g_variant_dup_bytestring_array (GVariant *value, |
2025 | | gsize *length) |
2026 | 0 | { |
2027 | 0 | gchar **strv; |
2028 | 0 | gsize n; |
2029 | 0 | gsize i; |
2030 | |
|
2031 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL); |
2032 | |
|
2033 | 0 | g_variant_get_data (value); |
2034 | 0 | n = g_variant_n_children (value); |
2035 | 0 | strv = g_new (gchar *, n + 1); |
2036 | |
|
2037 | 0 | for (i = 0; i < n; i++) |
2038 | 0 | { |
2039 | 0 | GVariant *string; |
2040 | |
|
2041 | 0 | string = g_variant_get_child_value (value, i); |
2042 | 0 | strv[i] = g_variant_dup_bytestring (string, NULL); |
2043 | 0 | g_variant_unref (string); |
2044 | 0 | } |
2045 | 0 | strv[i] = NULL; |
2046 | |
|
2047 | 0 | if (length) |
2048 | 0 | *length = n; |
2049 | |
|
2050 | 0 | return strv; |
2051 | 0 | } |
2052 | | |
2053 | | /* Type checking and querying {{{1 */ |
2054 | | /** |
2055 | | * g_variant_get_type: |
2056 | | * @value: a #GVariant |
2057 | | * |
2058 | | * Determines the type of @value. |
2059 | | * |
2060 | | * The return value is valid for the lifetime of @value and must not |
2061 | | * be freed. |
2062 | | * |
2063 | | * Returns: a #GVariantType |
2064 | | * |
2065 | | * Since: 2.24 |
2066 | | **/ |
2067 | | const GVariantType * |
2068 | | g_variant_get_type (GVariant *value) |
2069 | 0 | { |
2070 | 0 | GVariantTypeInfo *type_info; |
2071 | |
|
2072 | 0 | g_return_val_if_fail (value != NULL, NULL); |
2073 | | |
2074 | 0 | type_info = g_variant_get_type_info (value); |
2075 | |
|
2076 | 0 | return (GVariantType *) g_variant_type_info_get_type_string (type_info); |
2077 | 0 | } |
2078 | | |
2079 | | /** |
2080 | | * g_variant_get_type_string: |
2081 | | * @value: a #GVariant |
2082 | | * |
2083 | | * Returns the type string of @value. Unlike the result of calling |
2084 | | * g_variant_type_peek_string(), this string is nul-terminated. This |
2085 | | * string belongs to #GVariant and must not be freed. |
2086 | | * |
2087 | | * Returns: the type string for the type of @value |
2088 | | * |
2089 | | * Since: 2.24 |
2090 | | **/ |
2091 | | const gchar * |
2092 | | g_variant_get_type_string (GVariant *value) |
2093 | 0 | { |
2094 | 0 | GVariantTypeInfo *type_info; |
2095 | |
|
2096 | 0 | g_return_val_if_fail (value != NULL, NULL); |
2097 | | |
2098 | 0 | type_info = g_variant_get_type_info (value); |
2099 | |
|
2100 | 0 | return g_variant_type_info_get_type_string (type_info); |
2101 | 0 | } |
2102 | | |
2103 | | /** |
2104 | | * g_variant_is_of_type: |
2105 | | * @value: a #GVariant instance |
2106 | | * @type: a #GVariantType |
2107 | | * |
2108 | | * Checks if a value has a type matching the provided type. |
2109 | | * |
2110 | | * Returns: %TRUE if the type of @value matches @type |
2111 | | * |
2112 | | * Since: 2.24 |
2113 | | **/ |
2114 | | gboolean |
2115 | | g_variant_is_of_type (GVariant *value, |
2116 | | const GVariantType *type) |
2117 | 0 | { |
2118 | 0 | return g_variant_type_is_subtype_of (g_variant_get_type (value), type); |
2119 | 0 | } |
2120 | | |
2121 | | /** |
2122 | | * g_variant_is_container: |
2123 | | * @value: a #GVariant instance |
2124 | | * |
2125 | | * Checks if @value is a container. |
2126 | | * |
2127 | | * Returns: %TRUE if @value is a container |
2128 | | * |
2129 | | * Since: 2.24 |
2130 | | */ |
2131 | | gboolean |
2132 | | g_variant_is_container (GVariant *value) |
2133 | 0 | { |
2134 | 0 | return g_variant_type_is_container (g_variant_get_type (value)); |
2135 | 0 | } |
2136 | | |
2137 | | |
2138 | | /** |
2139 | | * g_variant_classify: |
2140 | | * @value: a #GVariant |
2141 | | * |
2142 | | * Classifies @value according to its top-level type. |
2143 | | * |
2144 | | * Returns: the #GVariantClass of @value |
2145 | | * |
2146 | | * Since: 2.24 |
2147 | | **/ |
2148 | | /** |
2149 | | * GVariantClass: |
2150 | | * @G_VARIANT_CLASS_BOOLEAN: The #GVariant is a boolean. |
2151 | | * @G_VARIANT_CLASS_BYTE: The #GVariant is a byte. |
2152 | | * @G_VARIANT_CLASS_INT16: The #GVariant is a signed 16 bit integer. |
2153 | | * @G_VARIANT_CLASS_UINT16: The #GVariant is an unsigned 16 bit integer. |
2154 | | * @G_VARIANT_CLASS_INT32: The #GVariant is a signed 32 bit integer. |
2155 | | * @G_VARIANT_CLASS_UINT32: The #GVariant is an unsigned 32 bit integer. |
2156 | | * @G_VARIANT_CLASS_INT64: The #GVariant is a signed 64 bit integer. |
2157 | | * @G_VARIANT_CLASS_UINT64: The #GVariant is an unsigned 64 bit integer. |
2158 | | * @G_VARIANT_CLASS_HANDLE: The #GVariant is a file handle index. |
2159 | | * @G_VARIANT_CLASS_DOUBLE: The #GVariant is a double precision floating |
2160 | | * point value. |
2161 | | * @G_VARIANT_CLASS_STRING: The #GVariant is a normal string. |
2162 | | * @G_VARIANT_CLASS_OBJECT_PATH: The #GVariant is a D-Bus object path |
2163 | | * string. |
2164 | | * @G_VARIANT_CLASS_SIGNATURE: The #GVariant is a D-Bus signature string. |
2165 | | * @G_VARIANT_CLASS_VARIANT: The #GVariant is a variant. |
2166 | | * @G_VARIANT_CLASS_MAYBE: The #GVariant is a maybe-typed value. |
2167 | | * @G_VARIANT_CLASS_ARRAY: The #GVariant is an array. |
2168 | | * @G_VARIANT_CLASS_TUPLE: The #GVariant is a tuple. |
2169 | | * @G_VARIANT_CLASS_DICT_ENTRY: The #GVariant is a dictionary entry. |
2170 | | * |
2171 | | * The range of possible top-level types of #GVariant instances. |
2172 | | * |
2173 | | * Since: 2.24 |
2174 | | **/ |
2175 | | GVariantClass |
2176 | | g_variant_classify (GVariant *value) |
2177 | 0 | { |
2178 | 0 | g_return_val_if_fail (value != NULL, 0); |
2179 | | |
2180 | 0 | return *g_variant_get_type_string (value); |
2181 | 0 | } |
2182 | | |
2183 | | /* Pretty printer {{{1 */ |
2184 | | /* This function is not introspectable because if @string is NULL, |
2185 | | @returns is (transfer full), otherwise it is (transfer none), which |
2186 | | is not supported by GObjectIntrospection */ |
2187 | | /** |
2188 | | * g_variant_print_string: (skip) |
2189 | | * @value: a #GVariant |
2190 | | * @string: (nullable) (default NULL): a #GString, or %NULL |
2191 | | * @type_annotate: %TRUE if type information should be included in |
2192 | | * the output |
2193 | | * |
2194 | | * Behaves as g_variant_print(), but operates on a #GString. |
2195 | | * |
2196 | | * If @string is non-%NULL then it is appended to and returned. Else, |
2197 | | * a new empty #GString is allocated and it is returned. |
2198 | | * |
2199 | | * Returns: a #GString containing the string |
2200 | | * |
2201 | | * Since: 2.24 |
2202 | | **/ |
2203 | | GString * |
2204 | | g_variant_print_string (GVariant *value, |
2205 | | GString *string, |
2206 | | gboolean type_annotate) |
2207 | 0 | { |
2208 | 0 | if G_UNLIKELY (string == NULL) |
2209 | 0 | string = g_string_new (NULL); |
2210 | |
|
2211 | 0 | switch (g_variant_classify (value)) |
2212 | 0 | { |
2213 | 0 | case G_VARIANT_CLASS_MAYBE: |
2214 | 0 | if (type_annotate) |
2215 | 0 | g_string_append_printf (string, "@%s ", |
2216 | 0 | g_variant_get_type_string (value)); |
2217 | |
|
2218 | 0 | if (g_variant_n_children (value)) |
2219 | 0 | { |
2220 | 0 | gchar *printed_child; |
2221 | 0 | GVariant *element; |
2222 | | |
2223 | | /* Nested maybes: |
2224 | | * |
2225 | | * Consider the case of the type "mmi". In this case we could |
2226 | | * write "just just 4", but "4" alone is totally unambiguous, |
2227 | | * so we try to drop "just" where possible. |
2228 | | * |
2229 | | * We have to be careful not to always drop "just", though, |
2230 | | * since "nothing" needs to be distinguishable from "just |
2231 | | * nothing". The case where we need to ensure we keep the |
2232 | | * "just" is actually exactly the case where we have a nested |
2233 | | * Nothing. |
2234 | | * |
2235 | | * Instead of searching for that nested Nothing, we just print |
2236 | | * the contained value into a separate string and see if we |
2237 | | * end up with "nothing" at the end of it. If so, we need to |
2238 | | * add "just" at our level. |
2239 | | */ |
2240 | 0 | element = g_variant_get_child_value (value, 0); |
2241 | 0 | printed_child = g_variant_print (element, FALSE); |
2242 | 0 | g_variant_unref (element); |
2243 | |
|
2244 | 0 | if (g_str_has_suffix (printed_child, "nothing")) |
2245 | 0 | g_string_append (string, "just "); |
2246 | 0 | g_string_append (string, printed_child); |
2247 | 0 | g_free (printed_child); |
2248 | 0 | } |
2249 | 0 | else |
2250 | 0 | g_string_append (string, "nothing"); |
2251 | |
|
2252 | 0 | break; |
2253 | | |
2254 | 0 | case G_VARIANT_CLASS_ARRAY: |
2255 | | /* it's an array so the first character of the type string is 'a' |
2256 | | * |
2257 | | * if the first two characters are 'ay' then it's a bytestring. |
2258 | | * under certain conditions we print those as strings. |
2259 | | */ |
2260 | 0 | if (g_variant_get_type_string (value)[1] == 'y') |
2261 | 0 | { |
2262 | 0 | const gchar *str; |
2263 | 0 | gsize size; |
2264 | 0 | gsize i; |
2265 | | |
2266 | | /* first determine if it is a byte string. |
2267 | | * that's when there's a single nul character: at the end. |
2268 | | */ |
2269 | 0 | str = g_variant_get_data (value); |
2270 | 0 | size = g_variant_get_size (value); |
2271 | |
|
2272 | 0 | for (i = 0; i < size; i++) |
2273 | 0 | if (str[i] == '\0') |
2274 | 0 | break; |
2275 | | |
2276 | | /* first nul byte is the last byte -> it's a byte string. */ |
2277 | 0 | if (i == size - 1) |
2278 | 0 | { |
2279 | 0 | gchar *escaped = g_strescape (str, NULL); |
2280 | | |
2281 | | /* use double quotes only if a ' is in the string */ |
2282 | 0 | if (strchr (str, '\'')) |
2283 | 0 | g_string_append_printf (string, "b\"%s\"", escaped); |
2284 | 0 | else |
2285 | 0 | g_string_append_printf (string, "b'%s'", escaped); |
2286 | |
|
2287 | 0 | g_free (escaped); |
2288 | 0 | break; |
2289 | 0 | } |
2290 | | |
2291 | 0 | else |
2292 | 0 | { |
2293 | | /* fall through and handle normally... */ |
2294 | 0 | } |
2295 | 0 | } |
2296 | | |
2297 | | /* |
2298 | | * if the first two characters are 'a{' then it's an array of |
2299 | | * dictionary entries (ie: a dictionary) so we print that |
2300 | | * differently. |
2301 | | */ |
2302 | 0 | if (g_variant_get_type_string (value)[1] == '{') |
2303 | | /* dictionary */ |
2304 | 0 | { |
2305 | 0 | const gchar *comma = ""; |
2306 | 0 | gsize n, i; |
2307 | |
|
2308 | 0 | if ((n = g_variant_n_children (value)) == 0) |
2309 | 0 | { |
2310 | 0 | if (type_annotate) |
2311 | 0 | g_string_append_printf (string, "@%s ", |
2312 | 0 | g_variant_get_type_string (value)); |
2313 | 0 | g_string_append (string, "{}"); |
2314 | 0 | break; |
2315 | 0 | } |
2316 | | |
2317 | 0 | g_string_append_c (string, '{'); |
2318 | 0 | for (i = 0; i < n; i++) |
2319 | 0 | { |
2320 | 0 | GVariant *entry, *key, *val; |
2321 | |
|
2322 | 0 | g_string_append (string, comma); |
2323 | 0 | comma = ", "; |
2324 | |
|
2325 | 0 | entry = g_variant_get_child_value (value, i); |
2326 | 0 | key = g_variant_get_child_value (entry, 0); |
2327 | 0 | val = g_variant_get_child_value (entry, 1); |
2328 | 0 | g_variant_unref (entry); |
2329 | |
|
2330 | 0 | g_variant_print_string (key, string, type_annotate); |
2331 | 0 | g_variant_unref (key); |
2332 | 0 | g_string_append (string, ": "); |
2333 | 0 | g_variant_print_string (val, string, type_annotate); |
2334 | 0 | g_variant_unref (val); |
2335 | 0 | type_annotate = FALSE; |
2336 | 0 | } |
2337 | 0 | g_string_append_c (string, '}'); |
2338 | 0 | } |
2339 | 0 | else |
2340 | | /* normal (non-dictionary) array */ |
2341 | 0 | { |
2342 | 0 | const gchar *comma = ""; |
2343 | 0 | gsize n, i; |
2344 | |
|
2345 | 0 | if ((n = g_variant_n_children (value)) == 0) |
2346 | 0 | { |
2347 | 0 | if (type_annotate) |
2348 | 0 | g_string_append_printf (string, "@%s ", |
2349 | 0 | g_variant_get_type_string (value)); |
2350 | 0 | g_string_append (string, "[]"); |
2351 | 0 | break; |
2352 | 0 | } |
2353 | | |
2354 | 0 | g_string_append_c (string, '['); |
2355 | 0 | for (i = 0; i < n; i++) |
2356 | 0 | { |
2357 | 0 | GVariant *element; |
2358 | |
|
2359 | 0 | g_string_append (string, comma); |
2360 | 0 | comma = ", "; |
2361 | |
|
2362 | 0 | element = g_variant_get_child_value (value, i); |
2363 | |
|
2364 | 0 | g_variant_print_string (element, string, type_annotate); |
2365 | 0 | g_variant_unref (element); |
2366 | 0 | type_annotate = FALSE; |
2367 | 0 | } |
2368 | 0 | g_string_append_c (string, ']'); |
2369 | 0 | } |
2370 | | |
2371 | 0 | break; |
2372 | | |
2373 | 0 | case G_VARIANT_CLASS_TUPLE: |
2374 | 0 | { |
2375 | 0 | gsize n, i; |
2376 | |
|
2377 | 0 | n = g_variant_n_children (value); |
2378 | |
|
2379 | 0 | g_string_append_c (string, '('); |
2380 | 0 | for (i = 0; i < n; i++) |
2381 | 0 | { |
2382 | 0 | GVariant *element; |
2383 | |
|
2384 | 0 | element = g_variant_get_child_value (value, i); |
2385 | 0 | g_variant_print_string (element, string, type_annotate); |
2386 | 0 | g_string_append (string, ", "); |
2387 | 0 | g_variant_unref (element); |
2388 | 0 | } |
2389 | | |
2390 | | /* for >1 item: remove final ", " |
2391 | | * for 1 item: remove final " ", but leave the "," |
2392 | | * for 0 items: there is only "(", so remove nothing |
2393 | | */ |
2394 | 0 | g_string_truncate (string, string->len - (n > 0) - (n > 1)); |
2395 | 0 | g_string_append_c (string, ')'); |
2396 | 0 | } |
2397 | 0 | break; |
2398 | | |
2399 | 0 | case G_VARIANT_CLASS_DICT_ENTRY: |
2400 | 0 | { |
2401 | 0 | GVariant *element; |
2402 | |
|
2403 | 0 | g_string_append_c (string, '{'); |
2404 | |
|
2405 | 0 | element = g_variant_get_child_value (value, 0); |
2406 | 0 | g_variant_print_string (element, string, type_annotate); |
2407 | 0 | g_variant_unref (element); |
2408 | |
|
2409 | 0 | g_string_append (string, ", "); |
2410 | |
|
2411 | 0 | element = g_variant_get_child_value (value, 1); |
2412 | 0 | g_variant_print_string (element, string, type_annotate); |
2413 | 0 | g_variant_unref (element); |
2414 | |
|
2415 | 0 | g_string_append_c (string, '}'); |
2416 | 0 | } |
2417 | 0 | break; |
2418 | | |
2419 | 0 | case G_VARIANT_CLASS_VARIANT: |
2420 | 0 | { |
2421 | 0 | GVariant *child = g_variant_get_variant (value); |
2422 | | |
2423 | | /* Always annotate types in nested variants, because they are |
2424 | | * (by nature) of variable type. |
2425 | | */ |
2426 | 0 | g_string_append_c (string, '<'); |
2427 | 0 | g_variant_print_string (child, string, TRUE); |
2428 | 0 | g_string_append_c (string, '>'); |
2429 | |
|
2430 | 0 | g_variant_unref (child); |
2431 | 0 | } |
2432 | 0 | break; |
2433 | | |
2434 | 0 | case G_VARIANT_CLASS_BOOLEAN: |
2435 | 0 | if (g_variant_get_boolean (value)) |
2436 | 0 | g_string_append (string, "true"); |
2437 | 0 | else |
2438 | 0 | g_string_append (string, "false"); |
2439 | 0 | break; |
2440 | | |
2441 | 0 | case G_VARIANT_CLASS_STRING: |
2442 | 0 | { |
2443 | 0 | const gchar *str = g_variant_get_string (value, NULL); |
2444 | 0 | gunichar quote = strchr (str, '\'') ? '"' : '\''; |
2445 | |
|
2446 | 0 | g_string_append_c (string, quote); |
2447 | |
|
2448 | 0 | while (*str) |
2449 | 0 | { |
2450 | 0 | gunichar c = g_utf8_get_char (str); |
2451 | |
|
2452 | 0 | if (c == quote || c == '\\') |
2453 | 0 | g_string_append_c (string, '\\'); |
2454 | |
|
2455 | 0 | if (g_unichar_isprint (c)) |
2456 | 0 | g_string_append_unichar (string, c); |
2457 | | |
2458 | 0 | else |
2459 | 0 | { |
2460 | 0 | g_string_append_c (string, '\\'); |
2461 | 0 | if (c < 0x10000) |
2462 | 0 | switch (c) |
2463 | 0 | { |
2464 | 0 | case '\a': |
2465 | 0 | g_string_append_c (string, 'a'); |
2466 | 0 | break; |
2467 | | |
2468 | 0 | case '\b': |
2469 | 0 | g_string_append_c (string, 'b'); |
2470 | 0 | break; |
2471 | | |
2472 | 0 | case '\f': |
2473 | 0 | g_string_append_c (string, 'f'); |
2474 | 0 | break; |
2475 | | |
2476 | 0 | case '\n': |
2477 | 0 | g_string_append_c (string, 'n'); |
2478 | 0 | break; |
2479 | | |
2480 | 0 | case '\r': |
2481 | 0 | g_string_append_c (string, 'r'); |
2482 | 0 | break; |
2483 | | |
2484 | 0 | case '\t': |
2485 | 0 | g_string_append_c (string, 't'); |
2486 | 0 | break; |
2487 | | |
2488 | 0 | case '\v': |
2489 | 0 | g_string_append_c (string, 'v'); |
2490 | 0 | break; |
2491 | | |
2492 | 0 | default: |
2493 | 0 | g_string_append_printf (string, "u%04x", c); |
2494 | 0 | break; |
2495 | 0 | } |
2496 | 0 | else |
2497 | 0 | g_string_append_printf (string, "U%08x", c); |
2498 | 0 | } |
2499 | | |
2500 | 0 | str = g_utf8_next_char (str); |
2501 | 0 | } |
2502 | | |
2503 | 0 | g_string_append_c (string, quote); |
2504 | 0 | } |
2505 | 0 | break; |
2506 | | |
2507 | 0 | case G_VARIANT_CLASS_BYTE: |
2508 | 0 | if (type_annotate) |
2509 | 0 | g_string_append (string, "byte "); |
2510 | 0 | g_string_append_printf (string, "0x%02x", |
2511 | 0 | g_variant_get_byte (value)); |
2512 | 0 | break; |
2513 | | |
2514 | 0 | case G_VARIANT_CLASS_INT16: |
2515 | 0 | if (type_annotate) |
2516 | 0 | g_string_append (string, "int16 "); |
2517 | 0 | g_string_append_printf (string, "%"G_GINT16_FORMAT, |
2518 | 0 | g_variant_get_int16 (value)); |
2519 | 0 | break; |
2520 | | |
2521 | 0 | case G_VARIANT_CLASS_UINT16: |
2522 | 0 | if (type_annotate) |
2523 | 0 | g_string_append (string, "uint16 "); |
2524 | 0 | g_string_append_printf (string, "%"G_GUINT16_FORMAT, |
2525 | 0 | g_variant_get_uint16 (value)); |
2526 | 0 | break; |
2527 | | |
2528 | 0 | case G_VARIANT_CLASS_INT32: |
2529 | | /* Never annotate this type because it is the default for numbers |
2530 | | * (and this is a *pretty* printer) |
2531 | | */ |
2532 | 0 | g_string_append_printf (string, "%"G_GINT32_FORMAT, |
2533 | 0 | g_variant_get_int32 (value)); |
2534 | 0 | break; |
2535 | | |
2536 | 0 | case G_VARIANT_CLASS_HANDLE: |
2537 | 0 | if (type_annotate) |
2538 | 0 | g_string_append (string, "handle "); |
2539 | 0 | g_string_append_printf (string, "%"G_GINT32_FORMAT, |
2540 | 0 | g_variant_get_handle (value)); |
2541 | 0 | break; |
2542 | | |
2543 | 0 | case G_VARIANT_CLASS_UINT32: |
2544 | 0 | if (type_annotate) |
2545 | 0 | g_string_append (string, "uint32 "); |
2546 | 0 | g_string_append_printf (string, "%"G_GUINT32_FORMAT, |
2547 | 0 | g_variant_get_uint32 (value)); |
2548 | 0 | break; |
2549 | | |
2550 | 0 | case G_VARIANT_CLASS_INT64: |
2551 | 0 | if (type_annotate) |
2552 | 0 | g_string_append (string, "int64 "); |
2553 | 0 | g_string_append_printf (string, "%"G_GINT64_FORMAT, |
2554 | 0 | g_variant_get_int64 (value)); |
2555 | 0 | break; |
2556 | | |
2557 | 0 | case G_VARIANT_CLASS_UINT64: |
2558 | 0 | if (type_annotate) |
2559 | 0 | g_string_append (string, "uint64 "); |
2560 | 0 | g_string_append_printf (string, "%"G_GUINT64_FORMAT, |
2561 | 0 | g_variant_get_uint64 (value)); |
2562 | 0 | break; |
2563 | | |
2564 | 0 | case G_VARIANT_CLASS_DOUBLE: |
2565 | 0 | { |
2566 | 0 | gchar buffer[100]; |
2567 | 0 | gint i; |
2568 | |
|
2569 | 0 | g_ascii_dtostr (buffer, sizeof buffer, g_variant_get_double (value)); |
2570 | |
|
2571 | 0 | for (i = 0; buffer[i]; i++) |
2572 | 0 | if (buffer[i] == '.' || buffer[i] == 'e' || |
2573 | 0 | buffer[i] == 'n' || buffer[i] == 'N') |
2574 | 0 | break; |
2575 | | |
2576 | | /* if there is no '.' or 'e' in the float then add one */ |
2577 | 0 | if (buffer[i] == '\0') |
2578 | 0 | { |
2579 | 0 | buffer[i++] = '.'; |
2580 | 0 | buffer[i++] = '0'; |
2581 | 0 | buffer[i++] = '\0'; |
2582 | 0 | } |
2583 | |
|
2584 | 0 | g_string_append (string, buffer); |
2585 | 0 | } |
2586 | 0 | break; |
2587 | | |
2588 | 0 | case G_VARIANT_CLASS_OBJECT_PATH: |
2589 | 0 | if (type_annotate) |
2590 | 0 | g_string_append (string, "objectpath "); |
2591 | 0 | g_string_append_printf (string, "\'%s\'", |
2592 | 0 | g_variant_get_string (value, NULL)); |
2593 | 0 | break; |
2594 | | |
2595 | 0 | case G_VARIANT_CLASS_SIGNATURE: |
2596 | 0 | if (type_annotate) |
2597 | 0 | g_string_append (string, "signature "); |
2598 | 0 | g_string_append_printf (string, "\'%s\'", |
2599 | 0 | g_variant_get_string (value, NULL)); |
2600 | 0 | break; |
2601 | | |
2602 | 0 | default: |
2603 | 0 | g_assert_not_reached (); |
2604 | 0 | } |
2605 | | |
2606 | 0 | return string; |
2607 | 0 | } |
2608 | | |
2609 | | /** |
2610 | | * g_variant_print: |
2611 | | * @value: a #GVariant |
2612 | | * @type_annotate: %TRUE if type information should be included in |
2613 | | * the output |
2614 | | * |
2615 | | * Pretty-prints @value in the format understood by g_variant_parse(). |
2616 | | * |
2617 | | * The format is described [here][gvariant-text]. |
2618 | | * |
2619 | | * If @type_annotate is %TRUE, then type information is included in |
2620 | | * the output. |
2621 | | * |
2622 | | * Returns: (transfer full): a newly-allocated string holding the result. |
2623 | | * |
2624 | | * Since: 2.24 |
2625 | | */ |
2626 | | gchar * |
2627 | | g_variant_print (GVariant *value, |
2628 | | gboolean type_annotate) |
2629 | 0 | { |
2630 | 0 | return g_string_free (g_variant_print_string (value, NULL, type_annotate), |
2631 | 0 | FALSE); |
2632 | 0 | } |
2633 | | |
2634 | | /* Hash, Equal, Compare {{{1 */ |
2635 | | /** |
2636 | | * g_variant_hash: |
2637 | | * @value: (type GVariant): a basic #GVariant value as a #gconstpointer |
2638 | | * |
2639 | | * Generates a hash value for a #GVariant instance. |
2640 | | * |
2641 | | * The output of this function is guaranteed to be the same for a given |
2642 | | * value only per-process. It may change between different processor |
2643 | | * architectures or even different versions of GLib. Do not use this |
2644 | | * function as a basis for building protocols or file formats. |
2645 | | * |
2646 | | * The type of @value is #gconstpointer only to allow use of this |
2647 | | * function with #GHashTable. @value must be a #GVariant. |
2648 | | * |
2649 | | * Returns: a hash value corresponding to @value |
2650 | | * |
2651 | | * Since: 2.24 |
2652 | | **/ |
2653 | | guint |
2654 | | g_variant_hash (gconstpointer value_) |
2655 | 0 | { |
2656 | 0 | GVariant *value = (GVariant *) value_; |
2657 | |
|
2658 | 0 | switch (g_variant_classify (value)) |
2659 | 0 | { |
2660 | 0 | case G_VARIANT_CLASS_STRING: |
2661 | 0 | case G_VARIANT_CLASS_OBJECT_PATH: |
2662 | 0 | case G_VARIANT_CLASS_SIGNATURE: |
2663 | 0 | return g_str_hash (g_variant_get_string (value, NULL)); |
2664 | | |
2665 | 0 | case G_VARIANT_CLASS_BOOLEAN: |
2666 | | /* this is a very odd thing to hash... */ |
2667 | 0 | return g_variant_get_boolean (value); |
2668 | | |
2669 | 0 | case G_VARIANT_CLASS_BYTE: |
2670 | 0 | return g_variant_get_byte (value); |
2671 | | |
2672 | 0 | case G_VARIANT_CLASS_INT16: |
2673 | 0 | case G_VARIANT_CLASS_UINT16: |
2674 | 0 | { |
2675 | 0 | const guint16 *ptr; |
2676 | |
|
2677 | 0 | ptr = g_variant_get_data (value); |
2678 | |
|
2679 | 0 | if (ptr) |
2680 | 0 | return *ptr; |
2681 | 0 | else |
2682 | 0 | return 0; |
2683 | 0 | } |
2684 | | |
2685 | 0 | case G_VARIANT_CLASS_INT32: |
2686 | 0 | case G_VARIANT_CLASS_UINT32: |
2687 | 0 | case G_VARIANT_CLASS_HANDLE: |
2688 | 0 | { |
2689 | 0 | const guint *ptr; |
2690 | |
|
2691 | 0 | ptr = g_variant_get_data (value); |
2692 | |
|
2693 | 0 | if (ptr) |
2694 | 0 | return *ptr; |
2695 | 0 | else |
2696 | 0 | return 0; |
2697 | 0 | } |
2698 | | |
2699 | 0 | case G_VARIANT_CLASS_INT64: |
2700 | 0 | case G_VARIANT_CLASS_UINT64: |
2701 | 0 | case G_VARIANT_CLASS_DOUBLE: |
2702 | | /* need a separate case for these guys because otherwise |
2703 | | * performance could be quite bad on big endian systems |
2704 | | */ |
2705 | 0 | { |
2706 | 0 | const guint *ptr; |
2707 | |
|
2708 | 0 | ptr = g_variant_get_data (value); |
2709 | |
|
2710 | 0 | if (ptr) |
2711 | 0 | return ptr[0] + ptr[1]; |
2712 | 0 | else |
2713 | 0 | return 0; |
2714 | 0 | } |
2715 | | |
2716 | 0 | default: |
2717 | 0 | g_return_val_if_fail (!g_variant_is_container (value), 0); |
2718 | 0 | g_assert_not_reached (); |
2719 | 0 | } |
2720 | 0 | } |
2721 | | |
2722 | | /** |
2723 | | * g_variant_equal: |
2724 | | * @one: (type GVariant): a #GVariant instance |
2725 | | * @two: (type GVariant): a #GVariant instance |
2726 | | * |
2727 | | * Checks if @one and @two have the same type and value. |
2728 | | * |
2729 | | * The types of @one and @two are #gconstpointer only to allow use of |
2730 | | * this function with #GHashTable. They must each be a #GVariant. |
2731 | | * |
2732 | | * Returns: %TRUE if @one and @two are equal |
2733 | | * |
2734 | | * Since: 2.24 |
2735 | | **/ |
2736 | | gboolean |
2737 | | g_variant_equal (gconstpointer one, |
2738 | | gconstpointer two) |
2739 | 0 | { |
2740 | 0 | gboolean equal; |
2741 | |
|
2742 | 0 | g_return_val_if_fail (one != NULL && two != NULL, FALSE); |
2743 | | |
2744 | 0 | if (g_variant_get_type_info ((GVariant *) one) != |
2745 | 0 | g_variant_get_type_info ((GVariant *) two)) |
2746 | 0 | return FALSE; |
2747 | | |
2748 | | /* if both values are trusted to be in their canonical serialised form |
2749 | | * then a simple memcmp() of their serialised data will answer the |
2750 | | * question. |
2751 | | * |
2752 | | * if not, then this might generate a false negative (since it is |
2753 | | * possible for two different byte sequences to represent the same |
2754 | | * value). for now we solve this by pretty-printing both values and |
2755 | | * comparing the result. |
2756 | | */ |
2757 | 0 | if (g_variant_is_trusted ((GVariant *) one) && |
2758 | 0 | g_variant_is_trusted ((GVariant *) two)) |
2759 | 0 | { |
2760 | 0 | gconstpointer data_one, data_two; |
2761 | 0 | gsize size_one, size_two; |
2762 | |
|
2763 | 0 | size_one = g_variant_get_size ((GVariant *) one); |
2764 | 0 | size_two = g_variant_get_size ((GVariant *) two); |
2765 | |
|
2766 | 0 | if (size_one != size_two) |
2767 | 0 | return FALSE; |
2768 | | |
2769 | 0 | data_one = g_variant_get_data ((GVariant *) one); |
2770 | 0 | data_two = g_variant_get_data ((GVariant *) two); |
2771 | |
|
2772 | 0 | if (size_one) |
2773 | 0 | equal = memcmp (data_one, data_two, size_one) == 0; |
2774 | 0 | else |
2775 | 0 | equal = TRUE; |
2776 | 0 | } |
2777 | 0 | else |
2778 | 0 | { |
2779 | 0 | gchar *strone, *strtwo; |
2780 | |
|
2781 | 0 | strone = g_variant_print ((GVariant *) one, FALSE); |
2782 | 0 | strtwo = g_variant_print ((GVariant *) two, FALSE); |
2783 | 0 | equal = strcmp (strone, strtwo) == 0; |
2784 | 0 | g_free (strone); |
2785 | 0 | g_free (strtwo); |
2786 | 0 | } |
2787 | | |
2788 | 0 | return equal; |
2789 | 0 | } |
2790 | | |
2791 | | /** |
2792 | | * g_variant_compare: |
2793 | | * @one: (type GVariant): a basic-typed #GVariant instance |
2794 | | * @two: (type GVariant): a #GVariant instance of the same type |
2795 | | * |
2796 | | * Compares @one and @two. |
2797 | | * |
2798 | | * The types of @one and @two are #gconstpointer only to allow use of |
2799 | | * this function with #GTree, #GPtrArray, etc. They must each be a |
2800 | | * #GVariant. |
2801 | | * |
2802 | | * Comparison is only defined for basic types (ie: booleans, numbers, |
2803 | | * strings). For booleans, %FALSE is less than %TRUE. Numbers are |
2804 | | * ordered in the usual way. Strings are in ASCII lexographical order. |
2805 | | * |
2806 | | * It is a programmer error to attempt to compare container values or |
2807 | | * two values that have types that are not exactly equal. For example, |
2808 | | * you cannot compare a 32-bit signed integer with a 32-bit unsigned |
2809 | | * integer. Also note that this function is not particularly |
2810 | | * well-behaved when it comes to comparison of doubles; in particular, |
2811 | | * the handling of incomparable values (ie: NaN) is undefined. |
2812 | | * |
2813 | | * If you only require an equality comparison, g_variant_equal() is more |
2814 | | * general. |
2815 | | * |
2816 | | * Returns: negative value if a < b; |
2817 | | * zero if a = b; |
2818 | | * positive value if a > b. |
2819 | | * |
2820 | | * Since: 2.26 |
2821 | | **/ |
2822 | | gint |
2823 | | g_variant_compare (gconstpointer one, |
2824 | | gconstpointer two) |
2825 | 0 | { |
2826 | 0 | GVariant *a = (GVariant *) one; |
2827 | 0 | GVariant *b = (GVariant *) two; |
2828 | |
|
2829 | 0 | g_return_val_if_fail (g_variant_classify (a) == g_variant_classify (b), 0); |
2830 | | |
2831 | 0 | switch (g_variant_classify (a)) |
2832 | 0 | { |
2833 | 0 | case G_VARIANT_CLASS_BOOLEAN: |
2834 | 0 | return g_variant_get_boolean (a) - |
2835 | 0 | g_variant_get_boolean (b); |
2836 | | |
2837 | 0 | case G_VARIANT_CLASS_BYTE: |
2838 | 0 | return ((gint) g_variant_get_byte (a)) - |
2839 | 0 | ((gint) g_variant_get_byte (b)); |
2840 | | |
2841 | 0 | case G_VARIANT_CLASS_INT16: |
2842 | 0 | return ((gint) g_variant_get_int16 (a)) - |
2843 | 0 | ((gint) g_variant_get_int16 (b)); |
2844 | | |
2845 | 0 | case G_VARIANT_CLASS_UINT16: |
2846 | 0 | return ((gint) g_variant_get_uint16 (a)) - |
2847 | 0 | ((gint) g_variant_get_uint16 (b)); |
2848 | | |
2849 | 0 | case G_VARIANT_CLASS_INT32: |
2850 | 0 | { |
2851 | 0 | gint32 a_val = g_variant_get_int32 (a); |
2852 | 0 | gint32 b_val = g_variant_get_int32 (b); |
2853 | |
|
2854 | 0 | return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1; |
2855 | 0 | } |
2856 | | |
2857 | 0 | case G_VARIANT_CLASS_UINT32: |
2858 | 0 | { |
2859 | 0 | guint32 a_val = g_variant_get_uint32 (a); |
2860 | 0 | guint32 b_val = g_variant_get_uint32 (b); |
2861 | |
|
2862 | 0 | return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1; |
2863 | 0 | } |
2864 | | |
2865 | 0 | case G_VARIANT_CLASS_INT64: |
2866 | 0 | { |
2867 | 0 | gint64 a_val = g_variant_get_int64 (a); |
2868 | 0 | gint64 b_val = g_variant_get_int64 (b); |
2869 | |
|
2870 | 0 | return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1; |
2871 | 0 | } |
2872 | | |
2873 | 0 | case G_VARIANT_CLASS_UINT64: |
2874 | 0 | { |
2875 | 0 | guint64 a_val = g_variant_get_uint64 (a); |
2876 | 0 | guint64 b_val = g_variant_get_uint64 (b); |
2877 | |
|
2878 | 0 | return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1; |
2879 | 0 | } |
2880 | | |
2881 | 0 | case G_VARIANT_CLASS_DOUBLE: |
2882 | 0 | { |
2883 | 0 | gdouble a_val = g_variant_get_double (a); |
2884 | 0 | gdouble b_val = g_variant_get_double (b); |
2885 | |
|
2886 | 0 | return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1; |
2887 | 0 | } |
2888 | | |
2889 | 0 | case G_VARIANT_CLASS_STRING: |
2890 | 0 | case G_VARIANT_CLASS_OBJECT_PATH: |
2891 | 0 | case G_VARIANT_CLASS_SIGNATURE: |
2892 | 0 | return strcmp (g_variant_get_string (a, NULL), |
2893 | 0 | g_variant_get_string (b, NULL)); |
2894 | | |
2895 | 0 | default: |
2896 | 0 | g_return_val_if_fail (!g_variant_is_container (a), 0); |
2897 | 0 | g_assert_not_reached (); |
2898 | 0 | } |
2899 | 0 | } |
2900 | | |
2901 | | /* GVariantIter {{{1 */ |
2902 | | /** |
2903 | | * GVariantIter: (skip) |
2904 | | * |
2905 | | * #GVariantIter is an opaque data structure and can only be accessed |
2906 | | * using the following functions. |
2907 | | **/ |
2908 | | struct stack_iter |
2909 | | { |
2910 | | GVariant *value; |
2911 | | gssize n, i; |
2912 | | |
2913 | | const gchar *loop_format; |
2914 | | |
2915 | | gsize padding[3]; |
2916 | | gsize magic; |
2917 | | }; |
2918 | | |
2919 | | G_STATIC_ASSERT (sizeof (struct stack_iter) <= sizeof (GVariantIter)); |
2920 | | |
2921 | | struct heap_iter |
2922 | | { |
2923 | | struct stack_iter iter; |
2924 | | |
2925 | | GVariant *value_ref; |
2926 | | gsize magic; |
2927 | | }; |
2928 | | |
2929 | 0 | #define GVSI(i) ((struct stack_iter *) (i)) |
2930 | 0 | #define GVHI(i) ((struct heap_iter *) (i)) |
2931 | 0 | #define GVSI_MAGIC ((gsize) 3579507750u) |
2932 | 0 | #define GVHI_MAGIC ((gsize) 1450270775u) |
2933 | | #define is_valid_iter(i) (i != NULL && \ |
2934 | | GVSI(i)->magic == GVSI_MAGIC) |
2935 | | #define is_valid_heap_iter(i) (is_valid_iter(i) && \ |
2936 | | GVHI(i)->magic == GVHI_MAGIC) |
2937 | | |
2938 | | /** |
2939 | | * g_variant_iter_new: |
2940 | | * @value: a container #GVariant |
2941 | | * |
2942 | | * Creates a heap-allocated #GVariantIter for iterating over the items |
2943 | | * in @value. |
2944 | | * |
2945 | | * Use g_variant_iter_free() to free the return value when you no longer |
2946 | | * need it. |
2947 | | * |
2948 | | * A reference is taken to @value and will be released only when |
2949 | | * g_variant_iter_free() is called. |
2950 | | * |
2951 | | * Returns: (transfer full): a new heap-allocated #GVariantIter |
2952 | | * |
2953 | | * Since: 2.24 |
2954 | | **/ |
2955 | | GVariantIter * |
2956 | | g_variant_iter_new (GVariant *value) |
2957 | 0 | { |
2958 | 0 | GVariantIter *iter; |
2959 | |
|
2960 | 0 | iter = (GVariantIter *) g_slice_new (struct heap_iter); |
2961 | 0 | GVHI(iter)->value_ref = g_variant_ref (value); |
2962 | 0 | GVHI(iter)->magic = GVHI_MAGIC; |
2963 | |
|
2964 | 0 | g_variant_iter_init (iter, value); |
2965 | |
|
2966 | 0 | return iter; |
2967 | 0 | } |
2968 | | |
2969 | | /** |
2970 | | * g_variant_iter_init: (skip) |
2971 | | * @iter: a pointer to a #GVariantIter |
2972 | | * @value: a container #GVariant |
2973 | | * |
2974 | | * Initialises (without allocating) a #GVariantIter. @iter may be |
2975 | | * completely uninitialised prior to this call; its old value is |
2976 | | * ignored. |
2977 | | * |
2978 | | * The iterator remains valid for as long as @value exists, and need not |
2979 | | * be freed in any way. |
2980 | | * |
2981 | | * Returns: the number of items in @value |
2982 | | * |
2983 | | * Since: 2.24 |
2984 | | **/ |
2985 | | gsize |
2986 | | g_variant_iter_init (GVariantIter *iter, |
2987 | | GVariant *value) |
2988 | 0 | { |
2989 | 0 | GVSI(iter)->magic = GVSI_MAGIC; |
2990 | 0 | GVSI(iter)->value = value; |
2991 | 0 | GVSI(iter)->n = g_variant_n_children (value); |
2992 | 0 | GVSI(iter)->i = -1; |
2993 | 0 | GVSI(iter)->loop_format = NULL; |
2994 | |
|
2995 | 0 | return GVSI(iter)->n; |
2996 | 0 | } |
2997 | | |
2998 | | /** |
2999 | | * g_variant_iter_copy: |
3000 | | * @iter: a #GVariantIter |
3001 | | * |
3002 | | * Creates a new heap-allocated #GVariantIter to iterate over the |
3003 | | * container that was being iterated over by @iter. Iteration begins on |
3004 | | * the new iterator from the current position of the old iterator but |
3005 | | * the two copies are independent past that point. |
3006 | | * |
3007 | | * Use g_variant_iter_free() to free the return value when you no longer |
3008 | | * need it. |
3009 | | * |
3010 | | * A reference is taken to the container that @iter is iterating over |
3011 | | * and will be related only when g_variant_iter_free() is called. |
3012 | | * |
3013 | | * Returns: (transfer full): a new heap-allocated #GVariantIter |
3014 | | * |
3015 | | * Since: 2.24 |
3016 | | **/ |
3017 | | GVariantIter * |
3018 | | g_variant_iter_copy (GVariantIter *iter) |
3019 | 0 | { |
3020 | 0 | GVariantIter *copy; |
3021 | |
|
3022 | 0 | g_return_val_if_fail (is_valid_iter (iter), 0); |
3023 | | |
3024 | 0 | copy = g_variant_iter_new (GVSI(iter)->value); |
3025 | 0 | GVSI(copy)->i = GVSI(iter)->i; |
3026 | |
|
3027 | 0 | return copy; |
3028 | 0 | } |
3029 | | |
3030 | | /** |
3031 | | * g_variant_iter_n_children: |
3032 | | * @iter: a #GVariantIter |
3033 | | * |
3034 | | * Queries the number of child items in the container that we are |
3035 | | * iterating over. This is the total number of items -- not the number |
3036 | | * of items remaining. |
3037 | | * |
3038 | | * This function might be useful for preallocation of arrays. |
3039 | | * |
3040 | | * Returns: the number of children in the container |
3041 | | * |
3042 | | * Since: 2.24 |
3043 | | **/ |
3044 | | gsize |
3045 | | g_variant_iter_n_children (GVariantIter *iter) |
3046 | 0 | { |
3047 | 0 | g_return_val_if_fail (is_valid_iter (iter), 0); |
3048 | | |
3049 | 0 | return GVSI(iter)->n; |
3050 | 0 | } |
3051 | | |
3052 | | /** |
3053 | | * g_variant_iter_free: |
3054 | | * @iter: (transfer full): a heap-allocated #GVariantIter |
3055 | | * |
3056 | | * Frees a heap-allocated #GVariantIter. Only call this function on |
3057 | | * iterators that were returned by g_variant_iter_new() or |
3058 | | * g_variant_iter_copy(). |
3059 | | * |
3060 | | * Since: 2.24 |
3061 | | **/ |
3062 | | void |
3063 | | g_variant_iter_free (GVariantIter *iter) |
3064 | 0 | { |
3065 | 0 | g_return_if_fail (is_valid_heap_iter (iter)); |
3066 | | |
3067 | 0 | g_variant_unref (GVHI(iter)->value_ref); |
3068 | 0 | GVHI(iter)->magic = 0; |
3069 | |
|
3070 | 0 | g_slice_free (struct heap_iter, GVHI(iter)); |
3071 | 0 | } |
3072 | | |
3073 | | /** |
3074 | | * g_variant_iter_next_value: |
3075 | | * @iter: a #GVariantIter |
3076 | | * |
3077 | | * Gets the next item in the container. If no more items remain then |
3078 | | * %NULL is returned. |
3079 | | * |
3080 | | * Use g_variant_unref() to drop your reference on the return value when |
3081 | | * you no longer need it. |
3082 | | * |
3083 | | * Here is an example for iterating with g_variant_iter_next_value(): |
3084 | | * |[<!-- language="C" --> |
3085 | | * // recursively iterate a container |
3086 | | * void |
3087 | | * iterate_container_recursive (GVariant *container) |
3088 | | * { |
3089 | | * GVariantIter iter; |
3090 | | * GVariant *child; |
3091 | | * |
3092 | | * g_variant_iter_init (&iter, container); |
3093 | | * while ((child = g_variant_iter_next_value (&iter))) |
3094 | | * { |
3095 | | * g_print ("type '%s'\n", g_variant_get_type_string (child)); |
3096 | | * |
3097 | | * if (g_variant_is_container (child)) |
3098 | | * iterate_container_recursive (child); |
3099 | | * |
3100 | | * g_variant_unref (child); |
3101 | | * } |
3102 | | * } |
3103 | | * ]| |
3104 | | * |
3105 | | * Returns: (nullable) (transfer full): a #GVariant, or %NULL |
3106 | | * |
3107 | | * Since: 2.24 |
3108 | | **/ |
3109 | | GVariant * |
3110 | | g_variant_iter_next_value (GVariantIter *iter) |
3111 | 0 | { |
3112 | 0 | g_return_val_if_fail (is_valid_iter (iter), FALSE); |
3113 | | |
3114 | 0 | if G_UNLIKELY (GVSI(iter)->i >= GVSI(iter)->n) |
3115 | 0 | { |
3116 | 0 | g_critical ("g_variant_iter_next_value: must not be called again " |
3117 | 0 | "after NULL has already been returned."); |
3118 | 0 | return NULL; |
3119 | 0 | } |
3120 | | |
3121 | 0 | GVSI(iter)->i++; |
3122 | |
|
3123 | 0 | if (GVSI(iter)->i < GVSI(iter)->n) |
3124 | 0 | return g_variant_get_child_value (GVSI(iter)->value, GVSI(iter)->i); |
3125 | | |
3126 | 0 | return NULL; |
3127 | 0 | } |
3128 | | |
3129 | | /* GVariantBuilder {{{1 */ |
3130 | | /** |
3131 | | * GVariantBuilder: |
3132 | | * |
3133 | | * A utility type for constructing container-type #GVariant instances. |
3134 | | * |
3135 | | * This is an opaque structure and may only be accessed using the |
3136 | | * following functions. |
3137 | | * |
3138 | | * #GVariantBuilder is not threadsafe in any way. Do not attempt to |
3139 | | * access it from more than one thread. |
3140 | | **/ |
3141 | | |
3142 | | struct stack_builder |
3143 | | { |
3144 | | GVariantBuilder *parent; |
3145 | | GVariantType *type; |
3146 | | |
3147 | | /* type constraint explicitly specified by 'type'. |
3148 | | * for tuple types, this moves along as we add more items. |
3149 | | */ |
3150 | | const GVariantType *expected_type; |
3151 | | |
3152 | | /* type constraint implied by previous array item. |
3153 | | */ |
3154 | | const GVariantType *prev_item_type; |
3155 | | |
3156 | | /* constraints on the number of children. max = -1 for unlimited. */ |
3157 | | gsize min_items; |
3158 | | gsize max_items; |
3159 | | |
3160 | | /* dynamically-growing pointer array */ |
3161 | | GVariant **children; |
3162 | | gsize allocated_children; |
3163 | | gsize offset; |
3164 | | |
3165 | | /* set to '1' if all items in the container will have the same type |
3166 | | * (ie: maybe, array, variant) '0' if not (ie: tuple, dict entry) |
3167 | | */ |
3168 | | guint uniform_item_types : 1; |
3169 | | |
3170 | | /* set to '1' initially and changed to '0' if an untrusted value is |
3171 | | * added |
3172 | | */ |
3173 | | guint trusted : 1; |
3174 | | |
3175 | | gsize magic; |
3176 | | }; |
3177 | | |
3178 | | G_STATIC_ASSERT (sizeof (struct stack_builder) <= sizeof (GVariantBuilder)); |
3179 | | |
3180 | | struct heap_builder |
3181 | | { |
3182 | | GVariantBuilder builder; |
3183 | | gsize magic; |
3184 | | |
3185 | | gint ref_count; |
3186 | | }; |
3187 | | |
3188 | 0 | #define GVSB(b) ((struct stack_builder *) (b)) |
3189 | 0 | #define GVHB(b) ((struct heap_builder *) (b)) |
3190 | 0 | #define GVSB_MAGIC ((gsize) 1033660112u) |
3191 | 0 | #define GVSB_MAGIC_PARTIAL ((gsize) 2942751021u) |
3192 | 0 | #define GVHB_MAGIC ((gsize) 3087242682u) |
3193 | 0 | #define is_valid_builder(b) (b != NULL && \ |
3194 | 0 | GVSB(b)->magic == GVSB_MAGIC) |
3195 | | #define is_valid_heap_builder(b) (GVHB(b)->magic == GVHB_MAGIC) |
3196 | | |
3197 | | /* Just to make sure that by adding a union to GVariantBuilder, we |
3198 | | * didn't accidentally change ABI. */ |
3199 | | G_STATIC_ASSERT (sizeof (GVariantBuilder) == sizeof (gsize[16])); |
3200 | | |
3201 | | static gboolean |
3202 | | ensure_valid_builder (GVariantBuilder *builder) |
3203 | 0 | { |
3204 | 0 | if (is_valid_builder (builder)) |
3205 | 0 | return TRUE; |
3206 | 0 | if (builder->u.s.partial_magic == GVSB_MAGIC_PARTIAL) |
3207 | 0 | { |
3208 | 0 | static GVariantBuilder cleared_builder; |
3209 | | |
3210 | | /* Make sure that only first two fields were set and the rest is |
3211 | | * zeroed to avoid messing up the builder that had parent |
3212 | | * address equal to GVSB_MAGIC_PARTIAL. */ |
3213 | 0 | if (memcmp (cleared_builder.u.s.y, builder->u.s.y, sizeof cleared_builder.u.s.y)) |
3214 | 0 | return FALSE; |
3215 | | |
3216 | 0 | g_variant_builder_init (builder, builder->u.s.type); |
3217 | 0 | } |
3218 | 0 | return is_valid_builder (builder); |
3219 | 0 | } |
3220 | | |
3221 | | /** |
3222 | | * g_variant_builder_new: |
3223 | | * @type: a container type |
3224 | | * |
3225 | | * Allocates and initialises a new #GVariantBuilder. |
3226 | | * |
3227 | | * You should call g_variant_builder_unref() on the return value when it |
3228 | | * is no longer needed. The memory will not be automatically freed by |
3229 | | * any other call. |
3230 | | * |
3231 | | * In most cases it is easier to place a #GVariantBuilder directly on |
3232 | | * the stack of the calling function and initialise it with |
3233 | | * g_variant_builder_init(). |
3234 | | * |
3235 | | * Returns: (transfer full): a #GVariantBuilder |
3236 | | * |
3237 | | * Since: 2.24 |
3238 | | **/ |
3239 | | GVariantBuilder * |
3240 | | g_variant_builder_new (const GVariantType *type) |
3241 | 0 | { |
3242 | 0 | GVariantBuilder *builder; |
3243 | |
|
3244 | 0 | builder = (GVariantBuilder *) g_slice_new (struct heap_builder); |
3245 | 0 | g_variant_builder_init (builder, type); |
3246 | 0 | GVHB(builder)->magic = GVHB_MAGIC; |
3247 | 0 | GVHB(builder)->ref_count = 1; |
3248 | |
|
3249 | 0 | return builder; |
3250 | 0 | } |
3251 | | |
3252 | | /** |
3253 | | * g_variant_builder_unref: |
3254 | | * @builder: (transfer full): a #GVariantBuilder allocated by g_variant_builder_new() |
3255 | | * |
3256 | | * Decreases the reference count on @builder. |
3257 | | * |
3258 | | * In the event that there are no more references, releases all memory |
3259 | | * associated with the #GVariantBuilder. |
3260 | | * |
3261 | | * Don't call this on stack-allocated #GVariantBuilder instances or bad |
3262 | | * things will happen. |
3263 | | * |
3264 | | * Since: 2.24 |
3265 | | **/ |
3266 | | void |
3267 | | g_variant_builder_unref (GVariantBuilder *builder) |
3268 | 0 | { |
3269 | 0 | g_return_if_fail (is_valid_heap_builder (builder)); |
3270 | | |
3271 | 0 | if (--GVHB(builder)->ref_count) |
3272 | 0 | return; |
3273 | | |
3274 | 0 | g_variant_builder_clear (builder); |
3275 | 0 | GVHB(builder)->magic = 0; |
3276 | |
|
3277 | 0 | g_slice_free (struct heap_builder, GVHB(builder)); |
3278 | 0 | } |
3279 | | |
3280 | | /** |
3281 | | * g_variant_builder_ref: |
3282 | | * @builder: a #GVariantBuilder allocated by g_variant_builder_new() |
3283 | | * |
3284 | | * Increases the reference count on @builder. |
3285 | | * |
3286 | | * Don't call this on stack-allocated #GVariantBuilder instances or bad |
3287 | | * things will happen. |
3288 | | * |
3289 | | * Returns: (transfer full): a new reference to @builder |
3290 | | * |
3291 | | * Since: 2.24 |
3292 | | **/ |
3293 | | GVariantBuilder * |
3294 | | g_variant_builder_ref (GVariantBuilder *builder) |
3295 | 0 | { |
3296 | 0 | g_return_val_if_fail (is_valid_heap_builder (builder), NULL); |
3297 | | |
3298 | 0 | GVHB(builder)->ref_count++; |
3299 | |
|
3300 | 0 | return builder; |
3301 | 0 | } |
3302 | | |
3303 | | /** |
3304 | | * g_variant_builder_clear: (skip) |
3305 | | * @builder: a #GVariantBuilder |
3306 | | * |
3307 | | * Releases all memory associated with a #GVariantBuilder without |
3308 | | * freeing the #GVariantBuilder structure itself. |
3309 | | * |
3310 | | * It typically only makes sense to do this on a stack-allocated |
3311 | | * #GVariantBuilder if you want to abort building the value part-way |
3312 | | * through. This function need not be called if you call |
3313 | | * g_variant_builder_end() and it also doesn't need to be called on |
3314 | | * builders allocated with g_variant_builder_new() (see |
3315 | | * g_variant_builder_unref() for that). |
3316 | | * |
3317 | | * This function leaves the #GVariantBuilder structure set to all-zeros. |
3318 | | * It is valid to call this function on either an initialised |
3319 | | * #GVariantBuilder or one that is set to all-zeros but it is not valid |
3320 | | * to call this function on uninitialised memory. |
3321 | | * |
3322 | | * Since: 2.24 |
3323 | | **/ |
3324 | | void |
3325 | | g_variant_builder_clear (GVariantBuilder *builder) |
3326 | 0 | { |
3327 | 0 | gsize i; |
3328 | |
|
3329 | 0 | if (GVSB(builder)->magic == 0) |
3330 | | /* all-zeros or partial case */ |
3331 | 0 | return; |
3332 | | |
3333 | 0 | g_return_if_fail (ensure_valid_builder (builder)); |
3334 | | |
3335 | 0 | g_variant_type_free (GVSB(builder)->type); |
3336 | |
|
3337 | 0 | for (i = 0; i < GVSB(builder)->offset; i++) |
3338 | 0 | g_variant_unref (GVSB(builder)->children[i]); |
3339 | |
|
3340 | 0 | g_free (GVSB(builder)->children); |
3341 | |
|
3342 | 0 | if (GVSB(builder)->parent) |
3343 | 0 | { |
3344 | 0 | g_variant_builder_clear (GVSB(builder)->parent); |
3345 | 0 | g_slice_free (GVariantBuilder, GVSB(builder)->parent); |
3346 | 0 | } |
3347 | |
|
3348 | 0 | memset (builder, 0, sizeof (GVariantBuilder)); |
3349 | 0 | } |
3350 | | |
3351 | | /** |
3352 | | * g_variant_builder_init: (skip) |
3353 | | * @builder: a #GVariantBuilder |
3354 | | * @type: a container type |
3355 | | * |
3356 | | * Initialises a #GVariantBuilder structure. |
3357 | | * |
3358 | | * @type must be non-%NULL. It specifies the type of container to |
3359 | | * construct. It can be an indefinite type such as |
3360 | | * %G_VARIANT_TYPE_ARRAY or a definite type such as "as" or "(ii)". |
3361 | | * Maybe, array, tuple, dictionary entry and variant-typed values may be |
3362 | | * constructed. |
3363 | | * |
3364 | | * After the builder is initialised, values are added using |
3365 | | * g_variant_builder_add_value() or g_variant_builder_add(). |
3366 | | * |
3367 | | * After all the child values are added, g_variant_builder_end() frees |
3368 | | * the memory associated with the builder and returns the #GVariant that |
3369 | | * was created. |
3370 | | * |
3371 | | * This function completely ignores the previous contents of @builder. |
3372 | | * On one hand this means that it is valid to pass in completely |
3373 | | * uninitialised memory. On the other hand, this means that if you are |
3374 | | * initialising over top of an existing #GVariantBuilder you need to |
3375 | | * first call g_variant_builder_clear() in order to avoid leaking |
3376 | | * memory. |
3377 | | * |
3378 | | * You must not call g_variant_builder_ref() or |
3379 | | * g_variant_builder_unref() on a #GVariantBuilder that was initialised |
3380 | | * with this function. If you ever pass a reference to a |
3381 | | * #GVariantBuilder outside of the control of your own code then you |
3382 | | * should assume that the person receiving that reference may try to use |
3383 | | * reference counting; you should use g_variant_builder_new() instead of |
3384 | | * this function. |
3385 | | * |
3386 | | * Since: 2.24 |
3387 | | **/ |
3388 | | void |
3389 | | g_variant_builder_init (GVariantBuilder *builder, |
3390 | | const GVariantType *type) |
3391 | 0 | { |
3392 | 0 | g_return_if_fail (type != NULL); |
3393 | 0 | g_return_if_fail (g_variant_type_is_container (type)); |
3394 | | |
3395 | 0 | memset (builder, 0, sizeof (GVariantBuilder)); |
3396 | |
|
3397 | 0 | GVSB(builder)->type = g_variant_type_copy (type); |
3398 | 0 | GVSB(builder)->magic = GVSB_MAGIC; |
3399 | 0 | GVSB(builder)->trusted = TRUE; |
3400 | |
|
3401 | 0 | switch (*(const gchar *) type) |
3402 | 0 | { |
3403 | 0 | case G_VARIANT_CLASS_VARIANT: |
3404 | 0 | GVSB(builder)->uniform_item_types = TRUE; |
3405 | 0 | GVSB(builder)->allocated_children = 1; |
3406 | 0 | GVSB(builder)->expected_type = NULL; |
3407 | 0 | GVSB(builder)->min_items = 1; |
3408 | 0 | GVSB(builder)->max_items = 1; |
3409 | 0 | break; |
3410 | | |
3411 | 0 | case G_VARIANT_CLASS_ARRAY: |
3412 | 0 | GVSB(builder)->uniform_item_types = TRUE; |
3413 | 0 | GVSB(builder)->allocated_children = 8; |
3414 | 0 | GVSB(builder)->expected_type = |
3415 | 0 | g_variant_type_element (GVSB(builder)->type); |
3416 | 0 | GVSB(builder)->min_items = 0; |
3417 | 0 | GVSB(builder)->max_items = -1; |
3418 | 0 | break; |
3419 | | |
3420 | 0 | case G_VARIANT_CLASS_MAYBE: |
3421 | 0 | GVSB(builder)->uniform_item_types = TRUE; |
3422 | 0 | GVSB(builder)->allocated_children = 1; |
3423 | 0 | GVSB(builder)->expected_type = |
3424 | 0 | g_variant_type_element (GVSB(builder)->type); |
3425 | 0 | GVSB(builder)->min_items = 0; |
3426 | 0 | GVSB(builder)->max_items = 1; |
3427 | 0 | break; |
3428 | | |
3429 | 0 | case G_VARIANT_CLASS_DICT_ENTRY: |
3430 | 0 | GVSB(builder)->uniform_item_types = FALSE; |
3431 | 0 | GVSB(builder)->allocated_children = 2; |
3432 | 0 | GVSB(builder)->expected_type = |
3433 | 0 | g_variant_type_key (GVSB(builder)->type); |
3434 | 0 | GVSB(builder)->min_items = 2; |
3435 | 0 | GVSB(builder)->max_items = 2; |
3436 | 0 | break; |
3437 | | |
3438 | 0 | case 'r': /* G_VARIANT_TYPE_TUPLE was given */ |
3439 | 0 | GVSB(builder)->uniform_item_types = FALSE; |
3440 | 0 | GVSB(builder)->allocated_children = 8; |
3441 | 0 | GVSB(builder)->expected_type = NULL; |
3442 | 0 | GVSB(builder)->min_items = 0; |
3443 | 0 | GVSB(builder)->max_items = -1; |
3444 | 0 | break; |
3445 | | |
3446 | 0 | case G_VARIANT_CLASS_TUPLE: /* a definite tuple type was given */ |
3447 | 0 | GVSB(builder)->allocated_children = g_variant_type_n_items (type); |
3448 | 0 | GVSB(builder)->expected_type = |
3449 | 0 | g_variant_type_first (GVSB(builder)->type); |
3450 | 0 | GVSB(builder)->min_items = GVSB(builder)->allocated_children; |
3451 | 0 | GVSB(builder)->max_items = GVSB(builder)->allocated_children; |
3452 | 0 | GVSB(builder)->uniform_item_types = FALSE; |
3453 | 0 | break; |
3454 | | |
3455 | 0 | default: |
3456 | 0 | g_assert_not_reached (); |
3457 | 0 | } |
3458 | | |
3459 | 0 | GVSB(builder)->children = g_new (GVariant *, |
3460 | 0 | GVSB(builder)->allocated_children); |
3461 | 0 | } |
3462 | | |
3463 | | static void |
3464 | | g_variant_builder_make_room (struct stack_builder *builder) |
3465 | 0 | { |
3466 | 0 | if (builder->offset == builder->allocated_children) |
3467 | 0 | { |
3468 | 0 | builder->allocated_children *= 2; |
3469 | 0 | builder->children = g_renew (GVariant *, builder->children, |
3470 | 0 | builder->allocated_children); |
3471 | 0 | } |
3472 | 0 | } |
3473 | | |
3474 | | /** |
3475 | | * g_variant_builder_add_value: |
3476 | | * @builder: a #GVariantBuilder |
3477 | | * @value: a #GVariant |
3478 | | * |
3479 | | * Adds @value to @builder. |
3480 | | * |
3481 | | * It is an error to call this function in any way that would create an |
3482 | | * inconsistent value to be constructed. Some examples of this are |
3483 | | * putting different types of items into an array, putting the wrong |
3484 | | * types or number of items in a tuple, putting more than one value into |
3485 | | * a variant, etc. |
3486 | | * |
3487 | | * If @value is a floating reference (see g_variant_ref_sink()), |
3488 | | * the @builder instance takes ownership of @value. |
3489 | | * |
3490 | | * Since: 2.24 |
3491 | | **/ |
3492 | | void |
3493 | | g_variant_builder_add_value (GVariantBuilder *builder, |
3494 | | GVariant *value) |
3495 | 0 | { |
3496 | 0 | g_return_if_fail (ensure_valid_builder (builder)); |
3497 | 0 | g_return_if_fail (GVSB(builder)->offset < GVSB(builder)->max_items); |
3498 | 0 | g_return_if_fail (!GVSB(builder)->expected_type || |
3499 | 0 | g_variant_is_of_type (value, |
3500 | 0 | GVSB(builder)->expected_type)); |
3501 | 0 | g_return_if_fail (!GVSB(builder)->prev_item_type || |
3502 | 0 | g_variant_is_of_type (value, |
3503 | 0 | GVSB(builder)->prev_item_type)); |
3504 | | |
3505 | 0 | GVSB(builder)->trusted &= g_variant_is_trusted (value); |
3506 | |
|
3507 | 0 | if (!GVSB(builder)->uniform_item_types) |
3508 | 0 | { |
3509 | | /* advance our expected type pointers */ |
3510 | 0 | if (GVSB(builder)->expected_type) |
3511 | 0 | GVSB(builder)->expected_type = |
3512 | 0 | g_variant_type_next (GVSB(builder)->expected_type); |
3513 | |
|
3514 | 0 | if (GVSB(builder)->prev_item_type) |
3515 | 0 | GVSB(builder)->prev_item_type = |
3516 | 0 | g_variant_type_next (GVSB(builder)->prev_item_type); |
3517 | 0 | } |
3518 | 0 | else |
3519 | 0 | GVSB(builder)->prev_item_type = g_variant_get_type (value); |
3520 | |
|
3521 | 0 | g_variant_builder_make_room (GVSB(builder)); |
3522 | |
|
3523 | 0 | GVSB(builder)->children[GVSB(builder)->offset++] = |
3524 | 0 | g_variant_ref_sink (value); |
3525 | 0 | } |
3526 | | |
3527 | | /** |
3528 | | * g_variant_builder_open: |
3529 | | * @builder: a #GVariantBuilder |
3530 | | * @type: the #GVariantType of the container |
3531 | | * |
3532 | | * Opens a subcontainer inside the given @builder. When done adding |
3533 | | * items to the subcontainer, g_variant_builder_close() must be called. @type |
3534 | | * is the type of the container: so to build a tuple of several values, @type |
3535 | | * must include the tuple itself. |
3536 | | * |
3537 | | * It is an error to call this function in any way that would cause an |
3538 | | * inconsistent value to be constructed (ie: adding too many values or |
3539 | | * a value of an incorrect type). |
3540 | | * |
3541 | | * Example of building a nested variant: |
3542 | | * |[<!-- language="C" --> |
3543 | | * GVariantBuilder builder; |
3544 | | * guint32 some_number = get_number (); |
3545 | | * g_autoptr (GHashTable) some_dict = get_dict (); |
3546 | | * GHashTableIter iter; |
3547 | | * const gchar *key; |
3548 | | * const GVariant *value; |
3549 | | * g_autoptr (GVariant) output = NULL; |
3550 | | * |
3551 | | * g_variant_builder_init (&builder, G_VARIANT_TYPE ("(ua{sv})")); |
3552 | | * g_variant_builder_add (&builder, "u", some_number); |
3553 | | * g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}")); |
3554 | | * |
3555 | | * g_hash_table_iter_init (&iter, some_dict); |
3556 | | * while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value)) |
3557 | | * { |
3558 | | * g_variant_builder_open (&builder, G_VARIANT_TYPE ("{sv}")); |
3559 | | * g_variant_builder_add (&builder, "s", key); |
3560 | | * g_variant_builder_add (&builder, "v", value); |
3561 | | * g_variant_builder_close (&builder); |
3562 | | * } |
3563 | | * |
3564 | | * g_variant_builder_close (&builder); |
3565 | | * |
3566 | | * output = g_variant_builder_end (&builder); |
3567 | | * ]| |
3568 | | * |
3569 | | * Since: 2.24 |
3570 | | **/ |
3571 | | void |
3572 | | g_variant_builder_open (GVariantBuilder *builder, |
3573 | | const GVariantType *type) |
3574 | 0 | { |
3575 | 0 | GVariantBuilder *parent; |
3576 | |
|
3577 | 0 | g_return_if_fail (ensure_valid_builder (builder)); |
3578 | 0 | g_return_if_fail (GVSB(builder)->offset < GVSB(builder)->max_items); |
3579 | 0 | g_return_if_fail (!GVSB(builder)->expected_type || |
3580 | 0 | g_variant_type_is_subtype_of (type, |
3581 | 0 | GVSB(builder)->expected_type)); |
3582 | 0 | g_return_if_fail (!GVSB(builder)->prev_item_type || |
3583 | 0 | g_variant_type_is_subtype_of (GVSB(builder)->prev_item_type, |
3584 | 0 | type)); |
3585 | | |
3586 | 0 | parent = g_slice_dup (GVariantBuilder, builder); |
3587 | 0 | g_variant_builder_init (builder, type); |
3588 | 0 | GVSB(builder)->parent = parent; |
3589 | | |
3590 | | /* push the prev_item_type down into the subcontainer */ |
3591 | 0 | if (GVSB(parent)->prev_item_type) |
3592 | 0 | { |
3593 | 0 | if (!GVSB(builder)->uniform_item_types) |
3594 | | /* tuples and dict entries */ |
3595 | 0 | GVSB(builder)->prev_item_type = |
3596 | 0 | g_variant_type_first (GVSB(parent)->prev_item_type); |
3597 | | |
3598 | 0 | else if (!g_variant_type_is_variant (GVSB(builder)->type)) |
3599 | | /* maybes and arrays */ |
3600 | 0 | GVSB(builder)->prev_item_type = |
3601 | 0 | g_variant_type_element (GVSB(parent)->prev_item_type); |
3602 | 0 | } |
3603 | 0 | } |
3604 | | |
3605 | | /** |
3606 | | * g_variant_builder_close: |
3607 | | * @builder: a #GVariantBuilder |
3608 | | * |
3609 | | * Closes the subcontainer inside the given @builder that was opened by |
3610 | | * the most recent call to g_variant_builder_open(). |
3611 | | * |
3612 | | * It is an error to call this function in any way that would create an |
3613 | | * inconsistent value to be constructed (ie: too few values added to the |
3614 | | * subcontainer). |
3615 | | * |
3616 | | * Since: 2.24 |
3617 | | **/ |
3618 | | void |
3619 | | g_variant_builder_close (GVariantBuilder *builder) |
3620 | 0 | { |
3621 | 0 | GVariantBuilder *parent; |
3622 | |
|
3623 | 0 | g_return_if_fail (ensure_valid_builder (builder)); |
3624 | 0 | g_return_if_fail (GVSB(builder)->parent != NULL); |
3625 | | |
3626 | 0 | parent = GVSB(builder)->parent; |
3627 | 0 | GVSB(builder)->parent = NULL; |
3628 | |
|
3629 | 0 | g_variant_builder_add_value (parent, g_variant_builder_end (builder)); |
3630 | 0 | *builder = *parent; |
3631 | |
|
3632 | 0 | g_slice_free (GVariantBuilder, parent); |
3633 | 0 | } |
3634 | | |
3635 | | /*< private > |
3636 | | * g_variant_make_maybe_type: |
3637 | | * @element: a #GVariant |
3638 | | * |
3639 | | * Return the type of a maybe containing @element. |
3640 | | */ |
3641 | | static GVariantType * |
3642 | | g_variant_make_maybe_type (GVariant *element) |
3643 | 0 | { |
3644 | 0 | return g_variant_type_new_maybe (g_variant_get_type (element)); |
3645 | 0 | } |
3646 | | |
3647 | | /*< private > |
3648 | | * g_variant_make_array_type: |
3649 | | * @element: a #GVariant |
3650 | | * |
3651 | | * Return the type of an array containing @element. |
3652 | | */ |
3653 | | static GVariantType * |
3654 | | g_variant_make_array_type (GVariant *element) |
3655 | 0 | { |
3656 | 0 | return g_variant_type_new_array (g_variant_get_type (element)); |
3657 | 0 | } |
3658 | | |
3659 | | /** |
3660 | | * g_variant_builder_end: |
3661 | | * @builder: a #GVariantBuilder |
3662 | | * |
3663 | | * Ends the builder process and returns the constructed value. |
3664 | | * |
3665 | | * It is not permissible to use @builder in any way after this call |
3666 | | * except for reference counting operations (in the case of a |
3667 | | * heap-allocated #GVariantBuilder) or by reinitialising it with |
3668 | | * g_variant_builder_init() (in the case of stack-allocated). This |
3669 | | * means that for the stack-allocated builders there is no need to |
3670 | | * call g_variant_builder_clear() after the call to |
3671 | | * g_variant_builder_end(). |
3672 | | * |
3673 | | * It is an error to call this function in any way that would create an |
3674 | | * inconsistent value to be constructed (ie: insufficient number of |
3675 | | * items added to a container with a specific number of children |
3676 | | * required). It is also an error to call this function if the builder |
3677 | | * was created with an indefinite array or maybe type and no children |
3678 | | * have been added; in this case it is impossible to infer the type of |
3679 | | * the empty array. |
3680 | | * |
3681 | | * Returns: (transfer none): a new, floating, #GVariant |
3682 | | * |
3683 | | * Since: 2.24 |
3684 | | **/ |
3685 | | GVariant * |
3686 | | g_variant_builder_end (GVariantBuilder *builder) |
3687 | 0 | { |
3688 | 0 | GVariantType *my_type; |
3689 | 0 | GVariant *value; |
3690 | |
|
3691 | 0 | g_return_val_if_fail (ensure_valid_builder (builder), NULL); |
3692 | 0 | g_return_val_if_fail (GVSB(builder)->offset >= GVSB(builder)->min_items, |
3693 | 0 | NULL); |
3694 | 0 | g_return_val_if_fail (!GVSB(builder)->uniform_item_types || |
3695 | 0 | GVSB(builder)->prev_item_type != NULL || |
3696 | 0 | g_variant_type_is_definite (GVSB(builder)->type), |
3697 | 0 | NULL); |
3698 | | |
3699 | 0 | if (g_variant_type_is_definite (GVSB(builder)->type)) |
3700 | 0 | my_type = g_variant_type_copy (GVSB(builder)->type); |
3701 | | |
3702 | 0 | else if (g_variant_type_is_maybe (GVSB(builder)->type)) |
3703 | 0 | my_type = g_variant_make_maybe_type (GVSB(builder)->children[0]); |
3704 | | |
3705 | 0 | else if (g_variant_type_is_array (GVSB(builder)->type)) |
3706 | 0 | my_type = g_variant_make_array_type (GVSB(builder)->children[0]); |
3707 | | |
3708 | 0 | else if (g_variant_type_is_tuple (GVSB(builder)->type)) |
3709 | 0 | my_type = g_variant_make_tuple_type (GVSB(builder)->children, |
3710 | 0 | GVSB(builder)->offset); |
3711 | | |
3712 | 0 | else if (g_variant_type_is_dict_entry (GVSB(builder)->type)) |
3713 | 0 | my_type = g_variant_make_dict_entry_type (GVSB(builder)->children[0], |
3714 | 0 | GVSB(builder)->children[1]); |
3715 | 0 | else |
3716 | 0 | g_assert_not_reached (); |
3717 | | |
3718 | 0 | value = g_variant_new_from_children (my_type, |
3719 | 0 | g_renew (GVariant *, |
3720 | 0 | GVSB(builder)->children, |
3721 | 0 | GVSB(builder)->offset), |
3722 | 0 | GVSB(builder)->offset, |
3723 | 0 | GVSB(builder)->trusted); |
3724 | 0 | GVSB(builder)->children = NULL; |
3725 | 0 | GVSB(builder)->offset = 0; |
3726 | |
|
3727 | 0 | g_variant_builder_clear (builder); |
3728 | 0 | g_variant_type_free (my_type); |
3729 | |
|
3730 | 0 | return value; |
3731 | 0 | } |
3732 | | |
3733 | | /* GVariantDict {{{1 */ |
3734 | | |
3735 | | /** |
3736 | | * GVariantDict: |
3737 | | * |
3738 | | * #GVariantDict is a mutable interface to #GVariant dictionaries. |
3739 | | * |
3740 | | * It can be used for doing a sequence of dictionary lookups in an |
3741 | | * efficient way on an existing #GVariant dictionary or it can be used |
3742 | | * to construct new dictionaries with a hashtable-like interface. It |
3743 | | * can also be used for taking existing dictionaries and modifying them |
3744 | | * in order to create new ones. |
3745 | | * |
3746 | | * #GVariantDict can only be used with %G_VARIANT_TYPE_VARDICT |
3747 | | * dictionaries. |
3748 | | * |
3749 | | * It is possible to use #GVariantDict allocated on the stack or on the |
3750 | | * heap. When using a stack-allocated #GVariantDict, you begin with a |
3751 | | * call to g_variant_dict_init() and free the resources with a call to |
3752 | | * g_variant_dict_clear(). |
3753 | | * |
3754 | | * Heap-allocated #GVariantDict follows normal refcounting rules: you |
3755 | | * allocate it with g_variant_dict_new() and use g_variant_dict_ref() |
3756 | | * and g_variant_dict_unref(). |
3757 | | * |
3758 | | * g_variant_dict_end() is used to convert the #GVariantDict back into a |
3759 | | * dictionary-type #GVariant. When used with stack-allocated instances, |
3760 | | * this also implicitly frees all associated memory, but for |
3761 | | * heap-allocated instances, you must still call g_variant_dict_unref() |
3762 | | * afterwards. |
3763 | | * |
3764 | | * You will typically want to use a heap-allocated #GVariantDict when |
3765 | | * you expose it as part of an API. For most other uses, the |
3766 | | * stack-allocated form will be more convenient. |
3767 | | * |
3768 | | * Consider the following two examples that do the same thing in each |
3769 | | * style: take an existing dictionary and look up the "count" uint32 |
3770 | | * key, adding 1 to it if it is found, or returning an error if the |
3771 | | * key is not found. Each returns the new dictionary as a floating |
3772 | | * #GVariant. |
3773 | | * |
3774 | | * ## Using a stack-allocated GVariantDict |
3775 | | * |
3776 | | * |[<!-- language="C" --> |
3777 | | * GVariant * |
3778 | | * add_to_count (GVariant *orig, |
3779 | | * GError **error) |
3780 | | * { |
3781 | | * GVariantDict dict; |
3782 | | * guint32 count; |
3783 | | * |
3784 | | * g_variant_dict_init (&dict, orig); |
3785 | | * if (!g_variant_dict_lookup (&dict, "count", "u", &count)) |
3786 | | * { |
3787 | | * g_set_error (...); |
3788 | | * g_variant_dict_clear (&dict); |
3789 | | * return NULL; |
3790 | | * } |
3791 | | * |
3792 | | * g_variant_dict_insert (&dict, "count", "u", count + 1); |
3793 | | * |
3794 | | * return g_variant_dict_end (&dict); |
3795 | | * } |
3796 | | * ]| |
3797 | | * |
3798 | | * ## Using heap-allocated GVariantDict |
3799 | | * |
3800 | | * |[<!-- language="C" --> |
3801 | | * GVariant * |
3802 | | * add_to_count (GVariant *orig, |
3803 | | * GError **error) |
3804 | | * { |
3805 | | * GVariantDict *dict; |
3806 | | * GVariant *result; |
3807 | | * guint32 count; |
3808 | | * |
3809 | | * dict = g_variant_dict_new (orig); |
3810 | | * |
3811 | | * if (g_variant_dict_lookup (dict, "count", "u", &count)) |
3812 | | * { |
3813 | | * g_variant_dict_insert (dict, "count", "u", count + 1); |
3814 | | * result = g_variant_dict_end (dict); |
3815 | | * } |
3816 | | * else |
3817 | | * { |
3818 | | * g_set_error (...); |
3819 | | * result = NULL; |
3820 | | * } |
3821 | | * |
3822 | | * g_variant_dict_unref (dict); |
3823 | | * |
3824 | | * return result; |
3825 | | * } |
3826 | | * ]| |
3827 | | * |
3828 | | * Since: 2.40 |
3829 | | **/ |
3830 | | struct stack_dict |
3831 | | { |
3832 | | GHashTable *values; |
3833 | | gsize magic; |
3834 | | }; |
3835 | | |
3836 | | G_STATIC_ASSERT (sizeof (struct stack_dict) <= sizeof (GVariantDict)); |
3837 | | |
3838 | | struct heap_dict |
3839 | | { |
3840 | | struct stack_dict dict; |
3841 | | gint ref_count; |
3842 | | gsize magic; |
3843 | | }; |
3844 | | |
3845 | 0 | #define GVSD(d) ((struct stack_dict *) (d)) |
3846 | 0 | #define GVHD(d) ((struct heap_dict *) (d)) |
3847 | 0 | #define GVSD_MAGIC ((gsize) 2579507750u) |
3848 | 0 | #define GVSD_MAGIC_PARTIAL ((gsize) 3488698669u) |
3849 | 0 | #define GVHD_MAGIC ((gsize) 2450270775u) |
3850 | 0 | #define is_valid_dict(d) (d != NULL && \ |
3851 | 0 | GVSD(d)->magic == GVSD_MAGIC) |
3852 | | #define is_valid_heap_dict(d) (GVHD(d)->magic == GVHD_MAGIC) |
3853 | | |
3854 | | /* Just to make sure that by adding a union to GVariantDict, we didn't |
3855 | | * accidentally change ABI. */ |
3856 | | G_STATIC_ASSERT (sizeof (GVariantDict) == sizeof (gsize[16])); |
3857 | | |
3858 | | static gboolean |
3859 | | ensure_valid_dict (GVariantDict *dict) |
3860 | 0 | { |
3861 | 0 | if (is_valid_dict (dict)) |
3862 | 0 | return TRUE; |
3863 | 0 | if (dict->u.s.partial_magic == GVSD_MAGIC_PARTIAL) |
3864 | 0 | { |
3865 | 0 | static GVariantDict cleared_dict; |
3866 | | |
3867 | | /* Make sure that only first two fields were set and the rest is |
3868 | | * zeroed to avoid messing up the builder that had parent |
3869 | | * address equal to GVSB_MAGIC_PARTIAL. */ |
3870 | 0 | if (memcmp (cleared_dict.u.s.y, dict->u.s.y, sizeof cleared_dict.u.s.y)) |
3871 | 0 | return FALSE; |
3872 | | |
3873 | 0 | g_variant_dict_init (dict, dict->u.s.asv); |
3874 | 0 | } |
3875 | 0 | return is_valid_dict (dict); |
3876 | 0 | } |
3877 | | |
3878 | | /** |
3879 | | * g_variant_dict_new: |
3880 | | * @from_asv: (nullable): the #GVariant with which to initialise the |
3881 | | * dictionary |
3882 | | * |
3883 | | * Allocates and initialises a new #GVariantDict. |
3884 | | * |
3885 | | * You should call g_variant_dict_unref() on the return value when it |
3886 | | * is no longer needed. The memory will not be automatically freed by |
3887 | | * any other call. |
3888 | | * |
3889 | | * In some cases it may be easier to place a #GVariantDict directly on |
3890 | | * the stack of the calling function and initialise it with |
3891 | | * g_variant_dict_init(). This is particularly useful when you are |
3892 | | * using #GVariantDict to construct a #GVariant. |
3893 | | * |
3894 | | * Returns: (transfer full): a #GVariantDict |
3895 | | * |
3896 | | * Since: 2.40 |
3897 | | **/ |
3898 | | GVariantDict * |
3899 | | g_variant_dict_new (GVariant *from_asv) |
3900 | 0 | { |
3901 | 0 | GVariantDict *dict; |
3902 | |
|
3903 | 0 | dict = g_slice_alloc (sizeof (struct heap_dict)); |
3904 | 0 | g_variant_dict_init (dict, from_asv); |
3905 | 0 | GVHD(dict)->magic = GVHD_MAGIC; |
3906 | 0 | GVHD(dict)->ref_count = 1; |
3907 | |
|
3908 | 0 | return dict; |
3909 | 0 | } |
3910 | | |
3911 | | /** |
3912 | | * g_variant_dict_init: (skip) |
3913 | | * @dict: a #GVariantDict |
3914 | | * @from_asv: (nullable): the initial value for @dict |
3915 | | * |
3916 | | * Initialises a #GVariantDict structure. |
3917 | | * |
3918 | | * If @from_asv is given, it is used to initialise the dictionary. |
3919 | | * |
3920 | | * This function completely ignores the previous contents of @dict. On |
3921 | | * one hand this means that it is valid to pass in completely |
3922 | | * uninitialised memory. On the other hand, this means that if you are |
3923 | | * initialising over top of an existing #GVariantDict you need to first |
3924 | | * call g_variant_dict_clear() in order to avoid leaking memory. |
3925 | | * |
3926 | | * You must not call g_variant_dict_ref() or g_variant_dict_unref() on a |
3927 | | * #GVariantDict that was initialised with this function. If you ever |
3928 | | * pass a reference to a #GVariantDict outside of the control of your |
3929 | | * own code then you should assume that the person receiving that |
3930 | | * reference may try to use reference counting; you should use |
3931 | | * g_variant_dict_new() instead of this function. |
3932 | | * |
3933 | | * Since: 2.40 |
3934 | | **/ |
3935 | | void |
3936 | | g_variant_dict_init (GVariantDict *dict, |
3937 | | GVariant *from_asv) |
3938 | 0 | { |
3939 | 0 | GVariantIter iter; |
3940 | 0 | gchar *key; |
3941 | 0 | GVariant *value; |
3942 | |
|
3943 | 0 | GVSD(dict)->values = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref); |
3944 | 0 | GVSD(dict)->magic = GVSD_MAGIC; |
3945 | |
|
3946 | 0 | if (from_asv) |
3947 | 0 | { |
3948 | 0 | g_variant_iter_init (&iter, from_asv); |
3949 | 0 | while (g_variant_iter_next (&iter, "{sv}", &key, &value)) |
3950 | 0 | g_hash_table_insert (GVSD(dict)->values, key, value); |
3951 | 0 | } |
3952 | 0 | } |
3953 | | |
3954 | | /** |
3955 | | * g_variant_dict_lookup: |
3956 | | * @dict: a #GVariantDict |
3957 | | * @key: the key to look up in the dictionary |
3958 | | * @format_string: a GVariant format string |
3959 | | * @...: the arguments to unpack the value into |
3960 | | * |
3961 | | * Looks up a value in a #GVariantDict. |
3962 | | * |
3963 | | * This function is a wrapper around g_variant_dict_lookup_value() and |
3964 | | * g_variant_get(). In the case that %NULL would have been returned, |
3965 | | * this function returns %FALSE. Otherwise, it unpacks the returned |
3966 | | * value and returns %TRUE. |
3967 | | * |
3968 | | * @format_string determines the C types that are used for unpacking the |
3969 | | * values and also determines if the values are copied or borrowed, see the |
3970 | | * section on [GVariant format strings][gvariant-format-strings-pointers]. |
3971 | | * |
3972 | | * Returns: %TRUE if a value was unpacked |
3973 | | * |
3974 | | * Since: 2.40 |
3975 | | **/ |
3976 | | gboolean |
3977 | | g_variant_dict_lookup (GVariantDict *dict, |
3978 | | const gchar *key, |
3979 | | const gchar *format_string, |
3980 | | ...) |
3981 | 0 | { |
3982 | 0 | GVariant *value; |
3983 | 0 | va_list ap; |
3984 | |
|
3985 | 0 | g_return_val_if_fail (ensure_valid_dict (dict), FALSE); |
3986 | 0 | g_return_val_if_fail (key != NULL, FALSE); |
3987 | 0 | g_return_val_if_fail (format_string != NULL, FALSE); |
3988 | | |
3989 | 0 | value = g_hash_table_lookup (GVSD(dict)->values, key); |
3990 | |
|
3991 | 0 | if (value == NULL || !g_variant_check_format_string (value, format_string, FALSE)) |
3992 | 0 | return FALSE; |
3993 | | |
3994 | 0 | va_start (ap, format_string); |
3995 | 0 | g_variant_get_va (value, format_string, NULL, &ap); |
3996 | 0 | va_end (ap); |
3997 | |
|
3998 | 0 | return TRUE; |
3999 | 0 | } |
4000 | | |
4001 | | /** |
4002 | | * g_variant_dict_lookup_value: |
4003 | | * @dict: a #GVariantDict |
4004 | | * @key: the key to look up in the dictionary |
4005 | | * @expected_type: (nullable): a #GVariantType, or %NULL |
4006 | | * |
4007 | | * Looks up a value in a #GVariantDict. |
4008 | | * |
4009 | | * If @key is not found in @dictionary, %NULL is returned. |
4010 | | * |
4011 | | * The @expected_type string specifies what type of value is expected. |
4012 | | * If the value associated with @key has a different type then %NULL is |
4013 | | * returned. |
4014 | | * |
4015 | | * If the key is found and the value has the correct type, it is |
4016 | | * returned. If @expected_type was specified then any non-%NULL return |
4017 | | * value will have this type. |
4018 | | * |
4019 | | * Returns: (transfer full): the value of the dictionary key, or %NULL |
4020 | | * |
4021 | | * Since: 2.40 |
4022 | | **/ |
4023 | | GVariant * |
4024 | | g_variant_dict_lookup_value (GVariantDict *dict, |
4025 | | const gchar *key, |
4026 | | const GVariantType *expected_type) |
4027 | 0 | { |
4028 | 0 | GVariant *result; |
4029 | |
|
4030 | 0 | g_return_val_if_fail (ensure_valid_dict (dict), NULL); |
4031 | 0 | g_return_val_if_fail (key != NULL, NULL); |
4032 | | |
4033 | 0 | result = g_hash_table_lookup (GVSD(dict)->values, key); |
4034 | |
|
4035 | 0 | if (result && (!expected_type || g_variant_is_of_type (result, expected_type))) |
4036 | 0 | return g_variant_ref (result); |
4037 | | |
4038 | 0 | return NULL; |
4039 | 0 | } |
4040 | | |
4041 | | /** |
4042 | | * g_variant_dict_contains: |
4043 | | * @dict: a #GVariantDict |
4044 | | * @key: the key to look up in the dictionary |
4045 | | * |
4046 | | * Checks if @key exists in @dict. |
4047 | | * |
4048 | | * Returns: %TRUE if @key is in @dict |
4049 | | * |
4050 | | * Since: 2.40 |
4051 | | **/ |
4052 | | gboolean |
4053 | | g_variant_dict_contains (GVariantDict *dict, |
4054 | | const gchar *key) |
4055 | 0 | { |
4056 | 0 | g_return_val_if_fail (ensure_valid_dict (dict), FALSE); |
4057 | 0 | g_return_val_if_fail (key != NULL, FALSE); |
4058 | | |
4059 | 0 | return g_hash_table_contains (GVSD(dict)->values, key); |
4060 | 0 | } |
4061 | | |
4062 | | /** |
4063 | | * g_variant_dict_insert: |
4064 | | * @dict: a #GVariantDict |
4065 | | * @key: the key to insert a value for |
4066 | | * @format_string: a #GVariant varargs format string |
4067 | | * @...: arguments, as per @format_string |
4068 | | * |
4069 | | * Inserts a value into a #GVariantDict. |
4070 | | * |
4071 | | * This call is a convenience wrapper that is exactly equivalent to |
4072 | | * calling g_variant_new() followed by g_variant_dict_insert_value(). |
4073 | | * |
4074 | | * Since: 2.40 |
4075 | | **/ |
4076 | | void |
4077 | | g_variant_dict_insert (GVariantDict *dict, |
4078 | | const gchar *key, |
4079 | | const gchar *format_string, |
4080 | | ...) |
4081 | 0 | { |
4082 | 0 | va_list ap; |
4083 | |
|
4084 | 0 | g_return_if_fail (ensure_valid_dict (dict)); |
4085 | 0 | g_return_if_fail (key != NULL); |
4086 | 0 | g_return_if_fail (format_string != NULL); |
4087 | | |
4088 | 0 | va_start (ap, format_string); |
4089 | 0 | g_variant_dict_insert_value (dict, key, g_variant_new_va (format_string, NULL, &ap)); |
4090 | 0 | va_end (ap); |
4091 | 0 | } |
4092 | | |
4093 | | /** |
4094 | | * g_variant_dict_insert_value: |
4095 | | * @dict: a #GVariantDict |
4096 | | * @key: the key to insert a value for |
4097 | | * @value: the value to insert |
4098 | | * |
4099 | | * Inserts (or replaces) a key in a #GVariantDict. |
4100 | | * |
4101 | | * @value is consumed if it is floating. |
4102 | | * |
4103 | | * Since: 2.40 |
4104 | | **/ |
4105 | | void |
4106 | | g_variant_dict_insert_value (GVariantDict *dict, |
4107 | | const gchar *key, |
4108 | | GVariant *value) |
4109 | 0 | { |
4110 | 0 | g_return_if_fail (ensure_valid_dict (dict)); |
4111 | 0 | g_return_if_fail (key != NULL); |
4112 | 0 | g_return_if_fail (value != NULL); |
4113 | | |
4114 | 0 | g_hash_table_insert (GVSD(dict)->values, g_strdup (key), g_variant_ref_sink (value)); |
4115 | 0 | } |
4116 | | |
4117 | | /** |
4118 | | * g_variant_dict_remove: |
4119 | | * @dict: a #GVariantDict |
4120 | | * @key: the key to remove |
4121 | | * |
4122 | | * Removes a key and its associated value from a #GVariantDict. |
4123 | | * |
4124 | | * Returns: %TRUE if the key was found and removed |
4125 | | * |
4126 | | * Since: 2.40 |
4127 | | **/ |
4128 | | gboolean |
4129 | | g_variant_dict_remove (GVariantDict *dict, |
4130 | | const gchar *key) |
4131 | 0 | { |
4132 | 0 | g_return_val_if_fail (ensure_valid_dict (dict), FALSE); |
4133 | 0 | g_return_val_if_fail (key != NULL, FALSE); |
4134 | | |
4135 | 0 | return g_hash_table_remove (GVSD(dict)->values, key); |
4136 | 0 | } |
4137 | | |
4138 | | /** |
4139 | | * g_variant_dict_clear: |
4140 | | * @dict: a #GVariantDict |
4141 | | * |
4142 | | * Releases all memory associated with a #GVariantDict without freeing |
4143 | | * the #GVariantDict structure itself. |
4144 | | * |
4145 | | * It typically only makes sense to do this on a stack-allocated |
4146 | | * #GVariantDict if you want to abort building the value part-way |
4147 | | * through. This function need not be called if you call |
4148 | | * g_variant_dict_end() and it also doesn't need to be called on dicts |
4149 | | * allocated with g_variant_dict_new (see g_variant_dict_unref() for |
4150 | | * that). |
4151 | | * |
4152 | | * It is valid to call this function on either an initialised |
4153 | | * #GVariantDict or one that was previously cleared by an earlier call |
4154 | | * to g_variant_dict_clear() but it is not valid to call this function |
4155 | | * on uninitialised memory. |
4156 | | * |
4157 | | * Since: 2.40 |
4158 | | **/ |
4159 | | void |
4160 | | g_variant_dict_clear (GVariantDict *dict) |
4161 | 0 | { |
4162 | 0 | if (GVSD(dict)->magic == 0) |
4163 | | /* all-zeros case */ |
4164 | 0 | return; |
4165 | | |
4166 | 0 | g_return_if_fail (ensure_valid_dict (dict)); |
4167 | | |
4168 | 0 | g_hash_table_unref (GVSD(dict)->values); |
4169 | 0 | GVSD(dict)->values = NULL; |
4170 | |
|
4171 | 0 | GVSD(dict)->magic = 0; |
4172 | 0 | } |
4173 | | |
4174 | | /** |
4175 | | * g_variant_dict_end: |
4176 | | * @dict: a #GVariantDict |
4177 | | * |
4178 | | * Returns the current value of @dict as a #GVariant of type |
4179 | | * %G_VARIANT_TYPE_VARDICT, clearing it in the process. |
4180 | | * |
4181 | | * It is not permissible to use @dict in any way after this call except |
4182 | | * for reference counting operations (in the case of a heap-allocated |
4183 | | * #GVariantDict) or by reinitialising it with g_variant_dict_init() (in |
4184 | | * the case of stack-allocated). |
4185 | | * |
4186 | | * Returns: (transfer none): a new, floating, #GVariant |
4187 | | * |
4188 | | * Since: 2.40 |
4189 | | **/ |
4190 | | GVariant * |
4191 | | g_variant_dict_end (GVariantDict *dict) |
4192 | 0 | { |
4193 | 0 | GVariantBuilder builder; |
4194 | 0 | GHashTableIter iter; |
4195 | 0 | gpointer key, value; |
4196 | |
|
4197 | 0 | g_return_val_if_fail (ensure_valid_dict (dict), NULL); |
4198 | | |
4199 | 0 | g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT); |
4200 | |
|
4201 | 0 | g_hash_table_iter_init (&iter, GVSD(dict)->values); |
4202 | 0 | while (g_hash_table_iter_next (&iter, &key, &value)) |
4203 | 0 | g_variant_builder_add (&builder, "{sv}", (const gchar *) key, (GVariant *) value); |
4204 | |
|
4205 | 0 | g_variant_dict_clear (dict); |
4206 | |
|
4207 | 0 | return g_variant_builder_end (&builder); |
4208 | 0 | } |
4209 | | |
4210 | | /** |
4211 | | * g_variant_dict_ref: |
4212 | | * @dict: a heap-allocated #GVariantDict |
4213 | | * |
4214 | | * Increases the reference count on @dict. |
4215 | | * |
4216 | | * Don't call this on stack-allocated #GVariantDict instances or bad |
4217 | | * things will happen. |
4218 | | * |
4219 | | * Returns: (transfer full): a new reference to @dict |
4220 | | * |
4221 | | * Since: 2.40 |
4222 | | **/ |
4223 | | GVariantDict * |
4224 | | g_variant_dict_ref (GVariantDict *dict) |
4225 | 0 | { |
4226 | 0 | g_return_val_if_fail (is_valid_heap_dict (dict), NULL); |
4227 | | |
4228 | 0 | GVHD(dict)->ref_count++; |
4229 | |
|
4230 | 0 | return dict; |
4231 | 0 | } |
4232 | | |
4233 | | /** |
4234 | | * g_variant_dict_unref: |
4235 | | * @dict: (transfer full): a heap-allocated #GVariantDict |
4236 | | * |
4237 | | * Decreases the reference count on @dict. |
4238 | | * |
4239 | | * In the event that there are no more references, releases all memory |
4240 | | * associated with the #GVariantDict. |
4241 | | * |
4242 | | * Don't call this on stack-allocated #GVariantDict instances or bad |
4243 | | * things will happen. |
4244 | | * |
4245 | | * Since: 2.40 |
4246 | | **/ |
4247 | | void |
4248 | | g_variant_dict_unref (GVariantDict *dict) |
4249 | 0 | { |
4250 | 0 | g_return_if_fail (is_valid_heap_dict (dict)); |
4251 | | |
4252 | 0 | if (--GVHD(dict)->ref_count == 0) |
4253 | 0 | { |
4254 | 0 | g_variant_dict_clear (dict); |
4255 | 0 | g_slice_free (struct heap_dict, (struct heap_dict *) dict); |
4256 | 0 | } |
4257 | 0 | } |
4258 | | |
4259 | | |
4260 | | /* Format strings {{{1 */ |
4261 | | /*< private > |
4262 | | * g_variant_format_string_scan: |
4263 | | * @string: a string that may be prefixed with a format string |
4264 | | * @limit: (nullable) (default NULL): a pointer to the end of @string, |
4265 | | * or %NULL |
4266 | | * @endptr: (nullable) (default NULL): location to store the end pointer, |
4267 | | * or %NULL |
4268 | | * |
4269 | | * Checks the string pointed to by @string for starting with a properly |
4270 | | * formed #GVariant varargs format string. If no valid format string is |
4271 | | * found then %FALSE is returned. |
4272 | | * |
4273 | | * If @string does start with a valid format string then %TRUE is |
4274 | | * returned. If @endptr is non-%NULL then it is updated to point to the |
4275 | | * first character after the format string. |
4276 | | * |
4277 | | * If @limit is non-%NULL then @limit (and any character after it) will |
4278 | | * not be accessed and the effect is otherwise equivalent to if the |
4279 | | * character at @limit were nul. |
4280 | | * |
4281 | | * See the section on [GVariant format strings][gvariant-format-strings]. |
4282 | | * |
4283 | | * Returns: %TRUE if there was a valid format string |
4284 | | * |
4285 | | * Since: 2.24 |
4286 | | */ |
4287 | | gboolean |
4288 | | g_variant_format_string_scan (const gchar *string, |
4289 | | const gchar *limit, |
4290 | | const gchar **endptr) |
4291 | 0 | { |
4292 | 0 | #define next_char() (string == limit ? '\0' : *(string++)) |
4293 | 0 | #define peek_char() (string == limit ? '\0' : *string) |
4294 | 0 | char c; |
4295 | |
|
4296 | 0 | switch (next_char()) |
4297 | 0 | { |
4298 | 0 | case 'b': case 'y': case 'n': case 'q': case 'i': case 'u': |
4299 | 0 | case 'x': case 't': case 'h': case 'd': case 's': case 'o': |
4300 | 0 | case 'g': case 'v': case '*': case '?': case 'r': |
4301 | 0 | break; |
4302 | | |
4303 | 0 | case 'm': |
4304 | 0 | return g_variant_format_string_scan (string, limit, endptr); |
4305 | | |
4306 | 0 | case 'a': |
4307 | 0 | case '@': |
4308 | 0 | return g_variant_type_string_scan (string, limit, endptr); |
4309 | | |
4310 | 0 | case '(': |
4311 | 0 | while (peek_char() != ')') |
4312 | 0 | if (!g_variant_format_string_scan (string, limit, &string)) |
4313 | 0 | return FALSE; |
4314 | | |
4315 | 0 | next_char(); /* consume ')' */ |
4316 | 0 | break; |
4317 | | |
4318 | 0 | case '{': |
4319 | 0 | c = next_char(); |
4320 | |
|
4321 | 0 | if (c == '&') |
4322 | 0 | { |
4323 | 0 | c = next_char (); |
4324 | |
|
4325 | 0 | if (c != 's' && c != 'o' && c != 'g') |
4326 | 0 | return FALSE; |
4327 | 0 | } |
4328 | 0 | else |
4329 | 0 | { |
4330 | 0 | if (c == '@') |
4331 | 0 | c = next_char (); |
4332 | | |
4333 | | /* ISO/IEC 9899:1999 (C99) §7.21.5.2: |
4334 | | * The terminating null character is considered to be |
4335 | | * part of the string. |
4336 | | */ |
4337 | 0 | if (c != '\0' && strchr ("bynqiuxthdsog?", c) == NULL) |
4338 | 0 | return FALSE; |
4339 | 0 | } |
4340 | | |
4341 | 0 | if (!g_variant_format_string_scan (string, limit, &string)) |
4342 | 0 | return FALSE; |
4343 | | |
4344 | 0 | if (next_char() != '}') |
4345 | 0 | return FALSE; |
4346 | | |
4347 | 0 | break; |
4348 | | |
4349 | 0 | case '^': |
4350 | 0 | if ((c = next_char()) == 'a') |
4351 | 0 | { |
4352 | 0 | if ((c = next_char()) == '&') |
4353 | 0 | { |
4354 | 0 | if ((c = next_char()) == 'a') |
4355 | 0 | { |
4356 | 0 | if ((c = next_char()) == 'y') |
4357 | 0 | break; /* '^a&ay' */ |
4358 | 0 | } |
4359 | | |
4360 | 0 | else if (c == 's' || c == 'o') |
4361 | 0 | break; /* '^a&s', '^a&o' */ |
4362 | 0 | } |
4363 | | |
4364 | 0 | else if (c == 'a') |
4365 | 0 | { |
4366 | 0 | if ((c = next_char()) == 'y') |
4367 | 0 | break; /* '^aay' */ |
4368 | 0 | } |
4369 | | |
4370 | 0 | else if (c == 's' || c == 'o') |
4371 | 0 | break; /* '^as', '^ao' */ |
4372 | | |
4373 | 0 | else if (c == 'y') |
4374 | 0 | break; /* '^ay' */ |
4375 | 0 | } |
4376 | 0 | else if (c == '&') |
4377 | 0 | { |
4378 | 0 | if ((c = next_char()) == 'a') |
4379 | 0 | { |
4380 | 0 | if ((c = next_char()) == 'y') |
4381 | 0 | break; /* '^&ay' */ |
4382 | 0 | } |
4383 | 0 | } |
4384 | | |
4385 | 0 | return FALSE; |
4386 | | |
4387 | 0 | case '&': |
4388 | 0 | c = next_char(); |
4389 | |
|
4390 | 0 | if (c != 's' && c != 'o' && c != 'g') |
4391 | 0 | return FALSE; |
4392 | | |
4393 | 0 | break; |
4394 | | |
4395 | 0 | default: |
4396 | 0 | return FALSE; |
4397 | 0 | } |
4398 | | |
4399 | 0 | if (endptr != NULL) |
4400 | 0 | *endptr = string; |
4401 | |
|
4402 | 0 | #undef next_char |
4403 | 0 | #undef peek_char |
4404 | |
|
4405 | 0 | return TRUE; |
4406 | 0 | } |
4407 | | |
4408 | | /** |
4409 | | * g_variant_check_format_string: |
4410 | | * @value: a #GVariant |
4411 | | * @format_string: a valid #GVariant format string |
4412 | | * @copy_only: %TRUE to ensure the format string makes deep copies |
4413 | | * |
4414 | | * Checks if calling g_variant_get() with @format_string on @value would |
4415 | | * be valid from a type-compatibility standpoint. @format_string is |
4416 | | * assumed to be a valid format string (from a syntactic standpoint). |
4417 | | * |
4418 | | * If @copy_only is %TRUE then this function additionally checks that it |
4419 | | * would be safe to call g_variant_unref() on @value immediately after |
4420 | | * the call to g_variant_get() without invalidating the result. This is |
4421 | | * only possible if deep copies are made (ie: there are no pointers to |
4422 | | * the data inside of the soon-to-be-freed #GVariant instance). If this |
4423 | | * check fails then a g_critical() is printed and %FALSE is returned. |
4424 | | * |
4425 | | * This function is meant to be used by functions that wish to provide |
4426 | | * varargs accessors to #GVariant values of uncertain values (eg: |
4427 | | * g_variant_lookup() or g_menu_model_get_item_attribute()). |
4428 | | * |
4429 | | * Returns: %TRUE if @format_string is safe to use |
4430 | | * |
4431 | | * Since: 2.34 |
4432 | | */ |
4433 | | gboolean |
4434 | | g_variant_check_format_string (GVariant *value, |
4435 | | const gchar *format_string, |
4436 | | gboolean copy_only) |
4437 | 0 | { |
4438 | 0 | const gchar *original_format = format_string; |
4439 | 0 | const gchar *type_string; |
4440 | | |
4441 | | /* Interesting factoid: assuming a format string is valid, it can be |
4442 | | * converted to a type string by removing all '@' '&' and '^' |
4443 | | * characters. |
4444 | | * |
4445 | | * Instead of doing that, we can just skip those characters when |
4446 | | * comparing it to the type string of @value. |
4447 | | * |
4448 | | * For the copy-only case we can just drop the '&' from the list of |
4449 | | * characters to skip over. A '&' will never appear in a type string |
4450 | | * so we know that it won't be possible to return %TRUE if it is in a |
4451 | | * format string. |
4452 | | */ |
4453 | 0 | type_string = g_variant_get_type_string (value); |
4454 | |
|
4455 | 0 | while (*type_string || *format_string) |
4456 | 0 | { |
4457 | 0 | gchar format = *format_string++; |
4458 | |
|
4459 | 0 | switch (format) |
4460 | 0 | { |
4461 | 0 | case '&': |
4462 | 0 | if G_UNLIKELY (copy_only) |
4463 | 0 | { |
4464 | | /* for the love of all that is good, please don't mark this string for translation... */ |
4465 | 0 | g_critical ("g_variant_check_format_string() is being called by a function with a GVariant varargs " |
4466 | 0 | "interface to validate the passed format string for type safety. The passed format " |
4467 | 0 | "(%s) contains a '&' character which would result in a pointer being returned to the " |
4468 | 0 | "data inside of a GVariant instance that may no longer exist by the time the function " |
4469 | 0 | "returns. Modify your code to use a format string without '&'.", original_format); |
4470 | 0 | return FALSE; |
4471 | 0 | } |
4472 | | |
4473 | 0 | G_GNUC_FALLTHROUGH; |
4474 | 0 | case '^': |
4475 | 0 | case '@': |
4476 | | /* ignore these 2 (or 3) */ |
4477 | 0 | continue; |
4478 | | |
4479 | 0 | case '?': |
4480 | | /* attempt to consume one of 'bynqiuxthdsog' */ |
4481 | 0 | { |
4482 | 0 | char s = *type_string++; |
4483 | |
|
4484 | 0 | if (s == '\0' || strchr ("bynqiuxthdsog", s) == NULL) |
4485 | 0 | return FALSE; |
4486 | 0 | } |
4487 | 0 | continue; |
4488 | | |
4489 | 0 | case 'r': |
4490 | | /* ensure it's a tuple */ |
4491 | 0 | if (*type_string != '(') |
4492 | 0 | return FALSE; |
4493 | | |
4494 | 0 | G_GNUC_FALLTHROUGH; |
4495 | 0 | case '*': |
4496 | | /* consume a full type string for the '*' or 'r' */ |
4497 | 0 | if (!g_variant_type_string_scan (type_string, NULL, &type_string)) |
4498 | 0 | return FALSE; |
4499 | | |
4500 | 0 | continue; |
4501 | | |
4502 | 0 | default: |
4503 | | /* attempt to consume exactly one character equal to the format */ |
4504 | 0 | if (format != *type_string++) |
4505 | 0 | return FALSE; |
4506 | 0 | } |
4507 | 0 | } |
4508 | | |
4509 | 0 | return TRUE; |
4510 | 0 | } |
4511 | | |
4512 | | /*< private > |
4513 | | * g_variant_format_string_scan_type: |
4514 | | * @string: a string that may be prefixed with a format string |
4515 | | * @limit: (nullable) (default NULL): a pointer to the end of @string, |
4516 | | * or %NULL |
4517 | | * @endptr: (nullable) (default NULL): location to store the end pointer, |
4518 | | * or %NULL |
4519 | | * |
4520 | | * If @string starts with a valid format string then this function will |
4521 | | * return the type that the format string corresponds to. Otherwise |
4522 | | * this function returns %NULL. |
4523 | | * |
4524 | | * Use g_variant_type_free() to free the return value when you no longer |
4525 | | * need it. |
4526 | | * |
4527 | | * This function is otherwise exactly like |
4528 | | * g_variant_format_string_scan(). |
4529 | | * |
4530 | | * Returns: (nullable): a #GVariantType if there was a valid format string |
4531 | | * |
4532 | | * Since: 2.24 |
4533 | | */ |
4534 | | GVariantType * |
4535 | | g_variant_format_string_scan_type (const gchar *string, |
4536 | | const gchar *limit, |
4537 | | const gchar **endptr) |
4538 | 0 | { |
4539 | 0 | const gchar *my_end; |
4540 | 0 | gchar *dest; |
4541 | 0 | gchar *new; |
4542 | |
|
4543 | 0 | if (endptr == NULL) |
4544 | 0 | endptr = &my_end; |
4545 | |
|
4546 | 0 | if (!g_variant_format_string_scan (string, limit, endptr)) |
4547 | 0 | return NULL; |
4548 | | |
4549 | 0 | dest = new = g_malloc (*endptr - string + 1); |
4550 | 0 | while (string != *endptr) |
4551 | 0 | { |
4552 | 0 | if (*string != '@' && *string != '&' && *string != '^') |
4553 | 0 | *dest++ = *string; |
4554 | 0 | string++; |
4555 | 0 | } |
4556 | 0 | *dest = '\0'; |
4557 | |
|
4558 | 0 | return (GVariantType *) G_VARIANT_TYPE (new); |
4559 | 0 | } |
4560 | | |
4561 | | static gboolean |
4562 | | valid_format_string (const gchar *format_string, |
4563 | | gboolean single, |
4564 | | GVariant *value) |
4565 | 0 | { |
4566 | 0 | const gchar *endptr; |
4567 | 0 | GVariantType *type; |
4568 | |
|
4569 | 0 | type = g_variant_format_string_scan_type (format_string, NULL, &endptr); |
4570 | |
|
4571 | 0 | if G_UNLIKELY (type == NULL || (single && *endptr != '\0')) |
4572 | 0 | { |
4573 | 0 | if (single) |
4574 | 0 | g_critical ("'%s' is not a valid GVariant format string", |
4575 | 0 | format_string); |
4576 | 0 | else |
4577 | 0 | g_critical ("'%s' does not have a valid GVariant format " |
4578 | 0 | "string as a prefix", format_string); |
4579 | |
|
4580 | 0 | if (type != NULL) |
4581 | 0 | g_variant_type_free (type); |
4582 | |
|
4583 | 0 | return FALSE; |
4584 | 0 | } |
4585 | | |
4586 | 0 | if G_UNLIKELY (value && !g_variant_is_of_type (value, type)) |
4587 | 0 | { |
4588 | 0 | gchar *fragment; |
4589 | 0 | gchar *typestr; |
4590 | |
|
4591 | 0 | fragment = g_strndup (format_string, endptr - format_string); |
4592 | 0 | typestr = g_variant_type_dup_string (type); |
4593 | |
|
4594 | 0 | g_critical ("the GVariant format string '%s' has a type of " |
4595 | 0 | "'%s' but the given value has a type of '%s'", |
4596 | 0 | fragment, typestr, g_variant_get_type_string (value)); |
4597 | |
|
4598 | 0 | g_variant_type_free (type); |
4599 | 0 | g_free (fragment); |
4600 | 0 | g_free (typestr); |
4601 | |
|
4602 | 0 | return FALSE; |
4603 | 0 | } |
4604 | | |
4605 | 0 | g_variant_type_free (type); |
4606 | |
|
4607 | 0 | return TRUE; |
4608 | 0 | } |
4609 | | |
4610 | | /* Variable Arguments {{{1 */ |
4611 | | /* We consider 2 main classes of format strings: |
4612 | | * |
4613 | | * - recursive format strings |
4614 | | * these are ones that result in recursion and the collection of |
4615 | | * possibly more than one argument. Maybe types, tuples, |
4616 | | * dictionary entries. |
4617 | | * |
4618 | | * - leaf format string |
4619 | | * these result in the collection of a single argument. |
4620 | | * |
4621 | | * Leaf format strings are further subdivided into two categories: |
4622 | | * |
4623 | | * - single non-null pointer ("nnp") |
4624 | | * these either collect or return a single non-null pointer. |
4625 | | * |
4626 | | * - other |
4627 | | * these collect or return something else (bool, number, etc). |
4628 | | * |
4629 | | * Based on the above, the varargs handling code is split into 4 main parts: |
4630 | | * |
4631 | | * - nnp handling code |
4632 | | * - leaf handling code (which may invoke nnp code) |
4633 | | * - generic handling code (may be recursive, may invoke leaf code) |
4634 | | * - user-facing API (which invokes the generic code) |
4635 | | * |
4636 | | * Each section implements some of the following functions: |
4637 | | * |
4638 | | * - skip: |
4639 | | * collect the arguments for the format string as if |
4640 | | * g_variant_new() had been called, but do nothing with them. used |
4641 | | * for skipping over arguments when constructing a Nothing maybe |
4642 | | * type. |
4643 | | * |
4644 | | * - new: |
4645 | | * create a GVariant * |
4646 | | * |
4647 | | * - get: |
4648 | | * unpack a GVariant * |
4649 | | * |
4650 | | * - free (nnp only): |
4651 | | * free a previously allocated item |
4652 | | */ |
4653 | | |
4654 | | static gboolean |
4655 | | g_variant_format_string_is_leaf (const gchar *str) |
4656 | 0 | { |
4657 | 0 | return str[0] != 'm' && str[0] != '(' && str[0] != '{'; |
4658 | 0 | } |
4659 | | |
4660 | | static gboolean |
4661 | | g_variant_format_string_is_nnp (const gchar *str) |
4662 | 0 | { |
4663 | 0 | return str[0] == 'a' || str[0] == 's' || str[0] == 'o' || str[0] == 'g' || |
4664 | 0 | str[0] == '^' || str[0] == '@' || str[0] == '*' || str[0] == '?' || |
4665 | 0 | str[0] == 'r' || str[0] == 'v' || str[0] == '&'; |
4666 | 0 | } |
4667 | | |
4668 | | /* Single non-null pointer ("nnp") {{{2 */ |
4669 | | static void |
4670 | | g_variant_valist_free_nnp (const gchar *str, |
4671 | | gpointer ptr) |
4672 | 0 | { |
4673 | 0 | switch (*str) |
4674 | 0 | { |
4675 | 0 | case 'a': |
4676 | 0 | g_variant_iter_free (ptr); |
4677 | 0 | break; |
4678 | | |
4679 | 0 | case '^': |
4680 | 0 | if (g_str_has_suffix (str, "y")) |
4681 | 0 | { |
4682 | 0 | if (str[2] != 'a') /* '^a&ay', '^ay' */ |
4683 | 0 | g_free (ptr); |
4684 | 0 | else if (str[1] == 'a') /* '^aay' */ |
4685 | 0 | g_strfreev (ptr); |
4686 | 0 | break; /* '^&ay' */ |
4687 | 0 | } |
4688 | 0 | else if (str[2] != '&') /* '^as', '^ao' */ |
4689 | 0 | g_strfreev (ptr); |
4690 | 0 | else /* '^a&s', '^a&o' */ |
4691 | 0 | g_free (ptr); |
4692 | 0 | break; |
4693 | | |
4694 | 0 | case 's': |
4695 | 0 | case 'o': |
4696 | 0 | case 'g': |
4697 | 0 | g_free (ptr); |
4698 | 0 | break; |
4699 | | |
4700 | 0 | case '@': |
4701 | 0 | case '*': |
4702 | 0 | case '?': |
4703 | 0 | case 'v': |
4704 | 0 | g_variant_unref (ptr); |
4705 | 0 | break; |
4706 | | |
4707 | 0 | case '&': |
4708 | 0 | break; |
4709 | | |
4710 | 0 | default: |
4711 | 0 | g_assert_not_reached (); |
4712 | 0 | } |
4713 | 0 | } |
4714 | | |
4715 | | static gchar |
4716 | | g_variant_scan_convenience (const gchar **str, |
4717 | | gboolean *constant, |
4718 | | guint *arrays) |
4719 | 0 | { |
4720 | 0 | *constant = FALSE; |
4721 | 0 | *arrays = 0; |
4722 | |
|
4723 | 0 | for (;;) |
4724 | 0 | { |
4725 | 0 | char c = *(*str)++; |
4726 | |
|
4727 | 0 | if (c == '&') |
4728 | 0 | *constant = TRUE; |
4729 | | |
4730 | 0 | else if (c == 'a') |
4731 | 0 | (*arrays)++; |
4732 | | |
4733 | 0 | else |
4734 | 0 | return c; |
4735 | 0 | } |
4736 | 0 | } |
4737 | | |
4738 | | static GVariant * |
4739 | | g_variant_valist_new_nnp (const gchar **str, |
4740 | | gpointer ptr) |
4741 | 0 | { |
4742 | 0 | if (**str == '&') |
4743 | 0 | (*str)++; |
4744 | |
|
4745 | 0 | switch (*(*str)++) |
4746 | 0 | { |
4747 | 0 | case 'a': |
4748 | 0 | if (ptr != NULL) |
4749 | 0 | { |
4750 | 0 | const GVariantType *type; |
4751 | 0 | GVariant *value; |
4752 | |
|
4753 | 0 | value = g_variant_builder_end (ptr); |
4754 | 0 | type = g_variant_get_type (value); |
4755 | |
|
4756 | 0 | if G_UNLIKELY (!g_variant_type_is_array (type)) |
4757 | 0 | g_error ("g_variant_new: expected array GVariantBuilder but " |
4758 | 0 | "the built value has type '%s'", |
4759 | 0 | g_variant_get_type_string (value)); |
4760 | |
|
4761 | 0 | type = g_variant_type_element (type); |
4762 | |
|
4763 | 0 | if G_UNLIKELY (!g_variant_type_is_subtype_of (type, (GVariantType *) *str)) |
4764 | 0 | { |
4765 | 0 | gchar *type_string = g_variant_type_dup_string ((GVariantType *) *str); |
4766 | 0 | g_error ("g_variant_new: expected GVariantBuilder array element " |
4767 | 0 | "type '%s' but the built value has element type '%s'", |
4768 | 0 | type_string, g_variant_get_type_string (value) + 1); |
4769 | 0 | g_free (type_string); |
4770 | 0 | } |
4771 | |
|
4772 | 0 | g_variant_type_string_scan (*str, NULL, str); |
4773 | |
|
4774 | 0 | return value; |
4775 | 0 | } |
4776 | 0 | else |
4777 | | |
4778 | | /* special case: NULL pointer for empty array */ |
4779 | 0 | { |
4780 | 0 | const GVariantType *type = (GVariantType *) *str; |
4781 | |
|
4782 | 0 | g_variant_type_string_scan (*str, NULL, str); |
4783 | |
|
4784 | 0 | if G_UNLIKELY (!g_variant_type_is_definite (type)) |
4785 | 0 | g_error ("g_variant_new: NULL pointer given with indefinite " |
4786 | 0 | "array type; unable to determine which type of empty " |
4787 | 0 | "array to construct."); |
4788 | |
|
4789 | 0 | return g_variant_new_array (type, NULL, 0); |
4790 | 0 | } |
4791 | | |
4792 | 0 | case 's': |
4793 | 0 | { |
4794 | 0 | GVariant *value; |
4795 | |
|
4796 | 0 | value = g_variant_new_string (ptr); |
4797 | |
|
4798 | 0 | if (value == NULL) |
4799 | 0 | value = g_variant_new_string ("[Invalid UTF-8]"); |
4800 | |
|
4801 | 0 | return value; |
4802 | 0 | } |
4803 | | |
4804 | 0 | case 'o': |
4805 | 0 | return g_variant_new_object_path (ptr); |
4806 | | |
4807 | 0 | case 'g': |
4808 | 0 | return g_variant_new_signature (ptr); |
4809 | | |
4810 | 0 | case '^': |
4811 | 0 | { |
4812 | 0 | gboolean constant; |
4813 | 0 | guint arrays; |
4814 | 0 | gchar type; |
4815 | |
|
4816 | 0 | type = g_variant_scan_convenience (str, &constant, &arrays); |
4817 | |
|
4818 | 0 | if (type == 's') |
4819 | 0 | return g_variant_new_strv (ptr, -1); |
4820 | | |
4821 | 0 | if (type == 'o') |
4822 | 0 | return g_variant_new_objv (ptr, -1); |
4823 | | |
4824 | 0 | if (arrays > 1) |
4825 | 0 | return g_variant_new_bytestring_array (ptr, -1); |
4826 | | |
4827 | 0 | return g_variant_new_bytestring (ptr); |
4828 | 0 | } |
4829 | | |
4830 | 0 | case '@': |
4831 | 0 | if G_UNLIKELY (!g_variant_is_of_type (ptr, (GVariantType *) *str)) |
4832 | 0 | { |
4833 | 0 | gchar *type_string = g_variant_type_dup_string ((GVariantType *) *str); |
4834 | 0 | g_error ("g_variant_new: expected GVariant of type '%s' but " |
4835 | 0 | "received value has type '%s'", |
4836 | 0 | type_string, g_variant_get_type_string (ptr)); |
4837 | 0 | g_free (type_string); |
4838 | 0 | } |
4839 | |
|
4840 | 0 | g_variant_type_string_scan (*str, NULL, str); |
4841 | |
|
4842 | 0 | return ptr; |
4843 | | |
4844 | 0 | case '*': |
4845 | 0 | return ptr; |
4846 | | |
4847 | 0 | case '?': |
4848 | 0 | if G_UNLIKELY (!g_variant_type_is_basic (g_variant_get_type (ptr))) |
4849 | 0 | g_error ("g_variant_new: format string '?' expects basic-typed " |
4850 | 0 | "GVariant, but received value has type '%s'", |
4851 | 0 | g_variant_get_type_string (ptr)); |
4852 | |
|
4853 | 0 | return ptr; |
4854 | | |
4855 | 0 | case 'r': |
4856 | 0 | if G_UNLIKELY (!g_variant_type_is_tuple (g_variant_get_type (ptr))) |
4857 | 0 | g_error ("g_variant_new: format string 'r' expects tuple-typed " |
4858 | 0 | "GVariant, but received value has type '%s'", |
4859 | 0 | g_variant_get_type_string (ptr)); |
4860 | |
|
4861 | 0 | return ptr; |
4862 | | |
4863 | 0 | case 'v': |
4864 | 0 | return g_variant_new_variant (ptr); |
4865 | | |
4866 | 0 | default: |
4867 | 0 | g_assert_not_reached (); |
4868 | 0 | } |
4869 | 0 | } |
4870 | | |
4871 | | static gpointer |
4872 | | g_variant_valist_get_nnp (const gchar **str, |
4873 | | GVariant *value) |
4874 | 0 | { |
4875 | 0 | switch (*(*str)++) |
4876 | 0 | { |
4877 | 0 | case 'a': |
4878 | 0 | g_variant_type_string_scan (*str, NULL, str); |
4879 | 0 | return g_variant_iter_new (value); |
4880 | | |
4881 | 0 | case '&': |
4882 | 0 | (*str)++; |
4883 | 0 | return (gchar *) g_variant_get_string (value, NULL); |
4884 | | |
4885 | 0 | case 's': |
4886 | 0 | case 'o': |
4887 | 0 | case 'g': |
4888 | 0 | return g_variant_dup_string (value, NULL); |
4889 | | |
4890 | 0 | case '^': |
4891 | 0 | { |
4892 | 0 | gboolean constant; |
4893 | 0 | guint arrays; |
4894 | 0 | gchar type; |
4895 | |
|
4896 | 0 | type = g_variant_scan_convenience (str, &constant, &arrays); |
4897 | |
|
4898 | 0 | if (type == 's') |
4899 | 0 | { |
4900 | 0 | if (constant) |
4901 | 0 | return g_variant_get_strv (value, NULL); |
4902 | 0 | else |
4903 | 0 | return g_variant_dup_strv (value, NULL); |
4904 | 0 | } |
4905 | | |
4906 | 0 | else if (type == 'o') |
4907 | 0 | { |
4908 | 0 | if (constant) |
4909 | 0 | return g_variant_get_objv (value, NULL); |
4910 | 0 | else |
4911 | 0 | return g_variant_dup_objv (value, NULL); |
4912 | 0 | } |
4913 | | |
4914 | 0 | else if (arrays > 1) |
4915 | 0 | { |
4916 | 0 | if (constant) |
4917 | 0 | return g_variant_get_bytestring_array (value, NULL); |
4918 | 0 | else |
4919 | 0 | return g_variant_dup_bytestring_array (value, NULL); |
4920 | 0 | } |
4921 | | |
4922 | 0 | else |
4923 | 0 | { |
4924 | 0 | if (constant) |
4925 | 0 | return (gchar *) g_variant_get_bytestring (value); |
4926 | 0 | else |
4927 | 0 | return g_variant_dup_bytestring (value, NULL); |
4928 | 0 | } |
4929 | 0 | } |
4930 | | |
4931 | 0 | case '@': |
4932 | 0 | g_variant_type_string_scan (*str, NULL, str); |
4933 | 0 | G_GNUC_FALLTHROUGH; |
4934 | |
|
4935 | 0 | case '*': |
4936 | 0 | case '?': |
4937 | 0 | case 'r': |
4938 | 0 | return g_variant_ref (value); |
4939 | | |
4940 | 0 | case 'v': |
4941 | 0 | return g_variant_get_variant (value); |
4942 | | |
4943 | 0 | default: |
4944 | 0 | g_assert_not_reached (); |
4945 | 0 | } |
4946 | 0 | } |
4947 | | |
4948 | | /* Leaves {{{2 */ |
4949 | | static void |
4950 | | g_variant_valist_skip_leaf (const gchar **str, |
4951 | | va_list *app) |
4952 | 0 | { |
4953 | 0 | if (g_variant_format_string_is_nnp (*str)) |
4954 | 0 | { |
4955 | 0 | g_variant_format_string_scan (*str, NULL, str); |
4956 | 0 | va_arg (*app, gpointer); |
4957 | 0 | return; |
4958 | 0 | } |
4959 | | |
4960 | 0 | switch (*(*str)++) |
4961 | 0 | { |
4962 | 0 | case 'b': |
4963 | 0 | case 'y': |
4964 | 0 | case 'n': |
4965 | 0 | case 'q': |
4966 | 0 | case 'i': |
4967 | 0 | case 'u': |
4968 | 0 | case 'h': |
4969 | 0 | va_arg (*app, int); |
4970 | 0 | return; |
4971 | | |
4972 | 0 | case 'x': |
4973 | 0 | case 't': |
4974 | 0 | va_arg (*app, guint64); |
4975 | 0 | return; |
4976 | | |
4977 | 0 | case 'd': |
4978 | 0 | va_arg (*app, gdouble); |
4979 | 0 | return; |
4980 | | |
4981 | 0 | default: |
4982 | 0 | g_assert_not_reached (); |
4983 | 0 | } |
4984 | 0 | } |
4985 | | |
4986 | | static GVariant * |
4987 | | g_variant_valist_new_leaf (const gchar **str, |
4988 | | va_list *app) |
4989 | 0 | { |
4990 | 0 | if (g_variant_format_string_is_nnp (*str)) |
4991 | 0 | return g_variant_valist_new_nnp (str, va_arg (*app, gpointer)); |
4992 | | |
4993 | 0 | switch (*(*str)++) |
4994 | 0 | { |
4995 | 0 | case 'b': |
4996 | 0 | return g_variant_new_boolean (va_arg (*app, gboolean)); |
4997 | | |
4998 | 0 | case 'y': |
4999 | 0 | return g_variant_new_byte (va_arg (*app, guint)); |
5000 | | |
5001 | 0 | case 'n': |
5002 | 0 | return g_variant_new_int16 (va_arg (*app, gint)); |
5003 | | |
5004 | 0 | case 'q': |
5005 | 0 | return g_variant_new_uint16 (va_arg (*app, guint)); |
5006 | | |
5007 | 0 | case 'i': |
5008 | 0 | return g_variant_new_int32 (va_arg (*app, gint)); |
5009 | | |
5010 | 0 | case 'u': |
5011 | 0 | return g_variant_new_uint32 (va_arg (*app, guint)); |
5012 | | |
5013 | 0 | case 'x': |
5014 | 0 | return g_variant_new_int64 (va_arg (*app, gint64)); |
5015 | | |
5016 | 0 | case 't': |
5017 | 0 | return g_variant_new_uint64 (va_arg (*app, guint64)); |
5018 | | |
5019 | 0 | case 'h': |
5020 | 0 | return g_variant_new_handle (va_arg (*app, gint)); |
5021 | | |
5022 | 0 | case 'd': |
5023 | 0 | return g_variant_new_double (va_arg (*app, gdouble)); |
5024 | | |
5025 | 0 | default: |
5026 | 0 | g_assert_not_reached (); |
5027 | 0 | } |
5028 | 0 | } |
5029 | | |
5030 | | /* The code below assumes this */ |
5031 | | G_STATIC_ASSERT (sizeof (gboolean) == sizeof (guint32)); |
5032 | | G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64)); |
5033 | | |
5034 | | static void |
5035 | | g_variant_valist_get_leaf (const gchar **str, |
5036 | | GVariant *value, |
5037 | | gboolean free, |
5038 | | va_list *app) |
5039 | 0 | { |
5040 | 0 | gpointer ptr = va_arg (*app, gpointer); |
5041 | |
|
5042 | 0 | if (ptr == NULL) |
5043 | 0 | { |
5044 | 0 | g_variant_format_string_scan (*str, NULL, str); |
5045 | 0 | return; |
5046 | 0 | } |
5047 | | |
5048 | 0 | if (g_variant_format_string_is_nnp (*str)) |
5049 | 0 | { |
5050 | 0 | gpointer *nnp = (gpointer *) ptr; |
5051 | |
|
5052 | 0 | if (free && *nnp != NULL) |
5053 | 0 | g_variant_valist_free_nnp (*str, *nnp); |
5054 | |
|
5055 | 0 | *nnp = NULL; |
5056 | |
|
5057 | 0 | if (value != NULL) |
5058 | 0 | *nnp = g_variant_valist_get_nnp (str, value); |
5059 | 0 | else |
5060 | 0 | g_variant_format_string_scan (*str, NULL, str); |
5061 | |
|
5062 | 0 | return; |
5063 | 0 | } |
5064 | | |
5065 | 0 | if (value != NULL) |
5066 | 0 | { |
5067 | 0 | switch (*(*str)++) |
5068 | 0 | { |
5069 | 0 | case 'b': |
5070 | 0 | *(gboolean *) ptr = g_variant_get_boolean (value); |
5071 | 0 | return; |
5072 | | |
5073 | 0 | case 'y': |
5074 | 0 | *(guint8 *) ptr = g_variant_get_byte (value); |
5075 | 0 | return; |
5076 | | |
5077 | 0 | case 'n': |
5078 | 0 | *(gint16 *) ptr = g_variant_get_int16 (value); |
5079 | 0 | return; |
5080 | | |
5081 | 0 | case 'q': |
5082 | 0 | *(guint16 *) ptr = g_variant_get_uint16 (value); |
5083 | 0 | return; |
5084 | | |
5085 | 0 | case 'i': |
5086 | 0 | *(gint32 *) ptr = g_variant_get_int32 (value); |
5087 | 0 | return; |
5088 | | |
5089 | 0 | case 'u': |
5090 | 0 | *(guint32 *) ptr = g_variant_get_uint32 (value); |
5091 | 0 | return; |
5092 | | |
5093 | 0 | case 'x': |
5094 | 0 | *(gint64 *) ptr = g_variant_get_int64 (value); |
5095 | 0 | return; |
5096 | | |
5097 | 0 | case 't': |
5098 | 0 | *(guint64 *) ptr = g_variant_get_uint64 (value); |
5099 | 0 | return; |
5100 | | |
5101 | 0 | case 'h': |
5102 | 0 | *(gint32 *) ptr = g_variant_get_handle (value); |
5103 | 0 | return; |
5104 | | |
5105 | 0 | case 'd': |
5106 | 0 | *(gdouble *) ptr = g_variant_get_double (value); |
5107 | 0 | return; |
5108 | 0 | } |
5109 | 0 | } |
5110 | 0 | else |
5111 | 0 | { |
5112 | 0 | switch (*(*str)++) |
5113 | 0 | { |
5114 | 0 | case 'y': |
5115 | 0 | *(guint8 *) ptr = 0; |
5116 | 0 | return; |
5117 | | |
5118 | 0 | case 'n': |
5119 | 0 | case 'q': |
5120 | 0 | *(guint16 *) ptr = 0; |
5121 | 0 | return; |
5122 | | |
5123 | 0 | case 'i': |
5124 | 0 | case 'u': |
5125 | 0 | case 'h': |
5126 | 0 | case 'b': |
5127 | 0 | *(guint32 *) ptr = 0; |
5128 | 0 | return; |
5129 | | |
5130 | 0 | case 'x': |
5131 | 0 | case 't': |
5132 | 0 | case 'd': |
5133 | 0 | *(guint64 *) ptr = 0; |
5134 | 0 | return; |
5135 | 0 | } |
5136 | 0 | } |
5137 | | |
5138 | 0 | g_assert_not_reached (); |
5139 | 0 | } |
5140 | | |
5141 | | /* Generic (recursive) {{{2 */ |
5142 | | static void |
5143 | | g_variant_valist_skip (const gchar **str, |
5144 | | va_list *app) |
5145 | 0 | { |
5146 | 0 | if (g_variant_format_string_is_leaf (*str)) |
5147 | 0 | g_variant_valist_skip_leaf (str, app); |
5148 | | |
5149 | 0 | else if (**str == 'm') /* maybe */ |
5150 | 0 | { |
5151 | 0 | (*str)++; |
5152 | |
|
5153 | 0 | if (!g_variant_format_string_is_nnp (*str)) |
5154 | 0 | va_arg (*app, gboolean); |
5155 | |
|
5156 | 0 | g_variant_valist_skip (str, app); |
5157 | 0 | } |
5158 | 0 | else /* tuple, dictionary entry */ |
5159 | 0 | { |
5160 | 0 | g_assert (**str == '(' || **str == '{'); |
5161 | 0 | (*str)++; |
5162 | 0 | while (**str != ')' && **str != '}') |
5163 | 0 | g_variant_valist_skip (str, app); |
5164 | 0 | (*str)++; |
5165 | 0 | } |
5166 | 0 | } |
5167 | | |
5168 | | static GVariant * |
5169 | | g_variant_valist_new (const gchar **str, |
5170 | | va_list *app) |
5171 | 0 | { |
5172 | 0 | if (g_variant_format_string_is_leaf (*str)) |
5173 | 0 | return g_variant_valist_new_leaf (str, app); |
5174 | | |
5175 | 0 | if (**str == 'm') /* maybe */ |
5176 | 0 | { |
5177 | 0 | GVariantType *type = NULL; |
5178 | 0 | GVariant *value = NULL; |
5179 | |
|
5180 | 0 | (*str)++; |
5181 | |
|
5182 | 0 | if (g_variant_format_string_is_nnp (*str)) |
5183 | 0 | { |
5184 | 0 | gpointer nnp = va_arg (*app, gpointer); |
5185 | |
|
5186 | 0 | if (nnp != NULL) |
5187 | 0 | value = g_variant_valist_new_nnp (str, nnp); |
5188 | 0 | else |
5189 | 0 | type = g_variant_format_string_scan_type (*str, NULL, str); |
5190 | 0 | } |
5191 | 0 | else |
5192 | 0 | { |
5193 | 0 | gboolean just = va_arg (*app, gboolean); |
5194 | |
|
5195 | 0 | if (just) |
5196 | 0 | value = g_variant_valist_new (str, app); |
5197 | 0 | else |
5198 | 0 | { |
5199 | 0 | type = g_variant_format_string_scan_type (*str, NULL, NULL); |
5200 | 0 | g_variant_valist_skip (str, app); |
5201 | 0 | } |
5202 | 0 | } |
5203 | |
|
5204 | 0 | value = g_variant_new_maybe (type, value); |
5205 | |
|
5206 | 0 | if (type != NULL) |
5207 | 0 | g_variant_type_free (type); |
5208 | |
|
5209 | 0 | return value; |
5210 | 0 | } |
5211 | 0 | else /* tuple, dictionary entry */ |
5212 | 0 | { |
5213 | 0 | GVariantBuilder b; |
5214 | |
|
5215 | 0 | if (**str == '(') |
5216 | 0 | g_variant_builder_init (&b, G_VARIANT_TYPE_TUPLE); |
5217 | 0 | else |
5218 | 0 | { |
5219 | 0 | g_assert (**str == '{'); |
5220 | 0 | g_variant_builder_init (&b, G_VARIANT_TYPE_DICT_ENTRY); |
5221 | 0 | } |
5222 | | |
5223 | 0 | (*str)++; /* '(' */ |
5224 | 0 | while (**str != ')' && **str != '}') |
5225 | 0 | g_variant_builder_add_value (&b, g_variant_valist_new (str, app)); |
5226 | 0 | (*str)++; /* ')' */ |
5227 | |
|
5228 | 0 | return g_variant_builder_end (&b); |
5229 | 0 | } |
5230 | 0 | } |
5231 | | |
5232 | | static void |
5233 | | g_variant_valist_get (const gchar **str, |
5234 | | GVariant *value, |
5235 | | gboolean free, |
5236 | | va_list *app) |
5237 | 0 | { |
5238 | 0 | if (g_variant_format_string_is_leaf (*str)) |
5239 | 0 | g_variant_valist_get_leaf (str, value, free, app); |
5240 | | |
5241 | 0 | else if (**str == 'm') |
5242 | 0 | { |
5243 | 0 | (*str)++; |
5244 | |
|
5245 | 0 | if (value != NULL) |
5246 | 0 | value = g_variant_get_maybe (value); |
5247 | |
|
5248 | 0 | if (!g_variant_format_string_is_nnp (*str)) |
5249 | 0 | { |
5250 | 0 | gboolean *ptr = va_arg (*app, gboolean *); |
5251 | |
|
5252 | 0 | if (ptr != NULL) |
5253 | 0 | *ptr = value != NULL; |
5254 | 0 | } |
5255 | |
|
5256 | 0 | g_variant_valist_get (str, value, free, app); |
5257 | |
|
5258 | 0 | if (value != NULL) |
5259 | 0 | g_variant_unref (value); |
5260 | 0 | } |
5261 | | |
5262 | 0 | else /* tuple, dictionary entry */ |
5263 | 0 | { |
5264 | 0 | gint index = 0; |
5265 | |
|
5266 | 0 | g_assert (**str == '(' || **str == '{'); |
5267 | | |
5268 | 0 | (*str)++; |
5269 | 0 | while (**str != ')' && **str != '}') |
5270 | 0 | { |
5271 | 0 | if (value != NULL) |
5272 | 0 | { |
5273 | 0 | GVariant *child = g_variant_get_child_value (value, index++); |
5274 | 0 | g_variant_valist_get (str, child, free, app); |
5275 | 0 | g_variant_unref (child); |
5276 | 0 | } |
5277 | 0 | else |
5278 | 0 | g_variant_valist_get (str, NULL, free, app); |
5279 | 0 | } |
5280 | 0 | (*str)++; |
5281 | 0 | } |
5282 | 0 | } |
5283 | | |
5284 | | /* User-facing API {{{2 */ |
5285 | | /** |
5286 | | * g_variant_new: (skip) |
5287 | | * @format_string: a #GVariant format string |
5288 | | * @...: arguments, as per @format_string |
5289 | | * |
5290 | | * Creates a new #GVariant instance. |
5291 | | * |
5292 | | * Think of this function as an analogue to g_strdup_printf(). |
5293 | | * |
5294 | | * The type of the created instance and the arguments that are expected |
5295 | | * by this function are determined by @format_string. See the section on |
5296 | | * [GVariant format strings][gvariant-format-strings]. Please note that |
5297 | | * the syntax of the format string is very likely to be extended in the |
5298 | | * future. |
5299 | | * |
5300 | | * The first character of the format string must not be '*' '?' '@' or |
5301 | | * 'r'; in essence, a new #GVariant must always be constructed by this |
5302 | | * function (and not merely passed through it unmodified). |
5303 | | * |
5304 | | * Note that the arguments must be of the correct width for their types |
5305 | | * specified in @format_string. This can be achieved by casting them. See |
5306 | | * the [GVariant varargs documentation][gvariant-varargs]. |
5307 | | * |
5308 | | * |[<!-- language="C" --> |
5309 | | * MyFlags some_flags = FLAG_ONE | FLAG_TWO; |
5310 | | * const gchar *some_strings[] = { "a", "b", "c", NULL }; |
5311 | | * GVariant *new_variant; |
5312 | | * |
5313 | | * new_variant = g_variant_new ("(t^as)", |
5314 | | * // This cast is required. |
5315 | | * (guint64) some_flags, |
5316 | | * some_strings); |
5317 | | * ]| |
5318 | | * |
5319 | | * Returns: a new floating #GVariant instance |
5320 | | * |
5321 | | * Since: 2.24 |
5322 | | **/ |
5323 | | GVariant * |
5324 | | g_variant_new (const gchar *format_string, |
5325 | | ...) |
5326 | 0 | { |
5327 | 0 | GVariant *value; |
5328 | 0 | va_list ap; |
5329 | |
|
5330 | 0 | g_return_val_if_fail (valid_format_string (format_string, TRUE, NULL) && |
5331 | 0 | format_string[0] != '?' && format_string[0] != '@' && |
5332 | 0 | format_string[0] != '*' && format_string[0] != 'r', |
5333 | 0 | NULL); |
5334 | | |
5335 | 0 | va_start (ap, format_string); |
5336 | 0 | value = g_variant_new_va (format_string, NULL, &ap); |
5337 | 0 | va_end (ap); |
5338 | |
|
5339 | 0 | return value; |
5340 | 0 | } |
5341 | | |
5342 | | /** |
5343 | | * g_variant_new_va: (skip) |
5344 | | * @format_string: a string that is prefixed with a format string |
5345 | | * @endptr: (nullable) (default NULL): location to store the end pointer, |
5346 | | * or %NULL |
5347 | | * @app: a pointer to a #va_list |
5348 | | * |
5349 | | * This function is intended to be used by libraries based on |
5350 | | * #GVariant that want to provide g_variant_new()-like functionality |
5351 | | * to their users. |
5352 | | * |
5353 | | * The API is more general than g_variant_new() to allow a wider range |
5354 | | * of possible uses. |
5355 | | * |
5356 | | * @format_string must still point to a valid format string, but it only |
5357 | | * needs to be nul-terminated if @endptr is %NULL. If @endptr is |
5358 | | * non-%NULL then it is updated to point to the first character past the |
5359 | | * end of the format string. |
5360 | | * |
5361 | | * @app is a pointer to a #va_list. The arguments, according to |
5362 | | * @format_string, are collected from this #va_list and the list is left |
5363 | | * pointing to the argument following the last. |
5364 | | * |
5365 | | * Note that the arguments in @app must be of the correct width for their |
5366 | | * types specified in @format_string when collected into the #va_list. |
5367 | | * See the [GVariant varargs documentation][gvariant-varargs]. |
5368 | | * |
5369 | | * These two generalisations allow mixing of multiple calls to |
5370 | | * g_variant_new_va() and g_variant_get_va() within a single actual |
5371 | | * varargs call by the user. |
5372 | | * |
5373 | | * The return value will be floating if it was a newly created GVariant |
5374 | | * instance (for example, if the format string was "(ii)"). In the case |
5375 | | * that the format_string was '*', '?', 'r', or a format starting with |
5376 | | * '@' then the collected #GVariant pointer will be returned unmodified, |
5377 | | * without adding any additional references. |
5378 | | * |
5379 | | * In order to behave correctly in all cases it is necessary for the |
5380 | | * calling function to g_variant_ref_sink() the return result before |
5381 | | * returning control to the user that originally provided the pointer. |
5382 | | * At this point, the caller will have their own full reference to the |
5383 | | * result. This can also be done by adding the result to a container, |
5384 | | * or by passing it to another g_variant_new() call. |
5385 | | * |
5386 | | * Returns: a new, usually floating, #GVariant |
5387 | | * |
5388 | | * Since: 2.24 |
5389 | | **/ |
5390 | | GVariant * |
5391 | | g_variant_new_va (const gchar *format_string, |
5392 | | const gchar **endptr, |
5393 | | va_list *app) |
5394 | 0 | { |
5395 | 0 | GVariant *value; |
5396 | |
|
5397 | 0 | g_return_val_if_fail (valid_format_string (format_string, !endptr, NULL), |
5398 | 0 | NULL); |
5399 | 0 | g_return_val_if_fail (app != NULL, NULL); |
5400 | | |
5401 | 0 | value = g_variant_valist_new (&format_string, app); |
5402 | |
|
5403 | 0 | if (endptr != NULL) |
5404 | 0 | *endptr = format_string; |
5405 | |
|
5406 | 0 | return value; |
5407 | 0 | } |
5408 | | |
5409 | | /** |
5410 | | * g_variant_get: (skip) |
5411 | | * @value: a #GVariant instance |
5412 | | * @format_string: a #GVariant format string |
5413 | | * @...: arguments, as per @format_string |
5414 | | * |
5415 | | * Deconstructs a #GVariant instance. |
5416 | | * |
5417 | | * Think of this function as an analogue to scanf(). |
5418 | | * |
5419 | | * The arguments that are expected by this function are entirely |
5420 | | * determined by @format_string. @format_string also restricts the |
5421 | | * permissible types of @value. It is an error to give a value with |
5422 | | * an incompatible type. See the section on |
5423 | | * [GVariant format strings][gvariant-format-strings]. |
5424 | | * Please note that the syntax of the format string is very likely to be |
5425 | | * extended in the future. |
5426 | | * |
5427 | | * @format_string determines the C types that are used for unpacking |
5428 | | * the values and also determines if the values are copied or borrowed, |
5429 | | * see the section on |
5430 | | * [GVariant format strings][gvariant-format-strings-pointers]. |
5431 | | * |
5432 | | * Since: 2.24 |
5433 | | **/ |
5434 | | void |
5435 | | g_variant_get (GVariant *value, |
5436 | | const gchar *format_string, |
5437 | | ...) |
5438 | 0 | { |
5439 | 0 | va_list ap; |
5440 | |
|
5441 | 0 | g_return_if_fail (value != NULL); |
5442 | 0 | g_return_if_fail (valid_format_string (format_string, TRUE, value)); |
5443 | | |
5444 | | /* if any direct-pointer-access formats are in use, flatten first */ |
5445 | 0 | if (strchr (format_string, '&')) |
5446 | 0 | g_variant_get_data (value); |
5447 | |
|
5448 | 0 | va_start (ap, format_string); |
5449 | 0 | g_variant_get_va (value, format_string, NULL, &ap); |
5450 | 0 | va_end (ap); |
5451 | 0 | } |
5452 | | |
5453 | | /** |
5454 | | * g_variant_get_va: (skip) |
5455 | | * @value: a #GVariant |
5456 | | * @format_string: a string that is prefixed with a format string |
5457 | | * @endptr: (nullable) (default NULL): location to store the end pointer, |
5458 | | * or %NULL |
5459 | | * @app: a pointer to a #va_list |
5460 | | * |
5461 | | * This function is intended to be used by libraries based on #GVariant |
5462 | | * that want to provide g_variant_get()-like functionality to their |
5463 | | * users. |
5464 | | * |
5465 | | * The API is more general than g_variant_get() to allow a wider range |
5466 | | * of possible uses. |
5467 | | * |
5468 | | * @format_string must still point to a valid format string, but it only |
5469 | | * need to be nul-terminated if @endptr is %NULL. If @endptr is |
5470 | | * non-%NULL then it is updated to point to the first character past the |
5471 | | * end of the format string. |
5472 | | * |
5473 | | * @app is a pointer to a #va_list. The arguments, according to |
5474 | | * @format_string, are collected from this #va_list and the list is left |
5475 | | * pointing to the argument following the last. |
5476 | | * |
5477 | | * These two generalisations allow mixing of multiple calls to |
5478 | | * g_variant_new_va() and g_variant_get_va() within a single actual |
5479 | | * varargs call by the user. |
5480 | | * |
5481 | | * @format_string determines the C types that are used for unpacking |
5482 | | * the values and also determines if the values are copied or borrowed, |
5483 | | * see the section on |
5484 | | * [GVariant format strings][gvariant-format-strings-pointers]. |
5485 | | * |
5486 | | * Since: 2.24 |
5487 | | **/ |
5488 | | void |
5489 | | g_variant_get_va (GVariant *value, |
5490 | | const gchar *format_string, |
5491 | | const gchar **endptr, |
5492 | | va_list *app) |
5493 | 0 | { |
5494 | 0 | g_return_if_fail (valid_format_string (format_string, !endptr, value)); |
5495 | 0 | g_return_if_fail (value != NULL); |
5496 | 0 | g_return_if_fail (app != NULL); |
5497 | | |
5498 | | /* if any direct-pointer-access formats are in use, flatten first */ |
5499 | 0 | if (strchr (format_string, '&')) |
5500 | 0 | g_variant_get_data (value); |
5501 | |
|
5502 | 0 | g_variant_valist_get (&format_string, value, FALSE, app); |
5503 | |
|
5504 | 0 | if (endptr != NULL) |
5505 | 0 | *endptr = format_string; |
5506 | 0 | } |
5507 | | |
5508 | | /* Varargs-enabled Utility Functions {{{1 */ |
5509 | | |
5510 | | /** |
5511 | | * g_variant_builder_add: (skip) |
5512 | | * @builder: a #GVariantBuilder |
5513 | | * @format_string: a #GVariant varargs format string |
5514 | | * @...: arguments, as per @format_string |
5515 | | * |
5516 | | * Adds to a #GVariantBuilder. |
5517 | | * |
5518 | | * This call is a convenience wrapper that is exactly equivalent to |
5519 | | * calling g_variant_new() followed by g_variant_builder_add_value(). |
5520 | | * |
5521 | | * Note that the arguments must be of the correct width for their types |
5522 | | * specified in @format_string. This can be achieved by casting them. See |
5523 | | * the [GVariant varargs documentation][gvariant-varargs]. |
5524 | | * |
5525 | | * This function might be used as follows: |
5526 | | * |
5527 | | * |[<!-- language="C" --> |
5528 | | * GVariant * |
5529 | | * make_pointless_dictionary (void) |
5530 | | * { |
5531 | | * GVariantBuilder builder; |
5532 | | * int i; |
5533 | | * |
5534 | | * g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
5535 | | * for (i = 0; i < 16; i++) |
5536 | | * { |
5537 | | * gchar buf[3]; |
5538 | | * |
5539 | | * sprintf (buf, "%d", i); |
5540 | | * g_variant_builder_add (&builder, "{is}", i, buf); |
5541 | | * } |
5542 | | * |
5543 | | * return g_variant_builder_end (&builder); |
5544 | | * } |
5545 | | * ]| |
5546 | | * |
5547 | | * Since: 2.24 |
5548 | | */ |
5549 | | void |
5550 | | g_variant_builder_add (GVariantBuilder *builder, |
5551 | | const gchar *format_string, |
5552 | | ...) |
5553 | 0 | { |
5554 | 0 | GVariant *variant; |
5555 | 0 | va_list ap; |
5556 | |
|
5557 | 0 | va_start (ap, format_string); |
5558 | 0 | variant = g_variant_new_va (format_string, NULL, &ap); |
5559 | 0 | va_end (ap); |
5560 | |
|
5561 | 0 | g_variant_builder_add_value (builder, variant); |
5562 | 0 | } |
5563 | | |
5564 | | /** |
5565 | | * g_variant_get_child: (skip) |
5566 | | * @value: a container #GVariant |
5567 | | * @index_: the index of the child to deconstruct |
5568 | | * @format_string: a #GVariant format string |
5569 | | * @...: arguments, as per @format_string |
5570 | | * |
5571 | | * Reads a child item out of a container #GVariant instance and |
5572 | | * deconstructs it according to @format_string. This call is |
5573 | | * essentially a combination of g_variant_get_child_value() and |
5574 | | * g_variant_get(). |
5575 | | * |
5576 | | * @format_string determines the C types that are used for unpacking |
5577 | | * the values and also determines if the values are copied or borrowed, |
5578 | | * see the section on |
5579 | | * [GVariant format strings][gvariant-format-strings-pointers]. |
5580 | | * |
5581 | | * Since: 2.24 |
5582 | | **/ |
5583 | | void |
5584 | | g_variant_get_child (GVariant *value, |
5585 | | gsize index_, |
5586 | | const gchar *format_string, |
5587 | | ...) |
5588 | 0 | { |
5589 | 0 | GVariant *child; |
5590 | 0 | va_list ap; |
5591 | | |
5592 | | /* if any direct-pointer-access formats are in use, flatten first */ |
5593 | 0 | if (strchr (format_string, '&')) |
5594 | 0 | g_variant_get_data (value); |
5595 | |
|
5596 | 0 | child = g_variant_get_child_value (value, index_); |
5597 | 0 | g_return_if_fail (valid_format_string (format_string, TRUE, child)); |
5598 | | |
5599 | 0 | va_start (ap, format_string); |
5600 | 0 | g_variant_get_va (child, format_string, NULL, &ap); |
5601 | 0 | va_end (ap); |
5602 | |
|
5603 | 0 | g_variant_unref (child); |
5604 | 0 | } |
5605 | | |
5606 | | /** |
5607 | | * g_variant_iter_next: (skip) |
5608 | | * @iter: a #GVariantIter |
5609 | | * @format_string: a GVariant format string |
5610 | | * @...: the arguments to unpack the value into |
5611 | | * |
5612 | | * Gets the next item in the container and unpacks it into the variable |
5613 | | * argument list according to @format_string, returning %TRUE. |
5614 | | * |
5615 | | * If no more items remain then %FALSE is returned. |
5616 | | * |
5617 | | * All of the pointers given on the variable arguments list of this |
5618 | | * function are assumed to point at uninitialised memory. It is the |
5619 | | * responsibility of the caller to free all of the values returned by |
5620 | | * the unpacking process. |
5621 | | * |
5622 | | * Here is an example for memory management with g_variant_iter_next(): |
5623 | | * |[<!-- language="C" --> |
5624 | | * // Iterates a dictionary of type 'a{sv}' |
5625 | | * void |
5626 | | * iterate_dictionary (GVariant *dictionary) |
5627 | | * { |
5628 | | * GVariantIter iter; |
5629 | | * GVariant *value; |
5630 | | * gchar *key; |
5631 | | * |
5632 | | * g_variant_iter_init (&iter, dictionary); |
5633 | | * while (g_variant_iter_next (&iter, "{sv}", &key, &value)) |
5634 | | * { |
5635 | | * g_print ("Item '%s' has type '%s'\n", key, |
5636 | | * g_variant_get_type_string (value)); |
5637 | | * |
5638 | | * // must free data for ourselves |
5639 | | * g_variant_unref (value); |
5640 | | * g_free (key); |
5641 | | * } |
5642 | | * } |
5643 | | * ]| |
5644 | | * |
5645 | | * For a solution that is likely to be more convenient to C programmers |
5646 | | * when dealing with loops, see g_variant_iter_loop(). |
5647 | | * |
5648 | | * @format_string determines the C types that are used for unpacking |
5649 | | * the values and also determines if the values are copied or borrowed. |
5650 | | * |
5651 | | * See the section on |
5652 | | * [GVariant format strings][gvariant-format-strings-pointers]. |
5653 | | * |
5654 | | * Returns: %TRUE if a value was unpacked, or %FALSE if there as no value |
5655 | | * |
5656 | | * Since: 2.24 |
5657 | | **/ |
5658 | | gboolean |
5659 | | g_variant_iter_next (GVariantIter *iter, |
5660 | | const gchar *format_string, |
5661 | | ...) |
5662 | 0 | { |
5663 | 0 | GVariant *value; |
5664 | |
|
5665 | 0 | value = g_variant_iter_next_value (iter); |
5666 | |
|
5667 | 0 | g_return_val_if_fail (valid_format_string (format_string, TRUE, value), |
5668 | 0 | FALSE); |
5669 | | |
5670 | 0 | if (value != NULL) |
5671 | 0 | { |
5672 | 0 | va_list ap; |
5673 | |
|
5674 | 0 | va_start (ap, format_string); |
5675 | 0 | g_variant_valist_get (&format_string, value, FALSE, &ap); |
5676 | 0 | va_end (ap); |
5677 | |
|
5678 | 0 | g_variant_unref (value); |
5679 | 0 | } |
5680 | |
|
5681 | 0 | return value != NULL; |
5682 | 0 | } |
5683 | | |
5684 | | /** |
5685 | | * g_variant_iter_loop: (skip) |
5686 | | * @iter: a #GVariantIter |
5687 | | * @format_string: a GVariant format string |
5688 | | * @...: the arguments to unpack the value into |
5689 | | * |
5690 | | * Gets the next item in the container and unpacks it into the variable |
5691 | | * argument list according to @format_string, returning %TRUE. |
5692 | | * |
5693 | | * If no more items remain then %FALSE is returned. |
5694 | | * |
5695 | | * On the first call to this function, the pointers appearing on the |
5696 | | * variable argument list are assumed to point at uninitialised memory. |
5697 | | * On the second and later calls, it is assumed that the same pointers |
5698 | | * will be given and that they will point to the memory as set by the |
5699 | | * previous call to this function. This allows the previous values to |
5700 | | * be freed, as appropriate. |
5701 | | * |
5702 | | * This function is intended to be used with a while loop as |
5703 | | * demonstrated in the following example. This function can only be |
5704 | | * used when iterating over an array. It is only valid to call this |
5705 | | * function with a string constant for the format string and the same |
5706 | | * string constant must be used each time. Mixing calls to this |
5707 | | * function and g_variant_iter_next() or g_variant_iter_next_value() on |
5708 | | * the same iterator causes undefined behavior. |
5709 | | * |
5710 | | * If you break out of a such a while loop using g_variant_iter_loop() then |
5711 | | * you must free or unreference all the unpacked values as you would with |
5712 | | * g_variant_get(). Failure to do so will cause a memory leak. |
5713 | | * |
5714 | | * Here is an example for memory management with g_variant_iter_loop(): |
5715 | | * |[<!-- language="C" --> |
5716 | | * // Iterates a dictionary of type 'a{sv}' |
5717 | | * void |
5718 | | * iterate_dictionary (GVariant *dictionary) |
5719 | | * { |
5720 | | * GVariantIter iter; |
5721 | | * GVariant *value; |
5722 | | * gchar *key; |
5723 | | * |
5724 | | * g_variant_iter_init (&iter, dictionary); |
5725 | | * while (g_variant_iter_loop (&iter, "{sv}", &key, &value)) |
5726 | | * { |
5727 | | * g_print ("Item '%s' has type '%s'\n", key, |
5728 | | * g_variant_get_type_string (value)); |
5729 | | * |
5730 | | * // no need to free 'key' and 'value' here |
5731 | | * // unless breaking out of this loop |
5732 | | * } |
5733 | | * } |
5734 | | * ]| |
5735 | | * |
5736 | | * For most cases you should use g_variant_iter_next(). |
5737 | | * |
5738 | | * This function is really only useful when unpacking into #GVariant or |
5739 | | * #GVariantIter in order to allow you to skip the call to |
5740 | | * g_variant_unref() or g_variant_iter_free(). |
5741 | | * |
5742 | | * For example, if you are only looping over simple integer and string |
5743 | | * types, g_variant_iter_next() is definitely preferred. For string |
5744 | | * types, use the '&' prefix to avoid allocating any memory at all (and |
5745 | | * thereby avoiding the need to free anything as well). |
5746 | | * |
5747 | | * @format_string determines the C types that are used for unpacking |
5748 | | * the values and also determines if the values are copied or borrowed. |
5749 | | * |
5750 | | * See the section on |
5751 | | * [GVariant format strings][gvariant-format-strings-pointers]. |
5752 | | * |
5753 | | * Returns: %TRUE if a value was unpacked, or %FALSE if there was no |
5754 | | * value |
5755 | | * |
5756 | | * Since: 2.24 |
5757 | | **/ |
5758 | | gboolean |
5759 | | g_variant_iter_loop (GVariantIter *iter, |
5760 | | const gchar *format_string, |
5761 | | ...) |
5762 | 0 | { |
5763 | 0 | gboolean first_time = GVSI(iter)->loop_format == NULL; |
5764 | 0 | GVariant *value; |
5765 | 0 | va_list ap; |
5766 | |
|
5767 | 0 | g_return_val_if_fail (first_time || |
5768 | 0 | format_string == GVSI(iter)->loop_format, |
5769 | 0 | FALSE); |
5770 | | |
5771 | 0 | if (first_time) |
5772 | 0 | { |
5773 | 0 | TYPE_CHECK (GVSI(iter)->value, G_VARIANT_TYPE_ARRAY, FALSE); |
5774 | 0 | GVSI(iter)->loop_format = format_string; |
5775 | |
|
5776 | 0 | if (strchr (format_string, '&')) |
5777 | 0 | g_variant_get_data (GVSI(iter)->value); |
5778 | 0 | } |
5779 | | |
5780 | 0 | value = g_variant_iter_next_value (iter); |
5781 | |
|
5782 | 0 | g_return_val_if_fail (!first_time || |
5783 | 0 | valid_format_string (format_string, TRUE, value), |
5784 | 0 | FALSE); |
5785 | | |
5786 | 0 | va_start (ap, format_string); |
5787 | 0 | g_variant_valist_get (&format_string, value, !first_time, &ap); |
5788 | 0 | va_end (ap); |
5789 | |
|
5790 | 0 | if (value != NULL) |
5791 | 0 | g_variant_unref (value); |
5792 | |
|
5793 | 0 | return value != NULL; |
5794 | 0 | } |
5795 | | |
5796 | | /* Serialised data {{{1 */ |
5797 | | static GVariant * |
5798 | | g_variant_deep_copy (GVariant *value) |
5799 | 0 | { |
5800 | 0 | switch (g_variant_classify (value)) |
5801 | 0 | { |
5802 | 0 | case G_VARIANT_CLASS_MAYBE: |
5803 | 0 | case G_VARIANT_CLASS_ARRAY: |
5804 | 0 | case G_VARIANT_CLASS_TUPLE: |
5805 | 0 | case G_VARIANT_CLASS_DICT_ENTRY: |
5806 | 0 | case G_VARIANT_CLASS_VARIANT: |
5807 | 0 | { |
5808 | 0 | GVariantBuilder builder; |
5809 | 0 | GVariantIter iter; |
5810 | 0 | GVariant *child; |
5811 | |
|
5812 | 0 | g_variant_builder_init (&builder, g_variant_get_type (value)); |
5813 | 0 | g_variant_iter_init (&iter, value); |
5814 | |
|
5815 | 0 | while ((child = g_variant_iter_next_value (&iter))) |
5816 | 0 | { |
5817 | 0 | g_variant_builder_add_value (&builder, g_variant_deep_copy (child)); |
5818 | 0 | g_variant_unref (child); |
5819 | 0 | } |
5820 | |
|
5821 | 0 | return g_variant_builder_end (&builder); |
5822 | 0 | } |
5823 | | |
5824 | 0 | case G_VARIANT_CLASS_BOOLEAN: |
5825 | 0 | return g_variant_new_boolean (g_variant_get_boolean (value)); |
5826 | | |
5827 | 0 | case G_VARIANT_CLASS_BYTE: |
5828 | 0 | return g_variant_new_byte (g_variant_get_byte (value)); |
5829 | | |
5830 | 0 | case G_VARIANT_CLASS_INT16: |
5831 | 0 | return g_variant_new_int16 (g_variant_get_int16 (value)); |
5832 | | |
5833 | 0 | case G_VARIANT_CLASS_UINT16: |
5834 | 0 | return g_variant_new_uint16 (g_variant_get_uint16 (value)); |
5835 | | |
5836 | 0 | case G_VARIANT_CLASS_INT32: |
5837 | 0 | return g_variant_new_int32 (g_variant_get_int32 (value)); |
5838 | | |
5839 | 0 | case G_VARIANT_CLASS_UINT32: |
5840 | 0 | return g_variant_new_uint32 (g_variant_get_uint32 (value)); |
5841 | | |
5842 | 0 | case G_VARIANT_CLASS_INT64: |
5843 | 0 | return g_variant_new_int64 (g_variant_get_int64 (value)); |
5844 | | |
5845 | 0 | case G_VARIANT_CLASS_UINT64: |
5846 | 0 | return g_variant_new_uint64 (g_variant_get_uint64 (value)); |
5847 | | |
5848 | 0 | case G_VARIANT_CLASS_HANDLE: |
5849 | 0 | return g_variant_new_handle (g_variant_get_handle (value)); |
5850 | | |
5851 | 0 | case G_VARIANT_CLASS_DOUBLE: |
5852 | 0 | return g_variant_new_double (g_variant_get_double (value)); |
5853 | | |
5854 | 0 | case G_VARIANT_CLASS_STRING: |
5855 | 0 | return g_variant_new_string (g_variant_get_string (value, NULL)); |
5856 | | |
5857 | 0 | case G_VARIANT_CLASS_OBJECT_PATH: |
5858 | 0 | return g_variant_new_object_path (g_variant_get_string (value, NULL)); |
5859 | | |
5860 | 0 | case G_VARIANT_CLASS_SIGNATURE: |
5861 | 0 | return g_variant_new_signature (g_variant_get_string (value, NULL)); |
5862 | 0 | } |
5863 | | |
5864 | 0 | g_assert_not_reached (); |
5865 | 0 | } |
5866 | | |
5867 | | /** |
5868 | | * g_variant_get_normal_form: |
5869 | | * @value: a #GVariant |
5870 | | * |
5871 | | * Gets a #GVariant instance that has the same value as @value and is |
5872 | | * trusted to be in normal form. |
5873 | | * |
5874 | | * If @value is already trusted to be in normal form then a new |
5875 | | * reference to @value is returned. |
5876 | | * |
5877 | | * If @value is not already trusted, then it is scanned to check if it |
5878 | | * is in normal form. If it is found to be in normal form then it is |
5879 | | * marked as trusted and a new reference to it is returned. |
5880 | | * |
5881 | | * If @value is found not to be in normal form then a new trusted |
5882 | | * #GVariant is created with the same value as @value. |
5883 | | * |
5884 | | * It makes sense to call this function if you've received #GVariant |
5885 | | * data from untrusted sources and you want to ensure your serialised |
5886 | | * output is definitely in normal form. |
5887 | | * |
5888 | | * If @value is already in normal form, a new reference will be returned |
5889 | | * (which will be floating if @value is floating). If it is not in normal form, |
5890 | | * the newly created #GVariant will be returned with a single non-floating |
5891 | | * reference. Typically, g_variant_take_ref() should be called on the return |
5892 | | * value from this function to guarantee ownership of a single non-floating |
5893 | | * reference to it. |
5894 | | * |
5895 | | * Returns: (transfer full): a trusted #GVariant |
5896 | | * |
5897 | | * Since: 2.24 |
5898 | | **/ |
5899 | | GVariant * |
5900 | | g_variant_get_normal_form (GVariant *value) |
5901 | 0 | { |
5902 | 0 | GVariant *trusted; |
5903 | |
|
5904 | 0 | if (g_variant_is_normal_form (value)) |
5905 | 0 | return g_variant_ref (value); |
5906 | | |
5907 | 0 | trusted = g_variant_deep_copy (value); |
5908 | 0 | g_assert (g_variant_is_trusted (trusted)); |
5909 | | |
5910 | 0 | return g_variant_ref_sink (trusted); |
5911 | 0 | } |
5912 | | |
5913 | | /** |
5914 | | * g_variant_byteswap: |
5915 | | * @value: a #GVariant |
5916 | | * |
5917 | | * Performs a byteswapping operation on the contents of @value. The |
5918 | | * result is that all multi-byte numeric data contained in @value is |
5919 | | * byteswapped. That includes 16, 32, and 64bit signed and unsigned |
5920 | | * integers as well as file handles and double precision floating point |
5921 | | * values. |
5922 | | * |
5923 | | * This function is an identity mapping on any value that does not |
5924 | | * contain multi-byte numeric data. That include strings, booleans, |
5925 | | * bytes and containers containing only these things (recursively). |
5926 | | * |
5927 | | * The returned value is always in normal form and is marked as trusted. |
5928 | | * |
5929 | | * Returns: (transfer full): the byteswapped form of @value |
5930 | | * |
5931 | | * Since: 2.24 |
5932 | | **/ |
5933 | | GVariant * |
5934 | | g_variant_byteswap (GVariant *value) |
5935 | 0 | { |
5936 | 0 | GVariantTypeInfo *type_info; |
5937 | 0 | guint alignment; |
5938 | 0 | GVariant *new; |
5939 | |
|
5940 | 0 | type_info = g_variant_get_type_info (value); |
5941 | |
|
5942 | 0 | g_variant_type_info_query (type_info, &alignment, NULL); |
5943 | |
|
5944 | 0 | if (alignment) |
5945 | | /* (potentially) contains multi-byte numeric data */ |
5946 | 0 | { |
5947 | 0 | GVariantSerialised serialised; |
5948 | 0 | GVariant *trusted; |
5949 | 0 | GBytes *bytes; |
5950 | |
|
5951 | 0 | trusted = g_variant_get_normal_form (value); |
5952 | 0 | serialised.type_info = g_variant_get_type_info (trusted); |
5953 | 0 | serialised.size = g_variant_get_size (trusted); |
5954 | 0 | serialised.data = g_malloc (serialised.size); |
5955 | 0 | serialised.depth = g_variant_get_depth (trusted); |
5956 | 0 | g_variant_store (trusted, serialised.data); |
5957 | 0 | g_variant_unref (trusted); |
5958 | |
|
5959 | 0 | g_variant_serialised_byteswap (serialised); |
5960 | |
|
5961 | 0 | bytes = g_bytes_new_take (serialised.data, serialised.size); |
5962 | 0 | new = g_variant_new_from_bytes (g_variant_get_type (value), bytes, TRUE); |
5963 | 0 | g_bytes_unref (bytes); |
5964 | 0 | } |
5965 | 0 | else |
5966 | | /* contains no multi-byte data */ |
5967 | 0 | new = value; |
5968 | |
|
5969 | 0 | return g_variant_ref_sink (new); |
5970 | 0 | } |
5971 | | |
5972 | | /** |
5973 | | * g_variant_new_from_data: |
5974 | | * @type: a definite #GVariantType |
5975 | | * @data: (array length=size) (element-type guint8): the serialised data |
5976 | | * @size: the size of @data |
5977 | | * @trusted: %TRUE if @data is definitely in normal form |
5978 | | * @notify: (scope async): function to call when @data is no longer needed |
5979 | | * @user_data: data for @notify |
5980 | | * |
5981 | | * Creates a new #GVariant instance from serialised data. |
5982 | | * |
5983 | | * @type is the type of #GVariant instance that will be constructed. |
5984 | | * The interpretation of @data depends on knowing the type. |
5985 | | * |
5986 | | * @data is not modified by this function and must remain valid with an |
5987 | | * unchanging value until such a time as @notify is called with |
5988 | | * @user_data. If the contents of @data change before that time then |
5989 | | * the result is undefined. |
5990 | | * |
5991 | | * If @data is trusted to be serialised data in normal form then |
5992 | | * @trusted should be %TRUE. This applies to serialised data created |
5993 | | * within this process or read from a trusted location on the disk (such |
5994 | | * as a file installed in /usr/lib alongside your application). You |
5995 | | * should set trusted to %FALSE if @data is read from the network, a |
5996 | | * file in the user's home directory, etc. |
5997 | | * |
5998 | | * If @data was not stored in this machine's native endianness, any multi-byte |
5999 | | * numeric values in the returned variant will also be in non-native |
6000 | | * endianness. g_variant_byteswap() can be used to recover the original values. |
6001 | | * |
6002 | | * @notify will be called with @user_data when @data is no longer |
6003 | | * needed. The exact time of this call is unspecified and might even be |
6004 | | * before this function returns. |
6005 | | * |
6006 | | * Note: @data must be backed by memory that is aligned appropriately for the |
6007 | | * @type being loaded. Otherwise this function will internally create a copy of |
6008 | | * the memory (since GLib 2.60) or (in older versions) fail and exit the |
6009 | | * process. |
6010 | | * |
6011 | | * Returns: (transfer none): a new floating #GVariant of type @type |
6012 | | * |
6013 | | * Since: 2.24 |
6014 | | **/ |
6015 | | GVariant * |
6016 | | g_variant_new_from_data (const GVariantType *type, |
6017 | | gconstpointer data, |
6018 | | gsize size, |
6019 | | gboolean trusted, |
6020 | | GDestroyNotify notify, |
6021 | | gpointer user_data) |
6022 | 0 | { |
6023 | 0 | GVariant *value; |
6024 | 0 | GBytes *bytes; |
6025 | |
|
6026 | 0 | g_return_val_if_fail (g_variant_type_is_definite (type), NULL); |
6027 | 0 | g_return_val_if_fail (data != NULL || size == 0, NULL); |
6028 | | |
6029 | 0 | if (notify) |
6030 | 0 | bytes = g_bytes_new_with_free_func (data, size, notify, user_data); |
6031 | 0 | else |
6032 | 0 | bytes = g_bytes_new_static (data, size); |
6033 | |
|
6034 | 0 | value = g_variant_new_from_bytes (type, bytes, trusted); |
6035 | 0 | g_bytes_unref (bytes); |
6036 | |
|
6037 | 0 | return value; |
6038 | 0 | } |
6039 | | |
6040 | | /* Epilogue {{{1 */ |
6041 | | /* vim:set foldmethod=marker: */ |