Mercurial > audlegacy
annotate libaudacious/vfs_gnome.c @ 1811:a6a248bfafc3 trunk
[svn] - create the windows during skin load process after the skin.hints has been loaded
author | nenolod |
---|---|
date | Wed, 04 Oct 2006 23:16:49 -0700 |
parents | e9c24e35bd76 |
children |
rev | line source |
---|---|
0 | 1 /* This program is free software; you can redistribute it and/or modify |
1460 | 2 * it under the terms of the GNU General Public License as published by |
0 | 3 * the Free Software Foundation; either version 2 of the License, or |
4 * (at your option) any later version. | |
5 * | |
6 * This program is distributed in the hope that it will be useful, | |
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
9 * GNU General Public License for more details. | |
10 * | |
11 * You should have received a copy of the GNU General Public License | |
12 * along with this program; if not, write to the Free Software | |
1459 | 13 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
0 | 14 */ |
15 | |
16 #include "vfs.h" | |
17 #include <string.h> | |
18 #include <libgnomevfs/gnome-vfs.h> | |
19 | |
20 | |
21 struct _VFSFile | |
22 { | |
23 GnomeVFSHandle *handle; | |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
24 gboolean eof; |
1683
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
25 GSList *streamstack; |
0 | 26 }; |
27 | |
28 | |
29 static void mode_to_gnome_vfs(const gchar * mode, | |
30 GnomeVFSOpenMode * g_mode, | |
31 gboolean * truncate, | |
32 gboolean * append); | |
33 | |
34 gboolean | |
35 vfs_init(void) | |
36 { | |
37 if (!gnome_vfs_init()) | |
38 return FALSE; | |
39 | |
40 g_atexit(gnome_vfs_shutdown); | |
41 return TRUE; | |
42 } | |
43 | |
44 VFSFile * | |
45 vfs_fopen(const gchar * path, | |
46 const gchar * mode) | |
47 { | |
48 VFSFile *file; | |
49 GnomeVFSResult g_result; | |
50 GnomeVFSOpenMode g_mode; | |
51 gboolean truncate, append; | |
52 | |
550 | 53 if (!path || !mode) |
54 return NULL; | |
55 | |
0 | 56 file = g_new(VFSFile, 1); |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
57 file->eof = FALSE; |
1683
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
58 file->streamstack = NULL; |
0 | 59 |
60 mode_to_gnome_vfs(mode, &g_mode, &truncate, &append); | |
78
ae0af0523844
[svn] Call gnome_vfs_escape_path_string() before attempting to open the file.
msameer
parents:
0
diff
changeset
|
61 gchar *escaped_file = gnome_vfs_escape_path_string(path); |
0 | 62 |
63 if (!truncate) { | |
78
ae0af0523844
[svn] Call gnome_vfs_escape_path_string() before attempting to open the file.
msameer
parents:
0
diff
changeset
|
64 g_result = gnome_vfs_open(&(file->handle), escaped_file, g_mode); |
0 | 65 if (append && g_result == GNOME_VFS_ERROR_NOT_FOUND) { |
66 g_result = gnome_vfs_create(&(file->handle), | |
67 path, g_mode, TRUE, | |
68 S_IRUSR | S_IWUSR | | |
69 S_IRGRP | S_IWGRP | | |
70 S_IROTH | S_IWOTH); | |
71 } | |
72 | |
73 if (append && g_result == GNOME_VFS_OK) { | |
74 g_result = gnome_vfs_seek(file->handle, GNOME_VFS_SEEK_END, 0); | |
75 if (g_result != GNOME_VFS_OK) | |
76 gnome_vfs_close(file->handle); | |
77 } | |
78 } | |
79 else { | |
80 g_result = gnome_vfs_create(&(file->handle), | |
78
ae0af0523844
[svn] Call gnome_vfs_escape_path_string() before attempting to open the file.
msameer
parents:
0
diff
changeset
|
81 escaped_file, g_mode, FALSE, |
0 | 82 S_IRUSR | S_IWUSR | |
83 S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); | |
84 } | |
85 | |
86 if (g_result != GNOME_VFS_OK) | |
87 file->handle = NULL; | |
88 | |
89 if (file->handle == NULL) { | |
90 g_free(file); | |
91 file = NULL; | |
92 } | |
93 | |
78
ae0af0523844
[svn] Call gnome_vfs_escape_path_string() before attempting to open the file.
msameer
parents:
0
diff
changeset
|
94 g_free(escaped_file); |
ae0af0523844
[svn] Call gnome_vfs_escape_path_string() before attempting to open the file.
msameer
parents:
0
diff
changeset
|
95 |
0 | 96 return file; |
97 } | |
98 | |
99 gint | |
100 vfs_fclose(VFSFile * file) | |
101 { | |
102 gint ret = 0; | |
103 | |
821 | 104 if (file == NULL) |
105 return 0; | |
106 | |
0 | 107 if (file->handle) { |
108 if (gnome_vfs_close(file->handle) != GNOME_VFS_OK) | |
109 ret = -1; | |
110 } | |
111 | |
1683
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
112 /* free the streamstack */ |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
113 if ( file->streamstack != NULL ) |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
114 g_slist_free( file->streamstack ); |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
115 |
0 | 116 g_free(file); |
117 | |
118 return ret; | |
119 } | |
120 | |
121 size_t | |
122 vfs_fread(gpointer ptr, | |
123 size_t size, | |
124 size_t nmemb, | |
125 VFSFile * file) | |
126 { | |
127 GnomeVFSResult result; | |
128 GnomeVFSFileSize bytes_read; | |
129 | |
821 | 130 if (file == NULL) |
131 return 0; | |
132 | |
0 | 133 result = gnome_vfs_read(file->handle, ptr, size * nmemb, &bytes_read); |
134 if (result == GNOME_VFS_OK) | |
135 return bytes_read; | |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
136 |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
137 if (result == GNOME_VFS_ERROR_EOF) { |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
138 file->eof = TRUE; |
0 | 139 return 0; |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
140 } |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
141 |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
142 return -1; |
0 | 143 } |
144 | |
145 size_t | |
146 vfs_fwrite(gconstpointer ptr, | |
147 size_t size, | |
148 size_t nmemb, | |
149 VFSFile * file) | |
150 { | |
151 GnomeVFSResult result; | |
152 GnomeVFSFileSize bytes_written; | |
153 | |
821 | 154 if (file == NULL) |
155 return 0; | |
156 | |
0 | 157 result = gnome_vfs_write(file->handle, ptr, size * nmemb, &bytes_written); |
158 if (result == GNOME_VFS_OK) | |
159 return bytes_written; | |
160 else | |
161 return -1; | |
162 } | |
163 | |
164 gint | |
1683
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
165 vfs_getc(VFSFile *stream) |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
166 { |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
167 guchar uc; |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
168 if ( stream->streamstack != NULL ) /* check if some char was ungetc'ed before */ |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
169 { |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
170 uc = GPOINTER_TO_INT(stream->streamstack->data); |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
171 stream->streamstack = g_slist_delete_link( stream->streamstack , stream->streamstack ); |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
172 return uc; |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
173 } |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
174 else /* for gnomevfs, vfs_getc is emulated by vfs_fread */ |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
175 { |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
176 if (vfs_fread(&uc, 1, 1, stream)) |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
177 return uc; |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
178 else |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
179 return EOF; |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
180 } |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
181 } |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
182 |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
183 gint |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
184 vfs_ungetc(gint c, VFSFile *stream) |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
185 { |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
186 stream->streamstack = g_slist_prepend( stream->streamstack , GINT_TO_POINTER(c) ); |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
187 if ( stream->streamstack != NULL ) |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
188 return c; |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
189 else |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
190 return EOF; /* an error occurred */ |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
191 } |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
192 |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
193 gint |
0 | 194 vfs_fseek(VFSFile * file, |
195 glong offset, | |
196 gint whence) | |
197 { | |
198 GnomeVFSResult result; | |
199 GnomeVFSSeekPosition g_whence; | |
200 | |
821 | 201 if (file == NULL) |
202 return 0; | |
203 | |
0 | 204 switch (whence) { |
205 case SEEK_SET: | |
206 g_whence = GNOME_VFS_SEEK_START; | |
207 break; | |
208 case SEEK_CUR: | |
209 g_whence = GNOME_VFS_SEEK_CURRENT; | |
210 break; | |
211 case SEEK_END: | |
212 g_whence = GNOME_VFS_SEEK_END; | |
213 break; | |
214 default: | |
215 g_warning("vfs_fseek: invalid whence value"); | |
216 return -1; | |
217 } | |
218 | |
219 result = gnome_vfs_seek(file->handle, g_whence, offset); | |
220 | |
221 if (result == GNOME_VFS_OK) | |
222 return 0; | |
223 else | |
224 return -1; | |
225 } | |
226 | |
227 void | |
228 vfs_rewind(VFSFile * file) | |
229 { | |
821 | 230 if (file == NULL) |
231 return; | |
232 | |
0 | 233 vfs_fseek(file, 0L, SEEK_SET); |
234 } | |
235 | |
236 glong | |
237 vfs_ftell(VFSFile * file) | |
238 { | |
239 GnomeVFSResult result; | |
240 GnomeVFSFileSize position; | |
241 | |
821 | 242 if (file == NULL) |
243 return 0; | |
244 | |
0 | 245 result = gnome_vfs_tell(file->handle, &position); |
246 | |
247 if (result == GNOME_VFS_OK) | |
248 return position; | |
249 else | |
250 return -1; | |
251 } | |
252 | |
253 gboolean | |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
254 vfs_feof(VFSFile * file) |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
255 { |
821 | 256 if (file == NULL) |
257 return FALSE; | |
258 | |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
259 return file->eof; |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
260 } |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
261 |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
262 gboolean |
0 | 263 vfs_file_test(const gchar * path, |
264 GFileTest test) | |
265 { | |
266 GnomeVFSResult result; | |
267 GnomeVFSFileInfo info; | |
268 GFileTest file_test; | |
269 | |
270 result = gnome_vfs_get_file_info(path, &info, | |
271 GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS); | |
272 | |
273 if (test == G_FILE_TEST_EXISTS) | |
274 return (result == GNOME_VFS_OK) ? TRUE : FALSE; | |
275 else if (test == G_FILE_TEST_IS_EXECUTABLE) | |
276 return (info.permissions & GNOME_VFS_PERM_ACCESS_EXECUTABLE) | |
277 ? TRUE : FALSE; | |
278 | |
279 switch (info.type) { | |
280 case GNOME_VFS_FILE_TYPE_REGULAR: | |
281 file_test = G_FILE_TEST_IS_REGULAR; | |
282 break; | |
283 case GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK: | |
284 file_test = G_FILE_TEST_IS_SYMLINK; | |
285 break; | |
286 case GNOME_VFS_FILE_TYPE_DIRECTORY: | |
287 file_test = G_FILE_TEST_IS_DIR; | |
288 default: | |
289 return FALSE; | |
290 } | |
291 | |
292 if (test == file_test) | |
293 return TRUE; | |
294 else | |
295 return FALSE; | |
296 } | |
297 | |
298 gboolean | |
299 vfs_is_writeable(const gchar * path) | |
300 { | |
301 GnomeVFSFileInfo info; | |
302 | |
303 if (gnome_vfs_get_file_info(path, &info, GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS) | |
304 != GNOME_VFS_OK) { | |
305 return FALSE; | |
306 } | |
307 | |
308 return (info.permissions & GNOME_VFS_PERM_ACCESS_WRITABLE ? TRUE : FALSE); | |
309 } | |
310 | |
311 gint | |
312 vfs_truncate(VFSFile * file, | |
313 glong length) | |
314 { | |
315 GnomeVFSResult result; | |
316 | |
821 | 317 if (file == NULL) |
318 return -1; | |
319 | |
0 | 320 result = gnome_vfs_truncate_handle(file->handle, (GnomeVFSFileSize) length); |
321 | |
322 if (result == GNOME_VFS_OK) | |
323 return 0; | |
324 else | |
325 return -1; | |
326 } | |
327 | |
328 static gchar *strdup_exclude_chars(const gchar * s, | |
329 const gchar * c); | |
330 | |
331 static void | |
332 mode_to_gnome_vfs(const gchar * mode, | |
333 GnomeVFSOpenMode * g_mode, | |
334 gboolean * truncate, | |
335 gboolean * append) | |
336 { | |
337 gchar *s; | |
338 | |
339 *g_mode = GNOME_VFS_OPEN_RANDOM; | |
340 *truncate = *append = FALSE; | |
341 | |
342 s = strdup_exclude_chars(mode, "bt"); | |
343 switch (s[0]) { | |
344 case 'r': | |
345 *g_mode |= GNOME_VFS_OPEN_READ; | |
346 | |
347 if (s[1] == '+') | |
348 *g_mode |= GNOME_VFS_OPEN_WRITE; | |
349 | |
350 break; | |
351 case 'w': | |
352 case 'a': | |
353 *g_mode |= GNOME_VFS_OPEN_WRITE; | |
354 | |
355 if (s[0] == 'w') | |
356 *truncate = TRUE; | |
357 else | |
358 *append = TRUE; | |
359 | |
360 if (s[1] == '+') | |
361 *g_mode |= GNOME_VFS_OPEN_READ; | |
362 | |
363 break; | |
364 default: | |
365 g_warning("mode_to_gnome_vfs: unhandled mode character"); | |
366 } | |
367 g_free(s); | |
368 } | |
369 | |
370 static gchar * | |
371 strdup_exclude_chars(const gchar * s, | |
372 const gchar * c) | |
373 { | |
374 gint i, j, k; | |
375 gint newlen = 0; | |
376 gchar *newstr; | |
377 gboolean found; | |
378 | |
379 /* Calculate number of chars in new string */ | |
380 for (i = 0; s[i] != '\0'; i++) { | |
381 found = FALSE; | |
382 | |
383 for (j = 0; j < strlen(c) && !found; j++) | |
384 if (s[i] == c[j]) | |
385 found = TRUE; | |
386 | |
387 if (!found) | |
388 newlen++; | |
389 } | |
390 | |
391 newstr = g_malloc(newlen + 1); | |
392 | |
393 /* Copy valid chars to new string */ | |
394 for (i = k = 0; s[i] != '\0'; i++) { | |
395 found = FALSE; | |
396 | |
397 for (j = 0; j < strlen(c) && !found; j++) | |
398 if (s[i] == c[j]) | |
399 found = TRUE; | |
400 | |
401 if (!found) | |
402 newstr[k++] = s[i]; | |
403 } | |
404 | |
405 newstr[k] = '\0'; | |
406 | |
407 return newstr; | |
408 } |