Mercurial > audlegacy
annotate libaudacious/vfs.c @ 1997:93c59698f5fd trunk
[svn] - NewVFS lives ;)
author | nenolod |
---|---|
date | Thu, 23 Nov 2006 20:06:19 -0800 |
parents | 8045625b45d3 |
children | 1e0503521702 |
rev | line source |
---|---|
1997 | 1 /* Audacious |
2 * Copyright (c) 2006 William Pitcock | |
3 * | |
4 * This program is free software; you can redistribute it and/or modify | |
1460 | 5 * it under the terms of the GNU General Public License as published by |
0 | 6 * the Free Software Foundation; either version 2 of the License, or |
7 * (at your option) any later version. | |
8 * | |
9 * This program is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
15 * along with this program; if not, write to the Free Software | |
1459 | 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
0 | 17 */ |
18 | |
19 #include "vfs.h" | |
20 #include <stdio.h> | |
21 | |
22 #include <unistd.h> | |
23 #include <sys/stat.h> | |
24 #include <sys/types.h> | |
25 | |
1997 | 26 static GList *vfs_transports = NULL; |
27 | |
28 #define VFS_DEBUG | |
29 | |
30 #ifdef VFS_DEBUG | |
31 # define DBG(x, args...) g_print(x, ## args); | |
32 #else | |
33 # define DBG(x, args...) | |
34 #endif | |
35 | |
0 | 36 gboolean |
1997 | 37 vfs_register_transport(VFSConstructor *vtable) |
0 | 38 { |
1997 | 39 DBG("registering transport [%s]\n", vtable->uri_id); |
40 | |
41 vfs_transports = g_list_append(vfs_transports, vtable); | |
42 | |
0 | 43 return TRUE; |
44 } | |
45 | |
46 VFSFile * | |
47 vfs_fopen(const gchar * path, | |
48 const gchar * mode) | |
49 { | |
50 VFSFile *file; | |
1997 | 51 gchar **vec; |
52 VFSConstructor *vtable = NULL; | |
53 GList *node; | |
0 | 54 |
550 | 55 if (!path || !mode) |
56 return NULL; | |
57 | |
1997 | 58 vec = g_strsplit(path, "://", 2); |
59 | |
60 DBG("vec[0]: %s, vec[1]: %s\n", vec[0], vec[1]); | |
0 | 61 |
1997 | 62 /* special case: no transport specified, look for the "/" transport */ |
63 if (vec[1] == NULL) | |
64 { | |
65 for (node = vfs_transports; node != NULL; node = g_list_next(node)) | |
66 { | |
67 vtable = (VFSConstructor *) node->data; | |
68 | |
69 if (*vtable->uri_id == '/') | |
70 break; | |
71 } | |
72 } | |
73 else | |
74 { | |
75 for (node = vfs_transports; node != NULL; node = g_list_next(node)) | |
76 { | |
77 vtable = (VFSConstructor *) node->data; | |
0 | 78 |
1997 | 79 if (!g_strcasecmp(vec[0], vtable->uri_id)) |
80 break; | |
81 } | |
82 } | |
83 | |
84 /* no transport vtable has been registered, bail. */ | |
85 if (vtable == NULL) | |
86 { | |
87 return NULL; | |
0 | 88 } |
89 | |
1997 | 90 file = vtable->vfs_fopen_impl(vec[1] ? vec[1] : vec[0], mode); |
91 | |
92 if (file == NULL) | |
93 { | |
94 return NULL; | |
95 } | |
96 | |
97 file->uri = g_strdup(path); | |
98 file->base = vtable; | |
99 | |
100 DBG("returning %p", file); | |
101 | |
0 | 102 return file; |
103 } | |
104 | |
105 gint | |
106 vfs_fclose(VFSFile * file) | |
107 { | |
108 gint ret = 0; | |
109 | |
821 | 110 if (file == NULL) |
111 return -1; | |
112 | |
1997 | 113 if (file->base->vfs_fclose_impl(file) != 0) |
114 ret = -1; | |
0 | 115 |
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 { | |
821 | 127 if (file == NULL) |
128 return 0; | |
129 | |
1997 | 130 return file->base->vfs_fread_impl(ptr, size, nmemb, file); |
0 | 131 } |
132 | |
133 size_t | |
134 vfs_fwrite(gconstpointer ptr, | |
135 size_t size, | |
136 size_t nmemb, | |
137 VFSFile * file) | |
138 { | |
821 | 139 if (file == NULL) |
140 return 0; | |
141 | |
1997 | 142 return file->base->vfs_fwrite_impl(ptr, size, nmemb, file); |
0 | 143 } |
144 | |
145 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
|
146 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
|
147 { |
1997 | 148 if (stream == NULL) |
149 return -1; | |
150 | |
151 return stream->base->vfs_getc_impl(stream); | |
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
|
152 } |
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
|
153 |
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
|
154 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
|
155 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
|
156 { |
1997 | 157 if (stream == NULL) |
158 return -1; | |
159 | |
160 return stream->base->vfs_ungetc_impl(c, stream); | |
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
|
161 } |
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
|
162 |
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
|
163 gint |
0 | 164 vfs_fseek(VFSFile * file, |
165 glong offset, | |
166 gint whence) | |
167 { | |
821 | 168 if (file == NULL) |
169 return 0; | |
170 | |
1997 | 171 return file->base->vfs_fseek_impl(file, offset, whence); |
0 | 172 } |
173 | |
174 void | |
175 vfs_rewind(VFSFile * file) | |
176 { | |
821 | 177 if (file == NULL) |
178 return; | |
179 | |
1997 | 180 file->base->vfs_rewind_impl(file); |
0 | 181 } |
182 | |
183 glong | |
184 vfs_ftell(VFSFile * file) | |
185 { | |
821 | 186 if (file == NULL) |
187 return 0; | |
188 | |
1997 | 189 return file->base->vfs_ftell_impl(file); |
0 | 190 } |
191 | |
192 gboolean | |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
193 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
|
194 { |
821 | 195 if (file == NULL) |
196 return FALSE; | |
197 | |
1997 | 198 return (gboolean) file->base->vfs_feof_impl(file); |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
199 } |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
200 |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
201 gboolean |
0 | 202 vfs_file_test(const gchar * path, GFileTest test) |
203 { | |
204 return g_file_test(path, test); | |
205 } | |
206 | |
207 /* NOTE: stat() is not part of stdio */ | |
208 gboolean | |
209 vfs_is_writeable(const gchar * path) | |
210 { | |
211 struct stat info; | |
212 | |
213 if (stat(path, &info) == -1) | |
214 return FALSE; | |
215 | |
216 return (info.st_mode & S_IWUSR); | |
217 } | |
218 | |
219 gint | |
220 vfs_truncate(VFSFile * file, glong size) | |
221 { | |
821 | 222 if (file == NULL) |
223 return -1; | |
224 | |
1997 | 225 return file->base->vfs_truncate_impl(file, size); |
0 | 226 } |