2 * Copyright (C) 2003 David A. Schleef <ds@schleef.org>
4 * gststructure.c: lists of { GQuark, GValue } tuples
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.
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.
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.
28 #include "gst_private.h"
30 #include <gobject/gvaluecollector.h>
32 typedef struct _GstStructureField GstStructureField;
34 struct _GstStructureField
40 #define GST_STRUCTURE_FIELD(structure, index) \
41 &g_array_index((structure)->fields, GstStructureField, (index))
43 static void gst_structure_set_field (GstStructure * structure,
44 GstStructureField * field);
45 static GstStructureField *gst_structure_get_field (const GstStructure *
46 structure, const gchar * fieldname);
47 static GstStructureField *gst_structure_id_get_field (const GstStructure *
48 structure, GQuark field);
49 static void gst_structure_transform_to_string (const GValue * src_value,
51 static GstStructure *gst_structure_copy_conditional (const GstStructure *
53 static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
54 GValue * value, GType default_type);
55 static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
58 gst_structure_get_type (void)
60 static GType gst_structure_type;
62 if (!gst_structure_type) {
63 gst_structure_type = g_boxed_type_register_static ("GstStructure",
64 (GBoxedCopyFunc) gst_structure_copy_conditional,
65 (GBoxedFreeFunc) gst_structure_free);
67 g_value_register_transform_func (gst_structure_type, G_TYPE_STRING,
68 gst_structure_transform_to_string);
71 return gst_structure_type;
75 * gst_structure_id_empty_new:
76 * @quark: name of new structure
78 * Creates a new, empty #GstStructure with the given name.
80 * Returns: a new, empty #GstStructure
83 gst_structure_id_empty_new (GQuark quark)
85 GstStructure *structure;
87 g_return_val_if_fail (quark != 0, NULL);
89 structure = g_new0 (GstStructure, 1);
90 structure->name = quark;
91 structure->fields = g_array_new (FALSE, TRUE, sizeof (GstStructureField));
97 * gst_structure_empty_new:
98 * @name: name of new structure
100 * Creates a new, empty #GstStructure with the given name.
102 * Returns: a new, empty #GstStructure
105 gst_structure_empty_new (const gchar * name)
107 GstStructure *structure;
109 g_return_val_if_fail (name != NULL, NULL);
111 structure = g_new0 (GstStructure, 1);
112 structure->name = g_quark_from_string (name);
113 structure->fields = g_array_new (FALSE, TRUE, sizeof (GstStructureField));
120 * @name: name of new structure
121 * @firstfield: name of first field to set
122 * @...: additional arguments
124 * Creates a new #GstStructure with the given name. Parses the
125 * list of variable arguments and sets fields to the values listed.
126 * Variable arguments should be passed as field name, field type,
127 * and value. Last variable argument should be NULL.
129 * Returns: a new #GstStructure
132 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
134 GstStructure *structure;
137 g_return_val_if_fail (name != NULL, NULL);
139 va_start (varargs, firstfield);
141 structure = gst_structure_new_valist (name, firstfield, varargs);
149 * gst_structure_new_valist:
150 * @name: name of new structure
151 * @firstfield: name of first field to set
152 * @varargs: variable argument list
154 * Creates a new #GstStructure with the given name. Structure fields
155 * are set according to the varargs in a manner similar to
156 * @gst_structure_new.
158 * Returns: a new #GstStructure
161 gst_structure_new_valist (const gchar * name,
162 const gchar * firstfield, va_list varargs)
164 GstStructure *structure;
166 g_return_val_if_fail (name != NULL, NULL);
168 structure = gst_structure_empty_new (name);
169 gst_structure_set_valist (structure, firstfield, varargs);
175 * gst_structure_copy:
176 * @structure: a #GstStructure to duplicate
178 * Duplicates a #GstStructure and all its fields and values.
180 * Returns: a new #GstStructure.
183 gst_structure_copy (const GstStructure * structure)
185 GstStructure *new_structure;
186 GstStructureField *field;
189 g_return_val_if_fail (structure != NULL, NULL);
191 new_structure = gst_structure_empty_new (g_quark_to_string (structure->name));
192 new_structure->name = structure->name;
194 for (i = 0; i < structure->fields->len; i++) {
195 GstStructureField new_field = { 0 };
197 field = GST_STRUCTURE_FIELD (structure, i);
199 new_field.name = field->name;
200 gst_value_init_and_copy (&new_field.value, &field->value);
201 g_array_append_val (new_structure->fields, new_field);
204 return new_structure;
208 * gst_structure_free:
209 * @structure: the #GstStructure to free
211 * Frees a #GstStructure and all its fields and values.
214 gst_structure_free (GstStructure * structure)
216 GstStructureField *field;
219 g_return_if_fail (structure != NULL);
221 for (i = 0; i < structure->fields->len; i++) {
222 field = GST_STRUCTURE_FIELD (structure, i);
224 if (G_IS_VALUE (&field->value)) {
225 g_value_unset (&field->value);
228 g_array_free (structure->fields, TRUE);
230 memset (structure, 0xff, sizeof (GstStructure));
236 * gst_structure_get_name:
237 * @structure: a #GstStructure
241 * Returns: the name of the structure.
244 gst_structure_get_name (const GstStructure * structure)
246 g_return_val_if_fail (structure != NULL, NULL);
248 return g_quark_to_string (structure->name);
252 * gst_structure_get_name:
253 * @structure: a #GstStructure
257 * Returns: the quark representing the name of the structure.
260 gst_structure_get_name_id (const GstStructure * structure)
262 g_return_val_if_fail (structure != NULL, 0);
264 return structure->name;
268 * gst_structure_set_name:
269 * @structure: a #GstStructure
270 * @name: the new name of the structure
272 * Sets the name of the structure to the given name. The string
273 * provided is copied before being used.
276 gst_structure_set_name (GstStructure * structure, const gchar * name)
278 g_return_if_fail (structure != NULL);
279 g_return_if_fail (name != NULL);
281 structure->name = g_quark_from_string (name);
285 * gst_structure_id_set_value:
286 * @structure: a #GstStructure
287 * @field: a #GQuark representing a field
288 * @value: the new value of the field
290 * Sets the field with the given ID to the provided value. If the field
291 * does not exist, it is created. If the field exists, the previous
295 gst_structure_id_set_value (GstStructure * structure,
296 GQuark field, const GValue * value)
298 GstStructureField gsfield = { 0, {0,} };
300 g_return_if_fail (structure != NULL);
301 g_return_if_fail (G_IS_VALUE (value));
303 gsfield.name = field;
304 gst_value_init_and_copy (&gsfield.value, value);
306 gst_structure_set_field (structure, &gsfield);
310 * gst_structure_set_value:
311 * @structure: a #GstStructure
312 * @fieldname: the name of the field to set
313 * @value: the new value of the field
315 * Sets the field with the given name to the provided value. If the field
316 * does not exist, it is created. If the field exists, the previous
320 gst_structure_set_value (GstStructure * structure,
321 const gchar * fieldname, const GValue * value)
323 g_return_if_fail (structure != NULL);
324 g_return_if_fail (fieldname != NULL);
325 g_return_if_fail (G_IS_VALUE (value));
327 gst_structure_id_set_value (structure, g_quark_from_string (fieldname),
333 * @structure: a #GstStructure
334 * @fieldname: the name of the field to set
335 * @...: variable arguments
337 * Parses the variable arguments and sets fields accordingly.
338 * Variable arguments should be in the form field name, field type
339 * (as a GType), value. The last variable argument should be NULL.
342 gst_structure_set (GstStructure * structure, const gchar * field, ...)
346 g_return_if_fail (structure != NULL);
348 va_start (varargs, field);
350 gst_structure_set_valist (structure, field, varargs);
356 * gst_structure_set_valist:
357 * @structure: a #GstStructure
358 * @fieldname: the name of the field to set
359 * @varargs: variable arguments
361 * va_list form of #gst_structure_set.
364 gst_structure_set_valist (GstStructure * structure,
365 const gchar * fieldname, va_list varargs)
372 g_return_if_fail (structure != NULL);
375 GstStructureField field = { 0 };
377 field.name = g_quark_from_string (fieldname);
379 type = va_arg (varargs, GType);
383 i = va_arg (varargs, int);
385 g_value_init (&field.value, G_TYPE_INT);
386 g_value_set_int (&field.value, i);
389 d = va_arg (varargs, double);
391 g_value_init (&field.value, G_TYPE_DOUBLE);
392 g_value_set_double (&field.value, d);
395 i = va_arg (varargs, int);
397 g_value_init (&field.value, G_TYPE_BOOLEAN);
398 g_value_set_boolean (&field.value, i);
401 s = va_arg (varargs, char *);
403 g_value_init (&field.value, G_TYPE_STRING);
404 g_value_set_string (&field.value, s);
407 if (type == GST_TYPE_FOURCC) {
408 i = va_arg (varargs, int);
410 g_value_init (&field.value, GST_TYPE_FOURCC);
411 gst_value_set_fourcc (&field.value, i);
412 } else if (type == GST_TYPE_INT_RANGE) {
414 min = va_arg (varargs, int);
415 max = va_arg (varargs, int);
417 g_value_init (&field.value, GST_TYPE_INT_RANGE);
418 gst_value_set_int_range (&field.value, min, max);
419 } else if (type == GST_TYPE_DOUBLE_RANGE) {
421 min = va_arg (varargs, double);
422 max = va_arg (varargs, double);
424 g_value_init (&field.value, GST_TYPE_DOUBLE_RANGE);
425 gst_value_set_double_range (&field.value, min, max);
426 } else if (type == GST_TYPE_BUFFER) {
427 GstBuffer *buffer = va_arg (varargs, GstBuffer *);
429 g_value_init (&field.value, GST_TYPE_BUFFER);
430 g_value_set_boxed (&field.value, buffer);
432 g_critical ("unimplemented vararg field type %d\n", (int) type);
438 gst_structure_set_field (structure, &field);
440 fieldname = va_arg (varargs, gchar *);
445 * gst_structure_set_field:
446 * @structure: a #GstStructure
447 * @field: the #GstStructureField to set
449 * Sets a field in the structure. If the structure currently contains
450 * a field with the same name, it is replaced with the provided field.
451 * Otherwise, the field is added to the structure. The field's value
452 * is not deeply copied.
454 * This function is intended mainly for internal use. The function
455 * #gst_structure_set() is recommended instead of this one.
458 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
460 GstStructureField *f;
463 for (i = 0; i < structure->fields->len; i++) {
464 f = GST_STRUCTURE_FIELD (structure, i);
466 if (f->name == field->name) {
467 g_value_unset (&f->value);
468 memcpy (f, field, sizeof (GstStructureField));
473 g_array_append_val (structure->fields, *field);
476 /* FIXME: is this private ? if so remove gtk-doc
477 * gst_structure_id_get_field:
478 * @structure: a #GstStructure
479 * @field_id: the GQuark of the field to get
481 * Gets the specified field from the structure. If there is no
482 * field with the given ID, NULL is returned.
484 * Returns: the #GstStructureField with the given ID
486 static GstStructureField *
487 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
489 GstStructureField *field;
492 g_return_val_if_fail (structure != NULL, NULL);
494 for (i = 0; i < structure->fields->len; i++) {
495 field = GST_STRUCTURE_FIELD (structure, i);
497 if (field->name == field_id)
505 * gst_structure_get_field:
506 * @structure: a #GstStructure
507 * @fieldname: the name of the field to get
509 * Gets the specified field from the structure. If there is no
510 * field with the given ID, NULL is returned.
512 * Returns: the #GstStructureField with the given name
514 static GstStructureField *
515 gst_structure_get_field (const GstStructure * structure,
516 const gchar * fieldname)
518 g_return_val_if_fail (structure != NULL, NULL);
519 g_return_val_if_fail (fieldname != NULL, NULL);
521 return gst_structure_id_get_field (structure,
522 g_quark_from_string (fieldname));
526 * gst_structure_get_value:
527 * @structure: a #GstStructure
528 * @fieldname: the name of the field to get
532 * Returns: the #GValue corresponding to the field with the given name.
535 gst_structure_get_value (const GstStructure * structure,
536 const gchar * fieldname)
538 GstStructureField *field;
540 g_return_val_if_fail (structure != NULL, NULL);
541 g_return_val_if_fail (fieldname != NULL, NULL);
543 field = gst_structure_get_field (structure, fieldname);
547 return &field->value;
551 * gst_structure_id_get_value:
552 * @structure: a #GstStructure
553 * @field: the #GQuark of the field to get
557 * Returns: the #GValue corresponding to the field with the given name
561 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
563 GstStructureField *gsfield;
565 g_return_val_if_fail (structure != NULL, NULL);
567 gsfield = gst_structure_id_get_field (structure, field);
571 return &gsfield->value;
575 * gst_structure_remove_field:
576 * @structure: a #GstStructure
577 * @fieldname: the name of the field to remove
579 * Removes the field with the given name. If the field with the given
580 * name does not exist, the structure is unchanged.
583 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
585 GstStructureField *field;
589 g_return_if_fail (structure != NULL);
590 g_return_if_fail (fieldname != NULL);
592 id = g_quark_from_string (fieldname);
594 for (i = 0; i < structure->fields->len; i++) {
595 field = GST_STRUCTURE_FIELD (structure, i);
597 if (field->name == id) {
598 if (G_IS_VALUE (&field->value)) {
599 g_value_unset (&field->value);
601 structure->fields = g_array_remove_index (structure->fields, i);
608 * gst_structure_remove_fields:
609 * @structure: a #GstStructure
610 * @fieldname: the name of the field to remove
611 * @...: NULL-terminated list of more fieldnames to remove
613 * Removes the field with the given names. If a field does not exist, the
614 * argument is ignored.
617 gst_structure_remove_fields (GstStructure * structure,
618 const gchar * fieldname, ...)
622 g_return_if_fail (structure != NULL);
623 g_return_if_fail (fieldname != NULL);
625 va_start (varargs, fieldname);
627 gst_structure_remove_fields_valist (structure, fieldname, varargs);
633 * gst_structure_remove_fields_valist:
634 * @structure: a #GstStructure
635 * @fieldname: the name of the field to remove
636 * @varargs: NULL-terminated list of more fieldnames to remove
638 * Removes the field with the given names. If a field does not exist, the
639 * argument is ignored.
642 gst_structure_remove_fields_valist (GstStructure * structure,
643 const gchar * fieldname, va_list varargs)
645 gchar *field = (gchar *) fieldname;
647 g_return_if_fail (structure != NULL);
648 g_return_if_fail (fieldname != NULL);
651 gst_structure_remove_field (structure, field);
652 field = va_arg (varargs, char *);
657 * gst_structure_remove_all_fields:
658 * @structure: a #GstStructure
660 * Removes all fields in a GstStructure.
663 gst_structure_remove_all_fields (GstStructure * structure)
665 GstStructureField *field;
668 g_return_if_fail (structure != NULL);
670 for (i = structure->fields->len - 1; i >= 0; i--) {
671 field = GST_STRUCTURE_FIELD (structure, i);
673 if (G_IS_VALUE (&field->value)) {
674 g_value_unset (&field->value);
676 structure->fields = g_array_remove_index (structure->fields, i);
681 * gst_structure_get_field_type:
682 * @structure: a #GstStructure
683 * @fieldname: the name of the field
685 * Finds the field with the given name, and returns the type of the
686 * value it contains. If the field is not found, G_TYPE_INVALID is
689 * Returns: the #GValue of the field
692 gst_structure_get_field_type (const GstStructure * structure,
693 const gchar * fieldname)
695 GstStructureField *field;
697 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
698 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
700 field = gst_structure_get_field (structure, fieldname);
702 return G_TYPE_INVALID;
704 return G_VALUE_TYPE (&field->value);
708 * gst_structure_n_fields:
709 * @structure: a #GstStructure
713 * Returns: the number of fields in the structure
716 gst_structure_n_fields (const GstStructure * structure)
718 g_return_val_if_fail (structure != NULL, 0);
720 return structure->fields->len;
724 * gst_structure_foreach:
725 * @structure: a #GstStructure
726 * @func: a function to call for each field
727 * @user_data: private data
729 * Calls the provided function once for each field in the #GstStructure.
731 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
735 gst_structure_foreach (GstStructure * structure,
736 GstStructureForeachFunc func, gpointer user_data)
739 GstStructureField *field;
742 for (i = 0; i < structure->fields->len; i++) {
743 field = GST_STRUCTURE_FIELD (structure, i);
745 ret = func (field->name, &field->value, user_data);
754 * gst_structure_has_field:
755 * @structure: a #GstStructure
756 * @fieldname: the name of a field
760 * Returns: TRUE if the structure contains a field with the given name
763 gst_structure_has_field (const GstStructure * structure,
764 const gchar * fieldname)
766 GstStructureField *field;
768 g_return_val_if_fail (structure != NULL, 0);
769 g_return_val_if_fail (fieldname != NULL, 0);
771 field = gst_structure_get_field (structure, fieldname);
773 return (field != NULL);
777 * gst_structure_has_field_typed:
778 * @structure: a #GstStructure
779 * @fieldname: the name of a field
780 * @type: the type of a value
784 * Returns: TRUE if the structure contains a field with the given name and type
787 gst_structure_has_field_typed (const GstStructure * structure,
788 const gchar * fieldname, GType type)
790 GstStructureField *field;
792 g_return_val_if_fail (structure != NULL, 0);
793 g_return_val_if_fail (fieldname != NULL, 0);
795 field = gst_structure_get_field (structure, fieldname);
799 return (G_VALUE_TYPE (&field->value) == type);
803 /* utility functions */
806 * gst_structure_get_boolean:
807 * @structure: a #GstStructure
808 * @fieldname: the name of a field
809 * @value: a pointer to a #gboolean to set
811 * Sets the boolean pointed to by @value corresponding to the value of the
812 * given field. Caller is responsible for making sure the field exists
813 * and has the correct type.
815 * Returns: TRUE if the value could be set correctly
818 gst_structure_get_boolean (const GstStructure * structure,
819 const gchar * fieldname, gboolean * value)
821 GstStructureField *field;
823 g_return_val_if_fail (structure != NULL, FALSE);
824 g_return_val_if_fail (fieldname != NULL, FALSE);
826 field = gst_structure_get_field (structure, fieldname);
830 if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
833 *value = g_value_get_boolean (&field->value);
839 * gst_structure_get_int:
840 * @structure: a #GstStructure
841 * @fieldname: the name of a field
842 * @value: a pointer to an int to set
844 * Sets the int pointed to by @value corresponding to the value of the
845 * given field. Caller is responsible for making sure the field exists
846 * and has the correct type.
848 * Returns: TRUE if the value could be set correctly
851 gst_structure_get_int (const GstStructure * structure,
852 const gchar * fieldname, gint * value)
854 GstStructureField *field;
856 g_return_val_if_fail (structure != NULL, FALSE);
857 g_return_val_if_fail (fieldname != NULL, FALSE);
858 g_return_val_if_fail (value != NULL, FALSE);
860 field = gst_structure_get_field (structure, fieldname);
864 if (!G_VALUE_HOLDS_INT (&field->value))
867 *value = g_value_get_int (&field->value);
873 * gst_structure_get_fourcc:
874 * @structure: a #GstStructure
875 * @fieldname: the name of a field
876 * @value: a pointer to a #GstFourcc to set
878 * Sets the #GstFourcc pointed to by @value corresponding to the value of the
879 * given field. Caller is responsible for making sure the field exists
880 * and has the correct type.
882 * Returns: TRUE if the value could be set correctly
885 gst_structure_get_fourcc (const GstStructure * structure,
886 const gchar * fieldname, guint32 * value)
888 GstStructureField *field;
890 g_return_val_if_fail (structure != NULL, FALSE);
891 g_return_val_if_fail (fieldname != NULL, FALSE);
892 g_return_val_if_fail (value != NULL, FALSE);
894 field = gst_structure_get_field (structure, fieldname);
898 if (!GST_VALUE_HOLDS_FOURCC (&field->value))
901 *value = gst_value_get_fourcc (&field->value);
907 * gst_structure_get_double:
908 * @structure: a #GstStructure
909 * @fieldname: the name of a field
910 * @value: a pointer to a #GstFourcc to set
912 * Sets the double pointed to by @value corresponding to the value of the
913 * given field. Caller is responsible for making sure the field exists
914 * and has the correct type.
916 * Returns: TRUE if the value could be set correctly
919 gst_structure_get_double (const GstStructure * structure,
920 const gchar * fieldname, gdouble * value)
922 GstStructureField *field;
924 g_return_val_if_fail (structure != NULL, FALSE);
925 g_return_val_if_fail (fieldname != NULL, FALSE);
926 g_return_val_if_fail (value != NULL, FALSE);
928 field = gst_structure_get_field (structure, fieldname);
932 if (!G_VALUE_HOLDS_DOUBLE (&field->value))
935 *value = g_value_get_double (&field->value);
941 * gst_structure_get_string:
942 * @structure: a #GstStructure
943 * @fieldname: the name of a field
945 * Finds the field corresponding to @fieldname, and returns the string
946 * contained in the field's value. Caller is responsible for making
947 * sure the field exists and has the correct type.
949 * The string should not be modified, and remains valid until the next
950 * call to a gst_structure_*() function with the given structure.
952 * Returns: a pointer to the string
955 gst_structure_get_string (const GstStructure * structure,
956 const gchar * fieldname)
958 GstStructureField *field;
960 g_return_val_if_fail (structure != NULL, NULL);
961 g_return_val_if_fail (fieldname != NULL, NULL);
963 field = gst_structure_get_field (structure, fieldname);
967 if (!G_VALUE_HOLDS_STRING (&field->value))
970 return g_value_get_string (&field->value);
973 typedef struct _GstStructureAbbreviation
978 GstStructureAbbreviation;
980 static GstStructureAbbreviation gst_structure_abbrs[] = {
983 {"float", G_TYPE_FLOAT},
985 {"double", G_TYPE_DOUBLE},
986 {"d", G_TYPE_DOUBLE},
987 //{ "fourcc", GST_TYPE_FOURCC },
988 {"boolean", G_TYPE_BOOLEAN},
989 {"bool", G_TYPE_BOOLEAN},
990 {"b", G_TYPE_BOOLEAN},
991 {"string", G_TYPE_STRING},
992 {"str", G_TYPE_STRING},
997 gst_structure_from_abbr (const char *type_name)
1001 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1003 for (i = 0; i < G_N_ELEMENTS (gst_structure_abbrs); i++) {
1004 if (strcmp (type_name, gst_structure_abbrs[i].type_name) == 0) {
1005 return gst_structure_abbrs[i].type;
1009 /* FIXME shouldn't be a special case */
1010 if (strcmp (type_name, "fourcc") == 0 || strcmp (type_name, "4") == 0) {
1011 return GST_TYPE_FOURCC;
1013 if (strcmp (type_name, "buffer") == 0) {
1014 return GST_TYPE_BUFFER;
1017 return g_type_from_name (type_name);
1021 gst_structure_to_abbr (GType type)
1025 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1027 for (i = 0; i < G_N_ELEMENTS (gst_structure_abbrs); i++) {
1028 if (type == gst_structure_abbrs[i].type) {
1029 return gst_structure_abbrs[i].type_name;
1033 /* FIXME shouldn't be a special case */
1034 if (type == GST_TYPE_FOURCC) {
1037 if (type == GST_TYPE_BUFFER) {
1041 return g_type_name (type);
1045 gst_structure_value_get_generic_type (GValue * val)
1047 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1048 || G_VALUE_TYPE (val) == GST_TYPE_FIXED_LIST) {
1049 GArray *array = g_value_peek_pointer (val);
1051 if (array->len > 0) {
1052 GValue *value = &g_array_index (array, GValue, 0);
1054 return gst_structure_value_get_generic_type (value);
1058 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1060 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1061 return G_TYPE_DOUBLE;
1063 return G_VALUE_TYPE (val);
1066 #define GST_ASCII_IS_STRING(c) (g_ascii_isalnum((c)) || ((c) == '_') || \
1067 ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
1071 * gst_structure_to_string:
1072 * @structure: a #GstStructure
1074 * Converts @structure to a human-readable representation.
1076 * Returns: a pointer to string allocated by g_malloc()
1079 gst_structure_to_string (const GstStructure * structure)
1081 GstStructureField *field;
1085 /* NOTE: This function is potentially called by the debug system,
1086 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1087 * should be careful to avoid recursion. This includes any functions
1088 * called by gst_structure_to_string. In particular, calls should
1089 * not use the GST_PTR_FORMAT extension. */
1091 g_return_val_if_fail (structure != NULL, NULL);
1093 s = g_string_new ("");
1094 /* FIXME this string may need to be escaped */
1095 g_string_append_printf (s, "%s", g_quark_to_string (structure->name));
1096 for (i = 0; i < structure->fields->len; i++) {
1100 field = GST_STRUCTURE_FIELD (structure, i);
1102 t = gst_value_serialize (&field->value);
1103 type = gst_structure_value_get_generic_type (&field->value);
1105 g_string_append_printf (s, ", %s=(%s)%s", g_quark_to_string (field->name),
1106 gst_structure_to_abbr (type), t);
1109 return g_string_free (s, FALSE);
1113 * r will still point to the string. if end == next, the string will not be
1114 * null-terminated. In all other cases it will be.
1115 * end = pointer to char behind end of string, next = pointer to start of
1117 * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1120 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next)
1130 ret = gst_structure_parse_simple_string (s, end);
1159 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1162 GValue value1 = { 0 };
1163 GValue value2 = { 0 };
1172 ret = gst_structure_parse_value (s, &s, &value1, type);
1176 while (g_ascii_isspace (*s))
1183 while (g_ascii_isspace (*s))
1186 ret = gst_structure_parse_value (s, &s, &value2, type);
1190 while (g_ascii_isspace (*s))
1197 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1200 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1201 range_type = GST_TYPE_DOUBLE_RANGE;
1202 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1203 range_type = GST_TYPE_INT_RANGE;
1208 g_value_init (value, range_type);
1209 if (range_type == GST_TYPE_DOUBLE_RANGE) {
1210 gst_value_set_double_range (value, g_value_get_double (&value1),
1211 g_value_get_double (&value2));
1213 gst_value_set_int_range (value, g_value_get_int (&value1),
1214 g_value_get_int (&value2));
1222 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
1223 GType type, GType list_type, char begin, char end)
1225 GValue list_value = { 0 };
1229 g_value_init (value, list_type);
1230 array = g_value_peek_pointer (value);
1236 while (g_ascii_isspace (*s))
1244 ret = gst_structure_parse_value (s, &s, &list_value, type);
1248 g_array_append_val (array, list_value);
1250 while (g_ascii_isspace (*s))
1258 while (g_ascii_isspace (*s))
1261 memset (&list_value, 0, sizeof (list_value));
1262 ret = gst_structure_parse_value (s, &s, &list_value, type);
1266 g_array_append_val (array, list_value);
1267 while (g_ascii_isspace (*s))
1278 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
1280 return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
1285 gst_structure_parse_fixed_list (gchar * s, gchar ** after, GValue * value,
1288 return gst_structure_parse_any_list (s, after, value, type,
1289 GST_TYPE_FIXED_LIST, '<', '>');
1293 gst_structure_parse_simple_string (gchar * str, gchar ** end)
1297 while (GST_ASCII_IS_STRING (*s)) {
1307 gst_structure_parse_field (gchar * str,
1308 gchar ** after, GstStructureField * field)
1317 while (g_ascii_isspace (*s))
1320 if (!gst_structure_parse_simple_string (s, &name_end))
1324 while (g_ascii_isspace (*s))
1333 field->name = g_quark_from_string (name);
1336 if (!gst_structure_parse_value (s, &s, &field->value, G_TYPE_INVALID))
1344 gst_structure_parse_value (gchar * str,
1345 gchar ** after, GValue * value, GType default_type)
1354 GType type = default_type;
1358 while (g_ascii_isspace (*s))
1363 type = G_TYPE_INVALID;
1366 while (g_ascii_isspace (*s))
1369 if (!gst_structure_parse_simple_string (s, &type_end))
1372 while (g_ascii_isspace (*s))
1377 while (g_ascii_isspace (*s))
1382 type = gst_structure_from_abbr (type_name);
1385 if (type == G_TYPE_INVALID)
1389 while (g_ascii_isspace (*s))
1392 ret = gst_structure_parse_range (s, &s, value, type);
1393 } else if (*s == '{') {
1394 ret = gst_structure_parse_list (s, &s, value, type);
1395 } else if (*s == '<') {
1396 ret = gst_structure_parse_fixed_list (s, &s, value, type);
1399 if (!gst_structure_parse_string (s, &value_end, &s))
1404 if (type == G_TYPE_INVALID) {
1405 GType try_types[] = { G_TYPE_INT, G_TYPE_DOUBLE, G_TYPE_STRING };
1408 for (i = 0; i < 3; i++) {
1409 g_value_init (value, try_types[i]);
1410 ret = gst_value_deserialize (value, value_s);
1413 g_value_unset (value);
1416 g_value_init (value, type);
1418 ret = gst_value_deserialize (value, value_s);
1429 * gst_structure_from_string:
1430 * @string: a string representation of a #GstStructure.
1431 * @end: FIXME, deduce from code
1433 * Creates a #GstStructure from a string representation.
1435 * Returns: a new #GstStructure
1438 gst_structure_from_string (const gchar * string, gchar ** end)
1445 GstStructure *structure = NULL;
1446 GstStructureField field = { 0 };
1448 g_return_val_if_fail (string != NULL, NULL);
1450 copy = g_strdup (string);
1454 if (!gst_structure_parse_string (r, &w, &r))
1457 while (g_ascii_isspace (*r))
1459 if (*r != 0 && *r != ';' && *r != ',')
1464 structure = gst_structure_empty_new (name);
1467 while (*r && (*r != ';')) {
1471 while (*r && g_ascii_isspace (*r))
1474 memset (&field, 0, sizeof (field));
1475 if (!gst_structure_parse_field (r, &r, &field))
1477 gst_structure_set_field (structure, &field);
1478 while (*r && g_ascii_isspace (*r))
1483 *end = (char *) string + (r - copy);
1490 gst_structure_free (structure);
1496 gst_structure_transform_to_string (const GValue * src_value,
1497 GValue * dest_value)
1499 g_return_if_fail (src_value != NULL);
1500 g_return_if_fail (dest_value != NULL);
1502 dest_value->data[0].v_pointer =
1503 gst_structure_to_string (src_value->data[0].v_pointer);
1506 static GstStructure *
1507 gst_structure_copy_conditional (const GstStructure * structure)
1510 return gst_structure_copy (structure);