Doc updates.
[platform/upstream/gstreamer.git] / gst / glib-compat.c
1 /*
2  * glib-compat.c
3  * Functions copied from glib 2.6 and 2.8
4  *
5  * Copyright 2005 David Schleef <ds@schleef.org>
6  */
7
8 /* gfileutils.c - File utility functions
9  *
10  *  Copyright 2000 Red Hat, Inc.
11  *
12  * GLib is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU Lesser General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * GLib is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GLib; see the file COPYING.LIB.  If not,
24  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25  *   Boston, MA 02111-1307, USA.
26  */
27
28 #include "config.h"
29
30 #include <glib.h>
31 #include <glib/gstdio.h>
32
33 #include <stdio.h>
34 #include <errno.h>
35
36 #include "glib-compat.h"
37 #include "glib-compat-private.h"
38
39 #include <sys/stat.h>
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43 #include <sys/types.h>
44 #if 0
45 #include <stdlib.h>
46 #include <stdarg.h>
47 #include <string.h>
48 #ifndef G_OS_WIN32
49 #include <sys/wait.h>
50 #endif
51 #include <fcntl.h>
52 #endif
53
54
55 #ifdef G_OS_WIN32
56 #include <windows.h>
57 #include <io.h>
58 #endif /* G_OS_WIN32 */
59
60
61 #ifdef G_OS_WIN32
62 #define G_DIR_SEPARATOR '\\'
63 #define G_DIR_SEPARATOR_S "\\"
64 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR || (c) == '/')
65 #define G_SEARCHPATH_SEPARATOR ';'
66 #define G_SEARCHPATH_SEPARATOR_S ";"
67 #else
68 #define G_DIR_SEPARATOR '/'
69 #define G_DIR_SEPARATOR_S "/"
70 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR)
71 #define G_SEARCHPATH_SEPARATOR ':'
72 #define G_SEARCHPATH_SEPARATOR_S ":"
73 #endif
74
75
76 #if !GLIB_CHECK_VERSION (2, 8, 0)
77 /**
78  * g_mkdir_with_parents:
79  * @pathname: a pathname in the GLib file name encoding
80  * @mode: permissions to use for newly created directories
81  *
82  * Create a directory if it doesn't already exist. Create intermediate
83  * parent directories as needed, too.
84  *
85  * Returns: 0 if the directory already exists, or was successfully
86  * created. Returns -1 if an error occurred, with errno set.
87  *
88  * Since: 2.8
89  */
90 int
91 g_mkdir_with_parents (const gchar * pathname, int mode)
92 {
93   gchar *fn, *p;
94
95   if (pathname == NULL || *pathname == '\0') {
96     errno = EINVAL;
97     return -1;
98   }
99
100   fn = g_strdup (pathname);
101
102   if (g_path_is_absolute (fn))
103     p = (gchar *) g_path_skip_root (fn);
104   else
105     p = fn;
106
107   do {
108     while (*p && !G_IS_DIR_SEPARATOR (*p))
109       p++;
110
111     if (!*p)
112       p = NULL;
113     else
114       *p = '\0';
115
116     if (!g_file_test (fn, G_FILE_TEST_EXISTS)) {
117       if (g_mkdir (fn, mode) == -1) {
118         int errno_save = errno;
119
120         g_free (fn);
121         errno = errno_save;
122         return -1;
123       }
124     } else if (!g_file_test (fn, G_FILE_TEST_IS_DIR)) {
125       g_free (fn);
126       errno = ENOTDIR;
127       return -1;
128     }
129     if (p) {
130       *p++ = G_DIR_SEPARATOR;
131       while (*p && G_IS_DIR_SEPARATOR (*p))
132         p++;
133     }
134   }
135   while (p);
136
137   g_free (fn);
138
139   return 0;
140 }
141 #endif
142
143
144 /**
145  * gst_flags_get_first_value:
146  * @flags_class: a #GFlagsClass
147  * @value: the value
148  *
149  * Returns the first GFlagsValue which is set in value.
150  *
151  * This version is copied from GLib 2.8.
152  * In GLib 2.6, it didn't check for a flag value being NULL, hence it
153  * hits an infinite loop in our flags serialize function
154  *
155  * Returns: the first GFlagsValue which is set in value, or NULL if none is set.
156  */
157 GFlagsValue *
158 gst_flags_get_first_value (GFlagsClass * flags_class, guint value)
159 {
160   g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
161
162   if (flags_class->n_values) {
163     GFlagsValue *flags_value;
164
165     if (value == 0) {
166       for (flags_value = flags_class->values; flags_value->value_name;
167           flags_value++)
168         if (flags_value->value == 0)
169           return flags_value;
170     } else {
171       for (flags_value = flags_class->values; flags_value->value_name;
172           flags_value++)
173         if (flags_value->value != 0
174             && (flags_value->value & value) == flags_value->value)
175           return flags_value;
176     }
177   }
178
179   return NULL;
180 }
181
182 /* Adapted from g_value_dup_object to use gst_object_ref */
183 #include "gstobject.h"
184 /**
185  * g_value_dup_gst_object:
186  * @value: the #GstObject value to dup
187  *
188  * Get the contents of a G_TYPE_OBJECT derived GValue, increasing its reference count.
189  * This function exists because of unsafe reference counting in old glib versions.
190  *
191  * Returns: object content of value, should be unreferenced with gst_object_unref()
192  * when no longer needed.
193  */
194 GObject *
195 g_value_dup_gst_object (const GValue * value)
196 {
197   GObject *o;
198
199   g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
200
201   o = value->data[0].v_pointer;
202   if (!o)
203     return NULL;
204   g_return_val_if_fail (GST_IS_OBJECT (o), NULL);
205   return gst_object_ref (o);
206 }