gst/glib-compat.c: Attempt #2.
[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 #include <stdio.h>
33 #include <errno.h>
34
35 #include "glib-compat.h"
36
37 #if 0
38 #include <sys/stat.h>
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 #include <stdlib.h>
43 #include <stdarg.h>
44 #include <string.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #ifndef G_OS_WIN32
48 #include <sys/wait.h>
49 #endif
50 #include <fcntl.h>
51 #include <stdlib.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 #if !GLIB_CHECK_VERSION (2, 6, 0)
145 /**
146  * g_mkdir: 
147  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
148  * @mode: permissions to use for the newly created directory
149  *
150  * A wrapper for the POSIX mkdir() function. The mkdir() function 
151  * attempts to create a directory with the given name and permissions.
152  * 
153  * See the C library manual for more details about mkdir().
154  *
155  * Returns: 0 if the directory was successfully created, -1 if an error 
156  *    occurred
157  * 
158  * Since: 2.6
159  */
160 int
161 g_mkdir (const gchar * filename, int mode)
162 {
163 #ifdef G_OS_WIN32
164   if (G_WIN32_HAVE_WIDECHAR_API ()) {
165     wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
166     int retval;
167     int save_errno;
168
169     if (wfilename == NULL) {
170       errno = EINVAL;
171       return -1;
172     }
173
174     retval = _wmkdir (wfilename);
175     save_errno = errno;
176
177     g_free (wfilename);
178
179     errno = save_errno;
180     return retval;
181   } else {
182     gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
183     int retval;
184     int save_errno;
185
186     if (cp_filename == NULL) {
187       errno = EINVAL;
188       return -1;
189     }
190
191     retval = mkdir (cp_filename);
192     save_errno = errno;
193
194     g_free (cp_filename);
195
196     errno = save_errno;
197     return retval;
198   }
199 #else
200   return mkdir (filename, mode);
201 #endif
202 }
203 #endif
204
205 #if !GLIB_CHECK_VERSION (2, 6, 0)
206 /**
207  * g_stat: 
208  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
209  * @buf: a pointer to a <structname>stat</structname> struct, which
210  *    will be filled with the file information
211  *
212  * A wrapper for the POSIX stat() function. The stat() function 
213  * returns information about a file.
214  * 
215  * See the C library manual for more details about stat().
216  *
217  * Returns: 0 if the information was successfully retrieved, -1 if an error 
218  *    occurred
219  * 
220  * Since: 2.6
221  */
222 int
223 g_stat (const gchar * filename, struct stat *buf)
224 {
225 #ifdef G_OS_WIN32
226   if (G_WIN32_HAVE_WIDECHAR_API ()) {
227     wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
228     int retval;
229     int save_errno;
230
231     if (wfilename == NULL) {
232       errno = EINVAL;
233       return -1;
234     }
235
236     retval = _wstat (wfilename, (struct _stat *) buf);
237     save_errno = errno;
238
239     g_free (wfilename);
240
241     errno = save_errno;
242     return retval;
243   } else {
244     gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
245     int retval;
246     int save_errno;
247
248     if (cp_filename == NULL) {
249       errno = EINVAL;
250       return -1;
251     }
252
253     retval = stat (cp_filename, buf);
254     save_errno = errno;
255
256     g_free (cp_filename);
257
258     errno = save_errno;
259     return retval;
260   }
261 #else
262   return stat (filename, buf);
263 #endif
264 }
265 #endif