1 /*
2  * Copyright (C) 2010 Ole André Vadla Ravnås <oleavr@soundrop.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #include "vtutil.h"
21 
22 gchar *
gst_vtutil_object_to_string(CFTypeRef obj)23 gst_vtutil_object_to_string (CFTypeRef obj)
24 {
25   gchar *result;
26   CFStringRef s;
27 
28   if (obj == NULL)
29     return g_strdup ("(null)");
30 
31   s = CFCopyDescription (obj);
32   result = gst_vtutil_string_to_utf8 (s);
33   CFRelease (s);
34 
35   return result;
36 }
37 
38 gchar *
gst_vtutil_string_to_utf8(CFStringRef s)39 gst_vtutil_string_to_utf8 (CFStringRef s)
40 {
41   gchar *result;
42   CFIndex size;
43 
44   size = CFStringGetMaximumSizeForEncoding (CFStringGetLength (s),
45       kCFStringEncodingUTF8);
46   result = g_malloc (size + 1);
47   CFStringGetCString (s, result, size + 1, kCFStringEncodingUTF8);
48 
49   return result;
50 }
51 
52 void
gst_vtutil_dict_set_i32(CFMutableDictionaryRef dict,CFStringRef key,gint32 value)53 gst_vtutil_dict_set_i32 (CFMutableDictionaryRef dict, CFStringRef key,
54     gint32 value)
55 {
56   CFNumberRef number;
57 
58   number = CFNumberCreate (NULL, kCFNumberSInt32Type, &value);
59   CFDictionarySetValue (dict, key, number);
60   CFRelease (number);
61 }
62 
63 void
gst_vtutil_dict_set_string(CFMutableDictionaryRef dict,CFStringRef key,const gchar * value)64 gst_vtutil_dict_set_string (CFMutableDictionaryRef dict, CFStringRef key,
65     const gchar * value)
66 {
67   CFStringRef string;
68 
69   string = CFStringCreateWithCString (NULL, value, kCFStringEncodingASCII);
70   CFDictionarySetValue (dict, key, string);
71   CFRelease (string);
72 }
73 
74 void
gst_vtutil_dict_set_boolean(CFMutableDictionaryRef dict,CFStringRef key,gboolean value)75 gst_vtutil_dict_set_boolean (CFMutableDictionaryRef dict, CFStringRef key,
76     gboolean value)
77 {
78   CFDictionarySetValue (dict, key, value ? kCFBooleanTrue : kCFBooleanFalse);
79 }
80 
81 void
gst_vtutil_dict_set_data(CFMutableDictionaryRef dict,CFStringRef key,guint8 * value,guint64 length)82 gst_vtutil_dict_set_data (CFMutableDictionaryRef dict, CFStringRef key,
83     guint8 * value, guint64 length)
84 {
85   CFDataRef data;
86 
87   data = CFDataCreate (NULL, value, length);
88   CFDictionarySetValue (dict, key, data);
89   CFRelease (data);
90 }
91 
92 void
gst_vtutil_dict_set_object(CFMutableDictionaryRef dict,CFStringRef key,CFTypeRef * value)93 gst_vtutil_dict_set_object (CFMutableDictionaryRef dict, CFStringRef key,
94     CFTypeRef * value)
95 {
96   CFDictionarySetValue (dict, key, value);
97   CFRelease (value);
98 }
99