Merge remote-tracking branch 'origin/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     _num = G_N_ELEMENTS (dyn_abbrs);
1695     /* permanently allocate and copy the array now */
1696     abbrs = g_new0 (GstStructureAbbreviation, _num);
1697     memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * _num);
1698     g_once_init_leave (&num, _num);
1699   }
1700   *n_abbrs = num;
1701
1702   return abbrs;
1703 }
1704
1705 /* given a type_name that could be a type abbreviation or a registered GType,
1706  * return a matching GType */
1707 static GType
1708 gst_structure_gtype_from_abbr (const char *type_name)
1709 {
1710   int i;
1711   GstStructureAbbreviation *abbrs;
1712   gint n_abbrs;
1713
1714   g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1715
1716   abbrs = gst_structure_get_abbrs (&n_abbrs);
1717
1718   for (i = 0; i < n_abbrs; i++) {
1719     if (strcmp (type_name, abbrs[i].type_name) == 0) {
1720       return abbrs[i].type;
1721     }
1722   }
1723
1724   /* this is the fallback */
1725   return g_type_from_name (type_name);
1726 }
1727
1728 static const char *
1729 gst_structure_to_abbr (GType type)
1730 {
1731   int i;
1732   GstStructureAbbreviation *abbrs;
1733   gint n_abbrs;
1734
1735   g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1736
1737   abbrs = gst_structure_get_abbrs (&n_abbrs);
1738
1739   for (i = 0; i < n_abbrs; i++) {
1740     if (type == abbrs[i].type) {
1741       return abbrs[i].type_name;
1742     }
1743   }
1744
1745   return g_type_name (type);
1746 }
1747
1748 static GType
1749 gst_structure_value_get_generic_type (GValue * val)
1750 {
1751   if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1752       || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1753     GArray *array = g_value_peek_pointer (val);
1754
1755     if (array->len > 0) {
1756       GValue *value = &g_array_index (array, GValue, 0);
1757
1758       return gst_structure_value_get_generic_type (value);
1759     } else {
1760       return G_TYPE_INT;
1761     }
1762   } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1763     return G_TYPE_INT;
1764   } else if (G_VALUE_TYPE (val) == GST_TYPE_INT64_RANGE) {
1765     return G_TYPE_INT64;
1766   } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1767     return G_TYPE_DOUBLE;
1768   } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1769     return GST_TYPE_FRACTION;
1770   }
1771   return G_VALUE_TYPE (val);
1772 }
1773
1774 gboolean
1775 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1776     GString * s)
1777 {
1778   GstStructureField *field;
1779   guint i, len;
1780
1781   g_return_val_if_fail (s != NULL, FALSE);
1782
1783   g_string_append (s, g_quark_to_string (structure->name));
1784   len = GST_STRUCTURE_FIELDS (structure)->len;
1785   for (i = 0; i < len; i++) {
1786     char *t;
1787     GType type;
1788
1789     field = GST_STRUCTURE_FIELD (structure, i);
1790
1791     t = gst_value_serialize (&field->value);
1792     type = gst_structure_value_get_generic_type (&field->value);
1793
1794     g_string_append_len (s, ", ", 2);
1795     /* FIXME: do we need to escape fieldnames? */
1796     g_string_append (s, g_quark_to_string (field->name));
1797     g_string_append_len (s, "=(", 2);
1798     g_string_append (s, gst_structure_to_abbr (type));
1799     g_string_append_c (s, ')');
1800     g_string_append (s, t == NULL ? "NULL" : t);
1801     g_free (t);
1802   }
1803
1804   g_string_append_c (s, ';');
1805   return TRUE;
1806 }
1807
1808 /**
1809  * gst_structure_to_string:
1810  * @structure: a #GstStructure
1811  *
1812  * Converts @structure to a human-readable string representation.
1813  *
1814  * For debugging purposes its easier to do something like this:
1815  * |[
1816  * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1817  * ]|
1818  * This prints the structure in human readble form.
1819  *
1820  * Free-function: g_free
1821  *
1822  * Returns: (transfer full)L a pointer to string allocated by g_malloc().
1823  *     g_free() after usage.
1824  */
1825 gchar *
1826 gst_structure_to_string (const GstStructure * structure)
1827 {
1828   GString *s;
1829
1830   /* NOTE:  This function is potentially called by the debug system,
1831    * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1832    * should be careful to avoid recursion.  This includes any functions
1833    * called by gst_structure_to_string.  In particular, calls should
1834    * not use the GST_PTR_FORMAT extension.  */
1835
1836   g_return_val_if_fail (structure != NULL, NULL);
1837
1838   /* we estimate a minimum size based on the number of fields in order to
1839    * avoid unnecessary reallocs within GString */
1840   s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1841   priv_gst_structure_append_to_gstring (structure, s);
1842   return g_string_free (s, FALSE);
1843 }
1844
1845 /*
1846  * r will still point to the string. if end == next, the string will not be
1847  * null-terminated. In all other cases it will be.
1848  * end = pointer to char behind end of string, next = pointer to start of
1849  * unread data.
1850  * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1851  */
1852 static gboolean
1853 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next,
1854     gboolean unescape)
1855 {
1856   gchar *w;
1857
1858   if (*s == 0)
1859     return FALSE;
1860
1861   if (*s != '"') {
1862     int ret;
1863
1864     ret = gst_structure_parse_simple_string (s, end);
1865     *next = *end;
1866
1867     return ret;
1868   }
1869
1870   if (unescape) {
1871     w = s;
1872     s++;
1873     while (*s != '"') {
1874       if (G_UNLIKELY (*s == 0))
1875         return FALSE;
1876       if (G_UNLIKELY (*s == '\\'))
1877         s++;
1878       *w = *s;
1879       w++;
1880       s++;
1881     }
1882     s++;
1883   } else {
1884     /* Find the closing quotes */
1885     s++;
1886     while (*s != '"') {
1887       if (G_UNLIKELY (*s == 0))
1888         return FALSE;
1889       if (G_UNLIKELY (*s == '\\'))
1890         s++;
1891       s++;
1892     }
1893     s++;
1894     w = s;
1895   }
1896
1897   *end = w;
1898   *next = s;
1899
1900   return TRUE;
1901 }
1902
1903 static gboolean
1904 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1905     GType type)
1906 {
1907   GValue value1 = { 0 };
1908   GValue value2 = { 0 };
1909   GType range_type;
1910   gboolean ret;
1911
1912   if (*s != '[')
1913     return FALSE;
1914   s++;
1915
1916   ret = gst_structure_parse_value (s, &s, &value1, type);
1917   if (ret == FALSE)
1918     return FALSE;
1919
1920   while (g_ascii_isspace (*s))
1921     s++;
1922
1923   if (*s != ',')
1924     return FALSE;
1925   s++;
1926
1927   while (g_ascii_isspace (*s))
1928     s++;
1929
1930   ret = gst_structure_parse_value (s, &s, &value2, type);
1931   if (ret == FALSE)
1932     return FALSE;
1933
1934   while (g_ascii_isspace (*s))
1935     s++;
1936
1937   if (*s != ']')
1938     return FALSE;
1939   s++;
1940
1941   if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1942     return FALSE;
1943
1944   if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1945     range_type = GST_TYPE_DOUBLE_RANGE;
1946     g_value_init (value, range_type);
1947     gst_value_set_double_range (value,
1948         gst_g_value_get_double_unchecked (&value1),
1949         gst_g_value_get_double_unchecked (&value2));
1950   } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1951     range_type = GST_TYPE_INT_RANGE;
1952     g_value_init (value, range_type);
1953     gst_value_set_int_range (value, gst_g_value_get_int_unchecked (&value1),
1954         gst_g_value_get_int_unchecked (&value2));
1955   } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
1956     range_type = GST_TYPE_INT64_RANGE;
1957     g_value_init (value, range_type);
1958     gst_value_set_int64_range (value, gst_g_value_get_int64_unchecked (&value1),
1959         gst_g_value_get_int64_unchecked (&value2));
1960   } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
1961     range_type = GST_TYPE_FRACTION_RANGE;
1962     g_value_init (value, range_type);
1963     gst_value_set_fraction_range (value, &value1, &value2);
1964   } else {
1965     return FALSE;
1966   }
1967
1968   *after = s;
1969   return TRUE;
1970 }
1971
1972 static gboolean
1973 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
1974     GType type, GType list_type, char begin, char end)
1975 {
1976   GValue list_value = { 0 };
1977   gboolean ret;
1978   GArray *array;
1979
1980   g_value_init (value, list_type);
1981   array = g_value_peek_pointer (value);
1982
1983   if (*s != begin)
1984     return FALSE;
1985   s++;
1986
1987   while (g_ascii_isspace (*s))
1988     s++;
1989   if (*s == end) {
1990     s++;
1991     *after = s;
1992     return TRUE;
1993   }
1994
1995   ret = gst_structure_parse_value (s, &s, &list_value, type);
1996   if (ret == FALSE)
1997     return FALSE;
1998
1999   g_array_append_val (array, list_value);
2000
2001   while (g_ascii_isspace (*s))
2002     s++;
2003
2004   while (*s != end) {
2005     if (*s != ',')
2006       return FALSE;
2007     s++;
2008
2009     while (g_ascii_isspace (*s))
2010       s++;
2011
2012     memset (&list_value, 0, sizeof (list_value));
2013     ret = gst_structure_parse_value (s, &s, &list_value, type);
2014     if (ret == FALSE)
2015       return FALSE;
2016
2017     g_array_append_val (array, list_value);
2018     while (g_ascii_isspace (*s))
2019       s++;
2020   }
2021
2022   s++;
2023
2024   *after = s;
2025   return TRUE;
2026 }
2027
2028 static gboolean
2029 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
2030 {
2031   return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
2032       '{', '}');
2033 }
2034
2035 static gboolean
2036 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
2037     GType type)
2038 {
2039   return gst_structure_parse_any_list (s, after, value, type,
2040       GST_TYPE_ARRAY, '<', '>');
2041 }
2042
2043 static gboolean
2044 gst_structure_parse_simple_string (gchar * str, gchar ** end)
2045 {
2046   char *s = str;
2047
2048   while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
2049     s++;
2050   }
2051
2052   *end = s;
2053
2054   return (s != str);
2055 }
2056
2057 static gboolean
2058 gst_structure_parse_field (gchar * str,
2059     gchar ** after, GstStructureField * field)
2060 {
2061   gchar *name;
2062   gchar *name_end;
2063   gchar *s;
2064   gchar c;
2065
2066   s = str;
2067
2068   while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2069     s++;
2070   name = s;
2071   if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &name_end))) {
2072     GST_WARNING ("failed to parse simple string, str=%s", str);
2073     return FALSE;
2074   }
2075
2076   s = name_end;
2077   while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2078     s++;
2079
2080   if (G_UNLIKELY (*s != '=')) {
2081     GST_WARNING ("missing assignment operator in the field, str=%s", str);
2082     return FALSE;
2083   }
2084   s++;
2085
2086   c = *name_end;
2087   *name_end = '\0';
2088   field->name = g_quark_from_string (name);
2089   GST_DEBUG ("trying field name '%s'", name);
2090   *name_end = c;
2091
2092   if (G_UNLIKELY (!gst_structure_parse_value (s, &s, &field->value,
2093               G_TYPE_INVALID))) {
2094     GST_WARNING ("failed to parse value %s", str);
2095     return FALSE;
2096   }
2097
2098   *after = s;
2099   return TRUE;
2100 }
2101
2102 static gboolean
2103 gst_structure_parse_value (gchar * str,
2104     gchar ** after, GValue * value, GType default_type)
2105 {
2106   gchar *type_name;
2107   gchar *type_end;
2108   gchar *value_s;
2109   gchar *value_end;
2110   gchar *s;
2111   gchar c;
2112   int ret = 0;
2113   GType type = default_type;
2114
2115   s = str;
2116   while (g_ascii_isspace (*s))
2117     s++;
2118
2119   /* check if there's a (type_name) 'cast' */
2120   type_name = NULL;
2121   if (*s == '(') {
2122     s++;
2123     while (g_ascii_isspace (*s))
2124       s++;
2125     type_name = s;
2126     if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &type_end)))
2127       return FALSE;
2128     s = type_end;
2129     while (g_ascii_isspace (*s))
2130       s++;
2131     if (G_UNLIKELY (*s != ')'))
2132       return FALSE;
2133     s++;
2134     while (g_ascii_isspace (*s))
2135       s++;
2136
2137     c = *type_end;
2138     *type_end = 0;
2139     type = gst_structure_gtype_from_abbr (type_name);
2140     GST_DEBUG ("trying type name '%s'", type_name);
2141     *type_end = c;
2142
2143     if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2144       GST_WARNING ("invalid type");
2145       return FALSE;
2146     }
2147   }
2148
2149   while (g_ascii_isspace (*s))
2150     s++;
2151   if (*s == '[') {
2152     ret = gst_structure_parse_range (s, &s, value, type);
2153   } else if (*s == '{') {
2154     ret = gst_structure_parse_list (s, &s, value, type);
2155   } else if (*s == '<') {
2156     ret = gst_structure_parse_array (s, &s, value, type);
2157   } else {
2158     value_s = s;
2159
2160     if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2161       GType try_types[] =
2162           { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_BOOLEAN,
2163         G_TYPE_STRING
2164       };
2165       int i;
2166
2167       if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s, TRUE)))
2168         return FALSE;
2169       /* Set NULL terminator for deserialization */
2170       c = *value_end;
2171       *value_end = '\0';
2172
2173       for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2174         g_value_init (value, try_types[i]);
2175         ret = gst_value_deserialize (value, value_s);
2176         if (ret)
2177           break;
2178         g_value_unset (value);
2179       }
2180     } else {
2181       g_value_init (value, type);
2182
2183       if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s,
2184                   (type != G_TYPE_STRING))))
2185         return FALSE;
2186       /* Set NULL terminator for deserialization */
2187       c = *value_end;
2188       *value_end = '\0';
2189
2190       ret = gst_value_deserialize (value, value_s);
2191       if (G_UNLIKELY (!ret))
2192         g_value_unset (value);
2193     }
2194     *value_end = c;
2195   }
2196
2197   *after = s;
2198
2199   return ret;
2200 }
2201
2202 /**
2203  * gst_structure_from_string:
2204  * @string: a string representation of a #GstStructure.
2205  * @end: (out) (allow-none): pointer to store the end of the string in.
2206  *
2207  * Creates a #GstStructure from a string representation.
2208  * If end is not NULL, a pointer to the place inside the given string
2209  * where parsing ended will be returned.
2210  *
2211  * Free-function: gst_structure_free
2212  *
2213  * Returns: (transfer full): a new #GstStructure or NULL when the string could
2214  *     not be parsed. Free with gst_structure_free() after use.
2215  */
2216 GstStructure *
2217 gst_structure_from_string (const gchar * string, gchar ** end)
2218 {
2219   char *name;
2220   char *copy;
2221   char *w;
2222   char *r;
2223   char save;
2224   GstStructure *structure = NULL;
2225   GstStructureField field;
2226
2227   g_return_val_if_fail (string != NULL, NULL);
2228
2229   copy = g_strdup (string);
2230   r = copy;
2231
2232   /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
2233   while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2234               && g_ascii_isspace (r[1]))))
2235     r++;
2236
2237   name = r;
2238   if (G_UNLIKELY (!gst_structure_parse_string (r, &w, &r, TRUE))) {
2239     GST_WARNING ("Failed to parse structure string '%s'", string);
2240     goto error;
2241   }
2242
2243   save = *w;
2244   *w = '\0';
2245   structure = gst_structure_new_empty (name);
2246   *w = save;
2247
2248   if (G_UNLIKELY (structure == NULL))
2249     goto error;
2250
2251   do {
2252     while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2253                 && g_ascii_isspace (r[1]))))
2254       r++;
2255     if (*r == ';') {
2256       /* end of structure, get the next char and finish */
2257       r++;
2258       break;
2259     }
2260     if (*r == '\0') {
2261       /* accept \0 as end delimiter */
2262       break;
2263     }
2264     if (G_UNLIKELY (*r != ',')) {
2265       GST_WARNING ("Failed to find delimiter, r=%s", r);
2266       goto error;
2267     }
2268     r++;
2269     while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2270                 && g_ascii_isspace (r[1]))))
2271       r++;
2272
2273     memset (&field, 0, sizeof (field));
2274     if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
2275       GST_WARNING ("Failed to parse field, r=%s", r);
2276       goto error;
2277     }
2278     gst_structure_set_field (structure, &field);
2279   } while (TRUE);
2280
2281   if (end)
2282     *end = (char *) string + (r - copy);
2283   else if (*r)
2284     g_warning ("gst_structure_from_string did not consume whole string,"
2285         " but caller did not provide end pointer (\"%s\")", string);
2286
2287   g_free (copy);
2288   return structure;
2289
2290 error:
2291   if (structure)
2292     gst_structure_free (structure);
2293   g_free (copy);
2294   return NULL;
2295 }
2296
2297 static void
2298 gst_structure_transform_to_string (const GValue * src_value,
2299     GValue * dest_value)
2300 {
2301   g_return_if_fail (src_value != NULL);
2302   g_return_if_fail (dest_value != NULL);
2303
2304   dest_value->data[0].v_pointer =
2305       gst_structure_to_string (src_value->data[0].v_pointer);
2306 }
2307
2308 static GstStructure *
2309 gst_structure_copy_conditional (const GstStructure * structure)
2310 {
2311   if (structure)
2312     return gst_structure_copy (structure);
2313   return NULL;
2314 }
2315
2316 /* fixate utility functions */
2317
2318 /**
2319  * gst_structure_fixate_field_nearest_int:
2320  * @structure: a #GstStructure
2321  * @field_name: a field in @structure
2322  * @target: the target value of the fixation
2323  *
2324  * Fixates a #GstStructure by changing the given field to the nearest
2325  * integer to @target that is a subset of the existing field.
2326  *
2327  * Returns: TRUE if the structure could be fixated
2328  */
2329 gboolean
2330 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2331     const char *field_name, int target)
2332 {
2333   const GValue *value;
2334
2335   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2336   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2337
2338   value = gst_structure_get_value (structure, field_name);
2339
2340   if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2341     /* already fixed */
2342     return FALSE;
2343   } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2344     int x;
2345
2346     x = gst_value_get_int_range_min (value);
2347     if (target < x)
2348       target = x;
2349     x = gst_value_get_int_range_max (value);
2350     if (target > x)
2351       target = x;
2352     gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2353     return TRUE;
2354   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2355     const GValue *list_value;
2356     int i, n;
2357     int best = 0;
2358     int best_index = -1;
2359
2360     n = gst_value_list_get_size (value);
2361     for (i = 0; i < n; i++) {
2362       list_value = gst_value_list_get_value (value, i);
2363       if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2364         int x = gst_g_value_get_int_unchecked (list_value);
2365
2366         if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2367           best_index = i;
2368           best = x;
2369         }
2370       }
2371     }
2372     if (best_index != -1) {
2373       gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2374       return TRUE;
2375     }
2376     return FALSE;
2377   }
2378
2379   return FALSE;
2380 }
2381
2382 /**
2383  * gst_structure_fixate_field_nearest_double:
2384  * @structure: a #GstStructure
2385  * @field_name: a field in @structure
2386  * @target: the target value of the fixation
2387  *
2388  * Fixates a #GstStructure by changing the given field to the nearest
2389  * double to @target that is a subset of the existing field.
2390  *
2391  * Returns: TRUE if the structure could be fixated
2392  */
2393 gboolean
2394 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2395     const char *field_name, double target)
2396 {
2397   const GValue *value;
2398
2399   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2400   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2401
2402   value = gst_structure_get_value (structure, field_name);
2403
2404   if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2405     /* already fixed */
2406     return FALSE;
2407   } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2408     double x;
2409
2410     x = gst_value_get_double_range_min (value);
2411     if (target < x)
2412       target = x;
2413     x = gst_value_get_double_range_max (value);
2414     if (target > x)
2415       target = x;
2416     gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2417     return TRUE;
2418   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2419     const GValue *list_value;
2420     int i, n;
2421     double best = 0;
2422     int best_index = -1;
2423
2424     n = gst_value_list_get_size (value);
2425     for (i = 0; i < n; i++) {
2426       list_value = gst_value_list_get_value (value, i);
2427       if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2428         double x = gst_g_value_get_double_unchecked (list_value);
2429
2430         if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2431           best_index = i;
2432           best = x;
2433         }
2434       }
2435     }
2436     if (best_index != -1) {
2437       gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2438       return TRUE;
2439     }
2440     return FALSE;
2441   }
2442
2443   return FALSE;
2444
2445 }
2446
2447 /**
2448  * gst_structure_fixate_field_boolean:
2449  * @structure: a #GstStructure
2450  * @field_name: a field in @structure
2451  * @target: the target value of the fixation
2452  *
2453  * Fixates a #GstStructure by changing the given @field_name field to the given
2454  * @target boolean if that field is not fixed yet.
2455  *
2456  * Returns: TRUE if the structure could be fixated
2457  */
2458 gboolean
2459 gst_structure_fixate_field_boolean (GstStructure * structure,
2460     const char *field_name, gboolean target)
2461 {
2462   const GValue *value;
2463
2464   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2465   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2466
2467   value = gst_structure_get_value (structure, field_name);
2468
2469   if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2470     /* already fixed */
2471     return FALSE;
2472   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2473     const GValue *list_value;
2474     int i, n;
2475     int best = 0;
2476     int best_index = -1;
2477
2478     n = gst_value_list_get_size (value);
2479     for (i = 0; i < n; i++) {
2480       list_value = gst_value_list_get_value (value, i);
2481       if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2482         gboolean x = gst_g_value_get_boolean_unchecked (list_value);
2483
2484         if (best_index == -1 || x == target) {
2485           best_index = i;
2486           best = x;
2487         }
2488       }
2489     }
2490     if (best_index != -1) {
2491       gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2492       return TRUE;
2493     }
2494     return FALSE;
2495   }
2496
2497   return FALSE;
2498 }
2499
2500 /**
2501  * gst_structure_fixate_field_string:
2502  * @structure: a #GstStructure
2503  * @field_name: a field in @structure
2504  * @target: the target value of the fixation
2505  *
2506  * Fixates a #GstStructure by changing the given @field_name field to the given
2507  * @target string if that field is not fixed yet.
2508  *
2509  * Returns: TRUE if the structure could be fixated
2510  *
2511  * Since: 0.10.30
2512  */
2513 gboolean
2514 gst_structure_fixate_field_string (GstStructure * structure,
2515     const gchar * field_name, const gchar * target)
2516 {
2517   const GValue *value;
2518
2519   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2520   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2521
2522   value = gst_structure_get_value (structure, field_name);
2523
2524   if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
2525     /* already fixed */
2526     return FALSE;
2527   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2528     const GValue *list_value;
2529     int i, n;
2530     const gchar *best = NULL;
2531     int best_index = -1;
2532
2533     n = gst_value_list_get_size (value);
2534     for (i = 0; i < n; i++) {
2535       list_value = gst_value_list_get_value (value, i);
2536       if (G_VALUE_TYPE (list_value) == G_TYPE_STRING) {
2537         const gchar *x = g_value_get_string (list_value);
2538
2539         if (best_index == -1 || g_str_equal (x, target)) {
2540           best_index = i;
2541           best = x;
2542         }
2543       }
2544     }
2545     if (best_index != -1) {
2546       gst_structure_set (structure, field_name, G_TYPE_STRING, best, NULL);
2547       return TRUE;
2548     }
2549     return FALSE;
2550   }
2551
2552   return FALSE;
2553 }
2554
2555 /**
2556  * gst_structure_fixate_field_nearest_fraction:
2557  * @structure: a #GstStructure
2558  * @field_name: a field in @structure
2559  * @target_numerator: The numerator of the target value of the fixation
2560  * @target_denominator: The denominator of the target value of the fixation
2561  *
2562  * Fixates a #GstStructure by changing the given field to the nearest
2563  * fraction to @target_numerator/@target_denominator that is a subset
2564  * of the existing field.
2565  *
2566  * Returns: TRUE if the structure could be fixated
2567  */
2568 gboolean
2569 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2570     const char *field_name, const gint target_numerator,
2571     const gint target_denominator)
2572 {
2573   const GValue *value;
2574
2575   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2576   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2577
2578   value = gst_structure_get_value (structure, field_name);
2579
2580   if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2581     /* already fixed */
2582     return FALSE;
2583   } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2584     const GValue *x, *new_value;
2585     GValue target = { 0 };
2586     g_value_init (&target, GST_TYPE_FRACTION);
2587     gst_value_set_fraction (&target, target_numerator, target_denominator);
2588
2589     new_value = &target;
2590     x = gst_value_get_fraction_range_min (value);
2591     if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2592       new_value = x;
2593     x = gst_value_get_fraction_range_max (value);
2594     if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2595       new_value = x;
2596
2597     gst_structure_set_value (structure, field_name, new_value);
2598     g_value_unset (&target);
2599     return TRUE;
2600   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2601     const GValue *list_value;
2602     int i, n;
2603     const GValue *best = NULL;
2604     gdouble target;
2605     gdouble cur_diff;
2606     gdouble best_diff = G_MAXDOUBLE;
2607
2608     target = (gdouble) target_numerator / (gdouble) target_denominator;
2609
2610     GST_DEBUG ("target %g, best %g", target, best_diff);
2611
2612     best = NULL;
2613
2614     n = gst_value_list_get_size (value);
2615     for (i = 0; i < n; i++) {
2616       list_value = gst_value_list_get_value (value, i);
2617       if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2618         gint num, denom;
2619         gdouble list_double;
2620
2621         num = gst_value_get_fraction_numerator (list_value);
2622         denom = gst_value_get_fraction_denominator (list_value);
2623
2624         list_double = ((gdouble) num / (gdouble) denom);
2625         cur_diff = target - list_double;
2626
2627         GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2628
2629         if (cur_diff < 0)
2630           cur_diff = -cur_diff;
2631
2632         if (!best || cur_diff < best_diff) {
2633           GST_DEBUG ("new best %g", list_double);
2634           best = list_value;
2635           best_diff = cur_diff;
2636         }
2637       }
2638     }
2639     if (best != NULL) {
2640       gst_structure_set_value (structure, field_name, best);
2641       return TRUE;
2642     }
2643   }
2644
2645   return FALSE;
2646 }
2647
2648 static gboolean
2649 default_fixate (GQuark field_id, const GValue * value, gpointer data)
2650 {
2651   GstStructure *s = data;
2652   GValue v = { 0 };
2653
2654   if (gst_value_fixate (&v, value)) {
2655     gst_structure_id_set_value (s, field_id, &v);
2656     g_value_unset (&v);
2657   }
2658   return TRUE;
2659 }
2660
2661 /**
2662  * gst_structure_fixate_field:
2663  * @structure: a #GstStructure
2664  * @field_name: a field in @structure
2665  *
2666  * Fixates a #GstStructure by changing the given field with its fixated value.
2667  *
2668  * Returns: TRUE if the structure field could be fixated
2669  */
2670 gboolean
2671 gst_structure_fixate_field (GstStructure * structure, const char *field_name)
2672 {
2673   GstStructureField *field;
2674
2675   g_return_val_if_fail (structure != NULL, FALSE);
2676   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2677
2678   if (!(field = gst_structure_get_field (structure, field_name)))
2679     return FALSE;
2680
2681   return default_fixate (field->name, &field->value, structure);
2682 }
2683
2684 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2685  * (useful for message parsing functions where the return location is user
2686  * supplied and the user may pass NULL if the value isn't of interest) */
2687 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname)           \
2688 G_STMT_START {                                                                \
2689   const GValue *_value = (value);                                             \
2690   guint _flags = (flags);                                                     \
2691   GType _value_type = G_VALUE_TYPE (_value);                                  \
2692   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);           \
2693   gchar *_lcopy_format = _vtable->lcopy_format;                               \
2694   GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };      \
2695   guint _n_values = 0;                                                        \
2696                                                                               \
2697   while (*_lcopy_format != '\0') {                                            \
2698     g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER);                     \
2699     _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer);          \
2700     _lcopy_format++;                                                          \
2701   }                                                                           \
2702   if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2703     *(__error) = g_strdup_printf ("either all or none of the return "         \
2704         "locations for field '%s' need to be NULL", fieldname);               \
2705   } else if (_cvalues[0].v_pointer != NULL) {                                 \
2706     *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags);  \
2707   }                                                                           \
2708 } G_STMT_END
2709
2710 /**
2711  * gst_structure_get_valist:
2712  * @structure: a #GstStructure
2713  * @first_fieldname: the name of the first field to read
2714  * @args: variable arguments
2715  *
2716  * Parses the variable arguments and reads fields from @structure accordingly.
2717  * valist-variant of gst_structure_get(). Look at the documentation of
2718  * gst_structure_get() for more details.
2719  *
2720  * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2721  *
2722  * Since: 0.10.24
2723  */
2724 gboolean
2725 gst_structure_get_valist (const GstStructure * structure,
2726     const char *first_fieldname, va_list args)
2727 {
2728   const char *field_name;
2729   GType expected_type = G_TYPE_INVALID;
2730
2731   g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2732   g_return_val_if_fail (first_fieldname != NULL, FALSE);
2733
2734   field_name = first_fieldname;
2735   while (field_name) {
2736     const GValue *val = NULL;
2737     gchar *err = NULL;
2738
2739     expected_type = va_arg (args, GType);
2740
2741     val = gst_structure_get_value (structure, field_name);
2742
2743     if (val == NULL)
2744       goto no_such_field;
2745
2746     if (G_VALUE_TYPE (val) != expected_type)
2747       goto wrong_type;
2748
2749     GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2750     if (err) {
2751       g_warning ("%s: %s", G_STRFUNC, err);
2752       g_free (err);
2753       return FALSE;
2754     }
2755
2756     field_name = va_arg (args, const gchar *);
2757   }
2758
2759   return TRUE;
2760
2761 /* ERRORS */
2762 no_such_field:
2763   {
2764     GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2765         field_name, structure);
2766     return FALSE;
2767   }
2768 wrong_type:
2769   {
2770     GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
2771         "field was of type '%s': %" GST_PTR_FORMAT, field_name,
2772         GST_STR_NULL (g_type_name (expected_type)),
2773         G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
2774         structure);
2775     return FALSE;
2776   }
2777 }
2778
2779 /**
2780  * gst_structure_id_get_valist:
2781  * @structure: a #GstStructure
2782  * @first_field_id: the quark of the first field to read
2783  * @args: variable arguments
2784  *
2785  * Parses the variable arguments and reads fields from @structure accordingly.
2786  * valist-variant of gst_structure_id_get(). Look at the documentation of
2787  * gst_structure_id_get() for more details.
2788  *
2789  * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2790  *
2791  * Since: 0.10.24
2792  */
2793 gboolean
2794 gst_structure_id_get_valist (const GstStructure * structure,
2795     GQuark first_field_id, va_list args)
2796 {
2797   GQuark field_id;
2798   GType expected_type = G_TYPE_INVALID;
2799
2800   g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2801   g_return_val_if_fail (first_field_id != 0, FALSE);
2802
2803   field_id = first_field_id;
2804   while (field_id) {
2805     const GValue *val = NULL;
2806     gchar *err = NULL;
2807
2808     expected_type = va_arg (args, GType);
2809
2810     val = gst_structure_id_get_value (structure, field_id);
2811
2812     if (val == NULL)
2813       goto no_such_field;
2814
2815     if (G_VALUE_TYPE (val) != expected_type)
2816       goto wrong_type;
2817
2818     GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
2819     if (err) {
2820       g_warning ("%s: %s", G_STRFUNC, err);
2821       g_free (err);
2822       return FALSE;
2823     }
2824
2825     field_id = va_arg (args, GQuark);
2826   }
2827
2828   return TRUE;
2829
2830 /* ERRORS */
2831 no_such_field:
2832   {
2833     GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2834         GST_STR_NULL (g_quark_to_string (field_id)), structure);
2835     return FALSE;
2836   }
2837 wrong_type:
2838   {
2839     GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
2840         "field was of type '%s': %" GST_PTR_FORMAT,
2841         g_quark_to_string (field_id),
2842         GST_STR_NULL (g_type_name (expected_type)),
2843         G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
2844         structure);
2845     return FALSE;
2846   }
2847 }
2848
2849 /**
2850  * gst_structure_get:
2851  * @structure: a #GstStructure
2852  * @first_fieldname: the name of the first field to read
2853  * @...: variable arguments
2854  *
2855  * Parses the variable arguments and reads fields from @structure accordingly.
2856  * Variable arguments should be in the form field name, field type
2857  * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2858  * The last variable argument should be NULL.
2859  *
2860  * For refcounted (mini)objects you will acquire your own reference which
2861  * you must release with a suitable _unref() when no longer needed. For
2862  * strings and boxed types you will acquire a copy which you will need to
2863  * release with either g_free() or the suitable function for the boxed type.
2864  *
2865  * Returns: FALSE if there was a problem reading any of the fields (e.g.
2866  *     because the field requested did not exist, or was of a type other
2867  *     than the type specified), otherwise TRUE.
2868  *
2869  * Since: 0.10.24
2870  */
2871 gboolean
2872 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
2873     ...)
2874 {
2875   gboolean ret;
2876   va_list args;
2877
2878   g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2879   g_return_val_if_fail (first_fieldname != NULL, FALSE);
2880
2881   va_start (args, first_fieldname);
2882   ret = gst_structure_get_valist (structure, first_fieldname, args);
2883   va_end (args);
2884
2885   return ret;
2886 }
2887
2888 /**
2889  * gst_structure_id_get:
2890  * @structure: a #GstStructure
2891  * @first_field_id: the quark of the first field to read
2892  * @...: variable arguments
2893  *
2894  * Parses the variable arguments and reads fields from @structure accordingly.
2895  * Variable arguments should be in the form field id quark, field type
2896  * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2897  * The last variable argument should be NULL (technically it should be a
2898  * 0 quark, but we require NULL so compilers that support it can check for
2899  * the NULL terminator and warn if it's not there).
2900  *
2901  * This function is just like gst_structure_get() only that it is slightly
2902  * more efficient since it saves the string-to-quark lookup in the global
2903  * quark hashtable.
2904  *
2905  * For refcounted (mini)objects you will acquire your own reference which
2906  * you must release with a suitable _unref() when no longer needed. For
2907  * strings and boxed types you will acquire a copy which you will need to
2908  * release with either g_free() or the suitable function for the boxed type.
2909  *
2910  * Returns: FALSE if there was a problem reading any of the fields (e.g.
2911  *     because the field requested did not exist, or was of a type other
2912  *     than the type specified), otherwise TRUE.
2913  *
2914  * Since: 0.10.24
2915  */
2916 gboolean
2917 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
2918     ...)
2919 {
2920   gboolean ret;
2921   va_list args;
2922
2923   g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2924   g_return_val_if_fail (first_field_id != 0, FALSE);
2925
2926   va_start (args, first_field_id);
2927   ret = gst_structure_id_get_valist (structure, first_field_id, args);
2928   va_end (args);
2929
2930   return ret;
2931 }
2932
2933 static gboolean
2934 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
2935     gpointer data)
2936 {
2937   const GstStructure *struct1 = (const GstStructure *) data;
2938   const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
2939
2940   if (G_UNLIKELY (val1 == NULL))
2941     return FALSE;
2942   if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
2943     return TRUE;
2944   }
2945
2946   return FALSE;
2947 }
2948
2949 /**
2950  * gst_structure_is_equal:
2951  * @structure1: a #GstStructure.
2952  * @structure2: a #GstStructure.
2953  *
2954  * Tests if the two #GstStructure are equal.
2955  *
2956  * Returns: TRUE if the two structures have the same name and field.
2957  *
2958  * Since: 0.10.36
2959  **/
2960 gboolean
2961 gst_structure_is_equal (const GstStructure * structure1,
2962     const GstStructure * structure2)
2963 {
2964   g_return_val_if_fail (GST_IS_STRUCTURE (structure1), FALSE);
2965   g_return_val_if_fail (GST_IS_STRUCTURE (structure2), FALSE);
2966
2967   if (G_UNLIKELY (structure1 == structure2))
2968     return TRUE;
2969
2970   if (structure1->name != structure2->name) {
2971     return FALSE;
2972   }
2973   if (GST_STRUCTURE_FIELDS (structure1)->len !=
2974       GST_STRUCTURE_FIELDS (structure2)->len) {
2975     return FALSE;
2976   }
2977
2978   return gst_structure_foreach (structure1, gst_structure_is_equal_foreach,
2979       (gpointer) structure2);
2980 }
2981
2982
2983 typedef struct
2984 {
2985   GstStructure *dest;
2986   const GstStructure *intersect;
2987 }
2988 IntersectData;
2989
2990 static gboolean
2991 gst_structure_intersect_field1 (GQuark id, const GValue * val1, gpointer data)
2992 {
2993   IntersectData *idata = (IntersectData *) data;
2994   const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
2995
2996   if (G_UNLIKELY (val2 == NULL)) {
2997     gst_structure_id_set_value (idata->dest, id, val1);
2998   } else {
2999     GValue dest_value = { 0 };
3000     if (gst_value_intersect (&dest_value, val1, val2)) {
3001       gst_structure_id_set_value (idata->dest, id, &dest_value);
3002       g_value_unset (&dest_value);
3003     } else {
3004       return FALSE;
3005     }
3006   }
3007   return TRUE;
3008 }
3009
3010 static gboolean
3011 gst_structure_intersect_field2 (GQuark id, const GValue * val1, gpointer data)
3012 {
3013   IntersectData *idata = (IntersectData *) data;
3014   const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3015
3016   if (G_UNLIKELY (val2 == NULL)) {
3017     gst_structure_id_set_value (idata->dest, id, val1);
3018   }
3019   return TRUE;
3020 }
3021
3022 /**
3023  * gst_structure_intersect:
3024  * @struct1: a #GstStructure
3025  * @struct2: a #GstStructure
3026  *
3027  * Interesects @struct1 and @struct2 and returns the intersection.
3028  *
3029  * Returns: Intersection of @struct1 and @struct2
3030  *
3031  * Since: 0.10.36
3032  */
3033 GstStructure *
3034 gst_structure_intersect (const GstStructure * struct1,
3035     const GstStructure * struct2)
3036 {
3037   IntersectData data;
3038
3039   g_assert (struct1 != NULL);
3040   g_assert (struct2 != NULL);
3041
3042   if (G_UNLIKELY (struct1->name != struct2->name))
3043     return NULL;
3044
3045   /* copy fields from struct1 which we have not in struct2 to target
3046    * intersect if we have the field in both */
3047   data.dest = gst_structure_new_id_empty (struct1->name);
3048   data.intersect = struct2;
3049   if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct1,
3050               gst_structure_intersect_field1, &data)))
3051     goto error;
3052
3053   /* copy fields from struct2 which we have not in struct1 to target */
3054   data.intersect = struct1;
3055   if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct2,
3056               gst_structure_intersect_field2, &data)))
3057     goto error;
3058
3059   return data.dest;
3060
3061 error:
3062   gst_structure_free (data.dest);
3063   return NULL;
3064 }
3065
3066 static gboolean
3067 gst_caps_structure_can_intersect_field (GQuark id, const GValue * val1,
3068     gpointer data)
3069 {
3070   GstStructure *other = (GstStructure *) data;
3071   const GValue *val2 = gst_structure_id_get_value (other, id);
3072
3073   if (G_LIKELY (val2)) {
3074     if (!gst_value_can_intersect (val1, val2)) {
3075       return FALSE;
3076     } else {
3077       gint eq = gst_value_compare (val1, val2);
3078
3079       if (eq == GST_VALUE_UNORDERED) {
3080         /* we need to try interseting */
3081         if (!gst_value_intersect (NULL, val1, val2)) {
3082           return FALSE;
3083         }
3084       } else if (eq != GST_VALUE_EQUAL) {
3085         return FALSE;
3086       }
3087     }
3088   }
3089   return TRUE;
3090 }
3091
3092 /**
3093  * gst_structure_can_intersect:
3094  * @struct1: a #GstStructure
3095  * @struct2: a #GstStructure
3096  *
3097  * Tries intersecting @struct1 and @struct2 and reports whether the result
3098  * would not be empty.
3099  *
3100  * Returns: %TRUE if intersection would not be empty
3101  *
3102  * Since: 0.10.36
3103  */
3104 gboolean
3105 gst_structure_can_intersect (const GstStructure * struct1,
3106     const GstStructure * struct2)
3107 {
3108   g_return_val_if_fail (GST_IS_STRUCTURE (struct1), FALSE);
3109   g_return_val_if_fail (GST_IS_STRUCTURE (struct2), FALSE);
3110
3111   if (G_UNLIKELY (struct1->name != struct2->name))
3112     return FALSE;
3113
3114   /* tries to intersect if we have the field in both */
3115   return gst_structure_foreach ((GstStructure *) struct1,
3116       gst_caps_structure_can_intersect_field, (gpointer) struct2);
3117 }
3118
3119 static gboolean
3120 gst_caps_structure_is_subset_field (GQuark field_id, const GValue * value,
3121     gpointer user_data)
3122 {
3123   GstStructure *superset = user_data;
3124   const GValue *other;
3125   int comparison;
3126
3127   if (!(other = gst_structure_id_get_value (superset, field_id)))
3128     /* field is missing in the superset => is subset */
3129     return TRUE;
3130
3131   comparison = gst_value_compare (other, value);
3132
3133   /* equal values are subset */
3134   if (comparison == GST_VALUE_EQUAL)
3135     return TRUE;
3136
3137   /* ordered, but unequal, values are not */
3138   if (comparison != GST_VALUE_UNORDERED)
3139     return FALSE;
3140
3141   /*
3142    * 1 - [1,2] = empty
3143    * -> !subset
3144    *
3145    * [1,2] - 1 = 2
3146    *  -> 1 - [1,2] = empty
3147    *  -> subset
3148    *
3149    * [1,3] - [1,2] = 3
3150    * -> [1,2] - [1,3] = empty
3151    * -> subset
3152    *
3153    * {1,2} - {1,3} = 2
3154    * -> {1,3} - {1,2} = 3
3155    * -> !subset
3156    *
3157    *  First caps subtraction needs to return a non-empty set, second
3158    *  subtractions needs to give en empty set.
3159    *  Both substractions are switched below, as it's faster that way.
3160    */
3161   if (!gst_value_subtract (NULL, value, other)) {
3162     if (gst_value_subtract (NULL, other, value)) {
3163       return TRUE;
3164     }
3165   }
3166   return FALSE;
3167 }
3168
3169 /**
3170  * gst_structure_is_subset:
3171  * @subset: a #GstStructure
3172  * @superset: a potentially greater #GstStructure
3173  *
3174  * Checks if @subset is a subset of @superset, i.e. has the same
3175  * structure name and for all fields that are existing in @superset,
3176  * @subset has a value that is a subset of the value in @superset.
3177  *
3178  * Returns: %TRUE if @subset is a subset of @superset
3179  *
3180  * Since: 0.10.36
3181  */
3182 gboolean
3183 gst_structure_is_subset (const GstStructure * subset,
3184     const GstStructure * superset)
3185 {
3186   if ((superset->name != subset->name) ||
3187       (gst_structure_n_fields (superset) > gst_structure_n_fields (subset)))
3188     return FALSE;
3189
3190   return gst_structure_foreach ((GstStructure *) subset,
3191       gst_caps_structure_is_subset_field, (gpointer) superset);
3192 }
3193
3194
3195 /**
3196  * gst_structure_fixate:
3197  * @structure: a #GstStructure
3198  *
3199  * Fixate all values in @structure using gst_value_fixate().
3200  * @structure will be modified in-place and should be writable.
3201  */
3202 void
3203 gst_structure_fixate (GstStructure * structure)
3204 {
3205   g_return_if_fail (GST_IS_STRUCTURE (structure));
3206
3207   gst_structure_foreach (structure, default_fixate, structure);
3208 }