Merge branch 'master' into 0.11
[platform/upstream/gstreamer.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  */
21
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_new_empty() 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  */
57
58 #ifdef HAVE_CONFIG_H
59 #include "config.h"
60 #endif
61
62 #include <string.h>
63
64 #include "gst_private.h"
65 #include "gstquark.h"
66 #include <gst/gst.h>
67 #include <gobject/gvaluecollector.h>
68
69 typedef struct _GstStructureField GstStructureField;
70
71 struct _GstStructureField
72 {
73   GQuark name;
74   GValue value;
75 };
76
77 typedef struct
78 {
79   GstStructure s;
80
81   /* owned by parent structure, NULL if no parent */
82   gint *parent_refcount;
83
84   GArray *fields;
85 } GstStructureImpl;
86
87 #define GST_STRUCTURE_REFCOUNT(s) (((GstStructureImpl*)(s))->parent_refcount)
88 #define GST_STRUCTURE_FIELDS(s) (((GstStructureImpl*)(s))->fields)
89
90 #define GST_STRUCTURE_FIELD(structure, index) \
91     &g_array_index(GST_STRUCTURE_FIELDS(structure), GstStructureField, (index))
92
93 #define IS_MUTABLE(structure) \
94     (!GST_STRUCTURE_REFCOUNT(structure) || \
95      g_atomic_int_get (GST_STRUCTURE_REFCOUNT(structure)) == 1)
96
97 #define IS_TAGLIST(structure) \
98     (structure->name == GST_QUARK (TAGLIST))
99
100 static void gst_structure_set_field (GstStructure * structure,
101     GstStructureField * field);
102 static GstStructureField *gst_structure_get_field (const GstStructure *
103     structure, const gchar * fieldname);
104 static GstStructureField *gst_structure_id_get_field (const GstStructure *
105     structure, GQuark field);
106 static void gst_structure_transform_to_string (const GValue * src_value,
107     GValue * dest_value);
108 static GstStructure *gst_structure_copy_conditional (const GstStructure *
109     structure);
110 static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
111     GValue * value, GType default_type);
112 static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
113
114 GType _gst_structure_type = 0;
115
116
117 G_DEFINE_BOXED_TYPE (GstStructure, gst_structure,
118     gst_structure_copy_conditional, gst_structure_free);
119
120 void
121 _priv_gst_structure_initialize (void)
122 {
123   _gst_structure_type = gst_structure_get_type ();
124
125   g_value_register_transform_func (_gst_structure_type, G_TYPE_STRING,
126       gst_structure_transform_to_string);
127 }
128
129 static GstStructure *
130 gst_structure_new_id_empty_with_size (GQuark quark, guint prealloc)
131 {
132   GstStructureImpl *structure;
133
134   structure = g_slice_new (GstStructureImpl);
135   ((GstStructure *) structure)->type = _gst_structure_type;
136   ((GstStructure *) structure)->name = quark;
137   GST_STRUCTURE_REFCOUNT (structure) = NULL;
138   GST_STRUCTURE_FIELDS (structure) =
139       g_array_sized_new (FALSE, FALSE, sizeof (GstStructureField), prealloc);
140
141   return GST_STRUCTURE_CAST (structure);
142 }
143
144 /**
145  * gst_structure_new_id_empty:
146  * @quark: name of new structure
147  *
148  * Creates a new, empty #GstStructure with the given name as a GQuark.
149  *
150  * Free-function: gst_structure_free
151  *
152  * Returns: (transfer full): a new, empty #GstStructure
153  */
154 GstStructure *
155 gst_structure_new_id_empty (GQuark quark)
156 {
157   g_return_val_if_fail (quark != 0, NULL);
158
159   return gst_structure_new_id_empty_with_size (quark, 0);
160 }
161
162 #ifndef G_DISABLE_CHECKS
163 static gboolean
164 gst_structure_validate_name (const gchar * name)
165 {
166   const gchar *s;
167
168   g_return_val_if_fail (name != NULL, FALSE);
169
170   if (G_UNLIKELY (!g_ascii_isalpha (*name))) {
171     GST_WARNING ("Invalid character '%c' at offset 0 in structure name: %s",
172         *name, name);
173     return FALSE;
174   }
175
176   /* FIXME: test name string more */
177   s = &name[1];
178   while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+", *s) != NULL))
179     s++;
180   if (G_UNLIKELY (*s != '\0')) {
181     GST_WARNING ("Invalid character '%c' at offset %" G_GUINTPTR_FORMAT " in"
182         " structure name: %s", *s, ((guintptr) s - (guintptr) name), name);
183     return FALSE;
184   }
185
186   return TRUE;
187 }
188 #endif
189
190 /**
191  * gst_structure_new_empty:
192  * @name: name of new structure
193  *
194  * Creates a new, empty #GstStructure with the given @name.
195  *
196  * See gst_structure_set_name() for constraints on the @name parameter.
197  *
198  * Free-function: gst_structure_free
199  *
200  * Returns: (transfer full): a new, empty #GstStructure
201  */
202 GstStructure *
203 gst_structure_new_empty (const gchar * name)
204 {
205   g_return_val_if_fail (gst_structure_validate_name (name), NULL);
206
207   return gst_structure_new_id_empty_with_size (g_quark_from_string (name), 0);
208 }
209
210 /**
211  * gst_structure_new:
212  * @name: name of new structure
213  * @firstfield: name of first field to set
214  * @...: additional arguments
215  *
216  * Creates a new #GstStructure with the given name.  Parses the
217  * list of variable arguments and sets fields to the values listed.
218  * Variable arguments should be passed as field name, field type,
219  * and value.  Last variable argument should be NULL.
220  *
221  * Free-function: gst_structure_free
222  *
223  * Returns: (transfer full): a new #GstStructure
224  */
225 GstStructure *
226 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
227 {
228   GstStructure *structure;
229   va_list varargs;
230
231   va_start (varargs, firstfield);
232   structure = gst_structure_new_valist (name, firstfield, varargs);
233   va_end (varargs);
234
235   return structure;
236 }
237
238 /**
239  * gst_structure_new_valist:
240  * @name: name of new structure
241  * @firstfield: name of first field to set
242  * @varargs: variable argument list
243  *
244  * Creates a new #GstStructure with the given @name.  Structure fields
245  * are set according to the varargs in a manner similar to
246  * gst_structure_new().
247  *
248  * See gst_structure_set_name() for constraints on the @name parameter.
249  *
250  * Free-function: gst_structure_free
251  *
252  * Returns: (transfer full): a new #GstStructure
253  */
254 GstStructure *
255 gst_structure_new_valist (const gchar * name,
256     const gchar * firstfield, va_list varargs)
257 {
258   GstStructure *structure;
259
260   structure = gst_structure_new_empty (name);
261
262   if (structure)
263     gst_structure_set_valist (structure, firstfield, varargs);
264
265   return structure;
266 }
267
268 /**
269  * gst_structure_set_parent_refcount:
270  * @structure: a #GstStructure
271  * @refcount: (in): a pointer to the parent's refcount
272  *
273  * Sets the parent_refcount field of #GstStructure. This field is used to
274  * determine whether a structure is mutable or not. This function should only be
275  * called by code implementing parent objects of #GstStructure, as described in
276  * the MT Refcounting section of the design documents.
277  *
278  * Returns: %TRUE if the parent refcount could be set.
279  */
280 gboolean
281 gst_structure_set_parent_refcount (GstStructure * structure, gint * refcount)
282 {
283   g_return_val_if_fail (structure != NULL, FALSE);
284
285   /* if we have a parent_refcount already, we can only clear
286    * if with a NULL refcount */
287   if (GST_STRUCTURE_REFCOUNT (structure)) {
288     if (refcount != NULL) {
289       g_return_val_if_fail (refcount == NULL, FALSE);
290       return FALSE;
291     }
292   } else {
293     if (refcount == NULL) {
294       g_return_val_if_fail (refcount != NULL, FALSE);
295       return FALSE;
296     }
297   }
298
299   GST_STRUCTURE_REFCOUNT (structure) = refcount;
300
301   return TRUE;
302 }
303
304 /**
305  * gst_structure_copy:
306  * @structure: a #GstStructure to duplicate
307  *
308  * Duplicates a #GstStructure and all its fields and values.
309  *
310  * Free-function: gst_structure_free
311  *
312  * Returns: (transfer none): a new #GstStructure.
313  */
314 GstStructure *
315 gst_structure_copy (const GstStructure * structure)
316 {
317   GstStructure *new_structure;
318   GstStructureField *field;
319   guint i, len;
320
321   g_return_val_if_fail (structure != NULL, NULL);
322
323   len = GST_STRUCTURE_FIELDS (structure)->len;
324   new_structure = gst_structure_new_id_empty_with_size (structure->name, len);
325
326   for (i = 0; i < len; i++) {
327     GstStructureField new_field = { 0 };
328
329     field = GST_STRUCTURE_FIELD (structure, i);
330
331     new_field.name = field->name;
332     gst_value_init_and_copy (&new_field.value, &field->value);
333     g_array_append_val (GST_STRUCTURE_FIELDS (new_structure), new_field);
334   }
335
336   return new_structure;
337 }
338
339 /**
340  * gst_structure_free:
341  * @structure: (in) (transfer full): the #GstStructure to free
342  *
343  * Frees a #GstStructure and all its fields and values. The structure must not
344  * have a parent when this function is called.
345  */
346 void
347 gst_structure_free (GstStructure * structure)
348 {
349   GstStructureField *field;
350   guint i, len;
351
352   g_return_if_fail (structure != NULL);
353   g_return_if_fail (GST_STRUCTURE_REFCOUNT (structure) == NULL);
354
355   len = GST_STRUCTURE_FIELDS (structure)->len;
356   for (i = 0; i < len; i++) {
357     field = GST_STRUCTURE_FIELD (structure, i);
358
359     if (G_IS_VALUE (&field->value)) {
360       g_value_unset (&field->value);
361     }
362   }
363   g_array_free (GST_STRUCTURE_FIELDS (structure), TRUE);
364 #ifdef USE_POISONING
365   memset (structure, 0xff, sizeof (GstStructure));
366 #endif
367   g_slice_free1 (sizeof (GstStructureImpl), structure);
368 }
369
370 /**
371  * gst_structure_get_name:
372  * @structure: a #GstStructure
373  *
374  * Get the name of @structure as a string.
375  *
376  * Returns: the name of the structure.
377  */
378 const gchar *
379 gst_structure_get_name (const GstStructure * structure)
380 {
381   g_return_val_if_fail (structure != NULL, NULL);
382
383   return g_quark_to_string (structure->name);
384 }
385
386 /**
387  * gst_structure_has_name:
388  * @structure: a #GstStructure
389  * @name: structure name to check for
390  *
391  * Checks if the structure has the given name
392  *
393  * Returns: TRUE if @name matches the name of the structure.
394  */
395 gboolean
396 gst_structure_has_name (const GstStructure * structure, const gchar * name)
397 {
398   const gchar *structure_name;
399
400   g_return_val_if_fail (structure != NULL, FALSE);
401   g_return_val_if_fail (name != NULL, FALSE);
402
403   /* getting the string is cheap and comparing short strings is too
404    * should be faster than getting the quark for name and comparing the quarks
405    */
406   structure_name = g_quark_to_string (structure->name);
407
408   return (structure_name && strcmp (structure_name, name) == 0);
409 }
410
411 /**
412  * gst_structure_get_name_id:
413  * @structure: a #GstStructure
414  *
415  * Get the name of @structure as a GQuark.
416  *
417  * Returns: the quark representing the name of the structure.
418  */
419 GQuark
420 gst_structure_get_name_id (const GstStructure * structure)
421 {
422   g_return_val_if_fail (structure != NULL, 0);
423
424   return structure->name;
425 }
426
427 /**
428  * gst_structure_set_name:
429  * @structure: a #GstStructure
430  * @name: the new name of the structure
431  *
432  * Sets the name of the structure to the given @name.  The string
433  * provided is copied before being used. It must not be empty, start with a
434  * letter and can be followed by letters, numbers and any of "/-_.:".
435  */
436 void
437 gst_structure_set_name (GstStructure * structure, const gchar * name)
438 {
439   g_return_if_fail (structure != NULL);
440   g_return_if_fail (IS_MUTABLE (structure));
441   g_return_if_fail (gst_structure_validate_name (name));
442
443   structure->name = g_quark_from_string (name);
444 }
445
446 static inline void
447 gst_structure_id_set_value_internal (GstStructure * structure, GQuark field,
448     const GValue * value)
449 {
450   GstStructureField gsfield = { 0, {0,} };
451
452   gsfield.name = field;
453   gst_value_init_and_copy (&gsfield.value, value);
454
455   gst_structure_set_field (structure, &gsfield);
456 }
457
458 /**
459  * gst_structure_id_set_value:
460  * @structure: a #GstStructure
461  * @field: a #GQuark representing a field
462  * @value: the new value of the field
463  *
464  * Sets the field with the given GQuark @field to @value.  If the field
465  * does not exist, it is created.  If the field exists, the previous
466  * value is replaced and freed.
467  */
468 void
469 gst_structure_id_set_value (GstStructure * structure,
470     GQuark field, const GValue * value)
471 {
472
473   g_return_if_fail (structure != NULL);
474   g_return_if_fail (G_IS_VALUE (value));
475   g_return_if_fail (IS_MUTABLE (structure));
476
477   gst_structure_id_set_value_internal (structure, field, value);
478 }
479
480 /**
481  * gst_structure_set_value:
482  * @structure: a #GstStructure
483  * @fieldname: the name of the field to set
484  * @value: the new value of the field
485  *
486  * Sets the field with the given name @field to @value.  If the field
487  * does not exist, it is created.  If the field exists, the previous
488  * value is replaced and freed.
489  */
490 void
491 gst_structure_set_value (GstStructure * structure,
492     const gchar * fieldname, const GValue * value)
493 {
494   g_return_if_fail (structure != NULL);
495   g_return_if_fail (fieldname != NULL);
496   g_return_if_fail (G_IS_VALUE (value));
497   g_return_if_fail (IS_MUTABLE (structure));
498
499   gst_structure_id_set_value_internal (structure,
500       g_quark_from_string (fieldname), value);
501 }
502
503 static inline void
504 gst_structure_id_take_value_internal (GstStructure * structure, GQuark field,
505     GValue * value)
506 {
507   GstStructureField gsfield = { 0, {0,} };
508
509   gsfield.name = field;
510   gsfield.value = *value;
511
512   gst_structure_set_field (structure, &gsfield);
513
514   /* we took ownership */
515 #ifdef USE_POISONING
516   memset (value, 0, sizeof (GValue));
517 #else
518   value->g_type = G_TYPE_INVALID;
519 #endif
520 }
521
522 /**
523  * gst_structure_id_take_value:
524  * @structure: a #GstStructure
525  * @field: a #GQuark representing a field
526  * @value: (transfer full): the new value of the field
527  *
528  * Sets the field with the given GQuark @field to @value.  If the field
529  * does not exist, it is created.  If the field exists, the previous
530  * value is replaced and freed.
531  *
532  * Since: 0.10.31
533  */
534 void
535 gst_structure_id_take_value (GstStructure * structure, GQuark field,
536     GValue * value)
537 {
538   g_return_if_fail (structure != NULL);
539   g_return_if_fail (G_IS_VALUE (value));
540   g_return_if_fail (IS_MUTABLE (structure));
541
542   gst_structure_id_take_value_internal (structure, field, value);
543 }
544
545 /**
546  * gst_structure_take_value:
547  * @structure: a #GstStructure
548  * @fieldname: the name of the field to set
549  * @value: (transfer full): the new value of the field
550  *
551  * Sets the field with the given name @field to @value.  If the field
552  * does not exist, it is created.  If the field exists, the previous
553  * value is replaced and freed. The function will take ownership of @value.
554  *
555  * Since: 0.10.31
556  */
557 void
558 gst_structure_take_value (GstStructure * structure, const gchar * fieldname,
559     GValue * value)
560 {
561   g_return_if_fail (structure != NULL);
562   g_return_if_fail (fieldname != NULL);
563   g_return_if_fail (G_IS_VALUE (value));
564   g_return_if_fail (IS_MUTABLE (structure));
565
566   gst_structure_id_take_value_internal (structure,
567       g_quark_from_string (fieldname), value);
568 }
569
570 static void
571 gst_structure_set_valist_internal (GstStructure * structure,
572     const gchar * fieldname, va_list varargs)
573 {
574   gchar *err = NULL;
575   GType type;
576
577   while (fieldname) {
578     GstStructureField field = { 0 };
579
580     field.name = g_quark_from_string (fieldname);
581
582     type = va_arg (varargs, GType);
583
584     if (G_UNLIKELY (type == G_TYPE_DATE)) {
585       g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
586       type = GST_TYPE_DATE;
587     }
588     G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
589     if (G_UNLIKELY (err)) {
590       g_critical ("%s", err);
591       return;
592     }
593     gst_structure_set_field (structure, &field);
594
595     fieldname = va_arg (varargs, gchar *);
596   }
597 }
598
599 /**
600  * gst_structure_set:
601  * @structure: a #GstStructure
602  * @fieldname: the name of the field to set
603  * @...: variable arguments
604  *
605  * Parses the variable arguments and sets fields accordingly.
606  * Variable arguments should be in the form field name, field type
607  * (as a GType), value(s).  The last variable argument should be NULL.
608  */
609 void
610 gst_structure_set (GstStructure * structure, const gchar * field, ...)
611 {
612   va_list varargs;
613
614   g_return_if_fail (structure != NULL);
615   g_return_if_fail (IS_MUTABLE (structure) || field == NULL);
616
617   va_start (varargs, field);
618   gst_structure_set_valist_internal (structure, field, varargs);
619   va_end (varargs);
620 }
621
622 /**
623  * gst_structure_set_valist:
624  * @structure: a #GstStructure
625  * @fieldname: the name of the field to set
626  * @varargs: variable arguments
627  *
628  * va_list form of gst_structure_set().
629  */
630 void
631 gst_structure_set_valist (GstStructure * structure,
632     const gchar * fieldname, va_list varargs)
633 {
634   g_return_if_fail (structure != NULL);
635   g_return_if_fail (IS_MUTABLE (structure));
636
637   gst_structure_set_valist_internal (structure, fieldname, varargs);
638 }
639
640 static void
641 gst_structure_id_set_valist_internal (GstStructure * structure,
642     GQuark fieldname, va_list varargs)
643 {
644   gchar *err = NULL;
645   GType type;
646
647   while (fieldname) {
648     GstStructureField field = { 0 };
649
650     field.name = fieldname;
651
652     type = va_arg (varargs, GType);
653
654     if (G_UNLIKELY (type == G_TYPE_DATE)) {
655       g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
656       type = GST_TYPE_DATE;
657     }
658 #ifndef G_VALUE_COLLECT_INIT
659     g_value_init (&field.value, type);
660     G_VALUE_COLLECT (&field.value, varargs, 0, &err);
661 #else
662     G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
663 #endif
664     if (G_UNLIKELY (err)) {
665       g_critical ("%s", err);
666       return;
667     }
668     gst_structure_set_field (structure, &field);
669
670     fieldname = va_arg (varargs, GQuark);
671   }
672 }
673
674 /**
675  * gst_structure_id_set:
676  * @structure: a #GstStructure
677  * @fieldname: the GQuark for the name of the field to set
678  * @...: variable arguments
679  *
680  * Identical to gst_structure_set, except that field names are
681  * passed using the GQuark for the field name. This allows more efficient
682  * setting of the structure if the caller already knows the associated
683  * quark values.
684  * The last variable argument must be NULL.
685  *
686  * Since: 0.10.10
687  */
688 void
689 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
690 {
691   va_list varargs;
692
693   g_return_if_fail (structure != NULL);
694
695   va_start (varargs, field);
696   gst_structure_id_set_valist_internal (structure, field, varargs);
697   va_end (varargs);
698 }
699
700 /**
701  * gst_structure_id_set_valist:
702  * @structure: a #GstStructure
703  * @fieldname: the name of the field to set
704  * @varargs: variable arguments
705  *
706  * va_list form of gst_structure_id_set().
707  *
708  * Since: 0.10.10
709  */
710 void
711 gst_structure_id_set_valist (GstStructure * structure,
712     GQuark fieldname, va_list varargs)
713 {
714   g_return_if_fail (structure != NULL);
715   g_return_if_fail (IS_MUTABLE (structure));
716
717   gst_structure_id_set_valist_internal (structure, fieldname, varargs);
718 }
719
720 /**
721  * gst_structure_new_id:
722  * @name_quark: name of new structure
723  * @field_quark: the GQuark for the name of the field to set
724  * @...: variable arguments
725  *
726  * Creates a new #GstStructure with the given name as a GQuark, followed by
727  * fieldname quark, GType, argument(s) "triplets" in the same format as
728  * gst_structure_id_set(). Basically a convenience wrapper around
729  * gst_structure_new_id_empty() and gst_structure_id_set().
730  *
731  * The last variable argument must be NULL (or 0).
732  *
733  * Free-function: gst_structure_free
734  *
735  * Returns: (transfer full): a new #GstStructure
736  *
737  * Since: 0.10.24
738  */
739 GstStructure *
740 gst_structure_new_id (GQuark name_quark, GQuark field_quark, ...)
741 {
742   GstStructure *s;
743   va_list varargs;
744
745   g_return_val_if_fail (name_quark != 0, NULL);
746   g_return_val_if_fail (field_quark != 0, NULL);
747
748   s = gst_structure_new_id_empty (name_quark);
749
750   va_start (varargs, field_quark);
751   gst_structure_id_set_valist_internal (s, field_quark, varargs);
752   va_end (varargs);
753
754   return s;
755 }
756
757 #if GST_VERSION_NANO == 1
758 #define GIT_G_WARNING g_warning
759 #else
760 #define GIT_G_WARNING GST_WARNING
761 #endif
762
763 /* If the structure currently contains a field with the same name, it is
764  * replaced with the provided field. Otherwise, the field is added to the
765  * structure. The field's value is not deeply copied.
766  */
767 static void
768 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
769 {
770   GstStructureField *f;
771   guint i, len = GST_STRUCTURE_FIELDS (structure)->len;
772
773   if (G_UNLIKELY (G_VALUE_HOLDS_STRING (&field->value))) {
774     const gchar *s;
775
776     s = g_value_get_string (&field->value);
777     /* only check for NULL strings in taglists, as they are allowed in message
778      * structs, e.g. error message debug strings */
779     if (G_UNLIKELY (IS_TAGLIST (structure) && (s == NULL || *s == '\0'))) {
780       if (s == NULL) {
781         GIT_G_WARNING ("Trying to set NULL string on field '%s' on taglist. "
782             "Please file a bug.", g_quark_to_string (field->name));
783         g_value_unset (&field->value);
784         return;
785       } else {
786         /* empty strings never make sense */
787         GIT_G_WARNING ("Trying to set empty string on taglist field '%s'. "
788             "Please file a bug.", g_quark_to_string (field->name));
789         g_value_unset (&field->value);
790         return;
791       }
792     } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
793       g_warning ("Trying to set string on %s field '%s', but string is not "
794           "valid UTF-8. Please file a bug.",
795           IS_TAGLIST (structure) ? "taglist" : "structure",
796           g_quark_to_string (field->name));
797       g_value_unset (&field->value);
798       return;
799     }
800   } else if (G_UNLIKELY (GST_VALUE_HOLDS_DATE (&field->value))) {
801     const GDate *d;
802
803     d = gst_value_get_date (&field->value);
804     /* only check for NULL GDates in taglists, as they might make sense
805      * in other, generic structs */
806     if (G_UNLIKELY ((IS_TAGLIST (structure) && d == NULL))) {
807       GIT_G_WARNING ("Trying to set NULL GDate on field '%s' on taglist. "
808           "Please file a bug.", g_quark_to_string (field->name));
809       g_value_unset (&field->value);
810       return;
811     } else if (G_UNLIKELY (d != NULL && !g_date_valid (d))) {
812       g_warning
813           ("Trying to set invalid GDate on %s field '%s'. Please file a bug.",
814           IS_TAGLIST (structure) ? "taglist" : "structure",
815           g_quark_to_string (field->name));
816       g_value_unset (&field->value);
817       return;
818     }
819   }
820
821   for (i = 0; i < len; i++) {
822     f = GST_STRUCTURE_FIELD (structure, i);
823
824     if (G_UNLIKELY (f->name == field->name)) {
825       g_value_unset (&f->value);
826       memcpy (f, field, sizeof (GstStructureField));
827       return;
828     }
829   }
830
831   g_array_append_val (GST_STRUCTURE_FIELDS (structure), *field);
832 }
833
834 /* If there is no field with the given ID, NULL is returned.
835  */
836 static GstStructureField *
837 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
838 {
839   GstStructureField *field;
840   guint i, len;
841
842   len = GST_STRUCTURE_FIELDS (structure)->len;
843
844   for (i = 0; i < len; i++) {
845     field = GST_STRUCTURE_FIELD (structure, i);
846
847     if (G_UNLIKELY (field->name == field_id))
848       return field;
849   }
850
851   return NULL;
852 }
853
854 /* If there is no field with the given ID, NULL is returned.
855  */
856 static GstStructureField *
857 gst_structure_get_field (const GstStructure * structure,
858     const gchar * fieldname)
859 {
860   g_return_val_if_fail (structure != NULL, NULL);
861   g_return_val_if_fail (fieldname != NULL, NULL);
862
863   return gst_structure_id_get_field (structure,
864       g_quark_from_string (fieldname));
865 }
866
867 /**
868  * gst_structure_get_value:
869  * @structure: a #GstStructure
870  * @fieldname: the name of the field to get
871  *
872  * Get the value of the field with name @fieldname.
873  *
874  * Returns: the #GValue corresponding to the field with the given name.
875  */
876 const GValue *
877 gst_structure_get_value (const GstStructure * structure,
878     const gchar * fieldname)
879 {
880   GstStructureField *field;
881
882   g_return_val_if_fail (structure != NULL, NULL);
883   g_return_val_if_fail (fieldname != NULL, NULL);
884
885   field = gst_structure_get_field (structure, fieldname);
886   if (field == NULL)
887     return NULL;
888
889   return &field->value;
890 }
891
892 /**
893  * gst_structure_id_get_value:
894  * @structure: a #GstStructure
895  * @field: the #GQuark of the field to get
896  *
897  * Get the value of the field with GQuark @field.
898  *
899  * Returns: the #GValue corresponding to the field with the given name
900  *          identifier.
901  */
902 const GValue *
903 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
904 {
905   GstStructureField *gsfield;
906
907   g_return_val_if_fail (structure != NULL, NULL);
908
909   gsfield = gst_structure_id_get_field (structure, field);
910   if (gsfield == NULL)
911     return NULL;
912
913   return &gsfield->value;
914 }
915
916 /**
917  * gst_structure_remove_field:
918  * @structure: a #GstStructure
919  * @fieldname: the name of the field to remove
920  *
921  * Removes the field with the given name.  If the field with the given
922  * name does not exist, the structure is unchanged.
923  */
924 void
925 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
926 {
927   GstStructureField *field;
928   GQuark id;
929   guint i, len;
930
931   g_return_if_fail (structure != NULL);
932   g_return_if_fail (fieldname != NULL);
933   g_return_if_fail (IS_MUTABLE (structure));
934
935   id = g_quark_from_string (fieldname);
936   len = GST_STRUCTURE_FIELDS (structure)->len;
937
938   for (i = 0; i < len; i++) {
939     field = GST_STRUCTURE_FIELD (structure, i);
940
941     if (field->name == id) {
942       if (G_IS_VALUE (&field->value)) {
943         g_value_unset (&field->value);
944       }
945       GST_STRUCTURE_FIELDS (structure) =
946           g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
947       return;
948     }
949   }
950 }
951
952 /**
953  * gst_structure_remove_fields:
954  * @structure: a #GstStructure
955  * @fieldname: the name of the field to remove
956  * @...: NULL-terminated list of more fieldnames to remove
957  *
958  * Removes the fields with the given names. If a field does not exist, the
959  * argument is ignored.
960  */
961 void
962 gst_structure_remove_fields (GstStructure * structure,
963     const gchar * fieldname, ...)
964 {
965   va_list varargs;
966
967   g_return_if_fail (structure != NULL);
968   g_return_if_fail (fieldname != NULL);
969   /* mutability checked in remove_field */
970
971   va_start (varargs, fieldname);
972   gst_structure_remove_fields_valist (structure, fieldname, varargs);
973   va_end (varargs);
974 }
975
976 /**
977  * gst_structure_remove_fields_valist:
978  * @structure: a #GstStructure
979  * @fieldname: the name of the field to remove
980  * @varargs: NULL-terminated list of more fieldnames to remove
981  *
982  * va_list form of gst_structure_remove_fields().
983  */
984 void
985 gst_structure_remove_fields_valist (GstStructure * structure,
986     const gchar * fieldname, va_list varargs)
987 {
988   gchar *field = (gchar *) fieldname;
989
990   g_return_if_fail (structure != NULL);
991   g_return_if_fail (fieldname != NULL);
992   /* mutability checked in remove_field */
993
994   while (field) {
995     gst_structure_remove_field (structure, field);
996     field = va_arg (varargs, char *);
997   }
998 }
999
1000 /**
1001  * gst_structure_remove_all_fields:
1002  * @structure: a #GstStructure
1003  *
1004  * Removes all fields in a GstStructure.
1005  */
1006 void
1007 gst_structure_remove_all_fields (GstStructure * structure)
1008 {
1009   GstStructureField *field;
1010   int i;
1011
1012   g_return_if_fail (structure != NULL);
1013   g_return_if_fail (IS_MUTABLE (structure));
1014
1015   for (i = GST_STRUCTURE_FIELDS (structure)->len - 1; i >= 0; i--) {
1016     field = GST_STRUCTURE_FIELD (structure, i);
1017
1018     if (G_IS_VALUE (&field->value)) {
1019       g_value_unset (&field->value);
1020     }
1021     GST_STRUCTURE_FIELDS (structure) =
1022         g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
1023   }
1024 }
1025
1026 /**
1027  * gst_structure_get_field_type:
1028  * @structure: a #GstStructure
1029  * @fieldname: the name of the field
1030  *
1031  * Finds the field with the given name, and returns the type of the
1032  * value it contains.  If the field is not found, G_TYPE_INVALID is
1033  * returned.
1034  *
1035  * Returns: the #GValue of the field
1036  */
1037 GType
1038 gst_structure_get_field_type (const GstStructure * structure,
1039     const gchar * fieldname)
1040 {
1041   GstStructureField *field;
1042
1043   g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
1044   g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
1045
1046   field = gst_structure_get_field (structure, fieldname);
1047   if (field == NULL)
1048     return G_TYPE_INVALID;
1049
1050   return G_VALUE_TYPE (&field->value);
1051 }
1052
1053 /**
1054  * gst_structure_n_fields:
1055  * @structure: a #GstStructure
1056  *
1057  * Get the number of fields in the structure.
1058  *
1059  * Returns: the number of fields in the structure
1060  */
1061 gint
1062 gst_structure_n_fields (const GstStructure * structure)
1063 {
1064   g_return_val_if_fail (structure != NULL, 0);
1065
1066   return GST_STRUCTURE_FIELDS (structure)->len;
1067 }
1068
1069 /**
1070  * gst_structure_nth_field_name:
1071  * @structure: a #GstStructure
1072  * @index: the index to get the name of
1073  *
1074  * Get the name of the given field number, counting from 0 onwards.
1075  *
1076  * Returns: the name of the given field number
1077  */
1078 const gchar *
1079 gst_structure_nth_field_name (const GstStructure * structure, guint index)
1080 {
1081   GstStructureField *field;
1082
1083   g_return_val_if_fail (structure != NULL, NULL);
1084   g_return_val_if_fail (index < GST_STRUCTURE_FIELDS (structure)->len, NULL);
1085
1086   field = GST_STRUCTURE_FIELD (structure, index);
1087
1088   return g_quark_to_string (field->name);
1089 }
1090
1091 /**
1092  * gst_structure_foreach:
1093  * @structure: a #GstStructure
1094  * @func: (scope call): a function to call for each field
1095  * @user_data: (closure): private data
1096  *
1097  * Calls the provided function once for each field in the #GstStructure. The
1098  * function must not modify the fields. Also see gst_structure_map_in_place().
1099  *
1100  * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1101  * FALSE otherwise.
1102  */
1103 gboolean
1104 gst_structure_foreach (const GstStructure * structure,
1105     GstStructureForeachFunc func, gpointer user_data)
1106 {
1107   guint i, len;
1108   GstStructureField *field;
1109   gboolean ret;
1110
1111   g_return_val_if_fail (structure != NULL, FALSE);
1112   g_return_val_if_fail (func != NULL, FALSE);
1113
1114   len = GST_STRUCTURE_FIELDS (structure)->len;
1115
1116   for (i = 0; i < len; i++) {
1117     field = GST_STRUCTURE_FIELD (structure, i);
1118
1119     ret = func (field->name, &field->value, user_data);
1120     if (G_UNLIKELY (!ret))
1121       return FALSE;
1122   }
1123
1124   return TRUE;
1125 }
1126
1127 /**
1128  * gst_structure_map_in_place:
1129  * @structure: a #GstStructure
1130  * @func: (scope call): a function to call for each field
1131  * @user_data: (closure): private data
1132  *
1133  * Calls the provided function once for each field in the #GstStructure. In
1134  * contrast to gst_structure_foreach(), the function may modify but not delete the
1135  * fields. The structure must be mutable.
1136  *
1137  * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1138  * FALSE otherwise.
1139  */
1140 gboolean
1141 gst_structure_map_in_place (GstStructure * structure,
1142     GstStructureMapFunc func, gpointer user_data)
1143 {
1144   guint i, len;
1145   GstStructureField *field;
1146   gboolean ret;
1147
1148   g_return_val_if_fail (structure != NULL, FALSE);
1149   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1150   g_return_val_if_fail (func != NULL, FALSE);
1151   len = GST_STRUCTURE_FIELDS (structure)->len;
1152
1153   for (i = 0; i < len; i++) {
1154     field = GST_STRUCTURE_FIELD (structure, i);
1155
1156     ret = func (field->name, &field->value, user_data);
1157     if (!ret)
1158       return FALSE;
1159   }
1160
1161   return TRUE;
1162 }
1163
1164 /**
1165  * gst_structure_id_has_field:
1166  * @structure: a #GstStructure
1167  * @field: #GQuark of the field name
1168  *
1169  * Check if @structure contains a field named @field.
1170  *
1171  * Returns: TRUE if the structure contains a field with the given name
1172  *
1173  * Since: 0.10.26
1174  */
1175 gboolean
1176 gst_structure_id_has_field (const GstStructure * structure, GQuark field)
1177 {
1178   GstStructureField *f;
1179
1180   g_return_val_if_fail (structure != NULL, FALSE);
1181   g_return_val_if_fail (field != 0, FALSE);
1182
1183   f = gst_structure_id_get_field (structure, field);
1184
1185   return (f != NULL);
1186 }
1187
1188 /**
1189  * gst_structure_has_field:
1190  * @structure: a #GstStructure
1191  * @fieldname: the name of a field
1192  *
1193  * Check if @structure contains a field named @fieldname.
1194  *
1195  * Returns: TRUE if the structure contains a field with the given name
1196  */
1197 gboolean
1198 gst_structure_has_field (const GstStructure * structure,
1199     const gchar * fieldname)
1200 {
1201   g_return_val_if_fail (structure != NULL, FALSE);
1202   g_return_val_if_fail (fieldname != NULL, FALSE);
1203
1204   return gst_structure_id_has_field (structure,
1205       g_quark_from_string (fieldname));
1206 }
1207
1208 /**
1209  * gst_structure_id_has_field_typed:
1210  * @structure: a #GstStructure
1211  * @field: #GQuark of the field name
1212  * @type: the type of a value
1213  *
1214  * Check if @structure contains a field named @field and with GType @type.
1215  *
1216  * Returns: TRUE if the structure contains a field with the given name and type
1217  *
1218  * Since: 0.10.26
1219  */
1220 gboolean
1221 gst_structure_id_has_field_typed (const GstStructure * structure,
1222     GQuark field, GType type)
1223 {
1224   GstStructureField *f;
1225
1226   g_return_val_if_fail (structure != NULL, FALSE);
1227   g_return_val_if_fail (field != 0, FALSE);
1228
1229   f = gst_structure_id_get_field (structure, field);
1230   if (f == NULL)
1231     return FALSE;
1232
1233   return (G_VALUE_TYPE (&f->value) == type);
1234 }
1235
1236 /**
1237  * gst_structure_has_field_typed:
1238  * @structure: a #GstStructure
1239  * @fieldname: the name of a field
1240  * @type: the type of a value
1241  *
1242  * Check if @structure contains a field named @fieldname and with GType @type.
1243  *
1244  * Returns: TRUE if the structure contains a field with the given name and type
1245  */
1246 gboolean
1247 gst_structure_has_field_typed (const GstStructure * structure,
1248     const gchar * fieldname, GType type)
1249 {
1250   g_return_val_if_fail (structure != NULL, FALSE);
1251   g_return_val_if_fail (fieldname != NULL, FALSE);
1252
1253   return gst_structure_id_has_field_typed (structure,
1254       g_quark_from_string (fieldname), type);
1255 }
1256
1257 /* utility functions */
1258
1259 /**
1260  * gst_structure_get_boolean:
1261  * @structure: a #GstStructure
1262  * @fieldname: the name of a field
1263  * @value: (out): a pointer to a #gboolean to set
1264  *
1265  * Sets the boolean pointed to by @value corresponding to the value of the
1266  * given field.  Caller is responsible for making sure the field exists
1267  * and has the correct type.
1268  *
1269  * Returns: TRUE if the value could be set correctly. If there was no field
1270  * with @fieldname or the existing field did not contain a boolean, this
1271  * function returns FALSE.
1272  */
1273 gboolean
1274 gst_structure_get_boolean (const GstStructure * structure,
1275     const gchar * fieldname, gboolean * value)
1276 {
1277   GstStructureField *field;
1278
1279   g_return_val_if_fail (structure != NULL, FALSE);
1280   g_return_val_if_fail (fieldname != NULL, FALSE);
1281
1282   field = gst_structure_get_field (structure, fieldname);
1283
1284   if (field == NULL)
1285     return FALSE;
1286   if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
1287     return FALSE;
1288
1289   *value = gst_g_value_get_boolean_unchecked (&field->value);
1290
1291   return TRUE;
1292 }
1293
1294 /**
1295  * gst_structure_get_int:
1296  * @structure: a #GstStructure
1297  * @fieldname: the name of a field
1298  * @value: (out): a pointer to an int to set
1299  *
1300  * Sets the int 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 an int, this function
1306  * returns %FALSE.
1307  */
1308 gboolean
1309 gst_structure_get_int (const GstStructure * structure,
1310     const gchar * fieldname, gint * value)
1311 {
1312   GstStructureField *field;
1313
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);
1317
1318   field = gst_structure_get_field (structure, fieldname);
1319
1320   if (field == NULL)
1321     return FALSE;
1322   if (!G_VALUE_HOLDS_INT (&field->value))
1323     return FALSE;
1324
1325   *value = gst_g_value_get_int_unchecked (&field->value);
1326
1327   return TRUE;
1328 }
1329
1330 /**
1331  * gst_structure_get_uint:
1332  * @structure: a #GstStructure
1333  * @fieldname: the name of a field
1334  * @value: (out): a pointer to a uint to set
1335  *
1336  * Sets the uint pointed to by @value corresponding to the value of the
1337  * given field.  Caller is responsible for making sure the field exists
1338  * and has the correct type.
1339  *
1340  * Returns: %TRUE if the value could be set correctly. If there was no field
1341  * with @fieldname or the existing field did not contain a uint, this function
1342  * returns %FALSE.
1343  *
1344  * Since: 0.10.15
1345  */
1346 gboolean
1347 gst_structure_get_uint (const GstStructure * structure,
1348     const gchar * fieldname, guint * value)
1349 {
1350   GstStructureField *field;
1351
1352   g_return_val_if_fail (structure != NULL, FALSE);
1353   g_return_val_if_fail (fieldname != NULL, FALSE);
1354   g_return_val_if_fail (value != NULL, FALSE);
1355
1356   field = gst_structure_get_field (structure, fieldname);
1357
1358   if (field == NULL)
1359     return FALSE;
1360   if (!G_VALUE_HOLDS_UINT (&field->value))
1361     return FALSE;
1362
1363   *value = gst_g_value_get_uint_unchecked (&field->value);
1364
1365   return TRUE;
1366 }
1367
1368 /**
1369  * gst_structure_get_date:
1370  * @structure: a #GstStructure
1371  * @fieldname: the name of a field
1372  * @value: (out callee-allocates): a pointer to a #GDate to set
1373  *
1374  * Sets the date pointed to by @value corresponding to the date of the
1375  * given field.  Caller is responsible for making sure the field exists
1376  * and has the correct type.
1377  *
1378  * On success @value will point to a newly-allocated copy of the date which
1379  * should be freed with g_date_free() when no longer needed (note: this is
1380  * inconsistent with e.g. gst_structure_get_string() which doesn't return a
1381  * copy of the string).
1382  *
1383  * Returns: TRUE if the value could be set correctly. If there was no field
1384  * with @fieldname or the existing field did not contain a data, this function
1385  * returns FALSE.
1386  */
1387 gboolean
1388 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1389     GDate ** value)
1390 {
1391   GstStructureField *field;
1392
1393   g_return_val_if_fail (structure != NULL, FALSE);
1394   g_return_val_if_fail (fieldname != NULL, FALSE);
1395   g_return_val_if_fail (value != NULL, FALSE);
1396
1397   field = gst_structure_get_field (structure, fieldname);
1398
1399   if (field == NULL)
1400     return FALSE;
1401   if (!GST_VALUE_HOLDS_DATE (&field->value))
1402     return FALSE;
1403
1404   /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1405   *value = g_value_dup_boxed (&field->value);
1406
1407   return TRUE;
1408 }
1409
1410 /**
1411  * gst_structure_get_date_time:
1412  * @structure: a #GstStructure
1413  * @fieldname: the name of a field
1414  * @value: (out callee-allocates): a pointer to a #GstDateTime to set
1415  *
1416  * Sets the datetime pointed to by @value corresponding to the datetime of the
1417  * given field. Caller is responsible for making sure the field exists
1418  * and has the correct type.
1419  *
1420  * On success @value will point to a reference of the datetime which
1421  * should be unreffed with gst_date_time_unref() when no longer needed
1422  * (note: this is inconsistent with e.g. gst_structure_get_string()
1423  * which doesn't return a copy of the string).
1424  *
1425  * Returns: TRUE if the value could be set correctly. If there was no field
1426  * with @fieldname or the existing field did not contain a data, this function
1427  * returns FALSE.
1428  *
1429  * Since: 0.10.31
1430  */
1431 gboolean
1432 gst_structure_get_date_time (const GstStructure * structure,
1433     const gchar * fieldname, GstDateTime ** value)
1434 {
1435   GstStructureField *field;
1436
1437   g_return_val_if_fail (structure != NULL, FALSE);
1438   g_return_val_if_fail (fieldname != NULL, FALSE);
1439   g_return_val_if_fail (value != NULL, FALSE);
1440
1441   field = gst_structure_get_field (structure, fieldname);
1442
1443   if (field == NULL)
1444     return FALSE;
1445   if (!GST_VALUE_HOLDS_DATE_TIME (&field->value))
1446     return FALSE;
1447
1448   /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1449   *value = g_value_dup_boxed (&field->value);
1450
1451   return TRUE;
1452 }
1453
1454 /**
1455  * gst_structure_get_clock_time:
1456  * @structure: a #GstStructure
1457  * @fieldname: the name of a field
1458  * @value: (out): a pointer to a #GstClockTime to set
1459  *
1460  * Sets the clock time pointed to by @value corresponding to the clock time
1461  * of the given field.  Caller is responsible for making sure the field exists
1462  * and has the correct type.
1463  *
1464  * Returns: TRUE if the value could be set correctly. If there was no field
1465  * with @fieldname or the existing field did not contain a #GstClockTime, this
1466  * function returns FALSE.
1467  */
1468 gboolean
1469 gst_structure_get_clock_time (const GstStructure * structure,
1470     const gchar * fieldname, GstClockTime * value)
1471 {
1472   GstStructureField *field;
1473
1474   g_return_val_if_fail (structure != NULL, FALSE);
1475   g_return_val_if_fail (fieldname != NULL, FALSE);
1476   g_return_val_if_fail (value != NULL, FALSE);
1477
1478   field = gst_structure_get_field (structure, fieldname);
1479
1480   if (field == NULL)
1481     return FALSE;
1482   if (!G_VALUE_HOLDS_UINT64 (&field->value))
1483     return FALSE;
1484
1485   *value = gst_g_value_get_uint64_unchecked (&field->value);
1486
1487   return TRUE;
1488 }
1489
1490 /**
1491  * gst_structure_get_double:
1492  * @structure: a #GstStructure
1493  * @fieldname: the name of a field
1494  * @value: (out): a pointer to a gdouble to set
1495  *
1496  * Sets the double pointed to by @value corresponding to the value of the
1497  * given field.  Caller is responsible for making sure the field exists
1498  * and has the correct type.
1499  *
1500  * Returns: TRUE if the value could be set correctly. If there was no field
1501  * with @fieldname or the existing field did not contain a double, this
1502  * function returns FALSE.
1503  */
1504 gboolean
1505 gst_structure_get_double (const GstStructure * structure,
1506     const gchar * fieldname, gdouble * value)
1507 {
1508   GstStructureField *field;
1509
1510   g_return_val_if_fail (structure != NULL, FALSE);
1511   g_return_val_if_fail (fieldname != NULL, FALSE);
1512   g_return_val_if_fail (value != NULL, FALSE);
1513
1514   field = gst_structure_get_field (structure, fieldname);
1515
1516   if (field == NULL)
1517     return FALSE;
1518   if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1519     return FALSE;
1520
1521   *value = gst_g_value_get_double_unchecked (&field->value);
1522
1523   return TRUE;
1524 }
1525
1526 /**
1527  * gst_structure_get_string:
1528  * @structure: a #GstStructure
1529  * @fieldname: the name of a field
1530  *
1531  * Finds the field corresponding to @fieldname, and returns the string
1532  * contained in the field's value.  Caller is responsible for making
1533  * sure the field exists and has the correct type.
1534  *
1535  * The string should not be modified, and remains valid until the next
1536  * call to a gst_structure_*() function with the given structure.
1537  *
1538  * Returns: a pointer to the string or NULL when the field did not exist
1539  * or did not contain a string.
1540  */
1541 const gchar *
1542 gst_structure_get_string (const GstStructure * structure,
1543     const gchar * fieldname)
1544 {
1545   GstStructureField *field;
1546
1547   g_return_val_if_fail (structure != NULL, NULL);
1548   g_return_val_if_fail (fieldname != NULL, NULL);
1549
1550   field = gst_structure_get_field (structure, fieldname);
1551
1552   if (field == NULL)
1553     return NULL;
1554   if (!G_VALUE_HOLDS_STRING (&field->value))
1555     return NULL;
1556
1557   return gst_g_value_get_string_unchecked (&field->value);
1558 }
1559
1560 /**
1561  * gst_structure_get_enum:
1562  * @structure: a #GstStructure
1563  * @fieldname: the name of a field
1564  * @enumtype: the enum type of a field
1565  * @value: (out): a pointer to an int to set
1566  *
1567  * Sets the int pointed to by @value corresponding to the value of the
1568  * given field.  Caller is responsible for making sure the field exists,
1569  * has the correct type and that the enumtype is correct.
1570  *
1571  * Returns: TRUE if the value could be set correctly. If there was no field
1572  * with @fieldname or the existing field did not contain an enum of the given
1573  * type, this function returns FALSE.
1574  */
1575 gboolean
1576 gst_structure_get_enum (const GstStructure * structure,
1577     const gchar * fieldname, GType enumtype, gint * value)
1578 {
1579   GstStructureField *field;
1580
1581   g_return_val_if_fail (structure != NULL, FALSE);
1582   g_return_val_if_fail (fieldname != NULL, FALSE);
1583   g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1584   g_return_val_if_fail (value != NULL, FALSE);
1585
1586   field = gst_structure_get_field (structure, fieldname);
1587
1588   if (field == NULL)
1589     return FALSE;
1590   if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1591     return FALSE;
1592
1593   *value = g_value_get_enum (&field->value);
1594
1595   return TRUE;
1596 }
1597
1598 /**
1599  * gst_structure_get_fraction:
1600  * @structure: a #GstStructure
1601  * @fieldname: the name of a field
1602  * @value_numerator: (out): a pointer to an int to set
1603  * @value_denominator: (out): a pointer to an int to set
1604  *
1605  * Sets the integers pointed to by @value_numerator and @value_denominator
1606  * corresponding to the value of the given field.  Caller is responsible
1607  * for making sure the field exists and has the correct type.
1608  *
1609  * Returns: TRUE if the values could be set correctly. If there was no field
1610  * with @fieldname or the existing field did not contain a GstFraction, this
1611  * function returns FALSE.
1612  */
1613 gboolean
1614 gst_structure_get_fraction (const GstStructure * structure,
1615     const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1616 {
1617   GstStructureField *field;
1618
1619   g_return_val_if_fail (structure != NULL, FALSE);
1620   g_return_val_if_fail (fieldname != NULL, FALSE);
1621   g_return_val_if_fail (value_numerator != NULL, FALSE);
1622   g_return_val_if_fail (value_denominator != NULL, FALSE);
1623
1624   field = gst_structure_get_field (structure, fieldname);
1625
1626   if (field == NULL)
1627     return FALSE;
1628   if (!GST_VALUE_HOLDS_FRACTION (&field->value))
1629     return FALSE;
1630
1631   *value_numerator = gst_value_get_fraction_numerator (&field->value);
1632   *value_denominator = gst_value_get_fraction_denominator (&field->value);
1633
1634   return TRUE;
1635 }
1636
1637 typedef struct _GstStructureAbbreviation
1638 {
1639   const gchar *type_name;
1640   GType type;
1641 }
1642 GstStructureAbbreviation;
1643
1644 /* return a copy of an array of GstStructureAbbreviation containing all the
1645  * known type_string, GType maps, including abbreviations for common types */
1646 static GstStructureAbbreviation *
1647 gst_structure_get_abbrs (gint * n_abbrs)
1648 {
1649   static GstStructureAbbreviation *abbrs = NULL;
1650   static volatile gsize num = 0;
1651
1652   if (g_once_init_enter (&num)) {
1653     /* dynamically generate the array */
1654     gsize _num;
1655     GstStructureAbbreviation dyn_abbrs[] = {
1656       {"int", G_TYPE_INT}
1657       ,
1658       {"i", G_TYPE_INT}
1659       ,
1660       {"uint", G_TYPE_UINT}
1661       ,
1662       {"u", G_TYPE_UINT}
1663       ,
1664       {"float", G_TYPE_FLOAT}
1665       ,
1666       {"f", G_TYPE_FLOAT}
1667       ,
1668       {"double", G_TYPE_DOUBLE}
1669       ,
1670       {"d", G_TYPE_DOUBLE}
1671       ,
1672       {"buffer", GST_TYPE_BUFFER}
1673       ,
1674       {"fraction", GST_TYPE_FRACTION}
1675       ,
1676       {"boolean", G_TYPE_BOOLEAN}
1677       ,
1678       {"bool", G_TYPE_BOOLEAN}
1679       ,
1680       {"b", G_TYPE_BOOLEAN}
1681       ,
1682       {"string", G_TYPE_STRING}
1683       ,
1684       {"str", G_TYPE_STRING}
1685       ,
1686       {"s", G_TYPE_STRING}
1687       ,
1688       {"structure", GST_TYPE_STRUCTURE}
1689       ,
1690       {"date", GST_TYPE_DATE}
1691       ,
1692       {"datetime", GST_TYPE_DATE_TIME}
1693       ,
1694       {"bitmask", GST_TYPE_BITMASK}
1695     };
1696     _num = G_N_ELEMENTS (dyn_abbrs);
1697     /* permanently allocate and copy the array now */
1698     abbrs = g_new0 (GstStructureAbbreviation, _num);
1699     memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * _num);
1700     g_once_init_leave (&num, _num);
1701   }
1702   *n_abbrs = num;
1703
1704   return abbrs;
1705 }
1706
1707 /* given a type_name that could be a type abbreviation or a registered GType,
1708  * return a matching GType */
1709 static GType
1710 gst_structure_gtype_from_abbr (const char *type_name)
1711 {
1712   int i;
1713   GstStructureAbbreviation *abbrs;
1714   gint n_abbrs;
1715
1716   g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1717
1718   abbrs = gst_structure_get_abbrs (&n_abbrs);
1719
1720   for (i = 0; i < n_abbrs; i++) {
1721     if (strcmp (type_name, abbrs[i].type_name) == 0) {
1722       return abbrs[i].type;
1723     }
1724   }
1725
1726   /* this is the fallback */
1727   return g_type_from_name (type_name);
1728 }
1729
1730 static const char *
1731 gst_structure_to_abbr (GType type)
1732 {
1733   int i;
1734   GstStructureAbbreviation *abbrs;
1735   gint n_abbrs;
1736
1737   g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1738
1739   abbrs = gst_structure_get_abbrs (&n_abbrs);
1740
1741   for (i = 0; i < n_abbrs; i++) {
1742     if (type == abbrs[i].type) {
1743       return abbrs[i].type_name;
1744     }
1745   }
1746
1747   return g_type_name (type);
1748 }
1749
1750 static GType
1751 gst_structure_value_get_generic_type (GValue * val)
1752 {
1753   if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1754       || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1755     GArray *array = g_value_peek_pointer (val);
1756
1757     if (array->len > 0) {
1758       GValue *value = &g_array_index (array, GValue, 0);
1759
1760       return gst_structure_value_get_generic_type (value);
1761     } else {
1762       return G_TYPE_INT;
1763     }
1764   } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1765     return G_TYPE_INT;
1766   } else if (G_VALUE_TYPE (val) == GST_TYPE_INT64_RANGE) {
1767     return G_TYPE_INT64;
1768   } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1769     return G_TYPE_DOUBLE;
1770   } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1771     return GST_TYPE_FRACTION;
1772   }
1773   return G_VALUE_TYPE (val);
1774 }
1775
1776 gboolean
1777 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1778     GString * s)
1779 {
1780   GstStructureField *field;
1781   guint i, len;
1782
1783   g_return_val_if_fail (s != NULL, FALSE);
1784
1785   g_string_append (s, g_quark_to_string (structure->name));
1786   len = GST_STRUCTURE_FIELDS (structure)->len;
1787   for (i = 0; i < len; i++) {
1788     char *t;
1789     GType type;
1790
1791     field = GST_STRUCTURE_FIELD (structure, i);
1792
1793     t = gst_value_serialize (&field->value);
1794     type = gst_structure_value_get_generic_type (&field->value);
1795
1796     g_string_append_len (s, ", ", 2);
1797     /* FIXME: do we need to escape fieldnames? */
1798     g_string_append (s, g_quark_to_string (field->name));
1799     g_string_append_len (s, "=(", 2);
1800     g_string_append (s, gst_structure_to_abbr (type));
1801     g_string_append_c (s, ')');
1802     g_string_append (s, t == NULL ? "NULL" : t);
1803     g_free (t);
1804   }
1805
1806   g_string_append_c (s, ';');
1807   return TRUE;
1808 }
1809
1810 /**
1811  * gst_structure_to_string:
1812  * @structure: a #GstStructure
1813  *
1814  * Converts @structure to a human-readable string representation.
1815  *
1816  * For debugging purposes its easier to do something like this:
1817  * |[
1818  * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1819  * ]|
1820  * This prints the structure in human readble form.
1821  *
1822  * Free-function: g_free
1823  *
1824  * Returns: (transfer full)L a pointer to string allocated by g_malloc().
1825  *     g_free() after usage.
1826  */
1827 gchar *
1828 gst_structure_to_string (const GstStructure * structure)
1829 {
1830   GString *s;
1831
1832   /* NOTE:  This function is potentially called by the debug system,
1833    * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1834    * should be careful to avoid recursion.  This includes any functions
1835    * called by gst_structure_to_string.  In particular, calls should
1836    * not use the GST_PTR_FORMAT extension.  */
1837
1838   g_return_val_if_fail (structure != NULL, NULL);
1839
1840   /* we estimate a minimum size based on the number of fields in order to
1841    * avoid unnecessary reallocs within GString */
1842   s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1843   priv_gst_structure_append_to_gstring (structure, s);
1844   return g_string_free (s, FALSE);
1845 }
1846
1847 /*
1848  * r will still point to the string. if end == next, the string will not be
1849  * null-terminated. In all other cases it will be.
1850  * end = pointer to char behind end of string, next = pointer to start of
1851  * unread data.
1852  * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1853  */
1854 static gboolean
1855 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next,
1856     gboolean unescape)
1857 {
1858   gchar *w;
1859
1860   if (*s == 0)
1861     return FALSE;
1862
1863   if (*s != '"') {
1864     int ret;
1865
1866     ret = gst_structure_parse_simple_string (s, end);
1867     *next = *end;
1868
1869     return ret;
1870   }
1871
1872   if (unescape) {
1873     w = s;
1874     s++;
1875     while (*s != '"') {
1876       if (G_UNLIKELY (*s == 0))
1877         return FALSE;
1878       if (G_UNLIKELY (*s == '\\'))
1879         s++;
1880       *w = *s;
1881       w++;
1882       s++;
1883     }
1884     s++;
1885   } else {
1886     /* Find the closing quotes */
1887     s++;
1888     while (*s != '"') {
1889       if (G_UNLIKELY (*s == 0))
1890         return FALSE;
1891       if (G_UNLIKELY (*s == '\\'))
1892         s++;
1893       s++;
1894     }
1895     s++;
1896     w = s;
1897   }
1898
1899   *end = w;
1900   *next = s;
1901
1902   return TRUE;
1903 }
1904
1905 static gboolean
1906 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1907     GType type)
1908 {
1909   GValue value1 = { 0 };
1910   GValue value2 = { 0 };
1911   GType range_type;
1912   gboolean ret;
1913
1914   if (*s != '[')
1915     return FALSE;
1916   s++;
1917
1918   ret = gst_structure_parse_value (s, &s, &value1, type);
1919   if (ret == FALSE)
1920     return FALSE;
1921
1922   while (g_ascii_isspace (*s))
1923     s++;
1924
1925   if (*s != ',')
1926     return FALSE;
1927   s++;
1928
1929   while (g_ascii_isspace (*s))
1930     s++;
1931
1932   ret = gst_structure_parse_value (s, &s, &value2, type);
1933   if (ret == FALSE)
1934     return FALSE;
1935
1936   while (g_ascii_isspace (*s))
1937     s++;
1938
1939   if (*s != ']')
1940     return FALSE;
1941   s++;
1942
1943   if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1944     return FALSE;
1945
1946   if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1947     range_type = GST_TYPE_DOUBLE_RANGE;
1948     g_value_init (value, range_type);
1949     gst_value_set_double_range (value,
1950         gst_g_value_get_double_unchecked (&value1),
1951         gst_g_value_get_double_unchecked (&value2));
1952   } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1953     range_type = GST_TYPE_INT_RANGE;
1954     g_value_init (value, range_type);
1955     gst_value_set_int_range (value, gst_g_value_get_int_unchecked (&value1),
1956         gst_g_value_get_int_unchecked (&value2));
1957   } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
1958     range_type = GST_TYPE_INT64_RANGE;
1959     g_value_init (value, range_type);
1960     gst_value_set_int64_range (value, gst_g_value_get_int64_unchecked (&value1),
1961         gst_g_value_get_int64_unchecked (&value2));
1962   } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
1963     range_type = GST_TYPE_FRACTION_RANGE;
1964     g_value_init (value, range_type);
1965     gst_value_set_fraction_range (value, &value1, &value2);
1966   } else {
1967     return FALSE;
1968   }
1969
1970   *after = s;
1971   return TRUE;
1972 }
1973
1974 static gboolean
1975 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
1976     GType type, GType list_type, char begin, char end)
1977 {
1978   GValue list_value = { 0 };
1979   gboolean ret;
1980   GArray *array;
1981
1982   g_value_init (value, list_type);
1983   array = g_value_peek_pointer (value);
1984
1985   if (*s != begin)
1986     return FALSE;
1987   s++;
1988
1989   while (g_ascii_isspace (*s))
1990     s++;
1991   if (*s == end) {
1992     s++;
1993     *after = s;
1994     return TRUE;
1995   }
1996
1997   ret = gst_structure_parse_value (s, &s, &list_value, type);
1998   if (ret == FALSE)
1999     return FALSE;
2000
2001   g_array_append_val (array, list_value);
2002
2003   while (g_ascii_isspace (*s))
2004     s++;
2005
2006   while (*s != end) {
2007     if (*s != ',')
2008       return FALSE;
2009     s++;
2010
2011     while (g_ascii_isspace (*s))
2012       s++;
2013
2014     memset (&list_value, 0, sizeof (list_value));
2015     ret = gst_structure_parse_value (s, &s, &list_value, type);
2016     if (ret == FALSE)
2017       return FALSE;
2018
2019     g_array_append_val (array, list_value);
2020     while (g_ascii_isspace (*s))
2021       s++;
2022   }
2023
2024   s++;
2025
2026   *after = s;
2027   return TRUE;
2028 }
2029
2030 static gboolean
2031 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
2032 {
2033   return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
2034       '{', '}');
2035 }
2036
2037 static gboolean
2038 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
2039     GType type)
2040 {
2041   return gst_structure_parse_any_list (s, after, value, type,
2042       GST_TYPE_ARRAY, '<', '>');
2043 }
2044
2045 static gboolean
2046 gst_structure_parse_simple_string (gchar * str, gchar ** end)
2047 {
2048   char *s = str;
2049
2050   while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
2051     s++;
2052   }
2053
2054   *end = s;
2055
2056   return (s != str);
2057 }
2058
2059 static gboolean
2060 gst_structure_parse_field (gchar * str,
2061     gchar ** after, GstStructureField * field)
2062 {
2063   gchar *name;
2064   gchar *name_end;
2065   gchar *s;
2066   gchar c;
2067
2068   s = str;
2069
2070   while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2071     s++;
2072   name = s;
2073   if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &name_end))) {
2074     GST_WARNING ("failed to parse simple string, str=%s", str);
2075     return FALSE;
2076   }
2077
2078   s = name_end;
2079   while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2080     s++;
2081
2082   if (G_UNLIKELY (*s != '=')) {
2083     GST_WARNING ("missing assignment operator in the field, str=%s", str);
2084     return FALSE;
2085   }
2086   s++;
2087
2088   c = *name_end;
2089   *name_end = '\0';
2090   field->name = g_quark_from_string (name);
2091   GST_DEBUG ("trying field name '%s'", name);
2092   *name_end = c;
2093
2094   if (G_UNLIKELY (!gst_structure_parse_value (s, &s, &field->value,
2095               G_TYPE_INVALID))) {
2096     GST_WARNING ("failed to parse value %s", str);
2097     return FALSE;
2098   }
2099
2100   *after = s;
2101   return TRUE;
2102 }
2103
2104 static gboolean
2105 gst_structure_parse_value (gchar * str,
2106     gchar ** after, GValue * value, GType default_type)
2107 {
2108   gchar *type_name;
2109   gchar *type_end;
2110   gchar *value_s;
2111   gchar *value_end;
2112   gchar *s;
2113   gchar c;
2114   int ret = 0;
2115   GType type = default_type;
2116
2117   s = str;
2118   while (g_ascii_isspace (*s))
2119     s++;
2120
2121   /* check if there's a (type_name) 'cast' */
2122   type_name = NULL;
2123   if (*s == '(') {
2124     s++;
2125     while (g_ascii_isspace (*s))
2126       s++;
2127     type_name = s;
2128     if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &type_end)))
2129       return FALSE;
2130     s = type_end;
2131     while (g_ascii_isspace (*s))
2132       s++;
2133     if (G_UNLIKELY (*s != ')'))
2134       return FALSE;
2135     s++;
2136     while (g_ascii_isspace (*s))
2137       s++;
2138
2139     c = *type_end;
2140     *type_end = 0;
2141     type = gst_structure_gtype_from_abbr (type_name);
2142     GST_DEBUG ("trying type name '%s'", type_name);
2143     *type_end = c;
2144
2145     if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2146       GST_WARNING ("invalid type");
2147       return FALSE;
2148     }
2149   }
2150
2151   while (g_ascii_isspace (*s))
2152     s++;
2153   if (*s == '[') {
2154     ret = gst_structure_parse_range (s, &s, value, type);
2155   } else if (*s == '{') {
2156     ret = gst_structure_parse_list (s, &s, value, type);
2157   } else if (*s == '<') {
2158     ret = gst_structure_parse_array (s, &s, value, type);
2159   } else {
2160     value_s = s;
2161
2162     if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2163       GType try_types[] =
2164           { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_BOOLEAN,
2165         G_TYPE_STRING
2166       };
2167       int i;
2168
2169       if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s, TRUE)))
2170         return FALSE;
2171       /* Set NULL terminator for deserialization */
2172       c = *value_end;
2173       *value_end = '\0';
2174
2175       for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2176         g_value_init (value, try_types[i]);
2177         ret = gst_value_deserialize (value, value_s);
2178         if (ret)
2179           break;
2180         g_value_unset (value);
2181       }
2182     } else {
2183       g_value_init (value, type);
2184
2185       if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s,
2186                   (type != G_TYPE_STRING))))
2187         return FALSE;
2188       /* Set NULL terminator for deserialization */
2189       c = *value_end;
2190       *value_end = '\0';
2191
2192       ret = gst_value_deserialize (value, value_s);
2193       if (G_UNLIKELY (!ret))
2194         g_value_unset (value);
2195     }
2196     *value_end = c;
2197   }
2198
2199   *after = s;
2200
2201   return ret;
2202 }
2203
2204 /**
2205  * gst_structure_from_string:
2206  * @string: a string representation of a #GstStructure.
2207  * @end: (out) (allow-none): pointer to store the end of the string in.
2208  *
2209  * Creates a #GstStructure from a string representation.
2210  * If end is not NULL, a pointer to the place inside the given string
2211  * where parsing ended will be returned.
2212  *
2213  * Free-function: gst_structure_free
2214  *
2215  * Returns: (transfer full): a new #GstStructure or NULL when the string could
2216  *     not be parsed. Free with gst_structure_free() after use.
2217  */
2218 GstStructure *
2219 gst_structure_from_string (const gchar * string, gchar ** end)
2220 {
2221   char *name;
2222   char *copy;
2223   char *w;
2224   char *r;
2225   char save;
2226   GstStructure *structure = NULL;
2227   GstStructureField field;
2228
2229   g_return_val_if_fail (string != NULL, NULL);
2230
2231   copy = g_strdup (string);
2232   r = copy;
2233
2234   /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
2235   while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2236               && g_ascii_isspace (r[1]))))
2237     r++;
2238
2239   name = r;
2240   if (G_UNLIKELY (!gst_structure_parse_string (r, &w, &r, TRUE))) {
2241     GST_WARNING ("Failed to parse structure string '%s'", string);
2242     goto error;
2243   }
2244
2245   save = *w;
2246   *w = '\0';
2247   structure = gst_structure_new_empty (name);
2248   *w = save;
2249
2250   if (G_UNLIKELY (structure == NULL))
2251     goto error;
2252
2253   do {
2254     while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2255                 && g_ascii_isspace (r[1]))))
2256       r++;
2257     if (*r == ';') {
2258       /* end of structure, get the next char and finish */
2259       r++;
2260       break;
2261     }
2262     if (*r == '\0') {
2263       /* accept \0 as end delimiter */
2264       break;
2265     }
2266     if (G_UNLIKELY (*r != ',')) {
2267       GST_WARNING ("Failed to find delimiter, r=%s", r);
2268       goto error;
2269     }
2270     r++;
2271     while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2272                 && g_ascii_isspace (r[1]))))
2273       r++;
2274
2275     memset (&field, 0, sizeof (field));
2276     if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
2277       GST_WARNING ("Failed to parse field, r=%s", r);
2278       goto error;
2279     }
2280     gst_structure_set_field (structure, &field);
2281   } while (TRUE);
2282
2283   if (end)
2284     *end = (char *) string + (r - copy);
2285   else if (*r)
2286     g_warning ("gst_structure_from_string did not consume whole string,"
2287         " but caller did not provide end pointer (\"%s\")", string);
2288
2289   g_free (copy);
2290   return structure;
2291
2292 error:
2293   if (structure)
2294     gst_structure_free (structure);
2295   g_free (copy);
2296   return NULL;
2297 }
2298
2299 static void
2300 gst_structure_transform_to_string (const GValue * src_value,
2301     GValue * dest_value)
2302 {
2303   g_return_if_fail (src_value != NULL);
2304   g_return_if_fail (dest_value != NULL);
2305
2306   dest_value->data[0].v_pointer =
2307       gst_structure_to_string (src_value->data[0].v_pointer);
2308 }
2309
2310 static GstStructure *
2311 gst_structure_copy_conditional (const GstStructure * structure)
2312 {
2313   if (structure)
2314     return gst_structure_copy (structure);
2315   return NULL;
2316 }
2317
2318 /* fixate utility functions */
2319
2320 /**
2321  * gst_structure_fixate_field_nearest_int:
2322  * @structure: a #GstStructure
2323  * @field_name: a field in @structure
2324  * @target: the target value of the fixation
2325  *
2326  * Fixates a #GstStructure by changing the given field to the nearest
2327  * integer to @target that is a subset of the existing field.
2328  *
2329  * Returns: TRUE if the structure could be fixated
2330  */
2331 gboolean
2332 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2333     const char *field_name, int target)
2334 {
2335   const GValue *value;
2336
2337   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2338   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2339
2340   value = gst_structure_get_value (structure, field_name);
2341
2342   if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2343     /* already fixed */
2344     return FALSE;
2345   } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2346     int x;
2347
2348     x = gst_value_get_int_range_min (value);
2349     if (target < x)
2350       target = x;
2351     x = gst_value_get_int_range_max (value);
2352     if (target > x)
2353       target = x;
2354     gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2355     return TRUE;
2356   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2357     const GValue *list_value;
2358     int i, n;
2359     int best = 0;
2360     int best_index = -1;
2361
2362     n = gst_value_list_get_size (value);
2363     for (i = 0; i < n; i++) {
2364       list_value = gst_value_list_get_value (value, i);
2365       if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2366         int x = gst_g_value_get_int_unchecked (list_value);
2367
2368         if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2369           best_index = i;
2370           best = x;
2371         }
2372       }
2373     }
2374     if (best_index != -1) {
2375       gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2376       return TRUE;
2377     }
2378     return FALSE;
2379   }
2380
2381   return FALSE;
2382 }
2383
2384 /**
2385  * gst_structure_fixate_field_nearest_double:
2386  * @structure: a #GstStructure
2387  * @field_name: a field in @structure
2388  * @target: the target value of the fixation
2389  *
2390  * Fixates a #GstStructure by changing the given field to the nearest
2391  * double to @target that is a subset of the existing field.
2392  *
2393  * Returns: TRUE if the structure could be fixated
2394  */
2395 gboolean
2396 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2397     const char *field_name, double target)
2398 {
2399   const GValue *value;
2400
2401   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2402   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2403
2404   value = gst_structure_get_value (structure, field_name);
2405
2406   if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2407     /* already fixed */
2408     return FALSE;
2409   } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2410     double x;
2411
2412     x = gst_value_get_double_range_min (value);
2413     if (target < x)
2414       target = x;
2415     x = gst_value_get_double_range_max (value);
2416     if (target > x)
2417       target = x;
2418     gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2419     return TRUE;
2420   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2421     const GValue *list_value;
2422     int i, n;
2423     double best = 0;
2424     int best_index = -1;
2425
2426     n = gst_value_list_get_size (value);
2427     for (i = 0; i < n; i++) {
2428       list_value = gst_value_list_get_value (value, i);
2429       if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2430         double x = gst_g_value_get_double_unchecked (list_value);
2431
2432         if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2433           best_index = i;
2434           best = x;
2435         }
2436       }
2437     }
2438     if (best_index != -1) {
2439       gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2440       return TRUE;
2441     }
2442     return FALSE;
2443   }
2444
2445   return FALSE;
2446
2447 }
2448
2449 /**
2450  * gst_structure_fixate_field_boolean:
2451  * @structure: a #GstStructure
2452  * @field_name: a field in @structure
2453  * @target: the target value of the fixation
2454  *
2455  * Fixates a #GstStructure by changing the given @field_name field to the given
2456  * @target boolean if that field is not fixed yet.
2457  *
2458  * Returns: TRUE if the structure could be fixated
2459  */
2460 gboolean
2461 gst_structure_fixate_field_boolean (GstStructure * structure,
2462     const char *field_name, gboolean target)
2463 {
2464   const GValue *value;
2465
2466   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2467   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2468
2469   value = gst_structure_get_value (structure, field_name);
2470
2471   if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2472     /* already fixed */
2473     return FALSE;
2474   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2475     const GValue *list_value;
2476     int i, n;
2477     int best = 0;
2478     int best_index = -1;
2479
2480     n = gst_value_list_get_size (value);
2481     for (i = 0; i < n; i++) {
2482       list_value = gst_value_list_get_value (value, i);
2483       if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2484         gboolean x = gst_g_value_get_boolean_unchecked (list_value);
2485
2486         if (best_index == -1 || x == target) {
2487           best_index = i;
2488           best = x;
2489         }
2490       }
2491     }
2492     if (best_index != -1) {
2493       gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2494       return TRUE;
2495     }
2496     return FALSE;
2497   }
2498
2499   return FALSE;
2500 }
2501
2502 /**
2503  * gst_structure_fixate_field_string:
2504  * @structure: a #GstStructure
2505  * @field_name: a field in @structure
2506  * @target: the target value of the fixation
2507  *
2508  * Fixates a #GstStructure by changing the given @field_name field to the given
2509  * @target string if that field is not fixed yet.
2510  *
2511  * Returns: TRUE if the structure could be fixated
2512  *
2513  * Since: 0.10.30
2514  */
2515 gboolean
2516 gst_structure_fixate_field_string (GstStructure * structure,
2517     const gchar * field_name, const gchar * target)
2518 {
2519   const GValue *value;
2520
2521   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2522   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2523
2524   value = gst_structure_get_value (structure, field_name);
2525
2526   if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
2527     /* already fixed */
2528     return FALSE;
2529   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2530     const GValue *list_value;
2531     int i, n;
2532     const gchar *best = NULL;
2533     int best_index = -1;
2534
2535     n = gst_value_list_get_size (value);
2536     for (i = 0; i < n; i++) {
2537       list_value = gst_value_list_get_value (value, i);
2538       if (G_VALUE_TYPE (list_value) == G_TYPE_STRING) {
2539         const gchar *x = g_value_get_string (list_value);
2540
2541         if (best_index == -1 || g_str_equal (x, target)) {
2542           best_index = i;
2543           best = x;
2544         }
2545       }
2546     }
2547     if (best_index != -1) {
2548       gst_structure_set (structure, field_name, G_TYPE_STRING, best, NULL);
2549       return TRUE;
2550     }
2551     return FALSE;
2552   }
2553
2554   return FALSE;
2555 }
2556
2557 /**
2558  * gst_structure_fixate_field_nearest_fraction:
2559  * @structure: a #GstStructure
2560  * @field_name: a field in @structure
2561  * @target_numerator: The numerator of the target value of the fixation
2562  * @target_denominator: The denominator of the target value of the fixation
2563  *
2564  * Fixates a #GstStructure by changing the given field to the nearest
2565  * fraction to @target_numerator/@target_denominator that is a subset
2566  * of the existing field.
2567  *
2568  * Returns: TRUE if the structure could be fixated
2569  */
2570 gboolean
2571 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2572     const char *field_name, const gint target_numerator,
2573     const gint target_denominator)
2574 {
2575   const GValue *value;
2576
2577   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2578   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2579
2580   value = gst_structure_get_value (structure, field_name);
2581
2582   if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2583     /* already fixed */
2584     return FALSE;
2585   } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2586     const GValue *x, *new_value;
2587     GValue target = { 0 };
2588     g_value_init (&target, GST_TYPE_FRACTION);
2589     gst_value_set_fraction (&target, target_numerator, target_denominator);
2590
2591     new_value = &target;
2592     x = gst_value_get_fraction_range_min (value);
2593     if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2594       new_value = x;
2595     x = gst_value_get_fraction_range_max (value);
2596     if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2597       new_value = x;
2598
2599     gst_structure_set_value (structure, field_name, new_value);
2600     g_value_unset (&target);
2601     return TRUE;
2602   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2603     const GValue *list_value;
2604     int i, n;
2605     const GValue *best = NULL;
2606     gdouble target;
2607     gdouble cur_diff;
2608     gdouble best_diff = G_MAXDOUBLE;
2609
2610     target = (gdouble) target_numerator / (gdouble) target_denominator;
2611
2612     GST_DEBUG ("target %g, best %g", target, best_diff);
2613
2614     best = NULL;
2615
2616     n = gst_value_list_get_size (value);
2617     for (i = 0; i < n; i++) {
2618       list_value = gst_value_list_get_value (value, i);
2619       if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2620         gint num, denom;
2621         gdouble list_double;
2622
2623         num = gst_value_get_fraction_numerator (list_value);
2624         denom = gst_value_get_fraction_denominator (list_value);
2625
2626         list_double = ((gdouble) num / (gdouble) denom);
2627         cur_diff = target - list_double;
2628
2629         GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2630
2631         if (cur_diff < 0)
2632           cur_diff = -cur_diff;
2633
2634         if (!best || cur_diff < best_diff) {
2635           GST_DEBUG ("new best %g", list_double);
2636           best = list_value;
2637           best_diff = cur_diff;
2638         }
2639       }
2640     }
2641     if (best != NULL) {
2642       gst_structure_set_value (structure, field_name, best);
2643       return TRUE;
2644     }
2645   }
2646
2647   return FALSE;
2648 }
2649
2650 static gboolean
2651 default_fixate (GQuark field_id, const GValue * value, gpointer data)
2652 {
2653   GstStructure *s = data;
2654   GValue v = { 0 };
2655
2656   if (gst_value_fixate (&v, value)) {
2657     gst_structure_id_set_value (s, field_id, &v);
2658     g_value_unset (&v);
2659   }
2660   return TRUE;
2661 }
2662
2663 /**
2664  * gst_structure_fixate_field:
2665  * @structure: a #GstStructure
2666  * @field_name: a field in @structure
2667  *
2668  * Fixates a #GstStructure by changing the given field with its fixated value.
2669  *
2670  * Returns: TRUE if the structure field could be fixated
2671  */
2672 gboolean
2673 gst_structure_fixate_field (GstStructure * structure, const char *field_name)
2674 {
2675   GstStructureField *field;
2676
2677   g_return_val_if_fail (structure != NULL, FALSE);
2678   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2679
2680   if (!(field = gst_structure_get_field (structure, field_name)))
2681     return FALSE;
2682
2683   return default_fixate (field->name, &field->value, structure);
2684 }
2685
2686 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2687  * (useful for message parsing functions where the return location is user
2688  * supplied and the user may pass NULL if the value isn't of interest) */
2689 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname)           \
2690 G_STMT_START {                                                                \
2691   const GValue *_value = (value);                                             \
2692   guint _flags = (flags);                                                     \
2693   GType _value_type = G_VALUE_TYPE (_value);                                  \
2694   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);           \
2695   gchar *_lcopy_format = _vtable->lcopy_format;                               \
2696   GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };      \
2697   guint _n_values = 0;                                                        \
2698                                                                               \
2699   while (*_lcopy_format != '\0') {                                            \
2700     g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER);                     \
2701     _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer);          \
2702     _lcopy_format++;                                                          \
2703   }                                                                           \
2704   if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2705     *(__error) = g_strdup_printf ("either all or none of the return "         \
2706         "locations for field '%s' need to be NULL", fieldname);               \
2707   } else if (_cvalues[0].v_pointer != NULL) {                                 \
2708     *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags);  \
2709   }                                                                           \
2710 } G_STMT_END
2711
2712 /**
2713  * gst_structure_get_valist:
2714  * @structure: a #GstStructure
2715  * @first_fieldname: the name of the first field to read
2716  * @args: variable arguments
2717  *
2718  * Parses the variable arguments and reads fields from @structure accordingly.
2719  * valist-variant of gst_structure_get(). Look at the documentation of
2720  * gst_structure_get() for more details.
2721  *
2722  * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2723  *
2724  * Since: 0.10.24
2725  */
2726 gboolean
2727 gst_structure_get_valist (const GstStructure * structure,
2728     const char *first_fieldname, va_list args)
2729 {
2730   const char *field_name;
2731   GType expected_type = G_TYPE_INVALID;
2732
2733   g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2734   g_return_val_if_fail (first_fieldname != NULL, FALSE);
2735
2736   field_name = first_fieldname;
2737   while (field_name) {
2738     const GValue *val = NULL;
2739     gchar *err = NULL;
2740
2741     expected_type = va_arg (args, GType);
2742
2743     val = gst_structure_get_value (structure, field_name);
2744
2745     if (val == NULL)
2746       goto no_such_field;
2747
2748     if (G_VALUE_TYPE (val) != expected_type)
2749       goto wrong_type;
2750
2751     GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2752     if (err) {
2753       g_warning ("%s: %s", G_STRFUNC, err);
2754       g_free (err);
2755       return FALSE;
2756     }
2757
2758     field_name = va_arg (args, const gchar *);
2759   }
2760
2761   return TRUE;
2762
2763 /* ERRORS */
2764 no_such_field:
2765   {
2766     GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2767         field_name, structure);
2768     return FALSE;
2769   }
2770 wrong_type:
2771   {
2772     GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
2773         "field was of type '%s': %" GST_PTR_FORMAT, field_name,
2774         GST_STR_NULL (g_type_name (expected_type)),
2775         G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
2776         structure);
2777     return FALSE;
2778   }
2779 }
2780
2781 /**
2782  * gst_structure_id_get_valist:
2783  * @structure: a #GstStructure
2784  * @first_field_id: the quark of the first field to read
2785  * @args: variable arguments
2786  *
2787  * Parses the variable arguments and reads fields from @structure accordingly.
2788  * valist-variant of gst_structure_id_get(). Look at the documentation of
2789  * gst_structure_id_get() for more details.
2790  *
2791  * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2792  *
2793  * Since: 0.10.24
2794  */
2795 gboolean
2796 gst_structure_id_get_valist (const GstStructure * structure,
2797     GQuark first_field_id, va_list args)
2798 {
2799   GQuark field_id;
2800   GType expected_type = G_TYPE_INVALID;
2801
2802   g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2803   g_return_val_if_fail (first_field_id != 0, FALSE);
2804
2805   field_id = first_field_id;
2806   while (field_id) {
2807     const GValue *val = NULL;
2808     gchar *err = NULL;
2809
2810     expected_type = va_arg (args, GType);
2811
2812     val = gst_structure_id_get_value (structure, field_id);
2813
2814     if (val == NULL)
2815       goto no_such_field;
2816
2817     if (G_VALUE_TYPE (val) != expected_type)
2818       goto wrong_type;
2819
2820     GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
2821     if (err) {
2822       g_warning ("%s: %s", G_STRFUNC, err);
2823       g_free (err);
2824       return FALSE;
2825     }
2826
2827     field_id = va_arg (args, GQuark);
2828   }
2829
2830   return TRUE;
2831
2832 /* ERRORS */
2833 no_such_field:
2834   {
2835     GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2836         GST_STR_NULL (g_quark_to_string (field_id)), structure);
2837     return FALSE;
2838   }
2839 wrong_type:
2840   {
2841     GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
2842         "field was of type '%s': %" GST_PTR_FORMAT,
2843         g_quark_to_string (field_id),
2844         GST_STR_NULL (g_type_name (expected_type)),
2845         G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
2846         structure);
2847     return FALSE;
2848   }
2849 }
2850
2851 /**
2852  * gst_structure_get:
2853  * @structure: a #GstStructure
2854  * @first_fieldname: the name of the first field to read
2855  * @...: variable arguments
2856  *
2857  * Parses the variable arguments and reads fields from @structure accordingly.
2858  * Variable arguments should be in the form field name, field type
2859  * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2860  * The last variable argument should be NULL.
2861  *
2862  * For refcounted (mini)objects you will acquire your own reference which
2863  * you must release with a suitable _unref() when no longer needed. For
2864  * strings and boxed types you will acquire a copy which you will need to
2865  * release with either g_free() or the suitable function for the boxed type.
2866  *
2867  * Returns: FALSE if there was a problem reading any of the fields (e.g.
2868  *     because the field requested did not exist, or was of a type other
2869  *     than the type specified), otherwise TRUE.
2870  *
2871  * Since: 0.10.24
2872  */
2873 gboolean
2874 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
2875     ...)
2876 {
2877   gboolean ret;
2878   va_list args;
2879
2880   g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2881   g_return_val_if_fail (first_fieldname != NULL, FALSE);
2882
2883   va_start (args, first_fieldname);
2884   ret = gst_structure_get_valist (structure, first_fieldname, args);
2885   va_end (args);
2886
2887   return ret;
2888 }
2889
2890 /**
2891  * gst_structure_id_get:
2892  * @structure: a #GstStructure
2893  * @first_field_id: the quark of the first field to read
2894  * @...: variable arguments
2895  *
2896  * Parses the variable arguments and reads fields from @structure accordingly.
2897  * Variable arguments should be in the form field id quark, field type
2898  * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2899  * The last variable argument should be NULL (technically it should be a
2900  * 0 quark, but we require NULL so compilers that support it can check for
2901  * the NULL terminator and warn if it's not there).
2902  *
2903  * This function is just like gst_structure_get() only that it is slightly
2904  * more efficient since it saves the string-to-quark lookup in the global
2905  * quark hashtable.
2906  *
2907  * For refcounted (mini)objects you will acquire your own reference which
2908  * you must release with a suitable _unref() when no longer needed. For
2909  * strings and boxed types you will acquire a copy which you will need to
2910  * release with either g_free() or the suitable function for the boxed type.
2911  *
2912  * Returns: FALSE if there was a problem reading any of the fields (e.g.
2913  *     because the field requested did not exist, or was of a type other
2914  *     than the type specified), otherwise TRUE.
2915  *
2916  * Since: 0.10.24
2917  */
2918 gboolean
2919 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
2920     ...)
2921 {
2922   gboolean ret;
2923   va_list args;
2924
2925   g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2926   g_return_val_if_fail (first_field_id != 0, FALSE);
2927
2928   va_start (args, first_field_id);
2929   ret = gst_structure_id_get_valist (structure, first_field_id, args);
2930   va_end (args);
2931
2932   return ret;
2933 }
2934
2935 static gboolean
2936 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
2937     gpointer data)
2938 {
2939   const GstStructure *struct1 = (const GstStructure *) data;
2940   const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
2941
2942   if (G_UNLIKELY (val1 == NULL))
2943     return FALSE;
2944   if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
2945     return TRUE;
2946   }
2947
2948   return FALSE;
2949 }
2950
2951 /**
2952  * gst_structure_is_equal:
2953  * @structure1: a #GstStructure.
2954  * @structure2: a #GstStructure.
2955  *
2956  * Tests if the two #GstStructure are equal.
2957  *
2958  * Returns: TRUE if the two structures have the same name and field.
2959  *
2960  * Since: 0.10.36
2961  **/
2962 gboolean
2963 gst_structure_is_equal (const GstStructure * structure1,
2964     const GstStructure * structure2)
2965 {
2966   g_return_val_if_fail (GST_IS_STRUCTURE (structure1), FALSE);
2967   g_return_val_if_fail (GST_IS_STRUCTURE (structure2), FALSE);
2968
2969   if (G_UNLIKELY (structure1 == structure2))
2970     return TRUE;
2971
2972   if (structure1->name != structure2->name) {
2973     return FALSE;
2974   }
2975   if (GST_STRUCTURE_FIELDS (structure1)->len !=
2976       GST_STRUCTURE_FIELDS (structure2)->len) {
2977     return FALSE;
2978   }
2979
2980   return gst_structure_foreach (structure1, gst_structure_is_equal_foreach,
2981       (gpointer) structure2);
2982 }
2983
2984
2985 typedef struct
2986 {
2987   GstStructure *dest;
2988   const GstStructure *intersect;
2989 }
2990 IntersectData;
2991
2992 static gboolean
2993 gst_structure_intersect_field1 (GQuark id, const GValue * val1, gpointer data)
2994 {
2995   IntersectData *idata = (IntersectData *) data;
2996   const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
2997
2998   if (G_UNLIKELY (val2 == NULL)) {
2999     gst_structure_id_set_value (idata->dest, id, val1);
3000   } else {
3001     GValue dest_value = { 0 };
3002     if (gst_value_intersect (&dest_value, val1, val2)) {
3003       gst_structure_id_set_value (idata->dest, id, &dest_value);
3004       g_value_unset (&dest_value);
3005     } else {
3006       return FALSE;
3007     }
3008   }
3009   return TRUE;
3010 }
3011
3012 static gboolean
3013 gst_structure_intersect_field2 (GQuark id, const GValue * val1, gpointer data)
3014 {
3015   IntersectData *idata = (IntersectData *) data;
3016   const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3017
3018   if (G_UNLIKELY (val2 == NULL)) {
3019     gst_structure_id_set_value (idata->dest, id, val1);
3020   }
3021   return TRUE;
3022 }
3023
3024 /**
3025  * gst_structure_intersect:
3026  * @struct1: a #GstStructure
3027  * @struct2: a #GstStructure
3028  *
3029  * Interesects @struct1 and @struct2 and returns the intersection.
3030  *
3031  * Returns: Intersection of @struct1 and @struct2
3032  *
3033  * Since: 0.10.36
3034  */
3035 GstStructure *
3036 gst_structure_intersect (const GstStructure * struct1,
3037     const GstStructure * struct2)
3038 {
3039   IntersectData data;
3040
3041   g_assert (struct1 != NULL);
3042   g_assert (struct2 != NULL);
3043
3044   if (G_UNLIKELY (struct1->name != struct2->name))
3045     return NULL;
3046
3047   /* copy fields from struct1 which we have not in struct2 to target
3048    * intersect if we have the field in both */
3049   data.dest = gst_structure_new_id_empty (struct1->name);
3050   data.intersect = struct2;
3051   if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct1,
3052               gst_structure_intersect_field1, &data)))
3053     goto error;
3054
3055   /* copy fields from struct2 which we have not in struct1 to target */
3056   data.intersect = struct1;
3057   if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct2,
3058               gst_structure_intersect_field2, &data)))
3059     goto error;
3060
3061   return data.dest;
3062
3063 error:
3064   gst_structure_free (data.dest);
3065   return NULL;
3066 }
3067
3068 static gboolean
3069 gst_caps_structure_can_intersect_field (GQuark id, const GValue * val1,
3070     gpointer data)
3071 {
3072   GstStructure *other = (GstStructure *) data;
3073   const GValue *val2 = gst_structure_id_get_value (other, id);
3074
3075   if (G_LIKELY (val2)) {
3076     if (!gst_value_can_intersect (val1, val2)) {
3077       return FALSE;
3078     } else {
3079       gint eq = gst_value_compare (val1, val2);
3080
3081       if (eq == GST_VALUE_UNORDERED) {
3082         /* we need to try interseting */
3083         if (!gst_value_intersect (NULL, val1, val2)) {
3084           return FALSE;
3085         }
3086       } else if (eq != GST_VALUE_EQUAL) {
3087         return FALSE;
3088       }
3089     }
3090   }
3091   return TRUE;
3092 }
3093
3094 /**
3095  * gst_structure_can_intersect:
3096  * @struct1: a #GstStructure
3097  * @struct2: a #GstStructure
3098  *
3099  * Tries intersecting @struct1 and @struct2 and reports whether the result
3100  * would not be empty.
3101  *
3102  * Returns: %TRUE if intersection would not be empty
3103  *
3104  * Since: 0.10.36
3105  */
3106 gboolean
3107 gst_structure_can_intersect (const GstStructure * struct1,
3108     const GstStructure * struct2)
3109 {
3110   g_return_val_if_fail (GST_IS_STRUCTURE (struct1), FALSE);
3111   g_return_val_if_fail (GST_IS_STRUCTURE (struct2), FALSE);
3112
3113   if (G_UNLIKELY (struct1->name != struct2->name))
3114     return FALSE;
3115
3116   /* tries to intersect if we have the field in both */
3117   return gst_structure_foreach ((GstStructure *) struct1,
3118       gst_caps_structure_can_intersect_field, (gpointer) struct2);
3119 }
3120
3121 static gboolean
3122 gst_caps_structure_is_subset_field (GQuark field_id, const GValue * value,
3123     gpointer user_data)
3124 {
3125   GstStructure *superset = user_data;
3126   const GValue *other;
3127   int comparison;
3128
3129   if (!(other = gst_structure_id_get_value (superset, field_id)))
3130     /* field is missing in the superset => is subset */
3131     return TRUE;
3132
3133   comparison = gst_value_compare (other, value);
3134
3135   /* equal values are subset */
3136   if (comparison == GST_VALUE_EQUAL)
3137     return TRUE;
3138
3139   /* ordered, but unequal, values are not */
3140   if (comparison != GST_VALUE_UNORDERED)
3141     return FALSE;
3142
3143   /*
3144    * 1 - [1,2] = empty
3145    * -> !subset
3146    *
3147    * [1,2] - 1 = 2
3148    *  -> 1 - [1,2] = empty
3149    *  -> subset
3150    *
3151    * [1,3] - [1,2] = 3
3152    * -> [1,2] - [1,3] = empty
3153    * -> subset
3154    *
3155    * {1,2} - {1,3} = 2
3156    * -> {1,3} - {1,2} = 3
3157    * -> !subset
3158    *
3159    *  First caps subtraction needs to return a non-empty set, second
3160    *  subtractions needs to give en empty set.
3161    *  Both substractions are switched below, as it's faster that way.
3162    */
3163   if (!gst_value_subtract (NULL, value, other)) {
3164     if (gst_value_subtract (NULL, other, value)) {
3165       return TRUE;
3166     }
3167   }
3168   return FALSE;
3169 }
3170
3171 /**
3172  * gst_structure_is_subset:
3173  * @subset: a #GstStructure
3174  * @superset: a potentially greater #GstStructure
3175  *
3176  * Checks if @subset is a subset of @superset, i.e. has the same
3177  * structure name and for all fields that are existing in @superset,
3178  * @subset has a value that is a subset of the value in @superset.
3179  *
3180  * Returns: %TRUE if @subset is a subset of @superset
3181  *
3182  * Since: 0.10.36
3183  */
3184 gboolean
3185 gst_structure_is_subset (const GstStructure * subset,
3186     const GstStructure * superset)
3187 {
3188   if ((superset->name != subset->name) ||
3189       (gst_structure_n_fields (superset) > gst_structure_n_fields (subset)))
3190     return FALSE;
3191
3192   return gst_structure_foreach ((GstStructure *) subset,
3193       gst_caps_structure_is_subset_field, (gpointer) superset);
3194 }
3195
3196
3197 /**
3198  * gst_structure_fixate:
3199  * @structure: a #GstStructure
3200  *
3201  * Fixate all values in @structure using gst_value_fixate().
3202  * @structure will be modified in-place and should be writable.
3203  */
3204 void
3205 gst_structure_fixate (GstStructure * structure)
3206 {
3207   g_return_if_fail (GST_IS_STRUCTURE (structure));
3208
3209   gst_structure_foreach (structure, default_fixate, structure);
3210 }