Mercurial > audlegacy-plugins
comparison src/stdio/stdio.c @ 3161:6dd886b5c72b
revive stdio plugin for now. gio cannot write id3 tags.
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Mon, 13 Jul 2009 23:53:41 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3160:2fa63d8ef645 | 3161:6dd886b5c72b |
---|---|
1 /* Audacious | |
2 * Copyright (c) 2006 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; 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 | |
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 */ | |
18 | |
19 #include "config.h" | |
20 #include <audlegacy/plugin.h> | |
21 #include <stdio.h> | |
22 | |
23 #include <unistd.h> | |
24 #include <sys/stat.h> | |
25 #include <sys/types.h> | |
26 | |
27 #include <string.h> | |
28 | |
29 static gchar * | |
30 aud_vfs_stdio_urldecode_path(const gchar * encoded_path) | |
31 { | |
32 const gchar *cur, *ext; | |
33 gchar *path, *tmp; | |
34 gint realchar; | |
35 | |
36 if (!encoded_path) | |
37 return NULL; | |
38 | |
39 if (!aud_str_has_prefix_nocase(encoded_path, "file:")) | |
40 return NULL; | |
41 | |
42 cur = encoded_path + 5; | |
43 | |
44 if (aud_str_has_prefix_nocase(cur, "//localhost")) | |
45 cur += 11; | |
46 | |
47 if (*cur == '/') | |
48 while (cur[1] == '/') | |
49 cur++; | |
50 | |
51 tmp = g_malloc0(strlen(cur) + 1); | |
52 | |
53 while ((ext = strchr(cur, '%')) != NULL) { | |
54 strncat(tmp, cur, ext - cur); | |
55 ext++; | |
56 cur = ext + 2; | |
57 if (!sscanf(ext, "%2x", &realchar)) { | |
58 /* Assume it is a literal '%'. Several file | |
59 * managers send unencoded file: urls on drag | |
60 * and drop. */ | |
61 realchar = '%'; | |
62 cur -= 2; | |
63 } | |
64 tmp[strlen(tmp)] = realchar; | |
65 } | |
66 | |
67 path = g_strconcat(tmp, cur, NULL); | |
68 g_free(tmp); | |
69 return path; | |
70 } | |
71 | |
72 VFSFile * | |
73 stdio_aud_vfs_fopen_impl(const gchar * path, | |
74 const gchar * mode) | |
75 { | |
76 VFSFile *file; | |
77 gchar *decpath; | |
78 | |
79 if (!path || !mode) | |
80 return NULL; | |
81 | |
82 decpath = aud_vfs_stdio_urldecode_path(path); | |
83 | |
84 file = g_new(VFSFile, 1); | |
85 | |
86 file->handle = fopen(decpath != NULL ? decpath : path, mode); | |
87 | |
88 g_free(decpath); | |
89 | |
90 if (file->handle == NULL) { | |
91 g_free(file); | |
92 file = NULL; | |
93 } | |
94 | |
95 return file; | |
96 } | |
97 | |
98 gint | |
99 stdio_aud_vfs_fclose_impl(VFSFile * file) | |
100 { | |
101 gint ret = 0; | |
102 | |
103 if (file == NULL) | |
104 return -1; | |
105 | |
106 if (file->handle) | |
107 { | |
108 FILE *handle = (FILE *) file->handle; | |
109 | |
110 if (fclose(handle) != 0) | |
111 ret = -1; | |
112 file->handle = NULL; | |
113 } | |
114 | |
115 return ret; | |
116 } | |
117 | |
118 size_t | |
119 stdio_aud_vfs_fread_impl(gpointer ptr, | |
120 size_t size, | |
121 size_t nmemb, | |
122 VFSFile * file) | |
123 { | |
124 FILE *handle; | |
125 | |
126 if (file == NULL) | |
127 return 0; | |
128 | |
129 handle = (FILE *) file->handle; | |
130 | |
131 return fread(ptr, size, nmemb, handle); | |
132 } | |
133 | |
134 size_t | |
135 stdio_aud_vfs_fwrite_impl(gconstpointer ptr, | |
136 size_t size, | |
137 size_t nmemb, | |
138 VFSFile * file) | |
139 { | |
140 FILE *handle; | |
141 | |
142 if (file == NULL) | |
143 return 0; | |
144 | |
145 handle = (FILE *) file->handle; | |
146 | |
147 return fwrite(ptr, size, nmemb, handle); | |
148 } | |
149 | |
150 gint | |
151 stdio_aud_vfs_getc_impl(VFSFile *stream) | |
152 { | |
153 FILE *handle = (FILE *) stream->handle; | |
154 | |
155 return getc( handle ); | |
156 } | |
157 | |
158 gint | |
159 stdio_aud_vfs_ungetc_impl(gint c, VFSFile * file) | |
160 { | |
161 FILE *handle; | |
162 | |
163 if (file == NULL) | |
164 return -1; | |
165 | |
166 handle = (FILE *) file->handle; | |
167 | |
168 return ungetc(c, handle); | |
169 } | |
170 | |
171 gint | |
172 stdio_aud_vfs_fseek_impl(VFSFile * file, | |
173 glong offset, | |
174 gint whence) | |
175 { | |
176 FILE *handle; | |
177 | |
178 if (file == NULL) | |
179 return 0; | |
180 | |
181 handle = (FILE *) file->handle; | |
182 | |
183 return fseek(handle, offset, whence); | |
184 } | |
185 | |
186 void | |
187 stdio_aud_vfs_rewind_impl(VFSFile * file) | |
188 { | |
189 FILE *handle; | |
190 | |
191 if (file == NULL) | |
192 return; | |
193 | |
194 handle = (FILE *) file->handle; | |
195 | |
196 rewind(handle); | |
197 } | |
198 | |
199 glong | |
200 stdio_aud_vfs_ftell_impl(VFSFile * file) | |
201 { | |
202 FILE *handle; | |
203 | |
204 if (file == NULL) | |
205 return 0; | |
206 | |
207 handle = (FILE *) file->handle; | |
208 | |
209 return ftell(handle); | |
210 } | |
211 | |
212 gboolean | |
213 stdio_aud_vfs_feof_impl(VFSFile * file) | |
214 { | |
215 FILE *handle; | |
216 | |
217 if (file == NULL) | |
218 return FALSE; | |
219 | |
220 handle = (FILE *) file->handle; | |
221 | |
222 return (gboolean) feof(handle); | |
223 } | |
224 | |
225 gint | |
226 stdio_aud_vfs_truncate_impl(VFSFile * file, glong size) | |
227 { | |
228 FILE *handle; | |
229 | |
230 if (file == NULL) | |
231 return -1; | |
232 | |
233 handle = (FILE *) file->handle; | |
234 | |
235 return ftruncate(fileno(handle), size); | |
236 } | |
237 | |
238 off_t | |
239 stdio_aud_vfs_fsize_impl(VFSFile * file) | |
240 { | |
241 FILE *handle; | |
242 struct stat s; | |
243 | |
244 if (file == NULL) | |
245 return -1; | |
246 | |
247 handle = (FILE *) file->handle; | |
248 | |
249 if (fstat(fileno(handle), &s) == -1) | |
250 return -1; | |
251 | |
252 return s.st_size; | |
253 } | |
254 | |
255 VFSConstructor file_const = { | |
256 .uri_id = "file://", | |
257 .vfs_fopen_impl = stdio_aud_vfs_fopen_impl, | |
258 .vfs_fclose_impl = stdio_aud_vfs_fclose_impl, | |
259 .vfs_fread_impl = stdio_aud_vfs_fread_impl, | |
260 .vfs_fwrite_impl = stdio_aud_vfs_fwrite_impl, | |
261 .vfs_getc_impl = stdio_aud_vfs_getc_impl, | |
262 .vfs_ungetc_impl = stdio_aud_vfs_ungetc_impl, | |
263 .vfs_fseek_impl = stdio_aud_vfs_fseek_impl, | |
264 .vfs_rewind_impl = stdio_aud_vfs_rewind_impl, | |
265 .vfs_ftell_impl = stdio_aud_vfs_ftell_impl, | |
266 .vfs_feof_impl = stdio_aud_vfs_feof_impl, | |
267 .vfs_truncate_impl = stdio_aud_vfs_truncate_impl, | |
268 .vfs_fsize_impl = stdio_aud_vfs_fsize_impl | |
269 }; | |
270 | |
271 static void init(void) | |
272 { | |
273 aud_vfs_register_transport(&file_const); | |
274 } | |
275 | |
276 static void cleanup(void) | |
277 { | |
278 #if 0 | |
279 aud_vfs_unregister_transport(&file_const); | |
280 #endif | |
281 } | |
282 | |
283 DECLARE_PLUGIN(stdio, init, cleanup, NULL, NULL, NULL, NULL, NULL, NULL); |