1 /* GStreamer
2 * Copyright (C) 2003 David A. Schleef <ds@schleef.org>
3 *
4 * gststructure.c: lists of { GQuark, GValue } tuples
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
22 /**
23 * SECTION:gststructure
24 * @short_description: Generic structure containing fields of names and values
25 * @see_also: #GstCaps, #GstMessage, #GstEvent, #GstQuery
26 *
27 * A #GstStructure is a collection of key/value pairs. The keys are expressed
28 * as GQuarks and the values can be of any GType.
29 *
30 * In addition to the key/value pairs, a #GstStructure also has a name. The name
31 * starts with a letter and can be folled by letters, numbers and any of "/-_.:".
32 *
33 * #GstStructure is used by various GStreamer subsystems to store information
34 * in a flexible and extensible way. A #GstStructure does not have a refcount
35 * because it usually is part of a higher level object such as #GstCaps. It
36 * provides a means to enforce mutability using the refcount of the parent
37 * with the gst_structure_set_parent_refcount() method.
38 *
39 * A #GstStructure can be created with gst_structure_empty_new() or
40 * gst_structure_new(), which both take a name and an optional set of
41 * key/value pairs along with the types of the values.
42 *
43 * Field values can be changed with gst_structure_set_value() or
44 * gst_structure_set().
45 *
46 * Field values can be retrieved with gst_structure_get_value() or the more
47 * convenient gst_structure_get_*() functions.
48 *
49 * Fields can be removed with gst_structure_remove_field() or
50 * gst_structure_remove_fields().
51 *
52 * Strings in structures must be ASCII or UTF-8 encoded. Other encodings are
53 * not allowed. Strings must not be empty either, but may be NULL.
54 *
55 * Last reviewed on 2009-06-08 (0.10.23)
56 */
58 #ifdef HAVE_CONFIG_H
59 #include "config.h"
60 #endif
62 #include <string.h>
64 #include "gst_private.h"
65 #include "gstquark.h"
66 #include <gst/gst.h>
67 #include <gobject/gvaluecollector.h>
69 typedef struct _GstStructureField GstStructureField;
71 struct _GstStructureField
72 {
73 GQuark name;
74 GValue value;
75 };
77 #define GST_STRUCTURE_FIELD(structure, index) \
78 &g_array_index((structure)->fields, GstStructureField, (index))
80 #define IS_MUTABLE(structure) \
81 (!(structure)->parent_refcount || \
82 g_atomic_int_get ((structure)->parent_refcount) == 1)
84 #define IS_TAGLIST(structure) \
85 (structure->name == GST_QUARK (TAGLIST))
87 static void gst_structure_set_field (GstStructure * structure,
88 GstStructureField * field);
89 static GstStructureField *gst_structure_get_field (const GstStructure *
90 structure, const gchar * fieldname);
91 static GstStructureField *gst_structure_id_get_field (const GstStructure *
92 structure, GQuark field);
93 static void gst_structure_transform_to_string (const GValue * src_value,
94 GValue * dest_value);
95 static GstStructure *gst_structure_copy_conditional (const GstStructure *
96 structure);
97 static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
98 GValue * value, GType default_type);
99 static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
101 GType
102 gst_structure_get_type (void)
103 {
104 static GType gst_structure_type = 0;
106 if (G_UNLIKELY (gst_structure_type == 0)) {
107 gst_structure_type = g_boxed_type_register_static ("GstStructure",
108 (GBoxedCopyFunc) gst_structure_copy_conditional,
109 (GBoxedFreeFunc) gst_structure_free);
111 g_value_register_transform_func (gst_structure_type, G_TYPE_STRING,
112 gst_structure_transform_to_string);
113 }
115 return gst_structure_type;
116 }
118 static GstStructure *
119 gst_structure_id_empty_new_with_size (GQuark quark, guint prealloc)
120 {
121 GstStructure *structure;
123 structure = g_slice_new (GstStructure);
124 structure->type = gst_structure_get_type ();
125 structure->name = quark;
126 structure->parent_refcount = NULL;
127 structure->fields =
128 g_array_sized_new (FALSE, FALSE, sizeof (GstStructureField), prealloc);
130 return structure;
131 }
133 /**
134 * gst_structure_id_empty_new:
135 * @quark: name of new structure
136 *
137 * Creates a new, empty #GstStructure with the given name as a GQuark.
138 *
139 * Returns: a new, empty #GstStructure
140 */
141 GstStructure *
142 gst_structure_id_empty_new (GQuark quark)
143 {
144 g_return_val_if_fail (quark != 0, NULL);
146 return gst_structure_id_empty_new_with_size (quark, 0);
147 }
149 #ifndef G_DISABLE_CHECKS
150 static gboolean
151 gst_structure_validate_name (const gchar * name)
152 {
153 const gchar *s;
155 g_return_val_if_fail (name != NULL, FALSE);
157 /* FIXME 0.11: use g_ascii_isalpha() */
158 if (G_UNLIKELY (!g_ascii_isalnum (*name))) {
159 GST_WARNING ("Invalid character '%c' at offset 0 in structure name: %s",
160 *name, name);
161 return FALSE;
162 }
164 /* FIXME 0.11: don't allow spaces */
165 /* FIXME: test name string more */
166 s = &name[1];
167 while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+ ", *s) != NULL))
168 s++;
169 if (G_UNLIKELY (*s != '\0')) {
170 GST_WARNING ("Invalid character '%c' at offset %lu in structure name: %s",
171 *s, ((gulong) s - (gulong) name), name);
172 return FALSE;
173 }
175 return TRUE;
176 }
177 #endif
179 /**
180 * gst_structure_empty_new:
181 * @name: name of new structure
182 *
183 * Creates a new, empty #GstStructure with the given @name.
184 *
185 * See gst_structure_set_name() for constraints on the @name parameter.
186 *
187 * Returns: a new, empty #GstStructure
188 */
189 GstStructure *
190 gst_structure_empty_new (const gchar * name)
191 {
192 g_return_val_if_fail (gst_structure_validate_name (name), NULL);
194 return gst_structure_id_empty_new_with_size (g_quark_from_string (name), 0);
195 }
197 /**
198 * gst_structure_new:
199 * @name: name of new structure
200 * @firstfield: name of first field to set
201 * @...: additional arguments
202 *
203 * Creates a new #GstStructure with the given name. Parses the
204 * list of variable arguments and sets fields to the values listed.
205 * Variable arguments should be passed as field name, field type,
206 * and value. Last variable argument should be NULL.
207 *
208 * Returns: a new #GstStructure
209 */
210 GstStructure *
211 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
212 {
213 GstStructure *structure;
214 va_list varargs;
216 g_return_val_if_fail (name != NULL, NULL);
218 va_start (varargs, firstfield);
220 structure = gst_structure_new_valist (name, firstfield, varargs);
222 va_end (varargs);
224 return structure;
225 }
227 /**
228 * gst_structure_new_valist:
229 * @name: name of new structure
230 * @firstfield: name of first field to set
231 * @varargs: variable argument list
232 *
233 * Creates a new #GstStructure with the given @name. Structure fields
234 * are set according to the varargs in a manner similar to
235 * gst_structure_new().
236 *
237 * See gst_structure_set_name() for constraints on the @name parameter.
238 *
239 * Returns: a new #GstStructure
240 */
241 GstStructure *
242 gst_structure_new_valist (const gchar * name,
243 const gchar * firstfield, va_list varargs)
244 {
245 GstStructure *structure;
247 g_return_val_if_fail (name != NULL, NULL);
249 structure = gst_structure_empty_new (name);
251 if (structure)
252 gst_structure_set_valist (structure, firstfield, varargs);
254 return structure;
255 }
257 /**
258 * gst_structure_set_parent_refcount:
259 * @structure: a #GstStructure
260 * @refcount: a pointer to the parent's refcount
261 *
262 * Sets the parent_refcount field of #GstStructure. This field is used to
263 * determine whether a structure is mutable or not. This function should only be
264 * called by code implementing parent objects of #GstStructure, as described in
265 * the MT Refcounting section of the design documents.
266 */
267 void
268 gst_structure_set_parent_refcount (GstStructure * structure, gint * refcount)
269 {
270 g_return_if_fail (structure != NULL);
272 /* if we have a parent_refcount already, we can only clear
273 * if with a NULL refcount */
274 if (structure->parent_refcount)
275 g_return_if_fail (refcount == NULL);
276 else
277 g_return_if_fail (refcount != NULL);
279 structure->parent_refcount = refcount;
280 }
282 /**
283 * gst_structure_copy:
284 * @structure: a #GstStructure to duplicate
285 *
286 * Duplicates a #GstStructure and all its fields and values.
287 *
288 * Returns: a new #GstStructure.
289 */
290 GstStructure *
291 gst_structure_copy (const GstStructure * structure)
292 {
293 GstStructure *new_structure;
294 GstStructureField *field;
295 guint i, len;
297 g_return_val_if_fail (structure != NULL, NULL);
299 new_structure =
300 gst_structure_id_empty_new_with_size (structure->name,
301 structure->fields->len);
303 len = structure->fields->len;
304 for (i = 0; i < len; i++) {
305 GstStructureField new_field = { 0 };
307 field = GST_STRUCTURE_FIELD (structure, i);
309 new_field.name = field->name;
310 gst_value_init_and_copy (&new_field.value, &field->value);
311 g_array_append_val (new_structure->fields, new_field);
312 }
314 return new_structure;
315 }
317 /**
318 * gst_structure_free:
319 * @structure: the #GstStructure to free
320 *
321 * Frees a #GstStructure and all its fields and values. The structure must not
322 * have a parent when this function is called.
323 */
324 void
325 gst_structure_free (GstStructure * structure)
326 {
327 GstStructureField *field;
328 guint i, len;
330 g_return_if_fail (structure != NULL);
331 g_return_if_fail (structure->parent_refcount == NULL);
333 len = structure->fields->len;
334 for (i = 0; i < len; i++) {
335 field = GST_STRUCTURE_FIELD (structure, i);
337 if (G_IS_VALUE (&field->value)) {
338 g_value_unset (&field->value);
339 }
340 }
341 g_array_free (structure->fields, TRUE);
342 #ifdef USE_POISONING
343 memset (structure, 0xff, sizeof (GstStructure));
344 #endif
345 g_slice_free (GstStructure, structure);
346 }
348 /**
349 * gst_structure_get_name:
350 * @structure: a #GstStructure
351 *
352 * Get the name of @structure as a string.
353 *
354 * Returns: the name of the structure.
355 */
356 const gchar *
357 gst_structure_get_name (const GstStructure * structure)
358 {
359 g_return_val_if_fail (structure != NULL, NULL);
361 return g_quark_to_string (structure->name);
362 }
364 /**
365 * gst_structure_has_name:
366 * @structure: a #GstStructure
367 * @name: structure name to check for
368 *
369 * Checks if the structure has the given name
370 *
371 * Returns: TRUE if @name matches the name of the structure.
372 */
373 gboolean
374 gst_structure_has_name (const GstStructure * structure, const gchar * name)
375 {
376 const gchar *structure_name;
378 g_return_val_if_fail (structure != NULL, FALSE);
379 g_return_val_if_fail (name != NULL, FALSE);
381 /* getting the string is cheap and comparing short strings is too
382 * should be faster than getting the quark for name and comparing the quarks
383 */
384 structure_name = g_quark_to_string (structure->name);
386 return (structure_name && strcmp (structure_name, name) == 0);
387 }
389 /**
390 * gst_structure_get_name_id:
391 * @structure: a #GstStructure
392 *
393 * Get the name of @structure as a GQuark.
394 *
395 * Returns: the quark representing the name of the structure.
396 */
397 GQuark
398 gst_structure_get_name_id (const GstStructure * structure)
399 {
400 g_return_val_if_fail (structure != NULL, 0);
402 return structure->name;
403 }
405 /**
406 * gst_structure_set_name:
407 * @structure: a #GstStructure
408 * @name: the new name of the structure
409 *
410 * Sets the name of the structure to the given @name. The string
411 * provided is copied before being used. It must not be empty, start with a
412 * letter and can be followed by letters, numbers and any of "/-_.:".
413 */
414 void
415 gst_structure_set_name (GstStructure * structure, const gchar * name)
416 {
417 g_return_if_fail (structure != NULL);
418 g_return_if_fail (IS_MUTABLE (structure));
419 g_return_if_fail (gst_structure_validate_name (name));
421 structure->name = g_quark_from_string (name);
422 }
424 /**
425 * gst_structure_id_set_value:
426 * @structure: a #GstStructure
427 * @field: a #GQuark representing a field
428 * @value: the new value of the field
429 *
430 * Sets the field with the given GQuark @field to @value. If the field
431 * does not exist, it is created. If the field exists, the previous
432 * value is replaced and freed.
433 */
434 void
435 gst_structure_id_set_value (GstStructure * structure,
436 GQuark field, const GValue * value)
437 {
438 GstStructureField gsfield = { 0, {0,} };
440 g_return_if_fail (structure != NULL);
441 g_return_if_fail (G_IS_VALUE (value));
442 g_return_if_fail (IS_MUTABLE (structure));
444 gsfield.name = field;
445 gst_value_init_and_copy (&gsfield.value, value);
447 gst_structure_set_field (structure, &gsfield);
448 }
450 /**
451 * gst_structure_set_value:
452 * @structure: a #GstStructure
453 * @fieldname: the name of the field to set
454 * @value: the new value of the field
455 *
456 * Sets the field with the given name @field to @value. If the field
457 * does not exist, it is created. If the field exists, the previous
458 * value is replaced and freed.
459 */
460 void
461 gst_structure_set_value (GstStructure * structure,
462 const gchar * fieldname, const GValue * value)
463 {
464 g_return_if_fail (structure != NULL);
465 g_return_if_fail (fieldname != NULL);
466 g_return_if_fail (G_IS_VALUE (value));
467 g_return_if_fail (IS_MUTABLE (structure));
469 gst_structure_id_set_value (structure, g_quark_from_string (fieldname),
470 value);
471 }
473 /**
474 * gst_structure_set:
475 * @structure: a #GstStructure
476 * @fieldname: the name of the field to set
477 * @...: variable arguments
478 *
479 * Parses the variable arguments and sets fields accordingly.
480 * Variable arguments should be in the form field name, field type
481 * (as a GType), value(s). The last variable argument should be NULL.
482 */
483 void
484 gst_structure_set (GstStructure * structure, const gchar * field, ...)
485 {
486 va_list varargs;
488 g_return_if_fail (structure != NULL);
490 va_start (varargs, field);
492 gst_structure_set_valist (structure, field, varargs);
494 va_end (varargs);
495 }
497 /**
498 * gst_structure_set_valist:
499 * @structure: a #GstStructure
500 * @fieldname: the name of the field to set
501 * @varargs: variable arguments
502 *
503 * va_list form of gst_structure_set().
504 */
505 void
506 gst_structure_set_valist (GstStructure * structure,
507 const gchar * fieldname, va_list varargs)
508 {
509 gchar *err = NULL;
510 GType type;
512 g_return_if_fail (structure != NULL);
513 g_return_if_fail (IS_MUTABLE (structure));
515 while (fieldname) {
516 GstStructureField field = { 0 };
518 field.name = g_quark_from_string (fieldname);
520 type = va_arg (varargs, GType);
522 if (G_UNLIKELY (type == G_TYPE_DATE)) {
523 g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
524 type = GST_TYPE_DATE;
525 }
527 g_value_init (&field.value, type);
528 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
529 if (G_UNLIKELY (err)) {
530 g_critical ("%s", err);
531 return;
532 }
533 gst_structure_set_field (structure, &field);
535 fieldname = va_arg (varargs, gchar *);
536 }
537 }
539 /**
540 * gst_structure_id_set:
541 * @structure: a #GstStructure
542 * @fieldname: the GQuark for the name of the field to set
543 * @...: variable arguments
544 *
545 * Identical to gst_structure_set, except that field names are
546 * passed using the GQuark for the field name. This allows more efficient
547 * setting of the structure if the caller already knows the associated
548 * quark values.
549 * The last variable argument must be NULL.
550 *
551 * Since: 0.10.10
552 */
553 void
554 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
555 {
556 va_list varargs;
558 g_return_if_fail (structure != NULL);
560 va_start (varargs, field);
561 gst_structure_id_set_valist (structure, field, varargs);
562 va_end (varargs);
563 }
565 /**
566 * gst_structure_id_set_valist:
567 * @structure: a #GstStructure
568 * @fieldname: the name of the field to set
569 * @varargs: variable arguments
570 *
571 * va_list form of gst_structure_id_set().
572 *
573 * Since: 0.10.10
574 */
575 void
576 gst_structure_id_set_valist (GstStructure * structure,
577 GQuark fieldname, va_list varargs)
578 {
579 gchar *err = NULL;
580 GType type;
582 g_return_if_fail (structure != NULL);
583 g_return_if_fail (IS_MUTABLE (structure));
585 while (fieldname) {
586 GstStructureField field = { 0 };
588 field.name = fieldname;
590 type = va_arg (varargs, GType);
592 if (G_UNLIKELY (type == G_TYPE_DATE)) {
593 g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
594 type = GST_TYPE_DATE;
595 }
597 g_value_init (&field.value, type);
598 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
599 if (G_UNLIKELY (err)) {
600 g_critical ("%s", err);
601 return;
602 }
603 gst_structure_set_field (structure, &field);
605 fieldname = va_arg (varargs, GQuark);
606 }
607 }
609 /**
610 * gst_structure_id_new:
611 * @name_quark: name of new structure
612 * @field_quark: the GQuark for the name of the field to set
613 * @...: variable arguments
614 *
615 * Creates a new #GstStructure with the given name as a GQuark, followed by
616 * fieldname quark, GType, argument(s) "triplets" in the same format as
617 * gst_structure_id_set(). Basically a convenience wrapper around
618 * gst_structure_id_empty_new() and gst_structure_id_set().
619 *
620 * The last variable argument must be NULL (or 0).
621 *
622 * Returns: a new #GstStructure
623 *
624 * Since: 0.10.24
625 */
626 GstStructure *
627 gst_structure_id_new (GQuark name_quark, GQuark field_quark, ...)
628 {
629 GstStructure *s;
630 va_list varargs;
632 g_return_val_if_fail (name_quark != 0, NULL);
633 g_return_val_if_fail (field_quark != 0, NULL);
635 s = gst_structure_id_empty_new (name_quark);
637 va_start (varargs, field_quark);
638 gst_structure_id_set_valist (s, field_quark, varargs);
639 va_end (varargs);
641 return s;
642 }
644 /* If the structure currently contains a field with the same name, it is
645 * replaced with the provided field. Otherwise, the field is added to the
646 * structure. The field's value is not deeply copied.
647 */
648 static void
649 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
650 {
651 GstStructureField *f;
652 guint i, len = structure->fields->len;
654 if (G_UNLIKELY (G_VALUE_HOLDS_STRING (&field->value))) {
655 const gchar *s;
657 s = g_value_get_string (&field->value);
658 /* only check for NULL strings in taglists, as they are allowed in message
659 * structs, e.g. error message debug strings */
660 if (G_UNLIKELY (s == NULL && IS_TAGLIST (structure))) {
661 g_warning ("Trying to set NULL string on field '%s' on taglist. "
662 "Please file a bug.", g_quark_to_string (field->name));
663 g_value_unset (&field->value);
664 return;
665 } else if (G_UNLIKELY (s != NULL && *s == '\0')) {
666 /* empty strings never make sense */
667 g_warning ("Trying to set empty string on %s field '%s'. Please file a "
668 "bug.", IS_TAGLIST (structure) ? "taglist" : "structure",
669 g_quark_to_string (field->name));
670 g_value_unset (&field->value);
671 return;
672 } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
673 g_warning ("Trying to set string on %s field '%s', but string is not "
674 "valid UTF-8. Please file a bug.",
675 IS_TAGLIST (structure) ? "taglist" : "structure",
676 g_quark_to_string (field->name));
677 g_value_unset (&field->value);
678 return;
679 }
680 }
682 for (i = 0; i < len; i++) {
683 f = GST_STRUCTURE_FIELD (structure, i);
685 if (G_UNLIKELY (f->name == field->name)) {
686 g_value_unset (&f->value);
687 memcpy (f, field, sizeof (GstStructureField));
688 return;
689 }
690 }
692 g_array_append_val (structure->fields, *field);
693 }
695 /* If there is no field with the given ID, NULL is returned.
696 */
697 static GstStructureField *
698 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
699 {
700 GstStructureField *field;
701 guint i, len;
703 len = structure->fields->len;
704 g_return_val_if_fail (structure != NULL, NULL);
706 for (i = 0; i < len; i++) {
707 field = GST_STRUCTURE_FIELD (structure, i);
709 if (G_UNLIKELY (field->name == field_id))
710 return field;
711 }
713 return NULL;
714 }
716 /* If there is no field with the given ID, NULL is returned.
717 */
718 static GstStructureField *
719 gst_structure_get_field (const GstStructure * structure,
720 const gchar * fieldname)
721 {
722 g_return_val_if_fail (structure != NULL, NULL);
723 g_return_val_if_fail (fieldname != NULL, NULL);
725 return gst_structure_id_get_field (structure,
726 g_quark_from_string (fieldname));
727 }
729 /**
730 * gst_structure_get_value:
731 * @structure: a #GstStructure
732 * @fieldname: the name of the field to get
733 *
734 * Get the value of the field with name @fieldname.
735 *
736 * Returns: the #GValue corresponding to the field with the given name.
737 */
738 const GValue *
739 gst_structure_get_value (const GstStructure * structure,
740 const gchar * fieldname)
741 {
742 GstStructureField *field;
744 g_return_val_if_fail (structure != NULL, NULL);
745 g_return_val_if_fail (fieldname != NULL, NULL);
747 field = gst_structure_get_field (structure, fieldname);
748 if (field == NULL)
749 return NULL;
751 return &field->value;
752 }
754 /**
755 * gst_structure_id_get_value:
756 * @structure: a #GstStructure
757 * @field: the #GQuark of the field to get
758 *
759 * Get the value of the field with GQuark @field.
760 *
761 * Returns: the #GValue corresponding to the field with the given name
762 * identifier.
763 */
764 const GValue *
765 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
766 {
767 GstStructureField *gsfield;
769 g_return_val_if_fail (structure != NULL, NULL);
771 gsfield = gst_structure_id_get_field (structure, field);
772 if (gsfield == NULL)
773 return NULL;
775 return &gsfield->value;
776 }
778 /**
779 * gst_structure_remove_field:
780 * @structure: a #GstStructure
781 * @fieldname: the name of the field to remove
782 *
783 * Removes the field with the given name. If the field with the given
784 * name does not exist, the structure is unchanged.
785 */
786 void
787 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
788 {
789 GstStructureField *field;
790 GQuark id;
791 guint i, len;
793 g_return_if_fail (structure != NULL);
794 g_return_if_fail (fieldname != NULL);
795 g_return_if_fail (IS_MUTABLE (structure));
797 id = g_quark_from_string (fieldname);
798 len = structure->fields->len;
800 for (i = 0; i < len; i++) {
801 field = GST_STRUCTURE_FIELD (structure, i);
803 if (field->name == id) {
804 if (G_IS_VALUE (&field->value)) {
805 g_value_unset (&field->value);
806 }
807 structure->fields = g_array_remove_index (structure->fields, i);
808 return;
809 }
810 }
811 }
813 /**
814 * gst_structure_remove_fields:
815 * @structure: a #GstStructure
816 * @fieldname: the name of the field to remove
817 * @...: NULL-terminated list of more fieldnames to remove
818 *
819 * Removes the fields with the given names. If a field does not exist, the
820 * argument is ignored.
821 */
822 void
823 gst_structure_remove_fields (GstStructure * structure,
824 const gchar * fieldname, ...)
825 {
826 va_list varargs;
828 g_return_if_fail (structure != NULL);
829 g_return_if_fail (fieldname != NULL);
830 /* mutability checked in remove_field */
832 va_start (varargs, fieldname);
834 gst_structure_remove_fields_valist (structure, fieldname, varargs);
836 va_end (varargs);
837 }
839 /**
840 * gst_structure_remove_fields_valist:
841 * @structure: a #GstStructure
842 * @fieldname: the name of the field to remove
843 * @varargs: NULL-terminated list of more fieldnames to remove
844 *
845 * va_list form of gst_structure_remove_fields().
846 */
847 void
848 gst_structure_remove_fields_valist (GstStructure * structure,
849 const gchar * fieldname, va_list varargs)
850 {
851 gchar *field = (gchar *) fieldname;
853 g_return_if_fail (structure != NULL);
854 g_return_if_fail (fieldname != NULL);
855 /* mutability checked in remove_field */
857 while (field) {
858 gst_structure_remove_field (structure, field);
859 field = va_arg (varargs, char *);
860 }
861 }
863 /**
864 * gst_structure_remove_all_fields:
865 * @structure: a #GstStructure
866 *
867 * Removes all fields in a GstStructure.
868 */
869 void
870 gst_structure_remove_all_fields (GstStructure * structure)
871 {
872 GstStructureField *field;
873 int i;
875 g_return_if_fail (structure != NULL);
876 g_return_if_fail (IS_MUTABLE (structure));
878 for (i = structure->fields->len - 1; i >= 0; i--) {
879 field = GST_STRUCTURE_FIELD (structure, i);
881 if (G_IS_VALUE (&field->value)) {
882 g_value_unset (&field->value);
883 }
884 structure->fields = g_array_remove_index (structure->fields, i);
885 }
886 }
888 /**
889 * gst_structure_get_field_type:
890 * @structure: a #GstStructure
891 * @fieldname: the name of the field
892 *
893 * Finds the field with the given name, and returns the type of the
894 * value it contains. If the field is not found, G_TYPE_INVALID is
895 * returned.
896 *
897 * Returns: the #GValue of the field
898 */
899 GType
900 gst_structure_get_field_type (const GstStructure * structure,
901 const gchar * fieldname)
902 {
903 GstStructureField *field;
905 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
906 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
908 field = gst_structure_get_field (structure, fieldname);
909 if (field == NULL)
910 return G_TYPE_INVALID;
912 return G_VALUE_TYPE (&field->value);
913 }
915 /**
916 * gst_structure_n_fields:
917 * @structure: a #GstStructure
918 *
919 * Get the number of fields in the structure.
920 *
921 * Returns: the number of fields in the structure
922 */
923 gint
924 gst_structure_n_fields (const GstStructure * structure)
925 {
926 g_return_val_if_fail (structure != NULL, 0);
928 return structure->fields->len;
929 }
931 /**
932 * gst_structure_nth_field_name:
933 * @structure: a #GstStructure
934 * @index: the index to get the name of
935 *
936 * Get the name of the given field number, counting from 0 onwards.
937 *
938 * Returns: the name of the given field number
939 */
940 const gchar *
941 gst_structure_nth_field_name (const GstStructure * structure, guint index)
942 {
943 GstStructureField *field;
945 g_return_val_if_fail (structure != NULL, NULL);
946 g_return_val_if_fail (index < structure->fields->len, NULL);
948 field = GST_STRUCTURE_FIELD (structure, index);
950 return g_quark_to_string (field->name);
951 }
953 /**
954 * gst_structure_foreach:
955 * @structure: a #GstStructure
956 * @func: a function to call for each field
957 * @user_data: private data
958 *
959 * Calls the provided function once for each field in the #GstStructure. The
960 * function must not modify the fields. Also see gst_structure_map_in_place().
961 *
962 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
963 * FALSE otherwise.
964 */
965 gboolean
966 gst_structure_foreach (const GstStructure * structure,
967 GstStructureForeachFunc func, gpointer user_data)
968 {
969 guint i, len;
970 GstStructureField *field;
971 gboolean ret;
973 g_return_val_if_fail (structure != NULL, FALSE);
974 g_return_val_if_fail (func != NULL, FALSE);
976 len = structure->fields->len;
978 for (i = 0; i < len; i++) {
979 field = GST_STRUCTURE_FIELD (structure, i);
981 ret = func (field->name, &field->value, user_data);
982 if (G_UNLIKELY (!ret))
983 return FALSE;
984 }
986 return TRUE;
987 }
989 /**
990 * gst_structure_map_in_place:
991 * @structure: a #GstStructure
992 * @func: a function to call for each field
993 * @user_data: private data
994 *
995 * Calls the provided function once for each field in the #GstStructure. In
996 * contrast to gst_structure_foreach(), the function may modify but not delete the
997 * fields. The structure must be mutable.
998 *
999 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1000 * FALSE otherwise.
1001 */
1002 gboolean
1003 gst_structure_map_in_place (GstStructure * structure,
1004 GstStructureMapFunc func, gpointer user_data)
1005 {
1006 guint i, len;
1007 GstStructureField *field;
1008 gboolean ret;
1010 g_return_val_if_fail (structure != NULL, FALSE);
1011 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1012 g_return_val_if_fail (func != NULL, FALSE);
1013 len = structure->fields->len;
1015 for (i = 0; i < len; i++) {
1016 field = GST_STRUCTURE_FIELD (structure, i);
1018 ret = func (field->name, &field->value, user_data);
1019 if (!ret)
1020 return FALSE;
1021 }
1023 return TRUE;
1024 }
1026 /**
1027 * gst_structure_has_field:
1028 * @structure: a #GstStructure
1029 * @fieldname: the name of a field
1030 *
1031 * Check if @structure contains a field named @fieldname.
1032 *
1033 * Returns: TRUE if the structure contains a field with the given name
1034 */
1035 gboolean
1036 gst_structure_has_field (const GstStructure * structure,
1037 const gchar * fieldname)
1038 {
1039 GstStructureField *field;
1041 g_return_val_if_fail (structure != NULL, 0);
1042 g_return_val_if_fail (fieldname != NULL, 0);
1044 field = gst_structure_get_field (structure, fieldname);
1046 return (field != NULL);
1047 }
1049 /**
1050 * gst_structure_has_field_typed:
1051 * @structure: a #GstStructure
1052 * @fieldname: the name of a field
1053 * @type: the type of a value
1054 *
1055 * Check if @structure contains a field named @fieldname and with GType @type.
1056 *
1057 * Returns: TRUE if the structure contains a field with the given name and type
1058 */
1059 gboolean
1060 gst_structure_has_field_typed (const GstStructure * structure,
1061 const gchar * fieldname, GType type)
1062 {
1063 GstStructureField *field;
1065 g_return_val_if_fail (structure != NULL, 0);
1066 g_return_val_if_fail (fieldname != NULL, 0);
1068 field = gst_structure_get_field (structure, fieldname);
1069 if (field == NULL)
1070 return FALSE;
1072 return (G_VALUE_TYPE (&field->value) == type);
1073 }
1076 /* utility functions */
1078 /**
1079 * gst_structure_get_boolean:
1080 * @structure: a #GstStructure
1081 * @fieldname: the name of a field
1082 * @value: a pointer to a #gboolean to set
1083 *
1084 * Sets the boolean pointed to by @value corresponding to the value of the
1085 * given field. Caller is responsible for making sure the field exists
1086 * and has the correct type.
1087 *
1088 * Returns: TRUE if the value could be set correctly. If there was no field
1089 * with @fieldname or the existing field did not contain a boolean, this
1090 * function returns FALSE.
1091 */
1092 gboolean
1093 gst_structure_get_boolean (const GstStructure * structure,
1094 const gchar * fieldname, gboolean * value)
1095 {
1096 GstStructureField *field;
1098 g_return_val_if_fail (structure != NULL, FALSE);
1099 g_return_val_if_fail (fieldname != NULL, FALSE);
1101 field = gst_structure_get_field (structure, fieldname);
1103 if (field == NULL)
1104 return FALSE;
1105 if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
1106 return FALSE;
1108 *value = g_value_get_boolean (&field->value);
1110 return TRUE;
1111 }
1113 /**
1114 * gst_structure_get_int:
1115 * @structure: a #GstStructure
1116 * @fieldname: the name of a field
1117 * @value: a pointer to an int to set
1118 *
1119 * Sets the int pointed to by @value corresponding to the value of the
1120 * given field. Caller is responsible for making sure the field exists
1121 * and has the correct type.
1122 *
1123 * Returns: %TRUE if the value could be set correctly. If there was no field
1124 * with @fieldname or the existing field did not contain an int, this function
1125 * returns %FALSE.
1126 */
1127 gboolean
1128 gst_structure_get_int (const GstStructure * structure,
1129 const gchar * fieldname, gint * value)
1130 {
1131 GstStructureField *field;
1133 g_return_val_if_fail (structure != NULL, FALSE);
1134 g_return_val_if_fail (fieldname != NULL, FALSE);
1135 g_return_val_if_fail (value != NULL, FALSE);
1137 field = gst_structure_get_field (structure, fieldname);
1139 if (field == NULL)
1140 return FALSE;
1141 if (!G_VALUE_HOLDS_INT (&field->value))
1142 return FALSE;
1144 *value = g_value_get_int (&field->value);
1146 return TRUE;
1147 }
1149 /**
1150 * gst_structure_get_uint:
1151 * @structure: a #GstStructure
1152 * @fieldname: the name of a field
1153 * @value: a pointer to a uint to set
1154 *
1155 * Sets the uint pointed to by @value corresponding to the value of the
1156 * given field. Caller is responsible for making sure the field exists
1157 * and has the correct type.
1158 *
1159 * Returns: %TRUE if the value could be set correctly. If there was no field
1160 * with @fieldname or the existing field did not contain a uint, this function
1161 * returns %FALSE.
1162 *
1163 * Since: 0.10.15
1164 */
1165 gboolean
1166 gst_structure_get_uint (const GstStructure * structure,
1167 const gchar * fieldname, guint * value)
1168 {
1169 GstStructureField *field;
1171 g_return_val_if_fail (structure != NULL, FALSE);
1172 g_return_val_if_fail (fieldname != NULL, FALSE);
1173 g_return_val_if_fail (value != NULL, FALSE);
1175 field = gst_structure_get_field (structure, fieldname);
1177 if (field == NULL)
1178 return FALSE;
1179 if (!G_VALUE_HOLDS_UINT (&field->value))
1180 return FALSE;
1182 *value = g_value_get_uint (&field->value);
1184 return TRUE;
1185 }
1187 /**
1188 * gst_structure_get_fourcc:
1189 * @structure: a #GstStructure
1190 * @fieldname: the name of a field
1191 * @value: a pointer to a #GstFourcc to set
1192 *
1193 * Sets the #GstFourcc pointed to by @value corresponding to the value of the
1194 * given field. Caller is responsible for making sure the field exists
1195 * and has the correct type.
1196 *
1197 * Returns: TRUE if the value could be set correctly. If there was no field
1198 * with @fieldname or the existing field did not contain a fourcc, this function
1199 * returns FALSE.
1200 */
1201 gboolean
1202 gst_structure_get_fourcc (const GstStructure * structure,
1203 const gchar * fieldname, guint32 * value)
1204 {
1205 GstStructureField *field;
1207 g_return_val_if_fail (structure != NULL, FALSE);
1208 g_return_val_if_fail (fieldname != NULL, FALSE);
1209 g_return_val_if_fail (value != NULL, FALSE);
1211 field = gst_structure_get_field (structure, fieldname);
1213 if (field == NULL)
1214 return FALSE;
1215 if (!GST_VALUE_HOLDS_FOURCC (&field->value))
1216 return FALSE;
1218 *value = gst_value_get_fourcc (&field->value);
1220 return TRUE;
1221 }
1223 /**
1224 * gst_structure_get_date:
1225 * @structure: a #GstStructure
1226 * @fieldname: the name of a field
1227 * @value: a pointer to a #GDate to set
1228 *
1229 * Sets the date pointed to by @value corresponding to the date of the
1230 * given field. Caller is responsible for making sure the field exists
1231 * and has the correct type.
1232 *
1233 * On success @value will point to a newly-allocated copy of the date which
1234 * should be freed with g_date_free() when no longer needed (note: this is
1235 * inconsistent with e.g. gst_structure_get_string() which doesn't return a
1236 * copy of the string).
1237 *
1238 * Returns: TRUE if the value could be set correctly. If there was no field
1239 * with @fieldname or the existing field did not contain a data, this function
1240 * returns FALSE.
1241 */
1242 gboolean
1243 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1244 GDate ** value)
1245 {
1246 GstStructureField *field;
1248 g_return_val_if_fail (structure != NULL, FALSE);
1249 g_return_val_if_fail (fieldname != NULL, FALSE);
1250 g_return_val_if_fail (value != NULL, FALSE);
1252 field = gst_structure_get_field (structure, fieldname);
1254 if (field == NULL)
1255 return FALSE;
1256 if (!GST_VALUE_HOLDS_DATE (&field->value))
1257 return FALSE;
1259 /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1260 *value = g_value_dup_boxed (&field->value);
1262 return TRUE;
1263 }
1265 /**
1266 * gst_structure_get_clock_time:
1267 * @structure: a #GstStructure
1268 * @fieldname: the name of a field
1269 * @value: a pointer to a #GstClockTime to set
1270 *
1271 * Sets the clock time pointed to by @value corresponding to the clock time
1272 * of the given field. Caller is responsible for making sure the field exists
1273 * and has the correct type.
1274 *
1275 * Returns: TRUE if the value could be set correctly. If there was no field
1276 * with @fieldname or the existing field did not contain a #GstClockTime, this
1277 * function returns FALSE.
1278 */
1279 gboolean
1280 gst_structure_get_clock_time (const GstStructure * structure,
1281 const gchar * fieldname, GstClockTime * value)
1282 {
1283 GstStructureField *field;
1285 g_return_val_if_fail (structure != NULL, FALSE);
1286 g_return_val_if_fail (fieldname != NULL, FALSE);
1287 g_return_val_if_fail (value != NULL, FALSE);
1289 field = gst_structure_get_field (structure, fieldname);
1291 if (field == NULL)
1292 return FALSE;
1293 if (!G_VALUE_HOLDS_UINT64 (&field->value))
1294 return FALSE;
1296 *value = g_value_get_uint64 (&field->value);
1298 return TRUE;
1299 }
1301 /**
1302 * gst_structure_get_double:
1303 * @structure: a #GstStructure
1304 * @fieldname: the name of a field
1305 * @value: a pointer to a #GstFourcc to set
1306 *
1307 * Sets the double pointed to by @value corresponding to the value of the
1308 * given field. Caller is responsible for making sure the field exists
1309 * and has the correct type.
1310 *
1311 * Returns: TRUE if the value could be set correctly. If there was no field
1312 * with @fieldname or the existing field did not contain a double, this
1313 * function returns FALSE.
1314 */
1315 gboolean
1316 gst_structure_get_double (const GstStructure * structure,
1317 const gchar * fieldname, gdouble * value)
1318 {
1319 GstStructureField *field;
1321 g_return_val_if_fail (structure != NULL, FALSE);
1322 g_return_val_if_fail (fieldname != NULL, FALSE);
1323 g_return_val_if_fail (value != NULL, FALSE);
1325 field = gst_structure_get_field (structure, fieldname);
1327 if (field == NULL)
1328 return FALSE;
1329 if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1330 return FALSE;
1332 *value = g_value_get_double (&field->value);
1334 return TRUE;
1335 }
1337 /**
1338 * gst_structure_get_string:
1339 * @structure: a #GstStructure
1340 * @fieldname: the name of a field
1341 *
1342 * Finds the field corresponding to @fieldname, and returns the string
1343 * contained in the field's value. Caller is responsible for making
1344 * sure the field exists and has the correct type.
1345 *
1346 * The string should not be modified, and remains valid until the next
1347 * call to a gst_structure_*() function with the given structure.
1348 *
1349 * Returns: a pointer to the string or NULL when the field did not exist
1350 * or did not contain a string.
1351 */
1352 const gchar *
1353 gst_structure_get_string (const GstStructure * structure,
1354 const gchar * fieldname)
1355 {
1356 GstStructureField *field;
1358 g_return_val_if_fail (structure != NULL, NULL);
1359 g_return_val_if_fail (fieldname != NULL, NULL);
1361 field = gst_structure_get_field (structure, fieldname);
1363 if (field == NULL)
1364 return NULL;
1365 if (!G_VALUE_HOLDS_STRING (&field->value))
1366 return NULL;
1368 return g_value_get_string (&field->value);
1369 }
1371 /**
1372 * gst_structure_get_enum:
1373 * @structure: a #GstStructure
1374 * @fieldname: the name of a field
1375 * @enumtype: the enum type of a field
1376 * @value: a pointer to an int to set
1377 *
1378 * Sets the int pointed to by @value corresponding to the value of the
1379 * given field. Caller is responsible for making sure the field exists,
1380 * has the correct type and that the enumtype is correct.
1381 *
1382 * Returns: TRUE if the value could be set correctly. If there was no field
1383 * with @fieldname or the existing field did not contain an enum of the given
1384 * type, this function returns FALSE.
1385 */
1386 gboolean
1387 gst_structure_get_enum (const GstStructure * structure,
1388 const gchar * fieldname, GType enumtype, gint * value)
1389 {
1390 GstStructureField *field;
1392 g_return_val_if_fail (structure != NULL, FALSE);
1393 g_return_val_if_fail (fieldname != NULL, FALSE);
1394 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1395 g_return_val_if_fail (value != NULL, FALSE);
1397 field = gst_structure_get_field (structure, fieldname);
1399 if (field == NULL)
1400 return FALSE;
1401 if (!G_VALUE_HOLDS_ENUM (&field->value))
1402 return FALSE;
1403 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1404 return FALSE;
1406 *value = g_value_get_enum (&field->value);
1408 return TRUE;
1409 }
1411 /**
1412 * gst_structure_get_fraction:
1413 * @structure: a #GstStructure
1414 * @fieldname: the name of a field
1415 * @value_numerator: a pointer to an int to set
1416 * @value_denominator: a pointer to an int to set
1417 *
1418 * Sets the integers pointed to by @value_numerator and @value_denominator
1419 * corresponding to the value of the given field. Caller is responsible
1420 * for making sure the field exists and has the correct type.
1421 *
1422 * Returns: TRUE if the values could be set correctly. If there was no field
1423 * with @fieldname or the existing field did not contain a GstFraction, this
1424 * function returns FALSE.
1425 */
1426 gboolean
1427 gst_structure_get_fraction (const GstStructure * structure,
1428 const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1429 {
1430 GstStructureField *field;
1432 g_return_val_if_fail (structure != NULL, FALSE);
1433 g_return_val_if_fail (fieldname != NULL, FALSE);
1434 g_return_val_if_fail (value_numerator != NULL, FALSE);
1435 g_return_val_if_fail (value_denominator != NULL, FALSE);
1437 field = gst_structure_get_field (structure, fieldname);
1439 if (field == NULL)
1440 return FALSE;
1441 if (!GST_VALUE_HOLDS_FRACTION (&field->value))
1442 return FALSE;
1444 *value_numerator = gst_value_get_fraction_numerator (&field->value);
1445 *value_denominator = gst_value_get_fraction_denominator (&field->value);
1447 return TRUE;
1448 }
1450 typedef struct _GstStructureAbbreviation
1451 {
1452 gchar *type_name;
1453 GType type;
1454 }
1455 GstStructureAbbreviation;
1457 /* return a copy of an array of GstStructureAbbreviation containing all the
1458 * known type_string, GType maps, including abbreviations for common types */
1459 static GstStructureAbbreviation *
1460 gst_structure_get_abbrs (gint * n_abbrs)
1461 {
1462 static GstStructureAbbreviation *abbrs = NULL;
1463 static gint num = 0;
1465 if (abbrs == NULL) {
1466 /* dynamically generate the array */
1467 GstStructureAbbreviation dyn_abbrs[] = {
1468 {"int", G_TYPE_INT}
1469 ,
1470 {"i", G_TYPE_INT}
1471 ,
1472 {"float", G_TYPE_FLOAT}
1473 ,
1474 {"f", G_TYPE_FLOAT}
1475 ,
1476 {"double", G_TYPE_DOUBLE}
1477 ,
1478 {"d", G_TYPE_DOUBLE}
1479 ,
1480 {"buffer", GST_TYPE_BUFFER}
1481 ,
1482 {"fourcc", GST_TYPE_FOURCC}
1483 ,
1484 {"4", GST_TYPE_FOURCC}
1485 ,
1486 {"fraction", GST_TYPE_FRACTION}
1487 ,
1488 {"boolean", G_TYPE_BOOLEAN}
1489 ,
1490 {"bool", G_TYPE_BOOLEAN}
1491 ,
1492 {"b", G_TYPE_BOOLEAN}
1493 ,
1494 {"string", G_TYPE_STRING}
1495 ,
1496 {"str", G_TYPE_STRING}
1497 ,
1498 {"s", G_TYPE_STRING}
1499 ,
1500 {"structure", GST_TYPE_STRUCTURE}
1501 };
1502 num = G_N_ELEMENTS (dyn_abbrs);
1503 /* permanently allocate and copy the array now */
1504 abbrs = g_new0 (GstStructureAbbreviation, num);
1505 memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * num);
1506 }
1507 *n_abbrs = num;
1509 return abbrs;
1510 }
1512 /* given a type_name that could be a type abbreviation or a registered GType,
1513 * return a matching GType */
1514 static GType
1515 gst_structure_gtype_from_abbr (const char *type_name)
1516 {
1517 int i;
1518 GstStructureAbbreviation *abbrs;
1519 gint n_abbrs;
1521 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1523 abbrs = gst_structure_get_abbrs (&n_abbrs);
1525 for (i = 0; i < n_abbrs; i++) {
1526 if (strcmp (type_name, abbrs[i].type_name) == 0) {
1527 return abbrs[i].type;
1528 }
1529 }
1531 /* this is the fallback */
1532 return g_type_from_name (type_name);
1533 }
1535 static const char *
1536 gst_structure_to_abbr (GType type)
1537 {
1538 int i;
1539 GstStructureAbbreviation *abbrs;
1540 gint n_abbrs;
1542 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1544 abbrs = gst_structure_get_abbrs (&n_abbrs);
1546 for (i = 0; i < n_abbrs; i++) {
1547 if (type == abbrs[i].type) {
1548 return abbrs[i].type_name;
1549 }
1550 }
1552 return g_type_name (type);
1553 }
1555 static GType
1556 gst_structure_value_get_generic_type (GValue * val)
1557 {
1558 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1559 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1560 GArray *array = g_value_peek_pointer (val);
1562 if (array->len > 0) {
1563 GValue *value = &g_array_index (array, GValue, 0);
1565 return gst_structure_value_get_generic_type (value);
1566 } else {
1567 return G_TYPE_INT;
1568 }
1569 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1570 return G_TYPE_INT;
1571 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1572 return G_TYPE_DOUBLE;
1573 } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1574 return GST_TYPE_FRACTION;
1575 }
1576 return G_VALUE_TYPE (val);
1577 }
1579 gboolean
1580 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1581 GString * s)
1582 {
1583 GstStructureField *field;
1584 guint i, len;
1586 g_return_val_if_fail (s != NULL, FALSE);
1588 g_string_append (s, g_quark_to_string (structure->name));
1589 len = structure->fields->len;
1590 for (i = 0; i < len; i++) {
1591 char *t;
1592 GType type;
1594 field = GST_STRUCTURE_FIELD (structure, i);
1596 t = gst_value_serialize (&field->value);
1597 type = gst_structure_value_get_generic_type (&field->value);
1599 g_string_append_len (s, ", ", 2);
1600 /* FIXME: do we need to escape fieldnames? */
1601 g_string_append (s, g_quark_to_string (field->name));
1602 g_string_append_len (s, "=(", 2);
1603 g_string_append (s, gst_structure_to_abbr (type));
1604 g_string_append_c (s, ')');
1605 g_string_append (s, GST_STR_NULL (t));
1606 g_free (t);
1607 }
1609 g_string_append_c (s, ';');
1610 return TRUE;
1611 }
1613 /**
1614 * gst_structure_to_string:
1615 * @structure: a #GstStructure
1616 *
1617 * Converts @structure to a human-readable string representation.
1618 *
1619 * For debugging purposes its easier to do something like this:
1620 * |[
1621 * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1622 * ]|
1623 * This prints the structure in human readble form.
1624 *
1625 * Returns: a pointer to string allocated by g_malloc(). g_free() after
1626 * usage.
1627 */
1628 gchar *
1629 gst_structure_to_string (const GstStructure * structure)
1630 {
1631 GString *s;
1633 /* NOTE: This function is potentially called by the debug system,
1634 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1635 * should be careful to avoid recursion. This includes any functions
1636 * called by gst_structure_to_string. In particular, calls should
1637 * not use the GST_PTR_FORMAT extension. */
1639 g_return_val_if_fail (structure != NULL, NULL);
1641 /* we estimate a minimum size based on the number of fields in order to
1642 * avoid unnecessary reallocs within GString */
1643 s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1644 priv_gst_structure_append_to_gstring (structure, s);
1645 return g_string_free (s, FALSE);
1646 }
1648 /*
1649 * r will still point to the string. if end == next, the string will not be
1650 * null-terminated. In all other cases it will be.
1651 * end = pointer to char behind end of string, next = pointer to start of
1652 * unread data.
1653 * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1654 */
1655 static gboolean
1656 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next)
1657 {
1658 gchar *w;
1660 if (*s == 0)
1661 return FALSE;
1663 if (*s != '"') {
1664 int ret;
1666 ret = gst_structure_parse_simple_string (s, end);
1667 *next = *end;
1669 return ret;
1670 }
1672 w = s;
1673 s++;
1674 while (*s != '"') {
1675 if (*s == 0)
1676 return FALSE;
1678 if (*s == '\\') {
1679 s++;
1680 }
1682 *w = *s;
1683 w++;
1684 s++;
1685 }
1686 s++;
1688 *end = w;
1689 *next = s;
1691 return TRUE;
1692 }
1694 static gboolean
1695 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1696 GType type)
1697 {
1698 GValue value1 = { 0 };
1699 GValue value2 = { 0 };
1700 GType range_type;
1701 gboolean ret;
1703 if (*s != '[')
1704 return FALSE;
1705 s++;
1707 ret = gst_structure_parse_value (s, &s, &value1, type);
1708 if (ret == FALSE)
1709 return FALSE;
1711 while (g_ascii_isspace (*s))
1712 s++;
1714 if (*s != ',')
1715 return FALSE;
1716 s++;
1718 while (g_ascii_isspace (*s))
1719 s++;
1721 ret = gst_structure_parse_value (s, &s, &value2, type);
1722 if (ret == FALSE)
1723 return FALSE;
1725 while (g_ascii_isspace (*s))
1726 s++;
1728 if (*s != ']')
1729 return FALSE;
1730 s++;
1732 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1733 return FALSE;
1735 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1736 range_type = GST_TYPE_DOUBLE_RANGE;
1737 g_value_init (value, range_type);
1738 gst_value_set_double_range (value, g_value_get_double (&value1),
1739 g_value_get_double (&value2));
1740 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1741 range_type = GST_TYPE_INT_RANGE;
1742 g_value_init (value, range_type);
1743 gst_value_set_int_range (value, g_value_get_int (&value1),
1744 g_value_get_int (&value2));
1745 } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
1746 range_type = GST_TYPE_FRACTION_RANGE;
1747 g_value_init (value, range_type);
1748 gst_value_set_fraction_range (value, &value1, &value2);
1749 } else {
1750 return FALSE;
1751 }
1753 *after = s;
1754 return TRUE;
1755 }
1757 static gboolean
1758 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
1759 GType type, GType list_type, char begin, char end)
1760 {
1761 GValue list_value = { 0 };
1762 gboolean ret;
1763 GArray *array;
1765 g_value_init (value, list_type);
1766 array = g_value_peek_pointer (value);
1768 if (*s != begin)
1769 return FALSE;
1770 s++;
1772 while (g_ascii_isspace (*s))
1773 s++;
1774 if (*s == end) {
1775 s++;
1776 *after = s;
1777 return TRUE;
1778 }
1780 ret = gst_structure_parse_value (s, &s, &list_value, type);
1781 if (ret == FALSE)
1782 return FALSE;
1784 g_array_append_val (array, list_value);
1786 while (g_ascii_isspace (*s))
1787 s++;
1789 while (*s != end) {
1790 if (*s != ',')
1791 return FALSE;
1792 s++;
1794 while (g_ascii_isspace (*s))
1795 s++;
1797 memset (&list_value, 0, sizeof (list_value));
1798 ret = gst_structure_parse_value (s, &s, &list_value, type);
1799 if (ret == FALSE)
1800 return FALSE;
1802 g_array_append_val (array, list_value);
1803 while (g_ascii_isspace (*s))
1804 s++;
1805 }
1807 s++;
1809 *after = s;
1810 return TRUE;
1811 }
1813 static gboolean
1814 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
1815 {
1816 return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
1817 '{', '}');
1818 }
1820 static gboolean
1821 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
1822 GType type)
1823 {
1824 return gst_structure_parse_any_list (s, after, value, type,
1825 GST_TYPE_ARRAY, '<', '>');
1826 }
1828 static gboolean
1829 gst_structure_parse_simple_string (gchar * str, gchar ** end)
1830 {
1831 char *s = str;
1833 while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
1834 s++;
1835 }
1837 *end = s;
1839 return (s != str);
1840 }
1842 static gboolean
1843 gst_structure_parse_field (gchar * str,
1844 gchar ** after, GstStructureField * field)
1845 {
1846 gchar *name;
1847 gchar *name_end;
1848 gchar *s;
1849 gchar c;
1851 s = str;
1853 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1854 s++;
1855 name = s;
1856 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &name_end)))
1857 return FALSE;
1859 s = name_end;
1860 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1861 s++;
1863 if (G_UNLIKELY (*s != '='))
1864 return FALSE;
1865 s++;
1867 c = *name_end;
1868 *name_end = '\0';
1869 field->name = g_quark_from_string (name);
1870 *name_end = c;
1872 if (G_UNLIKELY (!gst_structure_parse_value (s, &s, &field->value,
1873 G_TYPE_INVALID)))
1874 return FALSE;
1876 *after = s;
1877 return TRUE;
1878 }
1880 static gboolean
1881 gst_structure_parse_value (gchar * str,
1882 gchar ** after, GValue * value, GType default_type)
1883 {
1884 gchar *type_name;
1885 gchar *type_end;
1886 gchar *value_s;
1887 gchar *value_end;
1888 gchar *s;
1889 gchar c;
1890 int ret = 0;
1891 GType type = default_type;
1893 s = str;
1894 while (g_ascii_isspace (*s))
1895 s++;
1897 /* check if there's a (type_name) 'cast' */
1898 type_name = NULL;
1899 if (*s == '(') {
1900 s++;
1901 while (g_ascii_isspace (*s))
1902 s++;
1903 type_name = s;
1904 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &type_end)))
1905 return FALSE;
1906 s = type_end;
1907 while (g_ascii_isspace (*s))
1908 s++;
1909 if (G_UNLIKELY (*s != ')'))
1910 return FALSE;
1911 s++;
1912 while (g_ascii_isspace (*s))
1913 s++;
1915 c = *type_end;
1916 *type_end = 0;
1917 type = gst_structure_gtype_from_abbr (type_name);
1918 *type_end = c;
1920 if (G_UNLIKELY (type == G_TYPE_INVALID))
1921 return FALSE;
1922 }
1924 while (g_ascii_isspace (*s))
1925 s++;
1926 if (*s == '[') {
1927 ret = gst_structure_parse_range (s, &s, value, type);
1928 } else if (*s == '{') {
1929 ret = gst_structure_parse_list (s, &s, value, type);
1930 } else if (*s == '<') {
1931 ret = gst_structure_parse_array (s, &s, value, type);
1932 } else {
1933 value_s = s;
1934 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s)))
1935 return FALSE;
1937 c = *value_end;
1938 *value_end = '\0';
1939 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
1940 GType try_types[] =
1941 { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_BOOLEAN,
1942 G_TYPE_STRING
1943 };
1944 int i;
1946 for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
1947 g_value_init (value, try_types[i]);
1948 ret = gst_value_deserialize (value, value_s);
1949 if (ret)
1950 break;
1951 g_value_unset (value);
1952 }
1953 } else {
1954 g_value_init (value, type);
1956 ret = gst_value_deserialize (value, value_s);
1957 if (G_UNLIKELY (!ret))
1958 g_value_unset (value);
1959 }
1960 *value_end = c;
1961 }
1963 *after = s;
1965 return ret;
1966 }
1968 /**
1969 * gst_structure_from_string:
1970 * @string: a string representation of a #GstStructure.
1971 * @end: pointer to store the end of the string in.
1972 *
1973 * Creates a #GstStructure from a string representation.
1974 * If end is not NULL, a pointer to the place inside the given string
1975 * where parsing ended will be returned.
1976 *
1977 * Returns: a new #GstStructure or NULL when the string could not
1978 * be parsed. Free with gst_structure_free() after use.
1979 */
1980 GstStructure *
1981 gst_structure_from_string (const gchar * string, gchar ** end)
1982 {
1983 char *name;
1984 char *copy;
1985 char *w;
1986 char *r;
1987 char save;
1988 GstStructure *structure = NULL;
1989 GstStructureField field;
1991 g_return_val_if_fail (string != NULL, NULL);
1993 copy = g_strdup (string);
1994 r = copy;
1996 /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
1997 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1998 && g_ascii_isspace (r[1]))))
1999 r++;
2001 name = r;
2002 if (G_UNLIKELY (!gst_structure_parse_string (r, &w, &r))) {
2003 GST_WARNING ("Failed to parse structure string");
2004 goto error;
2005 }
2007 save = *w;
2008 *w = '\0';
2009 structure = gst_structure_empty_new (name);
2010 *w = save;
2012 if (G_UNLIKELY (structure == NULL))
2013 goto error;
2015 do {
2016 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2017 && g_ascii_isspace (r[1]))))
2018 r++;
2019 if (*r == ';') {
2020 /* end of structure, get the next char and finish */
2021 r++;
2022 break;
2023 }
2024 if (*r == '\0') {
2025 /* accept \0 as end delimiter */
2026 break;
2027 }
2028 if (G_UNLIKELY (*r != ',')) {
2029 GST_WARNING ("Failed to find delimiter, r=%s", r);
2030 goto error;
2031 }
2032 r++;
2033 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2034 && g_ascii_isspace (r[1]))))
2035 r++;
2037 memset (&field, 0, sizeof (field));
2038 if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field)))
2039 goto error;
2040 gst_structure_set_field (structure, &field);
2041 } while (TRUE);
2043 if (end)
2044 *end = (char *) string + (r - copy);
2045 else if (*r)
2046 g_warning ("gst_structure_from_string did not consume whole string,"
2047 " but caller did not provide end pointer (\"%s\")", string);
2049 g_free (copy);
2050 return structure;
2052 error:
2053 if (structure)
2054 gst_structure_free (structure);
2055 g_free (copy);
2056 return NULL;
2057 }
2059 static void
2060 gst_structure_transform_to_string (const GValue * src_value,
2061 GValue * dest_value)
2062 {
2063 g_return_if_fail (src_value != NULL);
2064 g_return_if_fail (dest_value != NULL);
2066 dest_value->data[0].v_pointer =
2067 gst_structure_to_string (src_value->data[0].v_pointer);
2068 }
2070 static GstStructure *
2071 gst_structure_copy_conditional (const GstStructure * structure)
2072 {
2073 if (structure)
2074 return gst_structure_copy (structure);
2075 return NULL;
2076 }
2078 /* fixate utility functions */
2080 /**
2081 * gst_structure_fixate_field_nearest_int:
2082 * @structure: a #GstStructure
2083 * @field_name: a field in @structure
2084 * @target: the target value of the fixation
2085 *
2086 * Fixates a #GstStructure by changing the given field to the nearest
2087 * integer to @target that is a subset of the existing field.
2088 *
2089 * Returns: TRUE if the structure could be fixated
2090 */
2091 gboolean
2092 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2093 const char *field_name, int target)
2094 {
2095 const GValue *value;
2097 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2098 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2100 value = gst_structure_get_value (structure, field_name);
2102 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2103 /* already fixed */
2104 return FALSE;
2105 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2106 int x;
2108 x = gst_value_get_int_range_min (value);
2109 if (target < x)
2110 target = x;
2111 x = gst_value_get_int_range_max (value);
2112 if (target > x)
2113 target = x;
2114 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2115 return TRUE;
2116 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2117 const GValue *list_value;
2118 int i, n;
2119 int best = 0;
2120 int best_index = -1;
2122 n = gst_value_list_get_size (value);
2123 for (i = 0; i < n; i++) {
2124 list_value = gst_value_list_get_value (value, i);
2125 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2126 int x = g_value_get_int (list_value);
2128 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2129 best_index = i;
2130 best = x;
2131 }
2132 }
2133 }
2134 if (best_index != -1) {
2135 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2136 return TRUE;
2137 }
2138 return FALSE;
2139 }
2141 return FALSE;
2142 }
2144 /**
2145 * gst_structure_fixate_field_nearest_double:
2146 * @structure: a #GstStructure
2147 * @field_name: a field in @structure
2148 * @target: the target value of the fixation
2149 *
2150 * Fixates a #GstStructure by changing the given field to the nearest
2151 * double to @target that is a subset of the existing field.
2152 *
2153 * Returns: TRUE if the structure could be fixated
2154 */
2155 gboolean
2156 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2157 const char *field_name, double target)
2158 {
2159 const GValue *value;
2161 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2162 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2164 value = gst_structure_get_value (structure, field_name);
2166 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2167 /* already fixed */
2168 return FALSE;
2169 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2170 double x;
2172 x = gst_value_get_double_range_min (value);
2173 if (target < x)
2174 target = x;
2175 x = gst_value_get_double_range_max (value);
2176 if (target > x)
2177 target = x;
2178 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2179 return TRUE;
2180 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2181 const GValue *list_value;
2182 int i, n;
2183 double best = 0;
2184 int best_index = -1;
2186 n = gst_value_list_get_size (value);
2187 for (i = 0; i < n; i++) {
2188 list_value = gst_value_list_get_value (value, i);
2189 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2190 double x = g_value_get_double (list_value);
2192 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2193 best_index = i;
2194 best = x;
2195 }
2196 }
2197 }
2198 if (best_index != -1) {
2199 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2200 return TRUE;
2201 }
2202 return FALSE;
2203 }
2205 return FALSE;
2207 }
2209 /**
2210 * gst_structure_fixate_field_boolean:
2211 * @structure: a #GstStructure
2212 * @field_name: a field in @structure
2213 * @target: the target value of the fixation
2214 *
2215 * Fixates a #GstStructure by changing the given @field_name field to the given
2216 * @target boolean if that field is not fixed yet.
2217 *
2218 * Returns: TRUE if the structure could be fixated
2219 */
2220 gboolean
2221 gst_structure_fixate_field_boolean (GstStructure * structure,
2222 const char *field_name, gboolean target)
2223 {
2224 const GValue *value;
2226 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2227 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2229 value = gst_structure_get_value (structure, field_name);
2231 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2232 /* already fixed */
2233 return FALSE;
2234 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2235 const GValue *list_value;
2236 int i, n;
2237 int best = 0;
2238 int best_index = -1;
2240 n = gst_value_list_get_size (value);
2241 for (i = 0; i < n; i++) {
2242 list_value = gst_value_list_get_value (value, i);
2243 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2244 gboolean x = g_value_get_boolean (list_value);
2246 if (best_index == -1 || x == target) {
2247 best_index = i;
2248 best = x;
2249 }
2250 }
2251 }
2252 if (best_index != -1) {
2253 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2254 return TRUE;
2255 }
2256 return FALSE;
2257 }
2259 return FALSE;
2260 }
2262 /**
2263 * gst_structure_fixate_field_nearest_fraction:
2264 * @structure: a #GstStructure
2265 * @field_name: a field in @structure
2266 * @target_numerator: The numerator of the target value of the fixation
2267 * @target_denominator: The denominator of the target value of the fixation
2268 *
2269 * Fixates a #GstStructure by changing the given field to the nearest
2270 * fraction to @target_numerator/@target_denominator that is a subset
2271 * of the existing field.
2272 *
2273 * Returns: TRUE if the structure could be fixated
2274 */
2275 gboolean
2276 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2277 const char *field_name, const gint target_numerator,
2278 const gint target_denominator)
2279 {
2280 const GValue *value;
2282 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2283 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2285 value = gst_structure_get_value (structure, field_name);
2287 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2288 /* already fixed */
2289 return FALSE;
2290 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2291 const GValue *x, *new_value;
2292 GValue target = { 0 };
2293 g_value_init (&target, GST_TYPE_FRACTION);
2294 gst_value_set_fraction (&target, target_numerator, target_denominator);
2296 new_value = ⌖
2297 x = gst_value_get_fraction_range_min (value);
2298 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2299 new_value = x;
2300 x = gst_value_get_fraction_range_max (value);
2301 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2302 new_value = x;
2304 gst_structure_set_value (structure, field_name, new_value);
2305 g_value_unset (&target);
2306 return TRUE;
2307 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2308 const GValue *list_value;
2309 int i, n;
2310 const GValue *best = NULL;
2311 gdouble target;
2312 gdouble cur_diff;
2313 gdouble best_diff = G_MAXDOUBLE;
2315 target = (gdouble) target_numerator / (gdouble) target_denominator;
2317 GST_DEBUG ("target %g, best %g", target, best_diff);
2319 best = NULL;
2321 n = gst_value_list_get_size (value);
2322 for (i = 0; i < n; i++) {
2323 list_value = gst_value_list_get_value (value, i);
2324 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2325 gint num, denom;
2326 gdouble list_double;
2328 num = gst_value_get_fraction_numerator (list_value);
2329 denom = gst_value_get_fraction_denominator (list_value);
2331 list_double = ((gdouble) num / (gdouble) denom);
2332 cur_diff = target - list_double;
2334 GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2336 if (cur_diff < 0)
2337 cur_diff = -cur_diff;
2339 if (!best || cur_diff < best_diff) {
2340 GST_DEBUG ("new best %g", list_double);
2341 best = list_value;
2342 best_diff = cur_diff;
2343 }
2344 }
2345 }
2346 if (best != NULL) {
2347 gst_structure_set_value (structure, field_name, best);
2348 return TRUE;
2349 }
2350 }
2352 return FALSE;
2353 }
2355 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2356 * (useful for message parsing functions where the return location is user
2357 * supplied and the user may pass NULL if the value isn't of interest) */
2358 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname) \
2359 G_STMT_START { \
2360 const GValue *_value = (value); \
2361 guint _flags = (flags); \
2362 GType _value_type = G_VALUE_TYPE (_value); \
2363 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
2364 gchar *_lcopy_format = _vtable->lcopy_format; \
2365 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
2366 guint _n_values = 0; \
2367 \
2368 while (*_lcopy_format != '\0') { \
2369 g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER); \
2370 _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer); \
2371 _lcopy_format++; \
2372 } \
2373 if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2374 *(__error) = g_strdup_printf ("either all or none of the return " \
2375 "locations for field '%s' need to be NULL", fieldname); \
2376 } else if (_cvalues[0].v_pointer != NULL) { \
2377 *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags); \
2378 } \
2379 } G_STMT_END
2381 /**
2382 * gst_structure_get_valist:
2383 * @structure: a #GstStructure
2384 * @first_fieldname: the name of the first field to read
2385 * @args: variable arguments
2386 *
2387 * Parses the variable arguments and reads fields from @structure accordingly.
2388 * valist-variant of gst_structure_get(). Look at the documentation of
2389 * gst_structure_get() for more details.
2390 *
2391 * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2392 *
2393 * Since: 0.10.24
2394 */
2395 gboolean
2396 gst_structure_get_valist (GstStructure * structure,
2397 const char *first_fieldname, va_list args)
2398 {
2399 const char *field_name;
2400 GType expected_type = G_TYPE_INVALID;
2402 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2403 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2405 field_name = first_fieldname;
2406 while (field_name) {
2407 const GValue *val = NULL;
2408 gchar *err = NULL;
2410 expected_type = va_arg (args, GType);
2412 val = gst_structure_get_value (structure, field_name);
2414 if (val == NULL)
2415 goto no_such_field;
2417 if (G_VALUE_TYPE (val) != expected_type)
2418 goto wrong_type;
2420 GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2421 if (err) {
2422 g_warning ("%s: %s", G_STRFUNC, err);
2423 g_free (err);
2424 return FALSE;
2425 }
2427 field_name = va_arg (args, const gchar *);
2428 }
2430 return TRUE;
2432 /* ERRORS */
2433 no_such_field:
2434 {
2435 GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2436 field_name, structure);
2437 return FALSE;
2438 }
2439 wrong_type:
2440 {
2441 GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
2442 "field was of type '%s': %" GST_PTR_FORMAT, field_name,
2443 GST_STR_NULL (g_type_name (expected_type)),
2444 G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
2445 structure);
2446 return FALSE;
2447 }
2448 }
2450 /**
2451 * gst_structure_id_get_valist:
2452 * @structure: a #GstStructure
2453 * @first_field_id: the quark of the first field to read
2454 * @args: variable arguments
2455 *
2456 * Parses the variable arguments and reads fields from @structure accordingly.
2457 * valist-variant of gst_structure_id_get(). Look at the documentation of
2458 * gst_structure_id_get() for more details.
2459 *
2460 * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2461 *
2462 * Since: 0.10.24
2463 */
2464 gboolean
2465 gst_structure_id_get_valist (GstStructure * structure, GQuark first_field_id,
2466 va_list args)
2467 {
2468 GQuark field_id;
2469 GType expected_type = G_TYPE_INVALID;
2471 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2472 g_return_val_if_fail (first_field_id != 0, FALSE);
2474 field_id = first_field_id;
2475 while (field_id) {
2476 const GValue *val = NULL;
2477 gchar *err = NULL;
2479 expected_type = va_arg (args, GType);
2481 val = gst_structure_id_get_value (structure, field_id);
2483 if (val == NULL)
2484 goto no_such_field;
2486 if (G_VALUE_TYPE (val) != expected_type)
2487 goto wrong_type;
2489 GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
2490 if (err) {
2491 g_warning ("%s: %s", G_STRFUNC, err);
2492 g_free (err);
2493 return FALSE;
2494 }
2496 field_id = va_arg (args, GQuark);
2497 }
2499 return TRUE;
2501 /* ERRORS */
2502 no_such_field:
2503 {
2504 GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2505 GST_STR_NULL (g_quark_to_string (field_id)), structure);
2506 return FALSE;
2507 }
2508 wrong_type:
2509 {
2510 GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
2511 "field was of type '%s': %" GST_PTR_FORMAT,
2512 g_quark_to_string (field_id),
2513 GST_STR_NULL (g_type_name (expected_type)),
2514 G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
2515 structure);
2516 return FALSE;
2517 }
2518 }
2520 /**
2521 * gst_structure_get:
2522 * @structure: a #GstStructure
2523 * @first_fieldname: the name of the first field to read
2524 * @...: variable arguments
2525 *
2526 * Parses the variable arguments and reads fields from @structure accordingly.
2527 * Variable arguments should be in the form field name, field type
2528 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2529 * The last variable argument should be NULL.
2530 *
2531 * For refcounted (mini)objects you will acquire your own reference which
2532 * you must release with a suitable _unref() when no longer needed. For
2533 * strings and boxed types you will acquire a copy which you will need to
2534 * release with either g_free() or the suiteable function for the boxed type.
2535 *
2536 * Returns: FALSE if there was a problem reading any of the fields (e.g.
2537 * because the field requested did not exist, or was of a type other
2538 * than the type specified), otherwise TRUE.
2539 *
2540 * Since: 0.10.24
2541 */
2542 gboolean
2543 gst_structure_get (GstStructure * structure, const char *first_fieldname, ...)
2544 {
2545 gboolean ret;
2546 va_list args;
2548 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2549 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2551 va_start (args, first_fieldname);
2552 ret = gst_structure_get_valist (structure, first_fieldname, args);
2553 va_end (args);
2555 return ret;
2556 }
2558 /**
2559 * gst_structure_id_get:
2560 * @structure: a #GstStructure
2561 * @first_field_id: the quark of the first field to read
2562 * @...: variable arguments
2563 *
2564 * Parses the variable arguments and reads fields from @structure accordingly.
2565 * Variable arguments should be in the form field id quark, field type
2566 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2567 * The last variable argument should be NULL (technically it should be a
2568 * 0 quark, but we require NULL so compilers that support it can check for
2569 * the NULL terminator and warn if it's not there).
2570 *
2571 * This function is just like gst_structure_get() only that it is slightly
2572 * more efficient since it saves the string-to-quark lookup in the global
2573 * quark hashtable.
2574 *
2575 * For refcounted (mini)objects you will acquire your own reference which
2576 * you must release with a suitable _unref() when no longer needed. For
2577 * strings and boxed types you will acquire a copy which you will need to
2578 * release with either g_free() or the suiteable function for the boxed type.
2579 *
2580 * Returns: FALSE if there was a problem reading any of the fields (e.g.
2581 * because the field requested did not exist, or was of a type other
2582 * than the type specified), otherwise TRUE.
2583 *
2584 * Since: 0.10.24
2585 */
2586 gboolean
2587 gst_structure_id_get (GstStructure * structure, GQuark first_field_id, ...)
2588 {
2589 gboolean ret;
2590 va_list args;
2592 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2593 g_return_val_if_fail (first_field_id != 0, FALSE);
2595 va_start (args, first_field_id);
2596 ret = gst_structure_id_get_valist (structure, first_field_id, args);
2597 va_end (args);
2599 return ret;
2600 }