Mercurial > audlegacy
annotate src/audacious/vfs.c @ 2363:d9208536e270 trunk
[svn] reformatting and g_return_if_fail()
author | mf0102 |
---|---|
date | Thu, 18 Jan 2007 13:21:57 -0800 |
parents | 7d1dc0ca6df8 |
children | ebe01d05f1dc |
rev | line source |
---|---|
2313 | 1 /* Audacious |
2 * Copyright (c) 2006-2007 William Pitcock | |
3 * | |
4 * This program is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; under version 2 of the License. | |
7 * | |
8 * This program is distributed in the hope that it will be useful, | |
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 * GNU General Public License for more details. | |
12 * | |
13 * You should have received a copy of the GNU General Public License | |
14 * along with this program; if not, write to the Free Software | |
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
16 */ | |
17 | |
18 #include "vfs.h" | |
19 #include <stdio.h> | |
20 | |
21 #include <unistd.h> | |
22 #include <sys/stat.h> | |
23 #include <sys/types.h> | |
24 | |
2336 | 25 #include <string.h> |
26 | |
2313 | 27 #include "libaudacious/urldecode.h" |
28 | |
29 static GList *vfs_transports = NULL; | |
30 | |
2335
e80c9dfc93aa
[svn] - g_strsplit() wraps strsplit(3), and thus has different results on
nenolod
parents:
2313
diff
changeset
|
31 #define VFS_DEBUG |
e80c9dfc93aa
[svn] - g_strsplit() wraps strsplit(3), and thus has different results on
nenolod
parents:
2313
diff
changeset
|
32 |
2313 | 33 #ifdef VFS_DEBUG |
34 # define DBG(x, args...) g_print(x, ## args); | |
35 #else | |
36 # define DBG(x, args...) | |
37 #endif | |
38 | |
39 /** | |
40 * vfs_register_transport: | |
41 * @vtable: The #VFSConstructor vtable to register. | |
42 * | |
43 * Registers a #VFSConstructor vtable with the VFS system. | |
44 * | |
45 * Return value: TRUE on success, FALSE on failure. | |
46 **/ | |
47 gboolean | |
48 vfs_register_transport(VFSConstructor *vtable) | |
49 { | |
50 vfs_transports = g_list_append(vfs_transports, vtable); | |
51 | |
52 return TRUE; | |
53 } | |
54 | |
55 /** | |
56 * vfs_fopen: | |
57 * @path: The path or URI to open. | |
58 * @mode: The preferred access privileges (not guaranteed). | |
59 * | |
60 * Opens a stream from a VFS transport using a #VFSConstructor. | |
61 * | |
62 * Return value: On success, a #VFSFile object representing the stream. | |
63 **/ | |
64 VFSFile * | |
65 vfs_fopen(const gchar * path, | |
66 const gchar * mode) | |
67 { | |
68 VFSFile *file; | |
69 VFSConstructor *vtable = NULL; | |
70 GList *node; | |
71 gchar *decpath; | |
72 | |
73 if (!path || !mode) | |
74 return NULL; | |
75 | |
76 decpath = xmms_urldecode_plain(path); | |
77 | |
2335
e80c9dfc93aa
[svn] - g_strsplit() wraps strsplit(3), and thus has different results on
nenolod
parents:
2313
diff
changeset
|
78 for (node = vfs_transports; node != NULL; node = g_list_next(node)) |
2313 | 79 { |
2335
e80c9dfc93aa
[svn] - g_strsplit() wraps strsplit(3), and thus has different results on
nenolod
parents:
2313
diff
changeset
|
80 vtable = (VFSConstructor *) node->data; |
2313 | 81 |
2337 | 82 DBG("Does %s match %s?\n", decpath, vtable->uri_id); |
83 | |
2335
e80c9dfc93aa
[svn] - g_strsplit() wraps strsplit(3), and thus has different results on
nenolod
parents:
2313
diff
changeset
|
84 if (!strncasecmp(decpath, vtable->uri_id, strlen(vtable->uri_id))) |
e80c9dfc93aa
[svn] - g_strsplit() wraps strsplit(3), and thus has different results on
nenolod
parents:
2313
diff
changeset
|
85 break; |
2313 | 86 } |
87 | |
88 /* no transport vtable has been registered, bail. */ | |
89 if (vtable == NULL) | |
90 return NULL; | |
91 | |
2337 | 92 if (strlen(vtable->uri_id) > 1) |
93 file = vtable->vfs_fopen_impl(decpath + strlen(vtable->uri_id), mode); | |
94 else | |
95 file = vtable->vfs_fopen_impl(decpath, mode); | |
2313 | 96 |
97 if (file == NULL) | |
98 return NULL; | |
99 | |
100 file->uri = g_strdup(path); | |
101 file->base = vtable; | |
102 | |
103 g_free(decpath); | |
104 | |
105 return file; | |
106 } | |
107 | |
108 /** | |
109 * vfs_fclose: | |
110 * @file: A #VFSFile object to destroy. | |
111 * | |
112 * Closes a VFS stream and destroys a #VFSFile object. | |
113 * | |
114 * Return value: -1 on failure, 0 on success. | |
115 **/ | |
116 gint | |
117 vfs_fclose(VFSFile * file) | |
118 { | |
119 gint ret = 0; | |
120 | |
121 if (file == NULL) | |
122 return -1; | |
123 | |
124 if (file->base->vfs_fclose_impl(file) != 0) | |
125 ret = -1; | |
126 | |
127 if (file->uri != NULL) | |
128 g_free(file->uri); | |
129 | |
130 g_free(file); | |
131 | |
132 return ret; | |
133 } | |
134 | |
135 /** | |
136 * vfs_fread: | |
137 * @ptr: A pointer to the destination buffer. | |
138 * @size: The size of each element to read. | |
139 * @nmemb: The number of elements to read. | |
140 * @file: #VFSFile object that represents the VFS stream. | |
141 * | |
142 * Reads from a VFS stream. | |
143 * | |
144 * Return value: The amount of elements succesfully read. | |
145 **/ | |
146 size_t | |
147 vfs_fread(gpointer ptr, | |
148 size_t size, | |
149 size_t nmemb, | |
150 VFSFile * file) | |
151 { | |
152 if (file == NULL) | |
153 return 0; | |
154 | |
155 return file->base->vfs_fread_impl(ptr, size, nmemb, file); | |
156 } | |
157 | |
158 /** | |
159 * vfs_fwrite: | |
160 * @ptr: A const pointer to the source buffer. | |
161 * @size: The size of each element to write. | |
162 * @nmemb: The number of elements to write. | |
163 * @file: #VFSFile object that represents the VFS stream. | |
164 * | |
165 * Writes to a VFS stream. | |
166 * | |
167 * Return value: The amount of elements succesfully written. | |
168 **/ | |
169 size_t | |
170 vfs_fwrite(gconstpointer ptr, | |
171 size_t size, | |
172 size_t nmemb, | |
173 VFSFile * file) | |
174 { | |
175 if (file == NULL) | |
176 return 0; | |
177 | |
178 return file->base->vfs_fwrite_impl(ptr, size, nmemb, file); | |
179 } | |
180 | |
181 /** | |
182 * vfs_getc: | |
183 * @stream: #VFSFile object that represents the VFS stream. | |
184 * | |
185 * Reads a character from a VFS stream. | |
186 * | |
187 * Return value: On success, a character. Otherwise, -1. | |
188 **/ | |
189 gint | |
190 vfs_getc(VFSFile *stream) | |
191 { | |
192 if (stream == NULL) | |
193 return -1; | |
194 | |
195 return stream->base->vfs_getc_impl(stream); | |
196 } | |
197 | |
198 /** | |
199 * vfs_ungetc: | |
200 * @c: The character to push back. | |
201 * @stream: #VFSFile object that represents the VFS stream. | |
202 * | |
203 * Pushes a character back to the VFS stream. | |
204 * | |
205 * Return value: On success, 0. Otherwise, -1. | |
206 **/ | |
207 gint | |
208 vfs_ungetc(gint c, VFSFile *stream) | |
209 { | |
210 if (stream == NULL) | |
211 return -1; | |
212 | |
213 return stream->base->vfs_ungetc_impl(c, stream); | |
214 } | |
215 | |
216 /** | |
217 * vfs_fseek: | |
218 * @file: #VFSFile object that represents the VFS stream. | |
219 * @offset: The offset to seek to. | |
220 * @whence: Whether or not the seek is absolute or not. | |
221 * | |
222 * Seeks through a VFS stream. | |
223 * | |
224 * Return value: On success, 1. Otherwise, 0. | |
225 **/ | |
226 gint | |
227 vfs_fseek(VFSFile * file, | |
228 glong offset, | |
229 gint whence) | |
230 { | |
231 if (file == NULL) | |
232 return 0; | |
233 | |
234 return file->base->vfs_fseek_impl(file, offset, whence); | |
235 } | |
236 | |
237 /** | |
238 * vfs_rewind: | |
239 * @file: #VFSFile object that represents the VFS stream. | |
240 * | |
241 * Rewinds a VFS stream. | |
242 **/ | |
243 void | |
244 vfs_rewind(VFSFile * file) | |
245 { | |
246 if (file == NULL) | |
247 return; | |
248 | |
249 file->base->vfs_rewind_impl(file); | |
250 } | |
251 | |
252 /** | |
253 * vfs_ftell: | |
254 * @file: #VFSFile object that represents the VFS stream. | |
255 * | |
256 * Returns the current position in the VFS stream's buffer. | |
257 * | |
258 * Return value: On success, the current position. Otherwise, -1. | |
259 **/ | |
260 glong | |
261 vfs_ftell(VFSFile * file) | |
262 { | |
263 if (file == NULL) | |
264 return -1; | |
265 | |
266 return file->base->vfs_ftell_impl(file); | |
267 } | |
268 | |
269 /** | |
270 * vfs_feof: | |
271 * @file: #VFSFile object that represents the VFS stream. | |
272 * | |
273 * Returns whether or not the VFS stream has reached EOF. | |
274 * | |
275 * Return value: On success, whether or not the VFS stream is at EOF. Otherwise, FALSE. | |
276 **/ | |
277 gboolean | |
278 vfs_feof(VFSFile * file) | |
279 { | |
280 if (file == NULL) | |
281 return FALSE; | |
282 | |
283 return (gboolean) file->base->vfs_feof_impl(file); | |
284 } | |
285 | |
286 /** | |
287 * vfs_truncate: | |
288 * @file: #VFSFile object that represents the VFS stream. | |
289 * @length: The length to truncate at. | |
290 * | |
291 * Truncates a VFS stream to a certain size. | |
292 * | |
293 * Return value: On success, 0. Otherwise, -1. | |
294 **/ | |
295 gint | |
296 vfs_truncate(VFSFile * file, glong length) | |
297 { | |
298 if (file == NULL) | |
299 return -1; | |
300 | |
301 return file->base->vfs_truncate_impl(file, length); | |
302 } | |
303 | |
304 /** | |
305 * vfs_file_test: | |
306 * @path: A path to test. | |
307 * @test: A GFileTest to run. | |
308 * | |
309 * Wrapper for g_file_test(). | |
310 * | |
311 * Return value: The result of g_file_test(). | |
312 **/ | |
313 gboolean | |
314 vfs_file_test(const gchar * path, GFileTest test) | |
315 { | |
316 return g_file_test(path, test); | |
317 } | |
318 | |
319 /** | |
320 * vfs_is_writeable: | |
321 * @path: A path to test. | |
322 * | |
323 * Tests if a file is writeable. | |
324 * | |
325 * Return value: TRUE if the file is writeable, otherwise FALSE. | |
326 **/ | |
327 gboolean | |
328 vfs_is_writeable(const gchar * path) | |
329 { | |
330 struct stat info; | |
331 | |
332 if (stat(path, &info) == -1) | |
333 return FALSE; | |
334 | |
335 return (info.st_mode & S_IWUSR); | |
336 } |