gst/glib-compat.*: Add some functions that are in newer versions of glib than we...
[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
32 #if 0
33 #include <sys/stat.h>
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <stdarg.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #ifndef G_OS_WIN32
45 #include <sys/wait.h>
46 #endif
47 #include <fcntl.h>
48 #include <stdlib.h>
49 #endif
50
51
52 #ifdef G_OS_WIN32
53 #include <windows.h>
54 #include <io.h>
55 #endif /* G_OS_WIN32 */
56
57
58 #ifdef G_OS_WIN32
59 #define G_DIR_SEPARATOR '\\'
60 #define G_DIR_SEPARATOR_S "\\"
61 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR || (c) == '/')
62 #define G_SEARCHPATH_SEPARATOR ';'
63 #define G_SEARCHPATH_SEPARATOR_S ";"
64 #else
65 #define G_DIR_SEPARATOR '/'
66 #define G_DIR_SEPARATOR_S "/"
67 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR)
68 #define G_SEARCHPATH_SEPARATOR ':'
69 #define G_SEARCHPATH_SEPARATOR_S ":"
70 #endif
71
72
73 #if !GLIB_CHECK_VERSION (2, 8, 0)
74 /**
75  * g_mkdir_with_parents:
76  * @pathname: a pathname in the GLib file name encoding
77  * @mode: permissions to use for newly created directories
78  *
79  * Create a directory if it doesn't already exist. Create intermediate
80  * parent directories as needed, too.
81  *
82  * Returns: 0 if the directory already exists, or was successfully
83  * created. Returns -1 if an error occurred, with errno set.
84  *
85  * Since: 2.8
86  */
87 int
88 g_mkdir_with_parents (const gchar * pathname, int mode)
89 {
90   gchar *fn, *p;
91
92   if (pathname == NULL || *pathname == '\0') {
93     errno = EINVAL;
94     return -1;
95   }
96
97   fn = g_strdup (pathname);
98
99   if (g_path_is_absolute (fn))
100     p = (gchar *) g_path_skip_root (fn);
101   else
102     p = fn;
103
104   do {
105     while (*p && !G_IS_DIR_SEPARATOR (*p))
106       p++;
107
108     if (!*p)
109       p = NULL;
110     else
111       *p = '\0';
112
113     if (!g_file_test (fn, G_FILE_TEST_EXISTS)) {
114       if (g_mkdir (fn, mode) == -1) {
115         int errno_save = errno;
116
117         g_free (fn);
118         errno = errno_save;
119         return -1;
120       }
121     } else if (!g_file_test (fn, G_FILE_TEST_IS_DIR)) {
122       g_free (fn);
123       errno = ENOTDIR;
124       return -1;
125     }
126     if (p) {
127       *p++ = G_DIR_SEPARATOR;
128       while (*p && G_IS_DIR_SEPARATOR (*p))
129         p++;
130     }
131   }
132   while (p);
133
134   g_free (fn);
135
136   return 0;
137 }
138 #endif
139
140
141 #if !GLIB_CHECK_VERSION (2, 6, 0)
142 /**
143  * g_mkdir: 
144  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
145  * @mode: permissions to use for the newly created directory
146  *
147  * A wrapper for the POSIX mkdir() function. The mkdir() function 
148  * attempts to create a directory with the given name and permissions.
149  * 
150  * See the C library manual for more details about mkdir().
151  *
152  * Returns: 0 if the directory was successfully created, -1 if an error 
153  *    occurred
154  * 
155  * Since: 2.6
156  */
157 int
158 g_mkdir (const gchar * filename, int mode)
159 {
160 #ifdef G_OS_WIN32
161   if (G_WIN32_HAVE_WIDECHAR_API ()) {
162     wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
163     int retval;
164     int save_errno;
165
166     if (wfilename == NULL) {
167       errno = EINVAL;
168       return -1;
169     }
170
171     retval = _wmkdir (wfilename);
172     save_errno = errno;
173
174     g_free (wfilename);
175
176     errno = save_errno;
177     return retval;
178   } else {
179     gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
180     int retval;
181     int save_errno;
182
183     if (cp_filename == NULL) {
184       errno = EINVAL;
185       return -1;
186     }
187
188     retval = mkdir (cp_filename);
189     save_errno = errno;
190
191     g_free (cp_filename);
192
193     errno = save_errno;
194     return retval;
195   }
196 #else
197   return mkdir (filename, mode);
198 #endif
199 }
200 #endif
201
202 #if !GLIB_CHECK_VERSION (2, 6, 0)
203 /**
204  * g_stat: 
205  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
206  * @buf: a pointer to a <structname>stat</structname> struct, which
207  *    will be filled with the file information
208  *
209  * A wrapper for the POSIX stat() function. The stat() function 
210  * returns information about a file.
211  * 
212  * See the C library manual for more details about stat().
213  *
214  * Returns: 0 if the information was successfully retrieved, -1 if an error 
215  *    occurred
216  * 
217  * Since: 2.6
218  */
219 int
220 g_stat (const gchar * filename, struct stat *buf)
221 {
222 #ifdef G_OS_WIN32
223   if (G_WIN32_HAVE_WIDECHAR_API ()) {
224     wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
225     int retval;
226     int save_errno;
227
228     if (wfilename == NULL) {
229       errno = EINVAL;
230       return -1;
231     }
232
233     retval = _wstat (wfilename, (struct _stat *) buf);
234     save_errno = errno;
235
236     g_free (wfilename);
237
238     errno = save_errno;
239     return retval;
240   } else {
241     gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
242     int retval;
243     int save_errno;
244
245     if (cp_filename == NULL) {
246       errno = EINVAL;
247       return -1;
248     }
249
250     retval = stat (cp_filename, buf);
251     save_errno = errno;
252
253     g_free (cp_filename);
254
255     errno = save_errno;
256     return retval;
257   }
258 #else
259   return stat (filename, buf);
260 #endif
261 }
262 #endif