2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstutils.c: Utility functions: gtk_get_property stuff, etc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
26 #include "gst_private.h"
28 #include "gsturitype.h"
33 * @mem: a pointer to the memory to dump
34 * @size: the size of the memory block to dump
36 * Dumps the memory block into a hex representation. Useful for debugging.
39 gst_util_dump_mem (const guchar * mem, guint size)
42 GString *string = g_string_sized_new (50);
43 GString *chars = g_string_sized_new (18);
47 if (g_ascii_isprint (mem[i]))
48 g_string_append_printf (chars, "%c", mem[i]);
50 g_string_append_printf (chars, ".");
52 g_string_append_printf (string, "%02x ", mem[i]);
57 if (j == 16 || i == size) {
58 g_print ("%08x (%p): %-48.48s %-16.16s\n", i - j, mem + i - j,
59 string->str, chars->str);
60 g_string_set_size (string, 0);
61 g_string_set_size (chars, 0);
65 g_string_free (string, TRUE);
66 g_string_free (chars, TRUE);
71 * gst_util_set_value_from_string:
72 * @value: the value to set
73 * @value_str: the string to get the value from
75 * Converts the string to the type of the value and
76 * sets the value with it.
79 gst_util_set_value_from_string (GValue * value, const gchar * value_str)
82 g_return_if_fail (value != NULL);
83 g_return_if_fail (value_str != NULL);
85 GST_CAT_DEBUG (GST_CAT_PARAMS, "parsing '%s' to type %s", value_str,
86 g_type_name (G_VALUE_TYPE (value)));
88 switch (G_VALUE_TYPE (value)) {
90 g_value_set_string (value, g_strdup (value_str));
96 sscanf (value_str, "%d", &i);
97 g_value_set_int (value, i);
103 sscanf (value_str, "%u", &i);
104 g_value_set_uint (value, i);
110 sscanf (value_str, "%ld", &i);
111 g_value_set_long (value, i);
117 sscanf (value_str, "%lu", &i);
118 g_value_set_ulong (value, i);
121 case G_TYPE_BOOLEAN:{
124 if (!strncmp ("true", value_str, 4))
126 g_value_set_boolean (value, i);
132 sscanf (value_str, "%c", &i);
133 g_value_set_char (value, i);
139 sscanf (value_str, "%c", &i);
140 g_value_set_uchar (value, i);
146 sscanf (value_str, "%f", &i);
147 g_value_set_float (value, i);
153 sscanf (value_str, "%g", &i);
154 g_value_set_double (value, (gdouble) i);
163 * gst_util_set_object_arg:
164 * @object: the object to set the argument of
165 * @name: the name of the argument to set
166 * @value: the string value to set
168 * Convertes the string value to the type of the objects argument and
169 * sets the argument with it.
172 gst_util_set_object_arg (GObject * object, const gchar * name,
176 GParamSpec *paramspec;
179 g_object_class_find_property (G_OBJECT_GET_CLASS (object), name);
185 GST_DEBUG ("paramspec->flags is %d, paramspec->value_type is %d",
186 paramspec->flags, (gint) paramspec->value_type);
188 if (paramspec->flags & G_PARAM_WRITABLE) {
189 switch (paramspec->value_type) {
191 g_object_set (G_OBJECT (object), name, value, NULL);
197 sscanf (value, "%d", &i);
198 g_object_set (G_OBJECT (object), name, i, NULL);
204 sscanf (value, "%u", &i);
205 g_object_set (G_OBJECT (object), name, i, NULL);
211 sscanf (value, "%ld", &i);
212 g_object_set (G_OBJECT (object), name, i, NULL);
218 sscanf (value, "%lu", &i);
219 g_object_set (G_OBJECT (object), name, i, NULL);
222 case G_TYPE_BOOLEAN:{
225 if (!g_ascii_strncasecmp ("true", value, 4))
227 g_object_set (G_OBJECT (object), name, i, NULL);
233 sscanf (value, "%c", &i);
234 g_object_set (G_OBJECT (object), name, i, NULL);
240 sscanf (value, "%c", &i);
241 g_object_set (G_OBJECT (object), name, i, NULL);
247 sscanf (value, "%f", &i);
248 g_object_set (G_OBJECT (object), name, i, NULL);
254 sscanf (value, "%g", &i);
255 g_object_set (G_OBJECT (object), name, (gdouble) i, NULL);
259 if (G_IS_PARAM_SPEC_ENUM (paramspec)) {
262 sscanf (value, "%d", &i);
263 g_object_set (G_OBJECT (object), name, i, NULL);
264 } else if (paramspec->value_type == GST_TYPE_URI) {
265 g_object_set (G_OBJECT (object), name, value, NULL);
273 /* -----------------------------------------------------
275 * The following code will be moved out of the main
276 * gstreamer library someday.
282 string_append_indent (GString * str, gint count)
286 for (xx = 0; xx < count; xx++)
287 g_string_append_c (str, ' ');
291 * gst_print_pad_caps:
292 * @buf: the buffer to print the caps in
293 * @indent: initial indentation
294 * @pad: the pad to print the caps from
296 * Write the pad capabilities in a human readable format into
300 gst_print_pad_caps (GString * buf, gint indent, GstPad * pad)
305 realpad = GST_PAD_REALIZE (pad);
306 caps = realpad->caps;
309 string_append_indent (buf, indent);
310 g_string_printf (buf, "%s:%s has no capabilities",
311 GST_DEBUG_PAD_NAME (pad));
315 s = gst_caps_to_string (caps);
316 g_string_append (buf, s);
322 * gst_print_element_args:
323 * @buf: the buffer to print the args in
324 * @indent: initial indentation
325 * @element: the element to print the args of
327 * Print the element argument in a human readable format in the given
331 gst_print_element_args (GString * buf, gint indent, GstElement * element)
334 GValue value = { 0, }; /* the important thing is that value.type = 0 */
336 GParamSpec *spec, **specs, **walk;
338 specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (element), NULL);
341 for (walk = specs; *walk; walk++) {
343 if (width < strlen (spec->name))
344 width = strlen (spec->name);
347 for (walk = specs; *walk; walk++) {
350 if (spec->flags & G_PARAM_READABLE) {
351 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (spec));
352 g_object_get_property (G_OBJECT (element), spec->name, &value);
353 str = g_strdup_value_contents (&value);
354 g_value_unset (&value);
356 str = g_strdup ("Parameter not readable.");
359 string_append_indent (buf, indent);
360 g_string_append (buf, spec->name);
361 string_append_indent (buf, 2 + width - strlen (spec->name));
362 g_string_append (buf, str);
363 g_string_append_c (buf, '\n');