13ffd220cac95c15a8ad3615a19af6df12471f07
[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_empty_new() or
40  * gst_structure_new(), which both take a name and an optional set of
41  * key/value pairs along with the types of the values.
42  * 
43  * Field values can be changed with gst_structure_set_value() or
44  * gst_structure_set().
45  *
46  * Field values can be retrieved with gst_structure_get_value() or the more
47  * convenient gst_structure_get_*() functions.
48  *
49  * Fields can be removed with gst_structure_remove_field() or
50  * gst_structure_remove_fields().
51  *
52  * Last reviewed on 2007-10-16 (0.10.15)
53  */
54
55 #ifdef HAVE_CONFIG_H
56 #include "config.h"
57 #endif
58
59 #include <string.h>
60
61 #include "gst_private.h"
62 #include <gst/gst.h>
63 #include <gobject/gvaluecollector.h>
64
65 typedef struct _GstStructureField GstStructureField;
66
67 struct _GstStructureField
68 {
69   GQuark name;
70   GValue value;
71 };
72
73 #define GST_STRUCTURE_FIELD(structure, index) \
74     &g_array_index((structure)->fields, GstStructureField, (index))
75
76 #define IS_MUTABLE(structure) \
77     (!(structure)->parent_refcount || \
78      g_atomic_int_get ((structure)->parent_refcount) == 1)
79
80 static void gst_structure_set_field (GstStructure * structure,
81     GstStructureField * field);
82 static GstStructureField *gst_structure_get_field (const GstStructure *
83     structure, const gchar * fieldname);
84 static GstStructureField *gst_structure_id_get_field (const GstStructure *
85     structure, GQuark field);
86 static void gst_structure_transform_to_string (const GValue * src_value,
87     GValue * dest_value);
88 static GstStructure *gst_structure_copy_conditional (const GstStructure *
89     structure);
90 static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
91     GValue * value, GType default_type);
92 static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
93
94 GType
95 gst_structure_get_type (void)
96 {
97   static GType gst_structure_type = 0;
98
99   if (G_UNLIKELY (gst_structure_type == 0)) {
100     gst_structure_type = g_boxed_type_register_static ("GstStructure",
101         (GBoxedCopyFunc) gst_structure_copy_conditional,
102         (GBoxedFreeFunc) gst_structure_free);
103
104     g_value_register_transform_func (gst_structure_type, G_TYPE_STRING,
105         gst_structure_transform_to_string);
106   }
107
108   return gst_structure_type;
109 }
110
111 static GstStructure *
112 gst_structure_id_empty_new_with_size (GQuark quark, guint prealloc)
113 {
114   GstStructure *structure;
115
116   structure = g_slice_new (GstStructure);
117   structure->type = gst_structure_get_type ();
118   structure->name = quark;
119   structure->parent_refcount = NULL;
120   structure->fields =
121       g_array_sized_new (FALSE, FALSE, sizeof (GstStructureField), prealloc);
122
123   return structure;
124 }
125
126 /**
127  * gst_structure_id_empty_new:
128  * @quark: name of new structure
129  *
130  * Creates a new, empty #GstStructure with the given name as a GQuark.
131  *
132  * Returns: a new, empty #GstStructure
133  */
134 GstStructure *
135 gst_structure_id_empty_new (GQuark quark)
136 {
137   g_return_val_if_fail (quark != 0, NULL);
138
139   return gst_structure_id_empty_new_with_size (quark, 0);
140 }
141
142 #ifndef G_DISABLE_CHECKS
143 static gboolean
144 gst_structure_validate_name (const gchar * name)
145 {
146   const gchar *s;
147
148   g_return_val_if_fail (name != NULL, FALSE);
149
150   /* FIXME 0.11: use g_ascii_isalpha() */
151   if (!g_ascii_isalnum (*name)) {
152     GST_WARNING ("Invalid character '%c' at offset 0 in structure name: %s",
153         *name, name);
154     return FALSE;
155   }
156
157   /* FIXME 0.11: don't allow spaces */
158   /* FIXME: test name string more */
159   s = &name[1];
160   while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+ ", *s) != NULL))
161     s++;
162   if (*s != '\0') {
163     GST_WARNING ("Invalid character '%c' at offset %lu in structure name: %s",
164         *s, ((gulong) s - (gulong) name), name);
165     return FALSE;
166   }
167
168   return TRUE;
169 }
170 #endif
171
172 /**
173  * gst_structure_empty_new:
174  * @name: name of new structure
175  *
176  * Creates a new, empty #GstStructure with the given @name.
177  *
178  * See gst_structure_set_name() for constraints on the @name parameter.
179  *
180  * Returns: a new, empty #GstStructure
181  */
182 GstStructure *
183 gst_structure_empty_new (const gchar * name)
184 {
185   g_return_val_if_fail (gst_structure_validate_name (name), NULL);
186
187   return gst_structure_id_empty_new_with_size (g_quark_from_string (name), 0);
188 }
189
190 /**
191  * gst_structure_new:
192  * @name: name of new structure
193  * @firstfield: name of first field to set
194  * @...: additional arguments
195  *
196  * Creates a new #GstStructure with the given name.  Parses the
197  * list of variable arguments and sets fields to the values listed.
198  * Variable arguments should be passed as field name, field type,
199  * and value.  Last variable argument should be NULL.
200  *
201  * Returns: a new #GstStructure
202  */
203 GstStructure *
204 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
205 {
206   GstStructure *structure;
207   va_list varargs;
208
209   g_return_val_if_fail (name != NULL, NULL);
210
211   va_start (varargs, firstfield);
212
213   structure = gst_structure_new_valist (name, firstfield, varargs);
214
215   va_end (varargs);
216
217   return structure;
218 }
219
220 /**
221  * gst_structure_new_valist:
222  * @name: name of new structure
223  * @firstfield: name of first field to set
224  * @varargs: variable argument list
225  *
226  * Creates a new #GstStructure with the given @name.  Structure fields
227  * are set according to the varargs in a manner similar to
228  * gst_structure_new().
229  *
230  * See gst_structure_set_name() for constraints on the @name parameter.
231  *
232  * Returns: a new #GstStructure
233  */
234 GstStructure *
235 gst_structure_new_valist (const gchar * name,
236     const gchar * firstfield, va_list varargs)
237 {
238   GstStructure *structure;
239
240   g_return_val_if_fail (name != NULL, NULL);
241
242   structure = gst_structure_empty_new (name);
243
244   if (structure)
245     gst_structure_set_valist (structure, firstfield, varargs);
246
247   return structure;
248 }
249
250 /**
251  * gst_structure_set_parent_refcount:
252  * @structure: a #GstStructure
253  * @refcount: a pointer to the parent's refcount
254  *
255  * Sets the parent_refcount field of #GstStructure. This field is used to
256  * determine whether a structure is mutable or not. This function should only be
257  * called by code implementing parent objects of #GstStructure, as described in
258  * the MT Refcounting section of the design documents.
259  */
260 void
261 gst_structure_set_parent_refcount (GstStructure * structure, int *refcount)
262 {
263   g_return_if_fail (structure != NULL);
264
265   /* if we have a parent_refcount already, we can only clear
266    * if with a NULL refcount */
267   if (structure->parent_refcount)
268     g_return_if_fail (refcount == NULL);
269   else
270     g_return_if_fail (refcount != NULL);
271
272   structure->parent_refcount = refcount;
273 }
274
275 /**
276  * gst_structure_copy:
277  * @structure: a #GstStructure to duplicate
278  *
279  * Duplicates a #GstStructure and all its fields and values.
280  *
281  * Returns: a new #GstStructure.
282  */
283 GstStructure *
284 gst_structure_copy (const GstStructure * structure)
285 {
286   GstStructure *new_structure;
287   GstStructureField *field;
288   guint i;
289
290   g_return_val_if_fail (structure != NULL, NULL);
291
292   new_structure =
293       gst_structure_id_empty_new_with_size (structure->name,
294       structure->fields->len);
295
296   for (i = 0; i < structure->fields->len; i++) {
297     GstStructureField new_field = { 0 };
298
299     field = GST_STRUCTURE_FIELD (structure, i);
300
301     new_field.name = field->name;
302     gst_value_init_and_copy (&new_field.value, &field->value);
303     g_array_append_val (new_structure->fields, new_field);
304   }
305
306   return new_structure;
307 }
308
309 /**
310  * gst_structure_free:
311  * @structure: the #GstStructure to free
312  *
313  * Frees a #GstStructure and all its fields and values. The structure must not
314  * have a parent when this function is called.
315  */
316 void
317 gst_structure_free (GstStructure * structure)
318 {
319   GstStructureField *field;
320   guint i;
321
322   g_return_if_fail (structure != NULL);
323   g_return_if_fail (structure->parent_refcount == NULL);
324
325   for (i = 0; i < structure->fields->len; i++) {
326     field = GST_STRUCTURE_FIELD (structure, i);
327
328     if (G_IS_VALUE (&field->value)) {
329       g_value_unset (&field->value);
330     }
331   }
332   g_array_free (structure->fields, TRUE);
333 #ifdef USE_POISONING
334   memset (structure, 0xff, sizeof (GstStructure));
335 #endif
336   g_slice_free (GstStructure, structure);
337 }
338
339 /**
340  * gst_structure_get_name:
341  * @structure: a #GstStructure
342  *
343  * Get the name of @structure as a string.
344  *
345  * Returns: the name of the structure.
346  */
347 const gchar *
348 gst_structure_get_name (const GstStructure * structure)
349 {
350   g_return_val_if_fail (structure != NULL, NULL);
351
352   return g_quark_to_string (structure->name);
353 }
354
355 /**
356  * gst_structure_has_name:
357  * @structure: a #GstStructure
358  * @name: structure name to check for
359  *
360  * Checks if the structure has the given name
361  *
362  * Returns: TRUE if @name matches the name of the structure.
363  */
364 gboolean
365 gst_structure_has_name (const GstStructure * structure, const gchar * name)
366 {
367   const gchar *structure_name;
368
369   g_return_val_if_fail (structure != NULL, FALSE);
370   g_return_val_if_fail (name != NULL, FALSE);
371
372   /* getting the string is cheap and comparing short strings is too
373    * should be faster than getting the quark for name and comparing the quarks
374    */
375   structure_name = g_quark_to_string (structure->name);
376
377   return (structure_name && strcmp (structure_name, name) == 0);
378 }
379
380 /**
381  * gst_structure_get_name_id:
382  * @structure: a #GstStructure
383  *
384  * Get the name of @structure as a GQuark.
385  *
386  * Returns: the quark representing the name of the structure.
387  */
388 GQuark
389 gst_structure_get_name_id (const GstStructure * structure)
390 {
391   g_return_val_if_fail (structure != NULL, 0);
392
393   return structure->name;
394 }
395
396 /**
397  * gst_structure_set_name:
398  * @structure: a #GstStructure
399  * @name: the new name of the structure
400  *
401  * Sets the name of the structure to the given @name.  The string
402  * provided is copied before being used. It must not be empty, start with a
403  * letter and can be followed by letters, numbers and any of "/-_.:".
404  */
405 void
406 gst_structure_set_name (GstStructure * structure, const gchar * name)
407 {
408   g_return_if_fail (structure != NULL);
409   g_return_if_fail (IS_MUTABLE (structure));
410   g_return_if_fail (gst_structure_validate_name (name));
411
412   structure->name = g_quark_from_string (name);
413 }
414
415 /**
416  * gst_structure_id_set_value:
417  * @structure: a #GstStructure
418  * @field: a #GQuark representing a field
419  * @value: the new value of the field
420  *
421  * Sets the field with the given GQuark @field to @value.  If the field
422  * does not exist, it is created.  If the field exists, the previous
423  * value is replaced and freed.
424  */
425 void
426 gst_structure_id_set_value (GstStructure * structure,
427     GQuark field, const GValue * value)
428 {
429   GstStructureField gsfield = { 0, {0,} };
430
431   g_return_if_fail (structure != NULL);
432   g_return_if_fail (G_IS_VALUE (value));
433   g_return_if_fail (IS_MUTABLE (structure));
434
435   if (G_VALUE_HOLDS_STRING (value)) {
436     const gchar *s;
437
438     s = g_value_get_string (value);
439     if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
440       g_warning ("Trying to set string field '%s' on structure, but string is "
441           "not valid UTF-8. Please file a bug.", g_quark_to_string (field));
442       return;
443     }
444   }
445
446   gsfield.name = field;
447   gst_value_init_and_copy (&gsfield.value, value);
448
449   gst_structure_set_field (structure, &gsfield);
450 }
451
452 /**
453  * gst_structure_set_value:
454  * @structure: a #GstStructure
455  * @fieldname: the name of the field to set
456  * @value: the new value of the field
457  *
458  * Sets the field with the given name @field to @value.  If the field
459  * does not exist, it is created.  If the field exists, the previous
460  * value is replaced and freed.
461  */
462 void
463 gst_structure_set_value (GstStructure * structure,
464     const gchar * fieldname, const GValue * value)
465 {
466   g_return_if_fail (structure != NULL);
467   g_return_if_fail (fieldname != NULL);
468   g_return_if_fail (G_IS_VALUE (value));
469   g_return_if_fail (IS_MUTABLE (structure));
470
471   gst_structure_id_set_value (structure, g_quark_from_string (fieldname),
472       value);
473 }
474
475 /**
476  * gst_structure_set:
477  * @structure: a #GstStructure
478  * @fieldname: the name of the field to set
479  * @...: variable arguments
480  *
481  * Parses the variable arguments and sets fields accordingly.
482  * Variable arguments should be in the form field name, field type
483  * (as a GType), value(s).  The last variable argument should be NULL.
484  */
485 void
486 gst_structure_set (GstStructure * structure, const gchar * field, ...)
487 {
488   va_list varargs;
489
490   g_return_if_fail (structure != NULL);
491
492   va_start (varargs, field);
493
494   gst_structure_set_valist (structure, field, varargs);
495
496   va_end (varargs);
497 }
498
499 /**
500  * gst_structure_set_valist:
501  * @structure: a #GstStructure
502  * @fieldname: the name of the field to set
503  * @varargs: variable arguments
504  *
505  * va_list form of gst_structure_set().
506  */
507 void
508 gst_structure_set_valist (GstStructure * structure,
509     const gchar * fieldname, va_list varargs)
510 {
511   gchar *err = NULL;
512   GType type;
513
514   g_return_if_fail (structure != NULL);
515   g_return_if_fail (IS_MUTABLE (structure));
516
517   while (fieldname) {
518     GstStructureField field = { 0 };
519
520     field.name = g_quark_from_string (fieldname);
521
522     type = va_arg (varargs, GType);
523
524     if (type == G_TYPE_DATE) {
525       g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
526       type = GST_TYPE_DATE;
527     }
528
529     g_value_init (&field.value, type);
530     G_VALUE_COLLECT (&field.value, varargs, 0, &err);
531     if (err) {
532       g_critical ("%s", err);
533       return;
534     }
535     gst_structure_set_field (structure, &field);
536
537     fieldname = va_arg (varargs, gchar *);
538   }
539 }
540
541 /**
542  * gst_structure_id_set:
543  * @structure: a #GstStructure
544  * @fieldname: the GQuark for the name of the field to set
545  * @...: variable arguments
546  *
547  * Identical to gst_structure_set, except that field names are
548  * passed using the GQuark for the field name. This allows more efficient
549  * setting of the structure if the caller already knows the associated
550  * quark values.
551  * The last variable argument must be NULL.
552  *
553  * Since: 0.10.10
554  */
555 void
556 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
557 {
558   va_list varargs;
559
560   g_return_if_fail (structure != NULL);
561
562   va_start (varargs, field);
563   gst_structure_id_set_valist (structure, field, varargs);
564   va_end (varargs);
565 }
566
567 /**
568  * gst_structure_id_set_valist:
569  * @structure: a #GstStructure
570  * @fieldname: the name of the field to set
571  * @varargs: variable arguments
572  *
573  * va_list form of gst_structure_id_set().
574  *
575  * Since: 0.10.10
576  */
577 void
578 gst_structure_id_set_valist (GstStructure * structure,
579     GQuark fieldname, va_list varargs)
580 {
581   gchar *err = NULL;
582   GType type;
583
584   g_return_if_fail (structure != NULL);
585   g_return_if_fail (IS_MUTABLE (structure));
586
587   while (fieldname) {
588     GstStructureField field = { 0 };
589
590     field.name = fieldname;
591
592     type = va_arg (varargs, GType);
593
594     if (type == G_TYPE_DATE) {
595       g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
596       type = GST_TYPE_DATE;
597     }
598
599     g_value_init (&field.value, type);
600     G_VALUE_COLLECT (&field.value, varargs, 0, &err);
601     if (err) {
602       g_critical ("%s", err);
603       return;
604     }
605     gst_structure_set_field (structure, &field);
606
607     fieldname = va_arg (varargs, GQuark);
608   }
609 }
610
611 /* If the structure currently contains a field with the same name, it is
612  * replaced with the provided field. Otherwise, the field is added to the
613  * structure. The field's value is not deeply copied.
614  */
615 static void
616 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
617 {
618   GstStructureField *f;
619   guint i;
620
621   for (i = 0; i < structure->fields->len; i++) {
622     f = GST_STRUCTURE_FIELD (structure, i);
623
624     if (f->name == field->name) {
625       g_value_unset (&f->value);
626       memcpy (f, field, sizeof (GstStructureField));
627       return;
628     }
629   }
630
631   g_array_append_val (structure->fields, *field);
632 }
633
634 /* If there is no field with the given ID, NULL is returned.
635  */
636 static GstStructureField *
637 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
638 {
639   GstStructureField *field;
640   guint i;
641
642   g_return_val_if_fail (structure != NULL, NULL);
643
644   for (i = 0; i < structure->fields->len; i++) {
645     field = GST_STRUCTURE_FIELD (structure, i);
646
647     if (field->name == field_id)
648       return field;
649   }
650
651   return NULL;
652 }
653
654 /* If there is no field with the given ID, NULL is returned.
655  */
656 static GstStructureField *
657 gst_structure_get_field (const GstStructure * structure,
658     const gchar * fieldname)
659 {
660   g_return_val_if_fail (structure != NULL, NULL);
661   g_return_val_if_fail (fieldname != NULL, NULL);
662
663   return gst_structure_id_get_field (structure,
664       g_quark_from_string (fieldname));
665 }
666
667 /**
668  * gst_structure_get_value:
669  * @structure: a #GstStructure
670  * @fieldname: the name of the field to get
671  *
672  * Get the value of the field with name @fieldname.
673  *
674  * Returns: the #GValue corresponding to the field with the given name.
675  */
676 const GValue *
677 gst_structure_get_value (const GstStructure * structure,
678     const gchar * fieldname)
679 {
680   GstStructureField *field;
681
682   g_return_val_if_fail (structure != NULL, NULL);
683   g_return_val_if_fail (fieldname != NULL, NULL);
684
685   field = gst_structure_get_field (structure, fieldname);
686   if (field == NULL)
687     return NULL;
688
689   return &field->value;
690 }
691
692 /**
693  * gst_structure_id_get_value:
694  * @structure: a #GstStructure
695  * @field: the #GQuark of the field to get
696  *
697  * Get the value of the field with GQuark @field.
698  *
699  * Returns: the #GValue corresponding to the field with the given name
700  *          identifier.
701  */
702 const GValue *
703 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
704 {
705   GstStructureField *gsfield;
706
707   g_return_val_if_fail (structure != NULL, NULL);
708
709   gsfield = gst_structure_id_get_field (structure, field);
710   if (gsfield == NULL)
711     return NULL;
712
713   return &gsfield->value;
714 }
715
716 /**
717  * gst_structure_remove_field:
718  * @structure: a #GstStructure
719  * @fieldname: the name of the field to remove
720  *
721  * Removes the field with the given name.  If the field with the given
722  * name does not exist, the structure is unchanged.
723  */
724 void
725 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
726 {
727   GstStructureField *field;
728   GQuark id;
729   guint i;
730
731   g_return_if_fail (structure != NULL);
732   g_return_if_fail (fieldname != NULL);
733   g_return_if_fail (IS_MUTABLE (structure));
734
735   id = g_quark_from_string (fieldname);
736
737   for (i = 0; i < structure->fields->len; i++) {
738     field = GST_STRUCTURE_FIELD (structure, i);
739
740     if (field->name == id) {
741       if (G_IS_VALUE (&field->value)) {
742         g_value_unset (&field->value);
743       }
744       structure->fields = g_array_remove_index (structure->fields, i);
745       return;
746     }
747   }
748 }
749
750 /**
751  * gst_structure_remove_fields:
752  * @structure: a #GstStructure
753  * @fieldname: the name of the field to remove
754  * @...: NULL-terminated list of more fieldnames to remove
755  *
756  * Removes the fields with the given names. If a field does not exist, the
757  * argument is ignored.
758  */
759 void
760 gst_structure_remove_fields (GstStructure * structure,
761     const gchar * fieldname, ...)
762 {
763   va_list varargs;
764
765   g_return_if_fail (structure != NULL);
766   g_return_if_fail (fieldname != NULL);
767   /* mutability checked in remove_field */
768
769   va_start (varargs, fieldname);
770
771   gst_structure_remove_fields_valist (structure, fieldname, varargs);
772
773   va_end (varargs);
774 }
775
776 /**
777  * gst_structure_remove_fields_valist:
778  * @structure: a #GstStructure
779  * @fieldname: the name of the field to remove
780  * @varargs: NULL-terminated list of more fieldnames to remove
781  *
782  * va_list form of gst_structure_remove_fields().
783  */
784 void
785 gst_structure_remove_fields_valist (GstStructure * structure,
786     const gchar * fieldname, va_list varargs)
787 {
788   gchar *field = (gchar *) fieldname;
789
790   g_return_if_fail (structure != NULL);
791   g_return_if_fail (fieldname != NULL);
792   /* mutability checked in remove_field */
793
794   while (field) {
795     gst_structure_remove_field (structure, field);
796     field = va_arg (varargs, char *);
797   }
798 }
799
800 /**
801  * gst_structure_remove_all_fields:
802  * @structure: a #GstStructure
803  *
804  * Removes all fields in a GstStructure.
805  */
806 void
807 gst_structure_remove_all_fields (GstStructure * structure)
808 {
809   GstStructureField *field;
810   int i;
811
812   g_return_if_fail (structure != NULL);
813   g_return_if_fail (IS_MUTABLE (structure));
814
815   for (i = structure->fields->len - 1; i >= 0; i--) {
816     field = GST_STRUCTURE_FIELD (structure, i);
817
818     if (G_IS_VALUE (&field->value)) {
819       g_value_unset (&field->value);
820     }
821     structure->fields = g_array_remove_index (structure->fields, i);
822   }
823 }
824
825 /**
826  * gst_structure_get_field_type:
827  * @structure: a #GstStructure
828  * @fieldname: the name of the field
829  *
830  * Finds the field with the given name, and returns the type of the
831  * value it contains.  If the field is not found, G_TYPE_INVALID is
832  * returned.
833  *
834  * Returns: the #GValue of the field
835  */
836 GType
837 gst_structure_get_field_type (const GstStructure * structure,
838     const gchar * fieldname)
839 {
840   GstStructureField *field;
841
842   g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
843   g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
844
845   field = gst_structure_get_field (structure, fieldname);
846   if (field == NULL)
847     return G_TYPE_INVALID;
848
849   return G_VALUE_TYPE (&field->value);
850 }
851
852 /**
853  * gst_structure_n_fields:
854  * @structure: a #GstStructure
855  *
856  * Get the number of fields in the structure.
857  *
858  * Returns: the number of fields in the structure
859  */
860 gint
861 gst_structure_n_fields (const GstStructure * structure)
862 {
863   g_return_val_if_fail (structure != NULL, 0);
864
865   return structure->fields->len;
866 }
867
868 /**
869  * gst_structure_nth_field_name:
870  * @structure: a #GstStructure
871  * @index: the index to get the name of
872  *
873  * Get the name of the given field number, counting from 0 onwards.
874  *
875  * Returns: the name of the given field number
876  */
877 const gchar *
878 gst_structure_nth_field_name (const GstStructure * structure, guint index)
879 {
880   GstStructureField *field;
881
882   g_return_val_if_fail (structure != NULL, NULL);
883   g_return_val_if_fail (index < structure->fields->len, NULL);
884
885   field = GST_STRUCTURE_FIELD (structure, index);
886
887   return g_quark_to_string (field->name);
888 }
889
890 /**
891  * gst_structure_foreach:
892  * @structure: a #GstStructure
893  * @func: a function to call for each field
894  * @user_data: private data
895  *
896  * Calls the provided function once for each field in the #GstStructure. The
897  * function must not modify the fields. Also see gst_structure_map_in_place().
898  *
899  * Returns: TRUE if the supplied function returns TRUE For each of the fields,
900  * FALSE otherwise.
901  */
902 gboolean
903 gst_structure_foreach (const GstStructure * structure,
904     GstStructureForeachFunc func, gpointer user_data)
905 {
906   guint i;
907   GstStructureField *field;
908   gboolean ret;
909
910   g_return_val_if_fail (structure != NULL, FALSE);
911   g_return_val_if_fail (func != NULL, FALSE);
912
913   for (i = 0; i < structure->fields->len; i++) {
914     field = GST_STRUCTURE_FIELD (structure, i);
915
916     ret = func (field->name, &field->value, user_data);
917     if (!ret)
918       return FALSE;
919   }
920
921   return TRUE;
922 }
923
924 /**
925  * gst_structure_map_in_place:
926  * @structure: a #GstStructure
927  * @func: a function to call for each field
928  * @user_data: private data
929  *
930  * Calls the provided function once for each field in the #GstStructure. In
931  * contrast to gst_structure_foreach(), the function may modify but not delete the
932  * fields. The structure must be mutable.
933  *
934  * Returns: TRUE if the supplied function returns TRUE For each of the fields,
935  * FALSE otherwise.
936  */
937 gboolean
938 gst_structure_map_in_place (GstStructure * structure,
939     GstStructureMapFunc func, gpointer user_data)
940 {
941   guint i;
942   GstStructureField *field;
943   gboolean ret;
944
945   g_return_val_if_fail (structure != NULL, FALSE);
946   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
947   g_return_val_if_fail (func != NULL, FALSE);
948
949   for (i = 0; i < structure->fields->len; i++) {
950     field = GST_STRUCTURE_FIELD (structure, i);
951
952     ret = func (field->name, &field->value, user_data);
953     if (!ret)
954       return FALSE;
955   }
956
957   return TRUE;
958 }
959
960 /**
961  * gst_structure_has_field:
962  * @structure: a #GstStructure
963  * @fieldname: the name of a field
964  *
965  * Check if @structure contains a field named @fieldname.
966  *
967  * Returns: TRUE if the structure contains a field with the given name
968  */
969 gboolean
970 gst_structure_has_field (const GstStructure * structure,
971     const gchar * fieldname)
972 {
973   GstStructureField *field;
974
975   g_return_val_if_fail (structure != NULL, 0);
976   g_return_val_if_fail (fieldname != NULL, 0);
977
978   field = gst_structure_get_field (structure, fieldname);
979
980   return (field != NULL);
981 }
982
983 /**
984  * gst_structure_has_field_typed:
985  * @structure: a #GstStructure
986  * @fieldname: the name of a field
987  * @type: the type of a value
988  *
989  * Check if @structure contains a field named @fieldname and with GType @type.
990  *
991  * Returns: TRUE if the structure contains a field with the given name and type
992  */
993 gboolean
994 gst_structure_has_field_typed (const GstStructure * structure,
995     const gchar * fieldname, GType type)
996 {
997   GstStructureField *field;
998
999   g_return_val_if_fail (structure != NULL, 0);
1000   g_return_val_if_fail (fieldname != NULL, 0);
1001
1002   field = gst_structure_get_field (structure, fieldname);
1003   if (field == NULL)
1004     return FALSE;
1005
1006   return (G_VALUE_TYPE (&field->value) == type);
1007 }
1008
1009
1010 /* utility functions */
1011
1012 /**
1013  * gst_structure_get_boolean:
1014  * @structure: a #GstStructure
1015  * @fieldname: the name of a field
1016  * @value: a pointer to a #gboolean to set
1017  *
1018  * Sets the boolean pointed to by @value corresponding to the value of the
1019  * given field.  Caller is responsible for making sure the field exists
1020  * and has the correct type.
1021  *
1022  * Returns: TRUE if the value could be set correctly. If there was no field
1023  * with @fieldname or the existing field did not contain a boolean, this
1024  * function returns FALSE.
1025  */
1026 gboolean
1027 gst_structure_get_boolean (const GstStructure * structure,
1028     const gchar * fieldname, gboolean * value)
1029 {
1030   GstStructureField *field;
1031
1032   g_return_val_if_fail (structure != NULL, FALSE);
1033   g_return_val_if_fail (fieldname != NULL, FALSE);
1034
1035   field = gst_structure_get_field (structure, fieldname);
1036
1037   if (field == NULL)
1038     return FALSE;
1039   if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
1040     return FALSE;
1041
1042   *value = g_value_get_boolean (&field->value);
1043
1044   return TRUE;
1045 }
1046
1047 /**
1048  * gst_structure_get_int:
1049  * @structure: a #GstStructure
1050  * @fieldname: the name of a field
1051  * @value: a pointer to an int to set
1052  *
1053  * Sets the int pointed to by @value corresponding to the value of the
1054  * given field.  Caller is responsible for making sure the field exists
1055  * and has the correct type.
1056  *
1057  * Returns: %TRUE if the value could be set correctly. If there was no field
1058  * with @fieldname or the existing field did not contain an int, this function
1059  * returns %FALSE.
1060  */
1061 gboolean
1062 gst_structure_get_int (const GstStructure * structure,
1063     const gchar * fieldname, gint * value)
1064 {
1065   GstStructureField *field;
1066
1067   g_return_val_if_fail (structure != NULL, FALSE);
1068   g_return_val_if_fail (fieldname != NULL, FALSE);
1069   g_return_val_if_fail (value != NULL, FALSE);
1070
1071   field = gst_structure_get_field (structure, fieldname);
1072
1073   if (field == NULL)
1074     return FALSE;
1075   if (!G_VALUE_HOLDS_INT (&field->value))
1076     return FALSE;
1077
1078   *value = g_value_get_int (&field->value);
1079
1080   return TRUE;
1081 }
1082
1083 /**
1084  * gst_structure_get_uint:
1085  * @structure: a #GstStructure
1086  * @fieldname: the name of a field
1087  * @value: a pointer to a uint to set
1088  *
1089  * Sets the uint pointed to by @value corresponding to the value of the
1090  * given field.  Caller is responsible for making sure the field exists
1091  * and has the correct type.
1092  *
1093  * Returns: %TRUE if the value could be set correctly. If there was no field
1094  * with @fieldname or the existing field did not contain a uint, this function
1095  * returns %FALSE.
1096  *
1097  * Since: 0.10.15
1098  */
1099 gboolean
1100 gst_structure_get_uint (const GstStructure * structure,
1101     const gchar * fieldname, guint * value)
1102 {
1103   GstStructureField *field;
1104
1105   g_return_val_if_fail (structure != NULL, FALSE);
1106   g_return_val_if_fail (fieldname != NULL, FALSE);
1107   g_return_val_if_fail (value != NULL, FALSE);
1108
1109   field = gst_structure_get_field (structure, fieldname);
1110
1111   if (field == NULL)
1112     return FALSE;
1113   if (!G_VALUE_HOLDS_UINT (&field->value))
1114     return FALSE;
1115
1116   *value = g_value_get_uint (&field->value);
1117
1118   return TRUE;
1119 }
1120
1121 /**
1122  * gst_structure_get_fourcc:
1123  * @structure: a #GstStructure
1124  * @fieldname: the name of a field
1125  * @value: a pointer to a #GstFourcc to set
1126  *
1127  * Sets the #GstFourcc pointed to by @value corresponding to the value of the
1128  * given field.  Caller is responsible for making sure the field exists
1129  * and has the correct type.
1130  *
1131  * Returns: TRUE if the value could be set correctly. If there was no field
1132  * with @fieldname or the existing field did not contain a fourcc, this function
1133  * returns FALSE.
1134  */
1135 gboolean
1136 gst_structure_get_fourcc (const GstStructure * structure,
1137     const gchar * fieldname, guint32 * value)
1138 {
1139   GstStructureField *field;
1140
1141   g_return_val_if_fail (structure != NULL, FALSE);
1142   g_return_val_if_fail (fieldname != NULL, FALSE);
1143   g_return_val_if_fail (value != NULL, FALSE);
1144
1145   field = gst_structure_get_field (structure, fieldname);
1146
1147   if (field == NULL)
1148     return FALSE;
1149   if (!GST_VALUE_HOLDS_FOURCC (&field->value))
1150     return FALSE;
1151
1152   *value = gst_value_get_fourcc (&field->value);
1153
1154   return TRUE;
1155 }
1156
1157 /**
1158  * gst_structure_get_date:
1159  * @structure: a #GstStructure
1160  * @fieldname: the name of a field
1161  * @value: a pointer to a #GDate to set
1162  *
1163  * Sets the date pointed to by @value corresponding to the date of the
1164  * given field.  Caller is responsible for making sure the field exists
1165  * and has the correct type.
1166  *
1167  * Returns: TRUE if the value could be set correctly. If there was no field
1168  * with @fieldname or the existing field did not contain a data, this function
1169  * returns FALSE.
1170  */
1171 gboolean
1172 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1173     GDate ** value)
1174 {
1175   GstStructureField *field;
1176
1177   g_return_val_if_fail (structure != NULL, FALSE);
1178   g_return_val_if_fail (fieldname != NULL, FALSE);
1179   g_return_val_if_fail (value != NULL, FALSE);
1180
1181   field = gst_structure_get_field (structure, fieldname);
1182
1183   if (field == NULL)
1184     return FALSE;
1185   if (!GST_VALUE_HOLDS_DATE (&field->value))
1186     return FALSE;
1187
1188   *value = g_value_dup_boxed (&field->value);
1189
1190   return TRUE;
1191 }
1192
1193 /**
1194  * gst_structure_get_clock_time:
1195  * @structure: a #GstStructure
1196  * @fieldname: the name of a field
1197  * @value: a pointer to a #GstClockTime to set
1198  *
1199  * Sets the clock time pointed to by @value corresponding to the clock time
1200  * of the given field.  Caller is responsible for making sure the field exists
1201  * and has the correct type.
1202  *
1203  * Returns: TRUE if the value could be set correctly. If there was no field
1204  * with @fieldname or the existing field did not contain a #GstClockTime, this 
1205  * function returns FALSE.
1206  */
1207 gboolean
1208 gst_structure_get_clock_time (const GstStructure * structure,
1209     const gchar * fieldname, GstClockTime * value)
1210 {
1211   GstStructureField *field;
1212
1213   g_return_val_if_fail (structure != NULL, FALSE);
1214   g_return_val_if_fail (fieldname != NULL, FALSE);
1215   g_return_val_if_fail (value != NULL, FALSE);
1216
1217   field = gst_structure_get_field (structure, fieldname);
1218
1219   if (field == NULL)
1220     return FALSE;
1221   if (!G_VALUE_HOLDS_UINT64 (&field->value))
1222     return FALSE;
1223
1224   *value = g_value_get_uint64 (&field->value);
1225
1226   return TRUE;
1227 }
1228
1229 /**
1230  * gst_structure_get_double:
1231  * @structure: a #GstStructure
1232  * @fieldname: the name of a field
1233  * @value: a pointer to a #GstFourcc to set
1234  *
1235  * Sets the double pointed to by @value corresponding to the value of the
1236  * given field.  Caller is responsible for making sure the field exists
1237  * and has the correct type.
1238  *
1239  * Returns: TRUE if the value could be set correctly. If there was no field
1240  * with @fieldname or the existing field did not contain a double, this 
1241  * function returns FALSE.
1242  */
1243 gboolean
1244 gst_structure_get_double (const GstStructure * structure,
1245     const gchar * fieldname, gdouble * value)
1246 {
1247   GstStructureField *field;
1248
1249   g_return_val_if_fail (structure != NULL, FALSE);
1250   g_return_val_if_fail (fieldname != NULL, FALSE);
1251   g_return_val_if_fail (value != NULL, FALSE);
1252
1253   field = gst_structure_get_field (structure, fieldname);
1254
1255   if (field == NULL)
1256     return FALSE;
1257   if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1258     return FALSE;
1259
1260   *value = g_value_get_double (&field->value);
1261
1262   return TRUE;
1263 }
1264
1265 /**
1266  * gst_structure_get_string:
1267  * @structure: a #GstStructure
1268  * @fieldname: the name of a field
1269  *
1270  * Finds the field corresponding to @fieldname, and returns the string
1271  * contained in the field's value.  Caller is responsible for making
1272  * sure the field exists and has the correct type.
1273  *
1274  * The string should not be modified, and remains valid until the next
1275  * call to a gst_structure_*() function with the given structure.
1276  *
1277  * Returns: a pointer to the string or NULL when the field did not exist
1278  * or did not contain a string.
1279  */
1280 const gchar *
1281 gst_structure_get_string (const GstStructure * structure,
1282     const gchar * fieldname)
1283 {
1284   GstStructureField *field;
1285
1286   g_return_val_if_fail (structure != NULL, NULL);
1287   g_return_val_if_fail (fieldname != NULL, NULL);
1288
1289   field = gst_structure_get_field (structure, fieldname);
1290
1291   if (field == NULL)
1292     return NULL;
1293   if (!G_VALUE_HOLDS_STRING (&field->value))
1294     return NULL;
1295
1296   return g_value_get_string (&field->value);
1297 }
1298
1299 /**
1300  * gst_structure_get_enum:
1301  * @structure: a #GstStructure
1302  * @fieldname: the name of a field
1303  * @enumtype: the enum type of a field
1304  * @value: a pointer to an int to set
1305  *
1306  * Sets the int pointed to by @value corresponding to the value of the
1307  * given field.  Caller is responsible for making sure the field exists,
1308  * has the correct type and that the enumtype is correct.
1309  *
1310  * Returns: TRUE if the value could be set correctly. If there was no field
1311  * with @fieldname or the existing field did not contain an enum of the given
1312  * type, this function returns FALSE.
1313  */
1314 gboolean
1315 gst_structure_get_enum (const GstStructure * structure,
1316     const gchar * fieldname, GType enumtype, gint * value)
1317 {
1318   GstStructureField *field;
1319
1320   g_return_val_if_fail (structure != NULL, FALSE);
1321   g_return_val_if_fail (fieldname != NULL, FALSE);
1322   g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1323   g_return_val_if_fail (value != NULL, FALSE);
1324
1325   field = gst_structure_get_field (structure, fieldname);
1326
1327   if (field == NULL)
1328     return FALSE;
1329   if (!G_VALUE_HOLDS_ENUM (&field->value))
1330     return FALSE;
1331   if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1332     return FALSE;
1333
1334   *value = g_value_get_enum (&field->value);
1335
1336   return TRUE;
1337 }
1338
1339 /**
1340  * gst_structure_get_fraction:
1341  * @structure: a #GstStructure
1342  * @fieldname: the name of a field
1343  * @value_numerator: a pointer to an int to set
1344  * @value_denominator: a pointer to an int to set
1345  *
1346  * Sets the integers pointed to by @value_numerator and @value_denominator 
1347  * corresponding to the value of the given field.  Caller is responsible 
1348  * for making sure the field exists and has the correct type.
1349  *
1350  * Returns: TRUE if the values could be set correctly. If there was no field
1351  * with @fieldname or the existing field did not contain a GstFraction, this 
1352  * function returns FALSE.
1353  */
1354 gboolean
1355 gst_structure_get_fraction (const GstStructure * structure,
1356     const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1357 {
1358   GstStructureField *field;
1359
1360   g_return_val_if_fail (structure != NULL, FALSE);
1361   g_return_val_if_fail (fieldname != NULL, FALSE);
1362   g_return_val_if_fail (value_numerator != NULL, FALSE);
1363   g_return_val_if_fail (value_denominator != NULL, FALSE);
1364
1365   field = gst_structure_get_field (structure, fieldname);
1366
1367   if (field == NULL)
1368     return FALSE;
1369   if (!GST_VALUE_HOLDS_FRACTION (&field->value))
1370     return FALSE;
1371
1372   *value_numerator = gst_value_get_fraction_numerator (&field->value);
1373   *value_denominator = gst_value_get_fraction_denominator (&field->value);
1374
1375   return TRUE;
1376 }
1377
1378 typedef struct _GstStructureAbbreviation
1379 {
1380   gchar *type_name;
1381   GType type;
1382 }
1383 GstStructureAbbreviation;
1384
1385 /* return a copy of an array of GstStructureAbbreviation containing all the
1386  * known type_string, GType maps, including abbreviations for common types */
1387 static GstStructureAbbreviation *
1388 gst_structure_get_abbrs (gint * n_abbrs)
1389 {
1390   static GstStructureAbbreviation *abbrs = NULL;
1391   static gint num = 0;
1392
1393   if (abbrs == NULL) {
1394     /* dynamically generate the array */
1395     GstStructureAbbreviation dyn_abbrs[] = {
1396       {"int", G_TYPE_INT}
1397       ,
1398       {"i", G_TYPE_INT}
1399       ,
1400       {"float", G_TYPE_FLOAT}
1401       ,
1402       {"f", G_TYPE_FLOAT}
1403       ,
1404       {"double", G_TYPE_DOUBLE}
1405       ,
1406       {"d", G_TYPE_DOUBLE}
1407       ,
1408       {"buffer", GST_TYPE_BUFFER}
1409       ,
1410       {"fourcc", GST_TYPE_FOURCC}
1411       ,
1412       {"4", GST_TYPE_FOURCC}
1413       ,
1414       {"fraction", GST_TYPE_FRACTION}
1415       ,
1416       {"boolean", G_TYPE_BOOLEAN}
1417       ,
1418       {"bool", G_TYPE_BOOLEAN}
1419       ,
1420       {"b", G_TYPE_BOOLEAN}
1421       ,
1422       {"string", G_TYPE_STRING}
1423       ,
1424       {"str", G_TYPE_STRING}
1425       ,
1426       {"s", G_TYPE_STRING}
1427       ,
1428       {"structure", GST_TYPE_STRUCTURE}
1429     };
1430     num = G_N_ELEMENTS (dyn_abbrs);
1431     /* permanently allocate and copy the array now */
1432     abbrs = g_new0 (GstStructureAbbreviation, num);
1433     memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * num);
1434   }
1435   *n_abbrs = num;
1436
1437   return abbrs;
1438 }
1439
1440 /* given a type_name that could be a type abbreviation or a registered GType,
1441  * return a matching GType */
1442 static GType
1443 gst_structure_gtype_from_abbr (const char *type_name)
1444 {
1445   int i;
1446   GstStructureAbbreviation *abbrs;
1447   gint n_abbrs;
1448
1449   g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1450
1451   abbrs = gst_structure_get_abbrs (&n_abbrs);
1452
1453   for (i = 0; i < n_abbrs; i++) {
1454     if (strcmp (type_name, abbrs[i].type_name) == 0) {
1455       return abbrs[i].type;
1456     }
1457   }
1458
1459   /* this is the fallback */
1460   return g_type_from_name (type_name);
1461 }
1462
1463 static const char *
1464 gst_structure_to_abbr (GType type)
1465 {
1466   int i;
1467   GstStructureAbbreviation *abbrs;
1468   gint n_abbrs;
1469
1470   g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1471
1472   abbrs = gst_structure_get_abbrs (&n_abbrs);
1473
1474   for (i = 0; i < n_abbrs; i++) {
1475     if (type == abbrs[i].type) {
1476       return abbrs[i].type_name;
1477     }
1478   }
1479
1480   return g_type_name (type);
1481 }
1482
1483 static GType
1484 gst_structure_value_get_generic_type (GValue * val)
1485 {
1486   if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1487       || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1488     GArray *array = g_value_peek_pointer (val);
1489
1490     if (array->len > 0) {
1491       GValue *value = &g_array_index (array, GValue, 0);
1492
1493       return gst_structure_value_get_generic_type (value);
1494     } else {
1495       return G_TYPE_INT;
1496     }
1497   } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1498     return G_TYPE_INT;
1499   } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1500     return G_TYPE_DOUBLE;
1501   } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1502     return GST_TYPE_FRACTION;
1503   }
1504   return G_VALUE_TYPE (val);
1505 }
1506
1507 /* keep in sync with gstvalue.c */
1508 #define GST_ASCII_IS_STRING(c) (g_ascii_isalnum((c)) || ((c) == '_') || \
1509       ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
1510       ((c) == '.'))
1511
1512 gboolean
1513 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1514     GString * s)
1515 {
1516   GstStructureField *field;
1517   guint i;
1518
1519   g_return_val_if_fail (s != NULL, FALSE);
1520
1521   g_string_append (s, g_quark_to_string (structure->name));
1522   for (i = 0; i < structure->fields->len; i++) {
1523     char *t;
1524     GType type;
1525
1526     field = GST_STRUCTURE_FIELD (structure, i);
1527
1528     t = gst_value_serialize (&field->value);
1529     type = gst_structure_value_get_generic_type (&field->value);
1530
1531     g_string_append_len (s, ", ", 2);
1532     /* FIXME: do we need to escape fieldnames? */
1533     g_string_append (s, g_quark_to_string (field->name));
1534     g_string_append_len (s, "=(", 2);
1535     g_string_append (s, gst_structure_to_abbr (type));
1536     g_string_append_c (s, ')');
1537     g_string_append (s, GST_STR_NULL (t));
1538     g_free (t);
1539   }
1540
1541   g_string_append_c (s, ';');
1542   return TRUE;
1543 }
1544
1545 /**
1546  * gst_structure_to_string:
1547  * @structure: a #GstStructure
1548  *
1549  * Converts @structure to a human-readable string representation.
1550  *
1551  * For debugging purposes its easier to do something like this:
1552  * |[
1553  * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1554  * ]|
1555  * This prints the structure in human readble form.
1556  *
1557  * Returns: a pointer to string allocated by g_malloc(). g_free() after
1558  * usage.
1559  */
1560 gchar *
1561 gst_structure_to_string (const GstStructure * structure)
1562 {
1563   GString *s;
1564
1565   /* NOTE:  This function is potentially called by the debug system,
1566    * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1567    * should be careful to avoid recursion.  This includes any functions
1568    * called by gst_structure_to_string.  In particular, calls should
1569    * not use the GST_PTR_FORMAT extension.  */
1570
1571   g_return_val_if_fail (structure != NULL, NULL);
1572
1573   /* we estimate a minimum size based on the number of fields in order to
1574    * avoid unnecessary reallocs within GString */
1575   s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1576   priv_gst_structure_append_to_gstring (structure, s);
1577   return g_string_free (s, FALSE);
1578 }
1579
1580 /*
1581  * r will still point to the string. if end == next, the string will not be
1582  * null-terminated. In all other cases it will be.
1583  * end = pointer to char behind end of string, next = pointer to start of
1584  * unread data.
1585  * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1586  */
1587 static gboolean
1588 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next)
1589 {
1590   gchar *w;
1591
1592   if (*s == 0)
1593     return FALSE;
1594
1595   if (*s != '"') {
1596     int ret;
1597
1598     ret = gst_structure_parse_simple_string (s, end);
1599     *next = *end;
1600
1601     return ret;
1602   }
1603
1604   w = s;
1605   s++;
1606   while (*s != '"') {
1607     if (*s == 0)
1608       return FALSE;
1609
1610     if (*s == '\\') {
1611       s++;
1612     }
1613
1614     *w = *s;
1615     w++;
1616     s++;
1617   }
1618   s++;
1619
1620   *end = w;
1621   *next = s;
1622
1623   return TRUE;
1624 }
1625
1626 static gboolean
1627 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1628     GType type)
1629 {
1630   GValue value1 = { 0 };
1631   GValue value2 = { 0 };
1632   GType range_type;
1633   gboolean ret;
1634
1635   if (*s != '[')
1636     return FALSE;
1637   s++;
1638
1639   ret = gst_structure_parse_value (s, &s, &value1, type);
1640   if (ret == FALSE)
1641     return FALSE;
1642
1643   while (g_ascii_isspace (*s))
1644     s++;
1645
1646   if (*s != ',')
1647     return FALSE;
1648   s++;
1649
1650   while (g_ascii_isspace (*s))
1651     s++;
1652
1653   ret = gst_structure_parse_value (s, &s, &value2, type);
1654   if (ret == FALSE)
1655     return FALSE;
1656
1657   while (g_ascii_isspace (*s))
1658     s++;
1659
1660   if (*s != ']')
1661     return FALSE;
1662   s++;
1663
1664   if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1665     return FALSE;
1666
1667   if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1668     range_type = GST_TYPE_DOUBLE_RANGE;
1669     g_value_init (value, range_type);
1670     gst_value_set_double_range (value, g_value_get_double (&value1),
1671         g_value_get_double (&value2));
1672   } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1673     range_type = GST_TYPE_INT_RANGE;
1674     g_value_init (value, range_type);
1675     gst_value_set_int_range (value, g_value_get_int (&value1),
1676         g_value_get_int (&value2));
1677   } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
1678     range_type = GST_TYPE_FRACTION_RANGE;
1679     g_value_init (value, range_type);
1680     gst_value_set_fraction_range (value, &value1, &value2);
1681   } else {
1682     return FALSE;
1683   }
1684
1685   *after = s;
1686   return TRUE;
1687 }
1688
1689 static gboolean
1690 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
1691     GType type, GType list_type, char begin, char end)
1692 {
1693   GValue list_value = { 0 };
1694   gboolean ret;
1695   GArray *array;
1696
1697   g_value_init (value, list_type);
1698   array = g_value_peek_pointer (value);
1699
1700   if (*s != begin)
1701     return FALSE;
1702   s++;
1703
1704   while (g_ascii_isspace (*s))
1705     s++;
1706   if (*s == end) {
1707     s++;
1708     *after = s;
1709     return TRUE;
1710   }
1711
1712   ret = gst_structure_parse_value (s, &s, &list_value, type);
1713   if (ret == FALSE)
1714     return FALSE;
1715
1716   g_array_append_val (array, list_value);
1717
1718   while (g_ascii_isspace (*s))
1719     s++;
1720
1721   while (*s != end) {
1722     if (*s != ',')
1723       return FALSE;
1724     s++;
1725
1726     while (g_ascii_isspace (*s))
1727       s++;
1728
1729     memset (&list_value, 0, sizeof (list_value));
1730     ret = gst_structure_parse_value (s, &s, &list_value, type);
1731     if (ret == FALSE)
1732       return FALSE;
1733
1734     g_array_append_val (array, list_value);
1735     while (g_ascii_isspace (*s))
1736       s++;
1737   }
1738
1739   s++;
1740
1741   *after = s;
1742   return TRUE;
1743 }
1744
1745 static gboolean
1746 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
1747 {
1748   return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
1749       '{', '}');
1750 }
1751
1752 static gboolean
1753 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
1754     GType type)
1755 {
1756   return gst_structure_parse_any_list (s, after, value, type,
1757       GST_TYPE_ARRAY, '<', '>');
1758 }
1759
1760 static gboolean
1761 gst_structure_parse_simple_string (gchar * str, gchar ** end)
1762 {
1763   char *s = str;
1764
1765   while (GST_ASCII_IS_STRING (*s)) {
1766     s++;
1767   }
1768
1769   *end = s;
1770
1771   return (s != str);
1772 }
1773
1774 static gboolean
1775 gst_structure_parse_field (gchar * str,
1776     gchar ** after, GstStructureField * field)
1777 {
1778   gchar *name;
1779   gchar *name_end;
1780   gchar *s;
1781   gchar c;
1782
1783   s = str;
1784
1785   while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1786     s++;
1787   name = s;
1788   if (!gst_structure_parse_simple_string (s, &name_end))
1789     return FALSE;
1790
1791   s = name_end;
1792   while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1793     s++;
1794
1795   if (*s != '=')
1796     return FALSE;
1797   s++;
1798
1799   c = *name_end;
1800   *name_end = 0;
1801   field->name = g_quark_from_string (name);
1802   *name_end = c;
1803
1804   if (!gst_structure_parse_value (s, &s, &field->value, G_TYPE_INVALID))
1805     return FALSE;
1806
1807   *after = s;
1808   return TRUE;
1809 }
1810
1811 static gboolean
1812 gst_structure_parse_value (gchar * str,
1813     gchar ** after, GValue * value, GType default_type)
1814 {
1815   gchar *type_name;
1816   gchar *type_end;
1817   gchar *value_s;
1818   gchar *value_end;
1819   gchar *s;
1820   gchar c;
1821   int ret = 0;
1822   GType type = default_type;
1823
1824
1825   s = str;
1826   while (g_ascii_isspace (*s))
1827     s++;
1828
1829   /* check if there's a (type_name) 'cast' */
1830   type_name = NULL;
1831   if (*s == '(') {
1832     s++;
1833     while (g_ascii_isspace (*s))
1834       s++;
1835     type_name = s;
1836     if (!gst_structure_parse_simple_string (s, &type_end))
1837       return FALSE;
1838     s = type_end;
1839     while (g_ascii_isspace (*s))
1840       s++;
1841     if (*s != ')')
1842       return FALSE;
1843     s++;
1844     while (g_ascii_isspace (*s))
1845       s++;
1846
1847     c = *type_end;
1848     *type_end = 0;
1849     type = gst_structure_gtype_from_abbr (type_name);
1850     *type_end = c;
1851
1852     if (type == G_TYPE_INVALID)
1853       return FALSE;
1854   }
1855
1856   while (g_ascii_isspace (*s))
1857     s++;
1858   if (*s == '[') {
1859     ret = gst_structure_parse_range (s, &s, value, type);
1860   } else if (*s == '{') {
1861     ret = gst_structure_parse_list (s, &s, value, type);
1862   } else if (*s == '<') {
1863     ret = gst_structure_parse_array (s, &s, value, type);
1864   } else {
1865     value_s = s;
1866     if (!gst_structure_parse_string (s, &value_end, &s))
1867       return FALSE;
1868
1869     c = *value_end;
1870     *value_end = 0;
1871     if (type == G_TYPE_INVALID) {
1872       GType try_types[] =
1873           { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_BOOLEAN,
1874         G_TYPE_STRING
1875       };
1876       int i;
1877
1878       for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
1879         g_value_init (value, try_types[i]);
1880         ret = gst_value_deserialize (value, value_s);
1881         if (ret)
1882           break;
1883         g_value_unset (value);
1884       }
1885     } else {
1886       g_value_init (value, type);
1887
1888       ret = gst_value_deserialize (value, value_s);
1889     }
1890     *value_end = c;
1891   }
1892
1893   *after = s;
1894
1895   return ret;
1896 }
1897
1898 /**
1899  * gst_structure_from_string:
1900  * @string: a string representation of a #GstStructure.
1901  * @end: pointer to store the end of the string in.
1902  *
1903  * Creates a #GstStructure from a string representation.
1904  * If end is not NULL, a pointer to the place inside the given string
1905  * where parsing ended will be returned.
1906  *
1907  * Returns: a new #GstStructure or NULL when the string could not
1908  * be parsed. Free with gst_structure_free() after use.
1909  */
1910 GstStructure *
1911 gst_structure_from_string (const gchar * string, gchar ** end)
1912 {
1913   char *name;
1914   char *copy;
1915   char *w;
1916   char *r;
1917   char save;
1918   GstStructure *structure = NULL;
1919   GstStructureField field = { 0 };
1920
1921   g_return_val_if_fail (string != NULL, NULL);
1922
1923   copy = g_strdup (string);
1924   r = copy;
1925
1926   /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
1927   while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1928               && g_ascii_isspace (r[1]))))
1929     r++;
1930
1931   name = r;
1932   if (!gst_structure_parse_string (r, &w, &r)) {
1933     GST_WARNING ("Failed to parse structure string");
1934     goto error;
1935   }
1936
1937   save = *w;
1938   *w = 0;
1939   structure = gst_structure_empty_new (name);
1940   *w = save;
1941
1942   if (structure == NULL)
1943     goto error;
1944
1945   do {
1946     while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1947                 && g_ascii_isspace (r[1]))))
1948       r++;
1949     if (*r == ';') {
1950       /* end of structure, get the next char and finish */
1951       r++;
1952       break;
1953     }
1954     if (*r == '\0') {
1955       /* accept \0 as end delimiter */
1956       break;
1957     }
1958     if (*r != ',') {
1959       GST_WARNING ("Failed to find delimiter, r=%s", r);
1960       goto error;
1961     }
1962     r++;
1963     while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1964                 && g_ascii_isspace (r[1]))))
1965       r++;
1966
1967     memset (&field, 0, sizeof (field));
1968     if (!gst_structure_parse_field (r, &r, &field))
1969       goto error;
1970     gst_structure_set_field (structure, &field);
1971
1972   } while (TRUE);
1973
1974   if (end)
1975     *end = (char *) string + (r - copy);
1976   else if (*r)
1977     g_warning ("gst_structure_from_string did not consume whole string,"
1978         " but caller did not provide end pointer (\"%s\")", string);
1979
1980   g_free (copy);
1981   return structure;
1982
1983 error:
1984   if (structure)
1985     gst_structure_free (structure);
1986   g_free (copy);
1987   return NULL;
1988 }
1989
1990 static void
1991 gst_structure_transform_to_string (const GValue * src_value,
1992     GValue * dest_value)
1993 {
1994   g_return_if_fail (src_value != NULL);
1995   g_return_if_fail (dest_value != NULL);
1996
1997   dest_value->data[0].v_pointer =
1998       gst_structure_to_string (src_value->data[0].v_pointer);
1999 }
2000
2001 static GstStructure *
2002 gst_structure_copy_conditional (const GstStructure * structure)
2003 {
2004   if (structure)
2005     return gst_structure_copy (structure);
2006   return NULL;
2007 }
2008
2009 /* fixate utility functions */
2010
2011 /**
2012  * gst_structure_fixate_field_nearest_int:
2013  * @structure: a #GstStructure
2014  * @field_name: a field in @structure
2015  * @target: the target value of the fixation
2016  *
2017  * Fixates a #GstStructure by changing the given field to the nearest
2018  * integer to @target that is a subset of the existing field.
2019  *
2020  * Returns: TRUE if the structure could be fixated
2021  */
2022 gboolean
2023 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2024     const char *field_name, int target)
2025 {
2026   const GValue *value;
2027
2028   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2029   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2030
2031   value = gst_structure_get_value (structure, field_name);
2032
2033   if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2034     /* already fixed */
2035     return FALSE;
2036   } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2037     int x;
2038
2039     x = gst_value_get_int_range_min (value);
2040     if (target < x)
2041       target = x;
2042     x = gst_value_get_int_range_max (value);
2043     if (target > x)
2044       target = x;
2045     gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2046     return TRUE;
2047   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2048     const GValue *list_value;
2049     int i, n;
2050     int best = 0;
2051     int best_index = -1;
2052
2053     n = gst_value_list_get_size (value);
2054     for (i = 0; i < n; i++) {
2055       list_value = gst_value_list_get_value (value, i);
2056       if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2057         int x = g_value_get_int (list_value);
2058
2059         if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2060           best_index = i;
2061           best = x;
2062         }
2063       }
2064     }
2065     if (best_index != -1) {
2066       gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2067       return TRUE;
2068     }
2069     return FALSE;
2070   }
2071
2072   return FALSE;
2073 }
2074
2075 /**
2076  * gst_structure_fixate_field_nearest_double:
2077  * @structure: a #GstStructure
2078  * @field_name: a field in @structure
2079  * @target: the target value of the fixation
2080  *
2081  * Fixates a #GstStructure by changing the given field to the nearest
2082  * double to @target that is a subset of the existing field.
2083  *
2084  * Returns: TRUE if the structure could be fixated
2085  */
2086 gboolean
2087 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2088     const char *field_name, double target)
2089 {
2090   const GValue *value;
2091
2092   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2093   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2094
2095   value = gst_structure_get_value (structure, field_name);
2096
2097   if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2098     /* already fixed */
2099     return FALSE;
2100   } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2101     double x;
2102
2103     x = gst_value_get_double_range_min (value);
2104     if (target < x)
2105       target = x;
2106     x = gst_value_get_double_range_max (value);
2107     if (target > x)
2108       target = x;
2109     gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2110     return TRUE;
2111   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2112     const GValue *list_value;
2113     int i, n;
2114     double best = 0;
2115     int best_index = -1;
2116
2117     n = gst_value_list_get_size (value);
2118     for (i = 0; i < n; i++) {
2119       list_value = gst_value_list_get_value (value, i);
2120       if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2121         double x = g_value_get_double (list_value);
2122
2123         if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2124           best_index = i;
2125           best = x;
2126         }
2127       }
2128     }
2129     if (best_index != -1) {
2130       gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2131       return TRUE;
2132     }
2133     return FALSE;
2134   }
2135
2136   return FALSE;
2137
2138 }
2139
2140 /**
2141  * gst_structure_fixate_field_boolean:
2142  * @structure: a #GstStructure
2143  * @field_name: a field in @structure
2144  * @target: the target value of the fixation
2145  *
2146  * Fixates a #GstStructure by changing the given @field_name field to the given
2147  * @target boolean if that field is not fixed yet.
2148  *
2149  * Returns: TRUE if the structure could be fixated
2150  */
2151 gboolean
2152 gst_structure_fixate_field_boolean (GstStructure * structure,
2153     const char *field_name, gboolean target)
2154 {
2155   const GValue *value;
2156
2157   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2158   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2159
2160   value = gst_structure_get_value (structure, field_name);
2161
2162   if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2163     /* already fixed */
2164     return FALSE;
2165   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2166     const GValue *list_value;
2167     int i, n;
2168     int best = 0;
2169     int best_index = -1;
2170
2171     n = gst_value_list_get_size (value);
2172     for (i = 0; i < n; i++) {
2173       list_value = gst_value_list_get_value (value, i);
2174       if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2175         gboolean x = g_value_get_boolean (list_value);
2176
2177         if (best_index == -1 || x == target) {
2178           best_index = i;
2179           best = x;
2180         }
2181       }
2182     }
2183     if (best_index != -1) {
2184       gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2185       return TRUE;
2186     }
2187     return FALSE;
2188   }
2189
2190   return FALSE;
2191 }
2192
2193 /**
2194  * gst_structure_fixate_field_nearest_fraction:
2195  * @structure: a #GstStructure
2196  * @field_name: a field in @structure
2197  * @target_numerator: The numerator of the target value of the fixation
2198  * @target_denominator: The denominator of the target value of the fixation
2199  *
2200  * Fixates a #GstStructure by changing the given field to the nearest
2201  * fraction to @target_numerator/@target_denominator that is a subset 
2202  * of the existing field.
2203  *
2204  * Returns: TRUE if the structure could be fixated
2205  */
2206 gboolean
2207 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2208     const char *field_name, const gint target_numerator,
2209     const gint target_denominator)
2210 {
2211   const GValue *value;
2212
2213   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2214   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2215
2216   value = gst_structure_get_value (structure, field_name);
2217
2218   if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2219     /* already fixed */
2220     return FALSE;
2221   } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2222     const GValue *x, *new_value;
2223     GValue target = { 0 };
2224     g_value_init (&target, GST_TYPE_FRACTION);
2225     gst_value_set_fraction (&target, target_numerator, target_denominator);
2226
2227     new_value = &target;
2228     x = gst_value_get_fraction_range_min (value);
2229     if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2230       new_value = x;
2231     x = gst_value_get_fraction_range_max (value);
2232     if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2233       new_value = x;
2234
2235     gst_structure_set_value (structure, field_name, new_value);
2236     g_value_unset (&target);
2237     return TRUE;
2238   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2239     const GValue *list_value;
2240     int i, n;
2241     const GValue *best = NULL;
2242     gdouble target;
2243     gdouble cur_diff;
2244     gdouble best_diff = G_MAXDOUBLE;
2245
2246     target = (gdouble) target_numerator / (gdouble) target_denominator;
2247
2248     GST_DEBUG ("target %g, best %g", target, best_diff);
2249
2250     best = NULL;
2251
2252     n = gst_value_list_get_size (value);
2253     for (i = 0; i < n; i++) {
2254       list_value = gst_value_list_get_value (value, i);
2255       if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2256         gint num, denom;
2257         gdouble list_double;
2258
2259         num = gst_value_get_fraction_numerator (list_value);
2260         denom = gst_value_get_fraction_denominator (list_value);
2261
2262         list_double = ((gdouble) num / (gdouble) denom);
2263         cur_diff = target - list_double;
2264
2265         GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2266
2267         if (cur_diff < 0)
2268           cur_diff = -cur_diff;
2269
2270         if (!best || cur_diff < best_diff) {
2271           GST_DEBUG ("new best %g", list_double);
2272           best = list_value;
2273           best_diff = cur_diff;
2274         }
2275       }
2276     }
2277     if (best != NULL) {
2278       gst_structure_set_value (structure, field_name, best);
2279       return TRUE;
2280     }
2281   }
2282
2283   return FALSE;
2284 }