fix doc build fix autogen
[platform/upstream/gstreamer.git] / gst / gstutils.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstutils.c: Utility functions: gtk_get_property stuff, etc.
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <ctype.h>
26
27 #include "gst_private.h"
28 #include "gstutils.h"
29 #include "gsturitype.h"
30 #include "gstinfo.h"
31
32 /**
33  * gst_util_dump_mem:
34  * @mem: a pointer to the memory to dump
35  * @size: the size of the memory block to dump
36  *
37  * Dumps the memory block into a hex representation. Useful for debugging.
38  */
39 void
40 gst_util_dump_mem (guchar *mem, guint size)
41 {
42   guint i, j;
43   GString *string = g_string_sized_new (50);
44   GString *chars = g_string_sized_new (18);
45
46   i = j = 0;
47   while (i < size) {
48     if (g_ascii_isprint (mem[i]))
49       g_string_append_printf (chars, "%c", mem[i]);
50     else 
51       g_string_append_printf (chars, ".");
52
53     g_string_append_printf (string, "%02x ", mem[i]);
54
55     j++;
56     i++;
57
58     if (j == 16 || i == size) {
59       g_print ("%08x (%p): %-48.48s %-16.16s\n", i-j, mem+i-j, string->str, chars->str);
60       g_string_set_size (string, 0);
61       g_string_set_size (chars, 0);
62       j = 0;
63     }
64   }
65   g_string_free (string, TRUE);
66   g_string_free (chars, TRUE);
67 }
68
69
70 /**
71  * gst_util_set_value_from_string:
72  * @value: the value to set
73  * @value_str: the string to get the value from
74  *
75  * Converts the string to the type of the value and
76  * sets the value with it.
77  */
78 void
79 gst_util_set_value_from_string(GValue *value, const gchar *value_str)
80 {
81
82         g_return_if_fail(value != NULL);
83         g_return_if_fail(value_str != NULL);
84         
85         GST_CAT_DEBUG (GST_CAT_PARAMS, "parsing '%s' to type %s", value_str, g_type_name(G_VALUE_TYPE(value)));
86
87         switch (G_VALUE_TYPE(value)) {
88                 case G_TYPE_STRING:
89                         g_value_set_string(value, g_strdup(value_str));
90                         break;
91                 case G_TYPE_ENUM: 
92                 case G_TYPE_INT: {
93                         gint i;
94                         sscanf (value_str, "%d", &i);
95                         g_value_set_int(value, i);
96                         break;
97                 }
98                 case G_TYPE_UINT: {
99                         guint i;
100                         sscanf (value_str, "%u", &i);
101                         g_value_set_uint(value, i);
102                         break;
103                 }
104                 case G_TYPE_LONG: {
105                         glong i;
106                         sscanf (value_str, "%ld", &i);
107                         g_value_set_long(value, i);
108                         break;
109                 }
110                 case G_TYPE_ULONG: {
111                         gulong i;
112                         sscanf (value_str, "%lu", &i);
113                         g_value_set_ulong(value, i);
114                         break;
115                 }
116                 case G_TYPE_BOOLEAN: {
117                         gboolean i = FALSE;
118                         if (!strncmp ("true", value_str, 4)) i = TRUE;
119                         g_value_set_boolean(value, i);
120                         break;
121                 }
122                 case G_TYPE_CHAR: {
123                         gchar i;
124                         sscanf (value_str, "%c", &i);
125                         g_value_set_char(value, i);
126                         break;
127                 }
128                 case G_TYPE_UCHAR: {
129                         guchar i;
130                         sscanf (value_str, "%c", &i);
131                         g_value_set_uchar(value, i);
132                         break;
133                 }
134                 case G_TYPE_FLOAT: {
135                         gfloat i;
136                         sscanf (value_str, "%f", &i);
137                         g_value_set_float(value, i);
138                         break;
139                 }
140                 case G_TYPE_DOUBLE: {
141                         gfloat i;
142                         sscanf (value_str, "%g", &i);
143                         g_value_set_double(value, (gdouble)i);
144                         break;
145                 }
146                 default:
147                         break;
148         }
149 }
150
151 /**
152  * gst_util_set_object_arg:
153  * @object: the object to set the argument of
154  * @name: the name of the argument to set
155  * @value: the string value to set
156  *
157  * Convertes the string value to the type of the objects argument and
158  * sets the argument with it.
159  */
160 void
161 gst_util_set_object_arg (GObject * object, const gchar * name, const gchar * value)
162 {
163   if (name && value) {
164     GParamSpec *paramspec;
165
166     paramspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), name);
167
168     if (!paramspec) {
169       return;
170     }
171
172     GST_DEBUG ( "paramspec->flags is %d, paramspec->value_type is %d",
173                paramspec->flags, (gint) paramspec->value_type);
174
175     if (paramspec->flags & G_PARAM_WRITABLE) {
176       switch (paramspec->value_type) {
177         case G_TYPE_STRING:
178           g_object_set (G_OBJECT (object), name, value, NULL);
179           break;
180         case G_TYPE_ENUM:
181         case G_TYPE_INT:{
182           gint i;
183
184           sscanf (value, "%d", &i);
185           g_object_set (G_OBJECT (object), name, i, NULL);
186           break;
187         }
188         case G_TYPE_UINT:{
189           guint i;
190
191           sscanf (value, "%u", &i);
192           g_object_set (G_OBJECT (object), name, i, NULL);
193           break;
194         }
195         case G_TYPE_LONG:{
196           glong i;
197
198           sscanf (value, "%ld", &i);
199           g_object_set (G_OBJECT (object), name, i, NULL);
200           break;
201         }
202         case G_TYPE_ULONG:{
203           gulong i;
204
205           sscanf (value, "%lu", &i);
206           g_object_set (G_OBJECT (object), name, i, NULL);
207           break;
208         }
209         case G_TYPE_BOOLEAN:{
210           gboolean i = FALSE;
211
212           if (!g_ascii_strncasecmp ("true", value, 4))
213             i = TRUE;
214           g_object_set (G_OBJECT (object), name, i, NULL);
215           break;
216         }
217         case G_TYPE_CHAR:{
218           gchar i;
219
220           sscanf (value, "%c", &i);
221           g_object_set (G_OBJECT (object), name, i, NULL);
222           break;
223         }
224         case G_TYPE_UCHAR:{
225           guchar i;
226
227           sscanf (value, "%c", &i);
228           g_object_set (G_OBJECT (object), name, i, NULL);
229           break;
230         }
231         case G_TYPE_FLOAT:{
232           gfloat i;
233
234           sscanf (value, "%f", &i);
235           g_object_set (G_OBJECT (object), name, i, NULL);
236           break;
237         }
238         case G_TYPE_DOUBLE:{
239           gfloat i;
240
241           sscanf (value, "%g", &i);
242           g_object_set (G_OBJECT (object), name, (gdouble) i, NULL);
243           break;
244         }
245         default:
246           if (G_IS_PARAM_SPEC_ENUM (paramspec)) {
247             gint i;
248
249             sscanf (value, "%d", &i);
250             g_object_set (G_OBJECT (object), name, i, NULL);
251           }
252           else if (paramspec->value_type == GST_TYPE_URI) {
253             g_object_set (G_OBJECT (object), name, value, NULL);
254           }
255           break;
256       }
257     }
258   }
259 }
260
261 /* -----------------------------------------------------
262  *
263  *  The following code will be moved out of the main
264  * gstreamer library someday.
265  */
266
267 #include "gstpad.h"
268
269 static void
270 string_append_indent (GString * str, gint count)
271 {
272   gint xx;
273
274   for (xx = 0; xx < count; xx++)
275     g_string_append_c (str, ' ');
276 }
277
278 #if 0
279 static void
280 gst_print_props (GString *buf, gint indent, GList *props, gboolean showname)
281 {
282   GList *elem;
283   guint width = 0;
284   GstPropsType type;
285
286   if (showname)
287     for (elem = props; elem; elem = g_list_next (elem)) {
288       GstPropsEntry *prop = elem->data;
289       const gchar *name = gst_props_entry_get_name (prop);
290
291       if (width < strlen (name))
292         width = strlen (name);
293     }
294
295   for (elem = props; elem; elem = g_list_next (elem)) {
296     GstPropsEntry *prop = elem->data;
297
298     string_append_indent (buf, indent);
299     if (showname) {
300       const gchar *name = gst_props_entry_get_name (prop);
301
302       g_string_append (buf, name);
303       string_append_indent (buf, 2 + width - strlen (name));
304     }
305
306     type = gst_props_entry_get_props_type (prop);
307     switch (type) {
308       case GST_PROPS_INT_TYPE:
309       {
310         gint val;
311         gst_props_entry_get_int (prop, &val);
312         g_string_append_printf (buf, "%d (int)\n", val);
313         break;
314       }
315       case GST_PROPS_INT_RANGE_TYPE:
316       {
317         gint min, max;
318         gst_props_entry_get_int_range (prop, &min, &max);
319         g_string_append_printf (buf, "%d - %d (int)\n", min, max);
320         break;
321       }
322       case GST_PROPS_FLOAT_TYPE:
323       {
324         gfloat val;
325         gst_props_entry_get_float (prop, &val);
326         g_string_append_printf (buf, "%f (float)\n", val);
327         break;
328       }
329       case GST_PROPS_FLOAT_RANGE_TYPE:
330       {
331         gfloat min, max;
332         gst_props_entry_get_float_range (prop, &min, &max);
333         g_string_append_printf (buf, "%f - %f (float)\n", min, max);
334         break;
335       }
336       case GST_PROPS_BOOLEAN_TYPE:
337       {
338         gboolean val;
339         gst_props_entry_get_boolean (prop, &val);
340         g_string_append_printf (buf, "%s\n", val ? "TRUE" : "FALSE");
341         break;
342       }
343       case GST_PROPS_STRING_TYPE:
344       {
345         const gchar *val;
346         gst_props_entry_get_string (prop, &val);
347         g_string_append_printf (buf, "\"%s\"\n", val);
348         break;
349       }
350       case GST_PROPS_FOURCC_TYPE:
351       {
352         guint32 val;
353         gst_props_entry_get_fourcc_int (prop, &val);
354         g_string_append_printf (buf, "'%c%c%c%c' (fourcc)\n",
355                                 (gchar)( val        & 0xff),
356                                 (gchar)((val >> 8)  & 0xff),
357                                 (gchar)((val >> 16) & 0xff),
358                                 (gchar)((val >> 24) & 0xff));
359         break;
360       }
361       case GST_PROPS_LIST_TYPE:
362       {
363         const GList *list;
364         gst_props_entry_get_list (prop, &list);
365         gst_print_props (buf, indent + 2, (GList *)list, FALSE);
366         break;
367       }
368       default:
369         g_string_append_printf (buf, "unknown proptype %d\n", type);
370         break;
371     }
372   }
373 }
374 #endif
375
376 /**
377  * gst_print_pad_caps:
378  * @buf: the buffer to print the caps in
379  * @indent: initial indentation
380  * @pad: the pad to print the caps from
381  *
382  * Write the pad capabilities in a human readable format into
383  * the given GString.
384  */
385 void
386 gst_print_pad_caps (GString * buf, gint indent, GstPad * pad)
387 {
388   GstRealPad *realpad;
389   GstCaps *caps;
390
391   realpad = GST_PAD_REALIZE (pad);
392   caps = realpad->caps;
393
394   if (!caps) {
395     string_append_indent (buf, indent);
396     g_string_printf (buf, "%s:%s has no capabilities", GST_DEBUG_PAD_NAME (pad));
397   }
398   else {
399     char *s;
400
401     s = gst_caps_to_string(caps);
402     g_string_append(buf, s);
403     g_free(s);
404   }
405 }
406
407 /**
408  * gst_print_element_args:
409  * @buf: the buffer to print the args in
410  * @indent: initial indentation
411  * @element: the element to print the args of
412  *
413  * Print the element argument in a human readable format in the given
414  * GString.
415  */
416 void
417 gst_print_element_args (GString * buf, gint indent, GstElement * element)
418 {
419   guint width;
420   GValue value = { 0, }; /* the important thing is that value.type = 0 */
421   gchar *str = 0;
422   GParamSpec *spec, **specs, **walk;
423
424   specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (element), NULL);
425   
426   width = 0;
427   for (walk = specs; *walk; walk++) {
428     spec = *walk;
429     if (width < strlen (spec->name))
430       width = strlen (spec->name);
431   }
432
433   for (walk = specs; *walk; walk++) {
434     spec = *walk;
435     
436     if (spec->flags & G_PARAM_READABLE) {
437       g_value_init(&value, G_PARAM_SPEC_VALUE_TYPE (spec));
438       g_object_get_property (G_OBJECT (element), spec->name, &value);
439       str = g_strdup_value_contents (&value);
440       g_value_unset(&value);
441     } else {
442       str = g_strdup ("Parameter not readable.");
443     }
444
445     string_append_indent (buf, indent);
446     g_string_append (buf, spec->name);
447     string_append_indent (buf, 2 + width - strlen (spec->name));
448     g_string_append (buf, str);
449     g_string_append_c (buf, '\n');
450     
451     g_free (str);
452   }
453
454   g_free (specs);
455 }