]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blob - gst/gststructure.c
docs: make gtk-doc happy
[glsdk/gstreamer0-10.git] / gst / gststructure.c
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)
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;
118 static GstStructure *
119 gst_structure_id_empty_new_with_size (GQuark quark, guint prealloc)
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;
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)
144   g_return_val_if_fail (quark != 0, NULL);
146   return gst_structure_id_empty_new_with_size (quark, 0);
149 #ifndef G_DISABLE_CHECKS
150 static gboolean
151 gst_structure_validate_name (const gchar * name)
153   const gchar *s;
155   g_return_val_if_fail (name != NULL, FALSE);
157   /* FIXME 0.11: use g_ascii_isalpha() */
158   if (!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 (*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;
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)
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);
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, ...)
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;
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)
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;
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, int *refcount)
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;
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)
293   GstStructure *new_structure;
294   GstStructureField *field;
295   guint i;
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   for (i = 0; i < structure->fields->len; i++) {
304     GstStructureField new_field = { 0 };
306     field = GST_STRUCTURE_FIELD (structure, i);
308     new_field.name = field->name;
309     gst_value_init_and_copy (&new_field.value, &field->value);
310     g_array_append_val (new_structure->fields, new_field);
311   }
313   return new_structure;
316 /**
317  * gst_structure_free:
318  * @structure: the #GstStructure to free
319  *
320  * Frees a #GstStructure and all its fields and values. The structure must not
321  * have a parent when this function is called.
322  */
323 void
324 gst_structure_free (GstStructure * structure)
326   GstStructureField *field;
327   guint i;
329   g_return_if_fail (structure != NULL);
330   g_return_if_fail (structure->parent_refcount == NULL);
332   for (i = 0; i < structure->fields->len; i++) {
333     field = GST_STRUCTURE_FIELD (structure, i);
335     if (G_IS_VALUE (&field->value)) {
336       g_value_unset (&field->value);
337     }
338   }
339   g_array_free (structure->fields, TRUE);
340 #ifdef USE_POISONING
341   memset (structure, 0xff, sizeof (GstStructure));
342 #endif
343   g_slice_free (GstStructure, structure);
346 /**
347  * gst_structure_get_name:
348  * @structure: a #GstStructure
349  *
350  * Get the name of @structure as a string.
351  *
352  * Returns: the name of the structure.
353  */
354 const gchar *
355 gst_structure_get_name (const GstStructure * structure)
357   g_return_val_if_fail (structure != NULL, NULL);
359   return g_quark_to_string (structure->name);
362 /**
363  * gst_structure_has_name:
364  * @structure: a #GstStructure
365  * @name: structure name to check for
366  *
367  * Checks if the structure has the given name
368  *
369  * Returns: TRUE if @name matches the name of the structure.
370  */
371 gboolean
372 gst_structure_has_name (const GstStructure * structure, const gchar * name)
374   const gchar *structure_name;
376   g_return_val_if_fail (structure != NULL, FALSE);
377   g_return_val_if_fail (name != NULL, FALSE);
379   /* getting the string is cheap and comparing short strings is too
380    * should be faster than getting the quark for name and comparing the quarks
381    */
382   structure_name = g_quark_to_string (structure->name);
384   return (structure_name && strcmp (structure_name, name) == 0);
387 /**
388  * gst_structure_get_name_id:
389  * @structure: a #GstStructure
390  *
391  * Get the name of @structure as a GQuark.
392  *
393  * Returns: the quark representing the name of the structure.
394  */
395 GQuark
396 gst_structure_get_name_id (const GstStructure * structure)
398   g_return_val_if_fail (structure != NULL, 0);
400   return structure->name;
403 /**
404  * gst_structure_set_name:
405  * @structure: a #GstStructure
406  * @name: the new name of the structure
407  *
408  * Sets the name of the structure to the given @name.  The string
409  * provided is copied before being used. It must not be empty, start with a
410  * letter and can be followed by letters, numbers and any of "/-_.:".
411  */
412 void
413 gst_structure_set_name (GstStructure * structure, const gchar * name)
415   g_return_if_fail (structure != NULL);
416   g_return_if_fail (IS_MUTABLE (structure));
417   g_return_if_fail (gst_structure_validate_name (name));
419   structure->name = g_quark_from_string (name);
422 /**
423  * gst_structure_id_set_value:
424  * @structure: a #GstStructure
425  * @field: a #GQuark representing a field
426  * @value: the new value of the field
427  *
428  * Sets the field with the given GQuark @field to @value.  If the field
429  * does not exist, it is created.  If the field exists, the previous
430  * value is replaced and freed.
431  */
432 void
433 gst_structure_id_set_value (GstStructure * structure,
434     GQuark field, const GValue * value)
436   GstStructureField gsfield = { 0, {0,} };
438   g_return_if_fail (structure != NULL);
439   g_return_if_fail (G_IS_VALUE (value));
440   g_return_if_fail (IS_MUTABLE (structure));
442   gsfield.name = field;
443   gst_value_init_and_copy (&gsfield.value, value);
445   gst_structure_set_field (structure, &gsfield);
448 /**
449  * gst_structure_set_value:
450  * @structure: a #GstStructure
451  * @fieldname: the name of the field to set
452  * @value: the new value of the field
453  *
454  * Sets the field with the given name @field to @value.  If the field
455  * does not exist, it is created.  If the field exists, the previous
456  * value is replaced and freed.
457  */
458 void
459 gst_structure_set_value (GstStructure * structure,
460     const gchar * fieldname, const GValue * value)
462   g_return_if_fail (structure != NULL);
463   g_return_if_fail (fieldname != NULL);
464   g_return_if_fail (G_IS_VALUE (value));
465   g_return_if_fail (IS_MUTABLE (structure));
467   gst_structure_id_set_value (structure, g_quark_from_string (fieldname),
468       value);
471 /**
472  * gst_structure_set:
473  * @structure: a #GstStructure
474  * @fieldname: the name of the field to set
475  * @...: variable arguments
476  *
477  * Parses the variable arguments and sets fields accordingly.
478  * Variable arguments should be in the form field name, field type
479  * (as a GType), value(s).  The last variable argument should be NULL.
480  */
481 void
482 gst_structure_set (GstStructure * structure, const gchar * field, ...)
484   va_list varargs;
486   g_return_if_fail (structure != NULL);
488   va_start (varargs, field);
490   gst_structure_set_valist (structure, field, varargs);
492   va_end (varargs);
495 /**
496  * gst_structure_set_valist:
497  * @structure: a #GstStructure
498  * @fieldname: the name of the field to set
499  * @varargs: variable arguments
500  *
501  * va_list form of gst_structure_set().
502  */
503 void
504 gst_structure_set_valist (GstStructure * structure,
505     const gchar * fieldname, va_list varargs)
507   gchar *err = NULL;
508   GType type;
510   g_return_if_fail (structure != NULL);
511   g_return_if_fail (IS_MUTABLE (structure));
513   while (fieldname) {
514     GstStructureField field = { 0 };
516     field.name = g_quark_from_string (fieldname);
518     type = va_arg (varargs, GType);
520     if (type == G_TYPE_DATE) {
521       g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
522       type = GST_TYPE_DATE;
523     }
525     g_value_init (&field.value, type);
526     G_VALUE_COLLECT (&field.value, varargs, 0, &err);
527     if (err) {
528       g_critical ("%s", err);
529       return;
530     }
531     gst_structure_set_field (structure, &field);
533     fieldname = va_arg (varargs, gchar *);
534   }
537 /**
538  * gst_structure_id_set:
539  * @structure: a #GstStructure
540  * @fieldname: the GQuark for the name of the field to set
541  * @...: variable arguments
542  *
543  * Identical to gst_structure_set, except that field names are
544  * passed using the GQuark for the field name. This allows more efficient
545  * setting of the structure if the caller already knows the associated
546  * quark values.
547  * The last variable argument must be NULL.
548  *
549  * Since: 0.10.10
550  */
551 void
552 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
554   va_list varargs;
556   g_return_if_fail (structure != NULL);
558   va_start (varargs, field);
559   gst_structure_id_set_valist (structure, field, varargs);
560   va_end (varargs);
563 /**
564  * gst_structure_id_set_valist:
565  * @structure: a #GstStructure
566  * @fieldname: the name of the field to set
567  * @varargs: variable arguments
568  *
569  * va_list form of gst_structure_id_set().
570  *
571  * Since: 0.10.10
572  */
573 void
574 gst_structure_id_set_valist (GstStructure * structure,
575     GQuark fieldname, va_list varargs)
577   gchar *err = NULL;
578   GType type;
580   g_return_if_fail (structure != NULL);
581   g_return_if_fail (IS_MUTABLE (structure));
583   while (fieldname) {
584     GstStructureField field = { 0 };
586     field.name = fieldname;
588     type = va_arg (varargs, GType);
590     if (type == G_TYPE_DATE) {
591       g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
592       type = GST_TYPE_DATE;
593     }
595     g_value_init (&field.value, type);
596     G_VALUE_COLLECT (&field.value, varargs, 0, &err);
597     if (err) {
598       g_critical ("%s", err);
599       return;
600     }
601     gst_structure_set_field (structure, &field);
603     fieldname = va_arg (varargs, GQuark);
604   }
607 /**
608  * gst_structure_id_new:
609  * @name_quark: name of new structure
610  * @field_quark: the GQuark for the name of the field to set
611  * @...: variable arguments
612  *
613  * Creates a new #GstStructure with the given name as a GQuark, followed by
614  * fieldname quark, GType, argument(s) "triplets" in the same format as
615  * gst_structure_id_set(). Basically a convenience wrapper around
616  * gst_structure_id_empty_new() and gst_structure_id_set().
617  *
618  * The last variable argument must be NULL (or 0).
619  *
620  * Returns: a new #GstStructure
621  *
622  * Since: 0.10.24
623  */
624 GstStructure *
625 gst_structure_id_new (GQuark name_quark, GQuark field_quark, ...)
627   GstStructure *s;
628   va_list varargs;
630   g_return_val_if_fail (name_quark != 0, NULL);
631   g_return_val_if_fail (field_quark != 0, NULL);
633   s = gst_structure_id_empty_new (name_quark);
635   va_start (varargs, field_quark);
636   gst_structure_id_set_valist (s, field_quark, varargs);
637   va_end (varargs);
639   return s;
642 /* If the structure currently contains a field with the same name, it is
643  * replaced with the provided field. Otherwise, the field is added to the
644  * structure. The field's value is not deeply copied.
645  */
646 static void
647 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
649   GstStructureField *f;
650   guint i;
652   if (G_UNLIKELY (G_VALUE_HOLDS_STRING (&field->value))) {
653     const gchar *s;
655     s = g_value_get_string (&field->value);
656     /* only check for NULL strings in taglists, as they are allowed in message
657      * structs, e.g. error message debug strings */
658     if (G_UNLIKELY (s == NULL && IS_TAGLIST (structure))) {
659       g_warning ("Trying to set NULL string on field '%s' on taglist. "
660           "Please file a bug.", g_quark_to_string (field->name));
661       g_value_unset (&field->value);
662       return;
663     } else if (G_UNLIKELY (s != NULL && *s == '\0')) {
664       /* empty strings never make sense */
665       g_warning ("Trying to set empty string on %s field '%s'. Please file a "
666           "bug.", IS_TAGLIST (structure) ? "taglist" : "structure",
667           g_quark_to_string (field->name));
668       g_value_unset (&field->value);
669       return;
670     } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
671       g_warning ("Trying to set string on %s field '%s', but string is not "
672           "valid UTF-8. Please file a bug.",
673           IS_TAGLIST (structure) ? "taglist" : "structure",
674           g_quark_to_string (field->name));
675       g_value_unset (&field->value);
676       return;
677     }
678   }
680   for (i = 0; i < structure->fields->len; i++) {
681     f = GST_STRUCTURE_FIELD (structure, i);
683     if (f->name == field->name) {
684       g_value_unset (&f->value);
685       memcpy (f, field, sizeof (GstStructureField));
686       return;
687     }
688   }
690   g_array_append_val (structure->fields, *field);
693 /* If there is no field with the given ID, NULL is returned.
694  */
695 static GstStructureField *
696 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
698   GstStructureField *field;
699   guint i;
701   g_return_val_if_fail (structure != NULL, NULL);
703   for (i = 0; i < structure->fields->len; i++) {
704     field = GST_STRUCTURE_FIELD (structure, i);
706     if (field->name == field_id)
707       return field;
708   }
710   return NULL;
713 /* If there is no field with the given ID, NULL is returned.
714  */
715 static GstStructureField *
716 gst_structure_get_field (const GstStructure * structure,
717     const gchar * fieldname)
719   g_return_val_if_fail (structure != NULL, NULL);
720   g_return_val_if_fail (fieldname != NULL, NULL);
722   return gst_structure_id_get_field (structure,
723       g_quark_from_string (fieldname));
726 /**
727  * gst_structure_get_value:
728  * @structure: a #GstStructure
729  * @fieldname: the name of the field to get
730  *
731  * Get the value of the field with name @fieldname.
732  *
733  * Returns: the #GValue corresponding to the field with the given name.
734  */
735 const GValue *
736 gst_structure_get_value (const GstStructure * structure,
737     const gchar * fieldname)
739   GstStructureField *field;
741   g_return_val_if_fail (structure != NULL, NULL);
742   g_return_val_if_fail (fieldname != NULL, NULL);
744   field = gst_structure_get_field (structure, fieldname);
745   if (field == NULL)
746     return NULL;
748   return &field->value;
751 /**
752  * gst_structure_id_get_value:
753  * @structure: a #GstStructure
754  * @field: the #GQuark of the field to get
755  *
756  * Get the value of the field with GQuark @field.
757  *
758  * Returns: the #GValue corresponding to the field with the given name
759  *          identifier.
760  */
761 const GValue *
762 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
764   GstStructureField *gsfield;
766   g_return_val_if_fail (structure != NULL, NULL);
768   gsfield = gst_structure_id_get_field (structure, field);
769   if (gsfield == NULL)
770     return NULL;
772   return &gsfield->value;
775 /**
776  * gst_structure_remove_field:
777  * @structure: a #GstStructure
778  * @fieldname: the name of the field to remove
779  *
780  * Removes the field with the given name.  If the field with the given
781  * name does not exist, the structure is unchanged.
782  */
783 void
784 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
786   GstStructureField *field;
787   GQuark id;
788   guint i;
790   g_return_if_fail (structure != NULL);
791   g_return_if_fail (fieldname != NULL);
792   g_return_if_fail (IS_MUTABLE (structure));
794   id = g_quark_from_string (fieldname);
796   for (i = 0; i < structure->fields->len; i++) {
797     field = GST_STRUCTURE_FIELD (structure, i);
799     if (field->name == id) {
800       if (G_IS_VALUE (&field->value)) {
801         g_value_unset (&field->value);
802       }
803       structure->fields = g_array_remove_index (structure->fields, i);
804       return;
805     }
806   }
809 /**
810  * gst_structure_remove_fields:
811  * @structure: a #GstStructure
812  * @fieldname: the name of the field to remove
813  * @...: NULL-terminated list of more fieldnames to remove
814  *
815  * Removes the fields with the given names. If a field does not exist, the
816  * argument is ignored.
817  */
818 void
819 gst_structure_remove_fields (GstStructure * structure,
820     const gchar * fieldname, ...)
822   va_list varargs;
824   g_return_if_fail (structure != NULL);
825   g_return_if_fail (fieldname != NULL);
826   /* mutability checked in remove_field */
828   va_start (varargs, fieldname);
830   gst_structure_remove_fields_valist (structure, fieldname, varargs);
832   va_end (varargs);
835 /**
836  * gst_structure_remove_fields_valist:
837  * @structure: a #GstStructure
838  * @fieldname: the name of the field to remove
839  * @varargs: NULL-terminated list of more fieldnames to remove
840  *
841  * va_list form of gst_structure_remove_fields().
842  */
843 void
844 gst_structure_remove_fields_valist (GstStructure * structure,
845     const gchar * fieldname, va_list varargs)
847   gchar *field = (gchar *) fieldname;
849   g_return_if_fail (structure != NULL);
850   g_return_if_fail (fieldname != NULL);
851   /* mutability checked in remove_field */
853   while (field) {
854     gst_structure_remove_field (structure, field);
855     field = va_arg (varargs, char *);
856   }
859 /**
860  * gst_structure_remove_all_fields:
861  * @structure: a #GstStructure
862  *
863  * Removes all fields in a GstStructure.
864  */
865 void
866 gst_structure_remove_all_fields (GstStructure * structure)
868   GstStructureField *field;
869   int i;
871   g_return_if_fail (structure != NULL);
872   g_return_if_fail (IS_MUTABLE (structure));
874   for (i = structure->fields->len - 1; i >= 0; i--) {
875     field = GST_STRUCTURE_FIELD (structure, i);
877     if (G_IS_VALUE (&field->value)) {
878       g_value_unset (&field->value);
879     }
880     structure->fields = g_array_remove_index (structure->fields, i);
881   }
884 /**
885  * gst_structure_get_field_type:
886  * @structure: a #GstStructure
887  * @fieldname: the name of the field
888  *
889  * Finds the field with the given name, and returns the type of the
890  * value it contains.  If the field is not found, G_TYPE_INVALID is
891  * returned.
892  *
893  * Returns: the #GValue of the field
894  */
895 GType
896 gst_structure_get_field_type (const GstStructure * structure,
897     const gchar * fieldname)
899   GstStructureField *field;
901   g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
902   g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
904   field = gst_structure_get_field (structure, fieldname);
905   if (field == NULL)
906     return G_TYPE_INVALID;
908   return G_VALUE_TYPE (&field->value);
911 /**
912  * gst_structure_n_fields:
913  * @structure: a #GstStructure
914  *
915  * Get the number of fields in the structure.
916  *
917  * Returns: the number of fields in the structure
918  */
919 gint
920 gst_structure_n_fields (const GstStructure * structure)
922   g_return_val_if_fail (structure != NULL, 0);
924   return structure->fields->len;
927 /**
928  * gst_structure_nth_field_name:
929  * @structure: a #GstStructure
930  * @index: the index to get the name of
931  *
932  * Get the name of the given field number, counting from 0 onwards.
933  *
934  * Returns: the name of the given field number
935  */
936 const gchar *
937 gst_structure_nth_field_name (const GstStructure * structure, guint index)
939   GstStructureField *field;
941   g_return_val_if_fail (structure != NULL, NULL);
942   g_return_val_if_fail (index < structure->fields->len, NULL);
944   field = GST_STRUCTURE_FIELD (structure, index);
946   return g_quark_to_string (field->name);
949 /**
950  * gst_structure_foreach:
951  * @structure: a #GstStructure
952  * @func: a function to call for each field
953  * @user_data: private data
954  *
955  * Calls the provided function once for each field in the #GstStructure. The
956  * function must not modify the fields. Also see gst_structure_map_in_place().
957  *
958  * Returns: TRUE if the supplied function returns TRUE For each of the fields,
959  * FALSE otherwise.
960  */
961 gboolean
962 gst_structure_foreach (const GstStructure * structure,
963     GstStructureForeachFunc func, gpointer user_data)
965   guint i;
966   GstStructureField *field;
967   gboolean ret;
969   g_return_val_if_fail (structure != NULL, FALSE);
970   g_return_val_if_fail (func != NULL, FALSE);
972   for (i = 0; i < structure->fields->len; i++) {
973     field = GST_STRUCTURE_FIELD (structure, i);
975     ret = func (field->name, &field->value, user_data);
976     if (!ret)
977       return FALSE;
978   }
980   return TRUE;
983 /**
984  * gst_structure_map_in_place:
985  * @structure: a #GstStructure
986  * @func: a function to call for each field
987  * @user_data: private data
988  *
989  * Calls the provided function once for each field in the #GstStructure. In
990  * contrast to gst_structure_foreach(), the function may modify but not delete the
991  * fields. The structure must be mutable.
992  *
993  * Returns: TRUE if the supplied function returns TRUE For each of the fields,
994  * FALSE otherwise.
995  */
996 gboolean
997 gst_structure_map_in_place (GstStructure * structure,
998     GstStructureMapFunc func, gpointer user_data)
1000   guint i;
1001   GstStructureField *field;
1002   gboolean ret;
1004   g_return_val_if_fail (structure != NULL, FALSE);
1005   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1006   g_return_val_if_fail (func != NULL, FALSE);
1008   for (i = 0; i < structure->fields->len; i++) {
1009     field = GST_STRUCTURE_FIELD (structure, i);
1011     ret = func (field->name, &field->value, user_data);
1012     if (!ret)
1013       return FALSE;
1014   }
1016   return TRUE;
1019 /**
1020  * gst_structure_has_field:
1021  * @structure: a #GstStructure
1022  * @fieldname: the name of a field
1023  *
1024  * Check if @structure contains a field named @fieldname.
1025  *
1026  * Returns: TRUE if the structure contains a field with the given name
1027  */
1028 gboolean
1029 gst_structure_has_field (const GstStructure * structure,
1030     const gchar * fieldname)
1032   GstStructureField *field;
1034   g_return_val_if_fail (structure != NULL, 0);
1035   g_return_val_if_fail (fieldname != NULL, 0);
1037   field = gst_structure_get_field (structure, fieldname);
1039   return (field != NULL);
1042 /**
1043  * gst_structure_has_field_typed:
1044  * @structure: a #GstStructure
1045  * @fieldname: the name of a field
1046  * @type: the type of a value
1047  *
1048  * Check if @structure contains a field named @fieldname and with GType @type.
1049  *
1050  * Returns: TRUE if the structure contains a field with the given name and type
1051  */
1052 gboolean
1053 gst_structure_has_field_typed (const GstStructure * structure,
1054     const gchar * fieldname, GType type)
1056   GstStructureField *field;
1058   g_return_val_if_fail (structure != NULL, 0);
1059   g_return_val_if_fail (fieldname != NULL, 0);
1061   field = gst_structure_get_field (structure, fieldname);
1062   if (field == NULL)
1063     return FALSE;
1065   return (G_VALUE_TYPE (&field->value) == type);
1069 /* utility functions */
1071 /**
1072  * gst_structure_get_boolean:
1073  * @structure: a #GstStructure
1074  * @fieldname: the name of a field
1075  * @value: a pointer to a #gboolean to set
1076  *
1077  * Sets the boolean pointed to by @value corresponding to the value of the
1078  * given field.  Caller is responsible for making sure the field exists
1079  * and has the correct type.
1080  *
1081  * Returns: TRUE if the value could be set correctly. If there was no field
1082  * with @fieldname or the existing field did not contain a boolean, this
1083  * function returns FALSE.
1084  */
1085 gboolean
1086 gst_structure_get_boolean (const GstStructure * structure,
1087     const gchar * fieldname, gboolean * value)
1089   GstStructureField *field;
1091   g_return_val_if_fail (structure != NULL, FALSE);
1092   g_return_val_if_fail (fieldname != NULL, FALSE);
1094   field = gst_structure_get_field (structure, fieldname);
1096   if (field == NULL)
1097     return FALSE;
1098   if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
1099     return FALSE;
1101   *value = g_value_get_boolean (&field->value);
1103   return TRUE;
1106 /**
1107  * gst_structure_get_int:
1108  * @structure: a #GstStructure
1109  * @fieldname: the name of a field
1110  * @value: a pointer to an int to set
1111  *
1112  * Sets the int pointed to by @value corresponding to the value of the
1113  * given field.  Caller is responsible for making sure the field exists
1114  * and has the correct type.
1115  *
1116  * Returns: %TRUE if the value could be set correctly. If there was no field
1117  * with @fieldname or the existing field did not contain an int, this function
1118  * returns %FALSE.
1119  */
1120 gboolean
1121 gst_structure_get_int (const GstStructure * structure,
1122     const gchar * fieldname, gint * value)
1124   GstStructureField *field;
1126   g_return_val_if_fail (structure != NULL, FALSE);
1127   g_return_val_if_fail (fieldname != NULL, FALSE);
1128   g_return_val_if_fail (value != NULL, FALSE);
1130   field = gst_structure_get_field (structure, fieldname);
1132   if (field == NULL)
1133     return FALSE;
1134   if (!G_VALUE_HOLDS_INT (&field->value))
1135     return FALSE;
1137   *value = g_value_get_int (&field->value);
1139   return TRUE;
1142 /**
1143  * gst_structure_get_uint:
1144  * @structure: a #GstStructure
1145  * @fieldname: the name of a field
1146  * @value: a pointer to a uint to set
1147  *
1148  * Sets the uint pointed to by @value corresponding to the value of the
1149  * given field.  Caller is responsible for making sure the field exists
1150  * and has the correct type.
1151  *
1152  * Returns: %TRUE if the value could be set correctly. If there was no field
1153  * with @fieldname or the existing field did not contain a uint, this function
1154  * returns %FALSE.
1155  *
1156  * Since: 0.10.15
1157  */
1158 gboolean
1159 gst_structure_get_uint (const GstStructure * structure,
1160     const gchar * fieldname, guint * value)
1162   GstStructureField *field;
1164   g_return_val_if_fail (structure != NULL, FALSE);
1165   g_return_val_if_fail (fieldname != NULL, FALSE);
1166   g_return_val_if_fail (value != NULL, FALSE);
1168   field = gst_structure_get_field (structure, fieldname);
1170   if (field == NULL)
1171     return FALSE;
1172   if (!G_VALUE_HOLDS_UINT (&field->value))
1173     return FALSE;
1175   *value = g_value_get_uint (&field->value);
1177   return TRUE;
1180 /**
1181  * gst_structure_get_fourcc:
1182  * @structure: a #GstStructure
1183  * @fieldname: the name of a field
1184  * @value: a pointer to a #GstFourcc to set
1185  *
1186  * Sets the #GstFourcc pointed to by @value corresponding to the value of the
1187  * given field.  Caller is responsible for making sure the field exists
1188  * and has the correct type.
1189  *
1190  * Returns: TRUE if the value could be set correctly. If there was no field
1191  * with @fieldname or the existing field did not contain a fourcc, this function
1192  * returns FALSE.
1193  */
1194 gboolean
1195 gst_structure_get_fourcc (const GstStructure * structure,
1196     const gchar * fieldname, guint32 * value)
1198   GstStructureField *field;
1200   g_return_val_if_fail (structure != NULL, FALSE);
1201   g_return_val_if_fail (fieldname != NULL, FALSE);
1202   g_return_val_if_fail (value != NULL, FALSE);
1204   field = gst_structure_get_field (structure, fieldname);
1206   if (field == NULL)
1207     return FALSE;
1208   if (!GST_VALUE_HOLDS_FOURCC (&field->value))
1209     return FALSE;
1211   *value = gst_value_get_fourcc (&field->value);
1213   return TRUE;
1216 /**
1217  * gst_structure_get_date:
1218  * @structure: a #GstStructure
1219  * @fieldname: the name of a field
1220  * @value: a pointer to a #GDate to set
1221  *
1222  * Sets the date pointed to by @value corresponding to the date of the
1223  * given field.  Caller is responsible for making sure the field exists
1224  * and has the correct type.
1225  *
1226  * On success @value will point to a newly-allocated copy of the date which
1227  * should be freed with g_date_free() when no longer needed (note: this is
1228  * inconsistent with e.g. gst_structure_get_string() which doesn't return a
1229  * copy of the string).
1230  *
1231  * Returns: TRUE if the value could be set correctly. If there was no field
1232  * with @fieldname or the existing field did not contain a data, this function
1233  * returns FALSE.
1234  */
1235 gboolean
1236 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1237     GDate ** value)
1239   GstStructureField *field;
1241   g_return_val_if_fail (structure != NULL, FALSE);
1242   g_return_val_if_fail (fieldname != NULL, FALSE);
1243   g_return_val_if_fail (value != NULL, FALSE);
1245   field = gst_structure_get_field (structure, fieldname);
1247   if (field == NULL)
1248     return FALSE;
1249   if (!GST_VALUE_HOLDS_DATE (&field->value))
1250     return FALSE;
1252   /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1253   *value = g_value_dup_boxed (&field->value);
1255   return TRUE;
1258 /**
1259  * gst_structure_get_clock_time:
1260  * @structure: a #GstStructure
1261  * @fieldname: the name of a field
1262  * @value: a pointer to a #GstClockTime to set
1263  *
1264  * Sets the clock time pointed to by @value corresponding to the clock time
1265  * of the given field.  Caller is responsible for making sure the field exists
1266  * and has the correct type.
1267  *
1268  * Returns: TRUE if the value could be set correctly. If there was no field
1269  * with @fieldname or the existing field did not contain a #GstClockTime, this 
1270  * function returns FALSE.
1271  */
1272 gboolean
1273 gst_structure_get_clock_time (const GstStructure * structure,
1274     const gchar * fieldname, GstClockTime * value)
1276   GstStructureField *field;
1278   g_return_val_if_fail (structure != NULL, FALSE);
1279   g_return_val_if_fail (fieldname != NULL, FALSE);
1280   g_return_val_if_fail (value != NULL, FALSE);
1282   field = gst_structure_get_field (structure, fieldname);
1284   if (field == NULL)
1285     return FALSE;
1286   if (!G_VALUE_HOLDS_UINT64 (&field->value))
1287     return FALSE;
1289   *value = g_value_get_uint64 (&field->value);
1291   return TRUE;
1294 /**
1295  * gst_structure_get_double:
1296  * @structure: a #GstStructure
1297  * @fieldname: the name of a field
1298  * @value: a pointer to a #GstFourcc to set
1299  *
1300  * Sets the double pointed to by @value corresponding to the value of the
1301  * given field.  Caller is responsible for making sure the field exists
1302  * and has the correct type.
1303  *
1304  * Returns: TRUE if the value could be set correctly. If there was no field
1305  * with @fieldname or the existing field did not contain a double, this 
1306  * function returns FALSE.
1307  */
1308 gboolean
1309 gst_structure_get_double (const GstStructure * structure,
1310     const gchar * fieldname, gdouble * value)
1312   GstStructureField *field;
1314   g_return_val_if_fail (structure != NULL, FALSE);
1315   g_return_val_if_fail (fieldname != NULL, FALSE);
1316   g_return_val_if_fail (value != NULL, FALSE);
1318   field = gst_structure_get_field (structure, fieldname);
1320   if (field == NULL)
1321     return FALSE;
1322   if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1323     return FALSE;
1325   *value = g_value_get_double (&field->value);
1327   return TRUE;
1330 /**
1331  * gst_structure_get_string:
1332  * @structure: a #GstStructure
1333  * @fieldname: the name of a field
1334  *
1335  * Finds the field corresponding to @fieldname, and returns the string
1336  * contained in the field's value.  Caller is responsible for making
1337  * sure the field exists and has the correct type.
1338  *
1339  * The string should not be modified, and remains valid until the next
1340  * call to a gst_structure_*() function with the given structure.
1341  *
1342  * Returns: a pointer to the string or NULL when the field did not exist
1343  * or did not contain a string.
1344  */
1345 const gchar *
1346 gst_structure_get_string (const GstStructure * structure,
1347     const gchar * fieldname)
1349   GstStructureField *field;
1351   g_return_val_if_fail (structure != NULL, NULL);
1352   g_return_val_if_fail (fieldname != NULL, NULL);
1354   field = gst_structure_get_field (structure, fieldname);
1356   if (field == NULL)
1357     return NULL;
1358   if (!G_VALUE_HOLDS_STRING (&field->value))
1359     return NULL;
1361   return g_value_get_string (&field->value);
1364 /**
1365  * gst_structure_get_enum:
1366  * @structure: a #GstStructure
1367  * @fieldname: the name of a field
1368  * @enumtype: the enum type of a field
1369  * @value: a pointer to an int to set
1370  *
1371  * Sets the int pointed to by @value corresponding to the value of the
1372  * given field.  Caller is responsible for making sure the field exists,
1373  * has the correct type and that the enumtype is correct.
1374  *
1375  * Returns: TRUE if the value could be set correctly. If there was no field
1376  * with @fieldname or the existing field did not contain an enum of the given
1377  * type, this function returns FALSE.
1378  */
1379 gboolean
1380 gst_structure_get_enum (const GstStructure * structure,
1381     const gchar * fieldname, GType enumtype, gint * value)
1383   GstStructureField *field;
1385   g_return_val_if_fail (structure != NULL, FALSE);
1386   g_return_val_if_fail (fieldname != NULL, FALSE);
1387   g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1388   g_return_val_if_fail (value != NULL, FALSE);
1390   field = gst_structure_get_field (structure, fieldname);
1392   if (field == NULL)
1393     return FALSE;
1394   if (!G_VALUE_HOLDS_ENUM (&field->value))
1395     return FALSE;
1396   if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1397     return FALSE;
1399   *value = g_value_get_enum (&field->value);
1401   return TRUE;
1404 /**
1405  * gst_structure_get_fraction:
1406  * @structure: a #GstStructure
1407  * @fieldname: the name of a field
1408  * @value_numerator: a pointer to an int to set
1409  * @value_denominator: a pointer to an int to set
1410  *
1411  * Sets the integers pointed to by @value_numerator and @value_denominator 
1412  * corresponding to the value of the given field.  Caller is responsible 
1413  * for making sure the field exists and has the correct type.
1414  *
1415  * Returns: TRUE if the values could be set correctly. If there was no field
1416  * with @fieldname or the existing field did not contain a GstFraction, this 
1417  * function returns FALSE.
1418  */
1419 gboolean
1420 gst_structure_get_fraction (const GstStructure * structure,
1421     const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1423   GstStructureField *field;
1425   g_return_val_if_fail (structure != NULL, FALSE);
1426   g_return_val_if_fail (fieldname != NULL, FALSE);
1427   g_return_val_if_fail (value_numerator != NULL, FALSE);
1428   g_return_val_if_fail (value_denominator != NULL, FALSE);
1430   field = gst_structure_get_field (structure, fieldname);
1432   if (field == NULL)
1433     return FALSE;
1434   if (!GST_VALUE_HOLDS_FRACTION (&field->value))
1435     return FALSE;
1437   *value_numerator = gst_value_get_fraction_numerator (&field->value);
1438   *value_denominator = gst_value_get_fraction_denominator (&field->value);
1440   return TRUE;
1443 typedef struct _GstStructureAbbreviation
1445   gchar *type_name;
1446   GType type;
1448 GstStructureAbbreviation;
1450 /* return a copy of an array of GstStructureAbbreviation containing all the
1451  * known type_string, GType maps, including abbreviations for common types */
1452 static GstStructureAbbreviation *
1453 gst_structure_get_abbrs (gint * n_abbrs)
1455   static GstStructureAbbreviation *abbrs = NULL;
1456   static gint num = 0;
1458   if (abbrs == NULL) {
1459     /* dynamically generate the array */
1460     GstStructureAbbreviation dyn_abbrs[] = {
1461       {"int", G_TYPE_INT}
1462       ,
1463       {"i", G_TYPE_INT}
1464       ,
1465       {"float", G_TYPE_FLOAT}
1466       ,
1467       {"f", G_TYPE_FLOAT}
1468       ,
1469       {"double", G_TYPE_DOUBLE}
1470       ,
1471       {"d", G_TYPE_DOUBLE}
1472       ,
1473       {"buffer", GST_TYPE_BUFFER}
1474       ,
1475       {"fourcc", GST_TYPE_FOURCC}
1476       ,
1477       {"4", GST_TYPE_FOURCC}
1478       ,
1479       {"fraction", GST_TYPE_FRACTION}
1480       ,
1481       {"boolean", G_TYPE_BOOLEAN}
1482       ,
1483       {"bool", G_TYPE_BOOLEAN}
1484       ,
1485       {"b", G_TYPE_BOOLEAN}
1486       ,
1487       {"string", G_TYPE_STRING}
1488       ,
1489       {"str", G_TYPE_STRING}
1490       ,
1491       {"s", G_TYPE_STRING}
1492       ,
1493       {"structure", GST_TYPE_STRUCTURE}
1494     };
1495     num = G_N_ELEMENTS (dyn_abbrs);
1496     /* permanently allocate and copy the array now */
1497     abbrs = g_new0 (GstStructureAbbreviation, num);
1498     memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * num);
1499   }
1500   *n_abbrs = num;
1502   return abbrs;
1505 /* given a type_name that could be a type abbreviation or a registered GType,
1506  * return a matching GType */
1507 static GType
1508 gst_structure_gtype_from_abbr (const char *type_name)
1510   int i;
1511   GstStructureAbbreviation *abbrs;
1512   gint n_abbrs;
1514   g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1516   abbrs = gst_structure_get_abbrs (&n_abbrs);
1518   for (i = 0; i < n_abbrs; i++) {
1519     if (strcmp (type_name, abbrs[i].type_name) == 0) {
1520       return abbrs[i].type;
1521     }
1522   }
1524   /* this is the fallback */
1525   return g_type_from_name (type_name);
1528 static const char *
1529 gst_structure_to_abbr (GType type)
1531   int i;
1532   GstStructureAbbreviation *abbrs;
1533   gint n_abbrs;
1535   g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1537   abbrs = gst_structure_get_abbrs (&n_abbrs);
1539   for (i = 0; i < n_abbrs; i++) {
1540     if (type == abbrs[i].type) {
1541       return abbrs[i].type_name;
1542     }
1543   }
1545   return g_type_name (type);
1548 static GType
1549 gst_structure_value_get_generic_type (GValue * val)
1551   if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1552       || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1553     GArray *array = g_value_peek_pointer (val);
1555     if (array->len > 0) {
1556       GValue *value = &g_array_index (array, GValue, 0);
1558       return gst_structure_value_get_generic_type (value);
1559     } else {
1560       return G_TYPE_INT;
1561     }
1562   } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1563     return G_TYPE_INT;
1564   } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1565     return G_TYPE_DOUBLE;
1566   } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1567     return GST_TYPE_FRACTION;
1568   }
1569   return G_VALUE_TYPE (val);
1572 gboolean
1573 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1574     GString * s)
1576   GstStructureField *field;
1577   guint i;
1579   g_return_val_if_fail (s != NULL, FALSE);
1581   g_string_append (s, g_quark_to_string (structure->name));
1582   for (i = 0; i < structure->fields->len; i++) {
1583     char *t;
1584     GType type;
1586     field = GST_STRUCTURE_FIELD (structure, i);
1588     t = gst_value_serialize (&field->value);
1589     type = gst_structure_value_get_generic_type (&field->value);
1591     g_string_append_len (s, ", ", 2);
1592     /* FIXME: do we need to escape fieldnames? */
1593     g_string_append (s, g_quark_to_string (field->name));
1594     g_string_append_len (s, "=(", 2);
1595     g_string_append (s, gst_structure_to_abbr (type));
1596     g_string_append_c (s, ')');
1597     g_string_append (s, GST_STR_NULL (t));
1598     g_free (t);
1599   }
1601   g_string_append_c (s, ';');
1602   return TRUE;
1605 /**
1606  * gst_structure_to_string:
1607  * @structure: a #GstStructure
1608  *
1609  * Converts @structure to a human-readable string representation.
1610  *
1611  * For debugging purposes its easier to do something like this:
1612  * |[
1613  * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1614  * ]|
1615  * This prints the structure in human readble form.
1616  *
1617  * Returns: a pointer to string allocated by g_malloc(). g_free() after
1618  * usage.
1619  */
1620 gchar *
1621 gst_structure_to_string (const GstStructure * structure)
1623   GString *s;
1625   /* NOTE:  This function is potentially called by the debug system,
1626    * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1627    * should be careful to avoid recursion.  This includes any functions
1628    * called by gst_structure_to_string.  In particular, calls should
1629    * not use the GST_PTR_FORMAT extension.  */
1631   g_return_val_if_fail (structure != NULL, NULL);
1633   /* we estimate a minimum size based on the number of fields in order to
1634    * avoid unnecessary reallocs within GString */
1635   s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1636   priv_gst_structure_append_to_gstring (structure, s);
1637   return g_string_free (s, FALSE);
1640 /*
1641  * r will still point to the string. if end == next, the string will not be
1642  * null-terminated. In all other cases it will be.
1643  * end = pointer to char behind end of string, next = pointer to start of
1644  * unread data.
1645  * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1646  */
1647 static gboolean
1648 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next)
1650   gchar *w;
1652   if (*s == 0)
1653     return FALSE;
1655   if (*s != '"') {
1656     int ret;
1658     ret = gst_structure_parse_simple_string (s, end);
1659     *next = *end;
1661     return ret;
1662   }
1664   w = s;
1665   s++;
1666   while (*s != '"') {
1667     if (*s == 0)
1668       return FALSE;
1670     if (*s == '\\') {
1671       s++;
1672     }
1674     *w = *s;
1675     w++;
1676     s++;
1677   }
1678   s++;
1680   *end = w;
1681   *next = s;
1683   return TRUE;
1686 static gboolean
1687 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1688     GType type)
1690   GValue value1 = { 0 };
1691   GValue value2 = { 0 };
1692   GType range_type;
1693   gboolean ret;
1695   if (*s != '[')
1696     return FALSE;
1697   s++;
1699   ret = gst_structure_parse_value (s, &s, &value1, type);
1700   if (ret == FALSE)
1701     return FALSE;
1703   while (g_ascii_isspace (*s))
1704     s++;
1706   if (*s != ',')
1707     return FALSE;
1708   s++;
1710   while (g_ascii_isspace (*s))
1711     s++;
1713   ret = gst_structure_parse_value (s, &s, &value2, type);
1714   if (ret == FALSE)
1715     return FALSE;
1717   while (g_ascii_isspace (*s))
1718     s++;
1720   if (*s != ']')
1721     return FALSE;
1722   s++;
1724   if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1725     return FALSE;
1727   if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1728     range_type = GST_TYPE_DOUBLE_RANGE;
1729     g_value_init (value, range_type);
1730     gst_value_set_double_range (value, g_value_get_double (&value1),
1731         g_value_get_double (&value2));
1732   } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1733     range_type = GST_TYPE_INT_RANGE;
1734     g_value_init (value, range_type);
1735     gst_value_set_int_range (value, g_value_get_int (&value1),
1736         g_value_get_int (&value2));
1737   } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
1738     range_type = GST_TYPE_FRACTION_RANGE;
1739     g_value_init (value, range_type);
1740     gst_value_set_fraction_range (value, &value1, &value2);
1741   } else {
1742     return FALSE;
1743   }
1745   *after = s;
1746   return TRUE;
1749 static gboolean
1750 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
1751     GType type, GType list_type, char begin, char end)
1753   GValue list_value = { 0 };
1754   gboolean ret;
1755   GArray *array;
1757   g_value_init (value, list_type);
1758   array = g_value_peek_pointer (value);
1760   if (*s != begin)
1761     return FALSE;
1762   s++;
1764   while (g_ascii_isspace (*s))
1765     s++;
1766   if (*s == end) {
1767     s++;
1768     *after = s;
1769     return TRUE;
1770   }
1772   ret = gst_structure_parse_value (s, &s, &list_value, type);
1773   if (ret == FALSE)
1774     return FALSE;
1776   g_array_append_val (array, list_value);
1778   while (g_ascii_isspace (*s))
1779     s++;
1781   while (*s != end) {
1782     if (*s != ',')
1783       return FALSE;
1784     s++;
1786     while (g_ascii_isspace (*s))
1787       s++;
1789     memset (&list_value, 0, sizeof (list_value));
1790     ret = gst_structure_parse_value (s, &s, &list_value, type);
1791     if (ret == FALSE)
1792       return FALSE;
1794     g_array_append_val (array, list_value);
1795     while (g_ascii_isspace (*s))
1796       s++;
1797   }
1799   s++;
1801   *after = s;
1802   return TRUE;
1805 static gboolean
1806 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
1808   return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
1809       '{', '}');
1812 static gboolean
1813 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
1814     GType type)
1816   return gst_structure_parse_any_list (s, after, value, type,
1817       GST_TYPE_ARRAY, '<', '>');
1820 static gboolean
1821 gst_structure_parse_simple_string (gchar * str, gchar ** end)
1823   char *s = str;
1825   while (GST_ASCII_IS_STRING (*s)) {
1826     s++;
1827   }
1829   *end = s;
1831   return (s != str);
1834 static gboolean
1835 gst_structure_parse_field (gchar * str,
1836     gchar ** after, GstStructureField * field)
1838   gchar *name;
1839   gchar *name_end;
1840   gchar *s;
1841   gchar c;
1843   s = str;
1845   while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1846     s++;
1847   name = s;
1848   if (!gst_structure_parse_simple_string (s, &name_end))
1849     return FALSE;
1851   s = name_end;
1852   while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1853     s++;
1855   if (*s != '=')
1856     return FALSE;
1857   s++;
1859   c = *name_end;
1860   *name_end = '\0';
1861   field->name = g_quark_from_string (name);
1862   *name_end = c;
1864   if (!gst_structure_parse_value (s, &s, &field->value, G_TYPE_INVALID))
1865     return FALSE;
1867   *after = s;
1868   return TRUE;
1871 static gboolean
1872 gst_structure_parse_value (gchar * str,
1873     gchar ** after, GValue * value, GType default_type)
1875   gchar *type_name;
1876   gchar *type_end;
1877   gchar *value_s;
1878   gchar *value_end;
1879   gchar *s;
1880   gchar c;
1881   int ret = 0;
1882   GType type = default_type;
1884   s = str;
1885   while (g_ascii_isspace (*s))
1886     s++;
1888   /* check if there's a (type_name) 'cast' */
1889   type_name = NULL;
1890   if (*s == '(') {
1891     s++;
1892     while (g_ascii_isspace (*s))
1893       s++;
1894     type_name = s;
1895     if (!gst_structure_parse_simple_string (s, &type_end))
1896       return FALSE;
1897     s = type_end;
1898     while (g_ascii_isspace (*s))
1899       s++;
1900     if (*s != ')')
1901       return FALSE;
1902     s++;
1903     while (g_ascii_isspace (*s))
1904       s++;
1906     c = *type_end;
1907     *type_end = 0;
1908     type = gst_structure_gtype_from_abbr (type_name);
1909     *type_end = c;
1911     if (G_UNLIKELY (type == G_TYPE_INVALID))
1912       return FALSE;
1913   }
1915   while (g_ascii_isspace (*s))
1916     s++;
1917   if (*s == '[') {
1918     ret = gst_structure_parse_range (s, &s, value, type);
1919   } else if (*s == '{') {
1920     ret = gst_structure_parse_list (s, &s, value, type);
1921   } else if (*s == '<') {
1922     ret = gst_structure_parse_array (s, &s, value, type);
1923   } else {
1924     value_s = s;
1925     if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s)))
1926       return FALSE;
1928     c = *value_end;
1929     *value_end = '\0';
1930     if (G_UNLIKELY (type == G_TYPE_INVALID)) {
1931       GType try_types[] =
1932           { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_BOOLEAN,
1933         G_TYPE_STRING
1934       };
1935       int i;
1937       for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
1938         g_value_init (value, try_types[i]);
1939         ret = gst_value_deserialize (value, value_s);
1940         if (ret)
1941           break;
1942         g_value_unset (value);
1943       }
1944     } else {
1945       g_value_init (value, type);
1947       ret = gst_value_deserialize (value, value_s);
1948       if (G_UNLIKELY (!ret))
1949         g_value_unset (value);
1950     }
1951     *value_end = c;
1952   }
1954   *after = s;
1956   return ret;
1959 /**
1960  * gst_structure_from_string:
1961  * @string: a string representation of a #GstStructure.
1962  * @end: pointer to store the end of the string in.
1963  *
1964  * Creates a #GstStructure from a string representation.
1965  * If end is not NULL, a pointer to the place inside the given string
1966  * where parsing ended will be returned.
1967  *
1968  * Returns: a new #GstStructure or NULL when the string could not
1969  * be parsed. Free with gst_structure_free() after use.
1970  */
1971 GstStructure *
1972 gst_structure_from_string (const gchar * string, gchar ** end)
1974   char *name;
1975   char *copy;
1976   char *w;
1977   char *r;
1978   char save;
1979   GstStructure *structure = NULL;
1980   GstStructureField field;
1982   g_return_val_if_fail (string != NULL, NULL);
1984   copy = g_strdup (string);
1985   r = copy;
1987   /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
1988   while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1989               && g_ascii_isspace (r[1]))))
1990     r++;
1992   name = r;
1993   if (!gst_structure_parse_string (r, &w, &r)) {
1994     GST_WARNING ("Failed to parse structure string");
1995     goto error;
1996   }
1998   save = *w;
1999   *w = '\0';
2000   structure = gst_structure_empty_new (name);
2001   *w = save;
2003   if (G_UNLIKELY (structure == NULL))
2004     goto error;
2006   do {
2007     while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2008                 && g_ascii_isspace (r[1]))))
2009       r++;
2010     if (*r == ';') {
2011       /* end of structure, get the next char and finish */
2012       r++;
2013       break;
2014     }
2015     if (*r == '\0') {
2016       /* accept \0 as end delimiter */
2017       break;
2018     }
2019     if (*r != ',') {
2020       GST_WARNING ("Failed to find delimiter, r=%s", r);
2021       goto error;
2022     }
2023     r++;
2024     while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2025                 && g_ascii_isspace (r[1]))))
2026       r++;
2028     memset (&field, 0, sizeof (field));
2029     if (!gst_structure_parse_field (r, &r, &field))
2030       goto error;
2031     gst_structure_set_field (structure, &field);
2032   } while (TRUE);
2034   if (end)
2035     *end = (char *) string + (r - copy);
2036   else if (*r)
2037     g_warning ("gst_structure_from_string did not consume whole string,"
2038         " but caller did not provide end pointer (\"%s\")", string);
2040   g_free (copy);
2041   return structure;
2043 error:
2044   if (structure)
2045     gst_structure_free (structure);
2046   g_free (copy);
2047   return NULL;
2050 static void
2051 gst_structure_transform_to_string (const GValue * src_value,
2052     GValue * dest_value)
2054   g_return_if_fail (src_value != NULL);
2055   g_return_if_fail (dest_value != NULL);
2057   dest_value->data[0].v_pointer =
2058       gst_structure_to_string (src_value->data[0].v_pointer);
2061 static GstStructure *
2062 gst_structure_copy_conditional (const GstStructure * structure)
2064   if (structure)
2065     return gst_structure_copy (structure);
2066   return NULL;
2069 /* fixate utility functions */
2071 /**
2072  * gst_structure_fixate_field_nearest_int:
2073  * @structure: a #GstStructure
2074  * @field_name: a field in @structure
2075  * @target: the target value of the fixation
2076  *
2077  * Fixates a #GstStructure by changing the given field to the nearest
2078  * integer to @target that is a subset of the existing field.
2079  *
2080  * Returns: TRUE if the structure could be fixated
2081  */
2082 gboolean
2083 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2084     const char *field_name, int target)
2086   const GValue *value;
2088   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2089   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2091   value = gst_structure_get_value (structure, field_name);
2093   if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2094     /* already fixed */
2095     return FALSE;
2096   } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2097     int x;
2099     x = gst_value_get_int_range_min (value);
2100     if (target < x)
2101       target = x;
2102     x = gst_value_get_int_range_max (value);
2103     if (target > x)
2104       target = x;
2105     gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2106     return TRUE;
2107   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2108     const GValue *list_value;
2109     int i, n;
2110     int best = 0;
2111     int best_index = -1;
2113     n = gst_value_list_get_size (value);
2114     for (i = 0; i < n; i++) {
2115       list_value = gst_value_list_get_value (value, i);
2116       if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2117         int x = g_value_get_int (list_value);
2119         if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2120           best_index = i;
2121           best = x;
2122         }
2123       }
2124     }
2125     if (best_index != -1) {
2126       gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2127       return TRUE;
2128     }
2129     return FALSE;
2130   }
2132   return FALSE;
2135 /**
2136  * gst_structure_fixate_field_nearest_double:
2137  * @structure: a #GstStructure
2138  * @field_name: a field in @structure
2139  * @target: the target value of the fixation
2140  *
2141  * Fixates a #GstStructure by changing the given field to the nearest
2142  * double to @target that is a subset of the existing field.
2143  *
2144  * Returns: TRUE if the structure could be fixated
2145  */
2146 gboolean
2147 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2148     const char *field_name, double target)
2150   const GValue *value;
2152   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2153   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2155   value = gst_structure_get_value (structure, field_name);
2157   if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2158     /* already fixed */
2159     return FALSE;
2160   } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2161     double x;
2163     x = gst_value_get_double_range_min (value);
2164     if (target < x)
2165       target = x;
2166     x = gst_value_get_double_range_max (value);
2167     if (target > x)
2168       target = x;
2169     gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2170     return TRUE;
2171   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2172     const GValue *list_value;
2173     int i, n;
2174     double best = 0;
2175     int best_index = -1;
2177     n = gst_value_list_get_size (value);
2178     for (i = 0; i < n; i++) {
2179       list_value = gst_value_list_get_value (value, i);
2180       if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2181         double x = g_value_get_double (list_value);
2183         if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2184           best_index = i;
2185           best = x;
2186         }
2187       }
2188     }
2189     if (best_index != -1) {
2190       gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2191       return TRUE;
2192     }
2193     return FALSE;
2194   }
2196   return FALSE;
2200 /**
2201  * gst_structure_fixate_field_boolean:
2202  * @structure: a #GstStructure
2203  * @field_name: a field in @structure
2204  * @target: the target value of the fixation
2205  *
2206  * Fixates a #GstStructure by changing the given @field_name field to the given
2207  * @target boolean if that field is not fixed yet.
2208  *
2209  * Returns: TRUE if the structure could be fixated
2210  */
2211 gboolean
2212 gst_structure_fixate_field_boolean (GstStructure * structure,
2213     const char *field_name, gboolean target)
2215   const GValue *value;
2217   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2218   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2220   value = gst_structure_get_value (structure, field_name);
2222   if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2223     /* already fixed */
2224     return FALSE;
2225   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2226     const GValue *list_value;
2227     int i, n;
2228     int best = 0;
2229     int best_index = -1;
2231     n = gst_value_list_get_size (value);
2232     for (i = 0; i < n; i++) {
2233       list_value = gst_value_list_get_value (value, i);
2234       if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2235         gboolean x = g_value_get_boolean (list_value);
2237         if (best_index == -1 || x == target) {
2238           best_index = i;
2239           best = x;
2240         }
2241       }
2242     }
2243     if (best_index != -1) {
2244       gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2245       return TRUE;
2246     }
2247     return FALSE;
2248   }
2250   return FALSE;
2253 /**
2254  * gst_structure_fixate_field_nearest_fraction:
2255  * @structure: a #GstStructure
2256  * @field_name: a field in @structure
2257  * @target_numerator: The numerator of the target value of the fixation
2258  * @target_denominator: The denominator of the target value of the fixation
2259  *
2260  * Fixates a #GstStructure by changing the given field to the nearest
2261  * fraction to @target_numerator/@target_denominator that is a subset 
2262  * of the existing field.
2263  *
2264  * Returns: TRUE if the structure could be fixated
2265  */
2266 gboolean
2267 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2268     const char *field_name, const gint target_numerator,
2269     const gint target_denominator)
2271   const GValue *value;
2273   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2274   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2276   value = gst_structure_get_value (structure, field_name);
2278   if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2279     /* already fixed */
2280     return FALSE;
2281   } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2282     const GValue *x, *new_value;
2283     GValue target = { 0 };
2284     g_value_init (&target, GST_TYPE_FRACTION);
2285     gst_value_set_fraction (&target, target_numerator, target_denominator);
2287     new_value = &target;
2288     x = gst_value_get_fraction_range_min (value);
2289     if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2290       new_value = x;
2291     x = gst_value_get_fraction_range_max (value);
2292     if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2293       new_value = x;
2295     gst_structure_set_value (structure, field_name, new_value);
2296     g_value_unset (&target);
2297     return TRUE;
2298   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2299     const GValue *list_value;
2300     int i, n;
2301     const GValue *best = NULL;
2302     gdouble target;
2303     gdouble cur_diff;
2304     gdouble best_diff = G_MAXDOUBLE;
2306     target = (gdouble) target_numerator / (gdouble) target_denominator;
2308     GST_DEBUG ("target %g, best %g", target, best_diff);
2310     best = NULL;
2312     n = gst_value_list_get_size (value);
2313     for (i = 0; i < n; i++) {
2314       list_value = gst_value_list_get_value (value, i);
2315       if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2316         gint num, denom;
2317         gdouble list_double;
2319         num = gst_value_get_fraction_numerator (list_value);
2320         denom = gst_value_get_fraction_denominator (list_value);
2322         list_double = ((gdouble) num / (gdouble) denom);
2323         cur_diff = target - list_double;
2325         GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2327         if (cur_diff < 0)
2328           cur_diff = -cur_diff;
2330         if (!best || cur_diff < best_diff) {
2331           GST_DEBUG ("new best %g", list_double);
2332           best = list_value;
2333           best_diff = cur_diff;
2334         }
2335       }
2336     }
2337     if (best != NULL) {
2338       gst_structure_set_value (structure, field_name, best);
2339       return TRUE;
2340     }
2341   }
2343   return FALSE;
2346 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2347  * (useful for message parsing functions where the return location is user
2348  * supplied and the user may pass NULL if the value isn't of interest) */
2349 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname)           \
2350 G_STMT_START {                                                                \
2351   const GValue *_value = (value);                                             \
2352   guint _flags = (flags);                                                     \
2353   GType _value_type = G_VALUE_TYPE (_value);                                  \
2354   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);           \
2355   gchar *_lcopy_format = _vtable->lcopy_format;                               \
2356   GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };      \
2357   guint _n_values = 0;                                                        \
2358                                                                               \
2359   while (*_lcopy_format != '\0') {                                            \
2360     g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER);                     \
2361     _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer);          \
2362     _lcopy_format++;                                                          \
2363   }                                                                           \
2364   if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2365     *(__error) = g_strdup_printf ("either all or none of the return "         \
2366         "locations for field '%s' need to be NULL", fieldname);               \
2367   } else if (_cvalues[0].v_pointer != NULL) {                                 \
2368     *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags);  \
2369   }                                                                           \
2370 } G_STMT_END
2372 /**
2373  * gst_structure_get_valist:
2374  * @structure: a #GstStructure
2375  * @first_fieldname: the name of the first field to read
2376  * @args: variable arguments
2377  *
2378  * Parses the variable arguments and reads fields from @structure accordingly.
2379  * valist-variant of gst_structure_get(). Look at the documentation of
2380  * gst_structure_get() for more details.
2381  *
2382  * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2383  *
2384  * Since: 0.10.24
2385  */
2386 gboolean
2387 gst_structure_get_valist (GstStructure * structure,
2388     const char *first_fieldname, va_list args)
2390   const char *field_name;
2391   GType expected_type = G_TYPE_INVALID;
2393   g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2394   g_return_val_if_fail (first_fieldname != NULL, FALSE);
2396   field_name = first_fieldname;
2397   while (field_name) {
2398     const GValue *val = NULL;
2399     gchar *err = NULL;
2401     expected_type = va_arg (args, GType);
2403     val = gst_structure_get_value (structure, field_name);
2405     if (val == NULL)
2406       goto no_such_field;
2408     if (G_VALUE_TYPE (val) != expected_type)
2409       goto wrong_type;
2411     GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2412     if (err) {
2413       g_warning ("%s: %s", G_STRFUNC, err);
2414       g_free (err);
2415       return FALSE;
2416     }
2418     field_name = va_arg (args, const gchar *);
2419   }
2421   return TRUE;
2423 /* ERRORS */
2424 no_such_field:
2425   {
2426     GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2427         field_name, structure);
2428     return FALSE;
2429   }
2430 wrong_type:
2431   {
2432     GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
2433         "field was of type '%s': %" GST_PTR_FORMAT, field_name,
2434         GST_STR_NULL (g_type_name (expected_type)),
2435         G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
2436         structure);
2437     return FALSE;
2438   }
2441 /**
2442  * gst_structure_id_get_valist:
2443  * @structure: a #GstStructure
2444  * @first_field_id: the quark of the first field to read
2445  * @args: variable arguments
2446  *
2447  * Parses the variable arguments and reads fields from @structure accordingly.
2448  * valist-variant of gst_structure_id_get(). Look at the documentation of
2449  * gst_structure_id_get() for more details.
2450  *
2451  * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2452  *
2453  * Since: 0.10.24
2454  */
2455 gboolean
2456 gst_structure_id_get_valist (GstStructure * structure, GQuark first_field_id,
2457     va_list args)
2459   GQuark field_id;
2460   GType expected_type = G_TYPE_INVALID;
2462   g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2463   g_return_val_if_fail (first_field_id != 0, FALSE);
2465   field_id = first_field_id;
2466   while (field_id) {
2467     const GValue *val = NULL;
2468     gchar *err = NULL;
2470     expected_type = va_arg (args, GType);
2472     val = gst_structure_id_get_value (structure, field_id);
2474     if (val == NULL)
2475       goto no_such_field;
2477     if (G_VALUE_TYPE (val) != expected_type)
2478       goto wrong_type;
2480     GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
2481     if (err) {
2482       g_warning ("%s: %s", G_STRFUNC, err);
2483       g_free (err);
2484       return FALSE;
2485     }
2487     field_id = va_arg (args, GQuark);
2488   }
2490   return TRUE;
2492 /* ERRORS */
2493 no_such_field:
2494   {
2495     GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2496         GST_STR_NULL (g_quark_to_string (field_id)), structure);
2497     return FALSE;
2498   }
2499 wrong_type:
2500   {
2501     GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
2502         "field was of type '%s': %" GST_PTR_FORMAT,
2503         g_quark_to_string (field_id),
2504         GST_STR_NULL (g_type_name (expected_type)),
2505         G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
2506         structure);
2507     return FALSE;
2508   }
2511 /**
2512  * gst_structure_get:
2513  * @structure: a #GstStructure
2514  * @first_fieldname: the name of the first field to read
2515  * @...: variable arguments
2516  *
2517  * Parses the variable arguments and reads fields from @structure accordingly.
2518  * Variable arguments should be in the form field name, field type
2519  * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2520  * The last variable argument should be NULL.
2521  *
2522  * For refcounted (mini)objects you will acquire your own reference which
2523  * you must release with a suitable _unref() when no longer needed. For
2524  * strings and boxed types you will acquire a copy which you will need to
2525  * release with either g_free() or the suiteable function for the boxed type.
2526  *
2527  * Returns: FALSE if there was a problem reading any of the fields (e.g.
2528  *     because the field requested did not exist, or was of a type other
2529  *     than the type specified), otherwise TRUE.
2530  *
2531  * Since: 0.10.24
2532  */
2533 gboolean
2534 gst_structure_get (GstStructure * structure, const char *first_fieldname, ...)
2536   gboolean ret;
2537   va_list args;
2539   g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2540   g_return_val_if_fail (first_fieldname != NULL, FALSE);
2542   va_start (args, first_fieldname);
2543   ret = gst_structure_get_valist (structure, first_fieldname, args);
2544   va_end (args);
2546   return ret;
2549 /**
2550  * gst_structure_id_get:
2551  * @structure: a #GstStructure
2552  * @first_field_id: the quark of the first field to read
2553  * @...: variable arguments
2554  *
2555  * Parses the variable arguments and reads fields from @structure accordingly.
2556  * Variable arguments should be in the form field id quark, field type
2557  * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2558  * The last variable argument should be NULL (technically it should be a
2559  * 0 quark, but we require NULL so compilers that support it can check for
2560  * the NULL terminator and warn if it's not there).
2561  *
2562  * This function is just like gst_structure_get() only that it is slightly
2563  * more efficient since it saves the string-to-quark lookup in the global
2564  * quark hashtable.
2565  *
2566  * For refcounted (mini)objects you will acquire your own reference which
2567  * you must release with a suitable _unref() when no longer needed. For
2568  * strings and boxed types you will acquire a copy which you will need to
2569  * release with either g_free() or the suiteable function for the boxed type.
2570  *
2571  * Returns: FALSE if there was a problem reading any of the fields (e.g.
2572  *     because the field requested did not exist, or was of a type other
2573  *     than the type specified), otherwise TRUE.
2574  *
2575  * Since: 0.10.24
2576  */
2577 gboolean
2578 gst_structure_id_get (GstStructure * structure, GQuark first_field_id, ...)
2580   gboolean ret;
2581   va_list args;
2583   g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2584   g_return_val_if_fail (first_field_id != 0, FALSE);
2586   va_start (args, first_field_id);
2587   ret = gst_structure_id_get_valist (structure, first_field_id, args);
2588   va_end (args);
2590   return ret;