Mercurial > audlegacy-plugins
annotate src/stdio/stdio.c @ 1205:c39a61e6a276
Fine-tune fadeout
author | mf0102 <0102@gmx.at> |
---|---|
date | Wed, 04 Jul 2007 10:43:33 +0200 |
parents | ed2d7787779e |
children | 594909468f53 |
rev | line source |
---|---|
306 | 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 <audacious/vfs.h> | |
20 #include <audacious/plugin.h> | |
1190
ed2d7787779e
more warning elimination.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
1146
diff
changeset
|
21 #include "audacious/strings.h" |
306 | 22 #include <stdio.h> |
23 | |
24 #include <unistd.h> | |
25 #include <sys/stat.h> | |
26 #include <sys/types.h> | |
27 | |
500
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
28 #include <string.h> |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
29 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
30 static gchar * |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
31 vfs_stdio_urldecode_path(const gchar * encoded_path) |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
32 { |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
33 const gchar *cur, *ext; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
34 gchar *path, *tmp; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
35 gint realchar; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
36 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
37 if (!encoded_path) |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
38 return NULL; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
39 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
40 if (!str_has_prefix_nocase(encoded_path, "file:")) |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
41 return NULL; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
42 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
43 cur = encoded_path + 5; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
44 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
45 if (str_has_prefix_nocase(cur, "//localhost")) |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
46 cur += 11; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
47 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
48 if (*cur == '/') |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
49 while (cur[1] == '/') |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
50 cur++; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
51 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
52 tmp = g_malloc0(strlen(cur) + 1); |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
53 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
54 while ((ext = strchr(cur, '%')) != NULL) { |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
55 strncat(tmp, cur, ext - cur); |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
56 ext++; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
57 cur = ext + 2; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
58 if (!sscanf(ext, "%2x", &realchar)) { |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
59 /* Assume it is a literal '%'. Several file |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
60 * managers send unencoded file: urls on drag |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
61 * and drop. */ |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
62 realchar = '%'; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
63 cur -= 2; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
64 } |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
65 tmp[strlen(tmp)] = realchar; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
66 } |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
67 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
68 path = g_strconcat(tmp, cur, NULL); |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
69 g_free(tmp); |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
70 return path; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
71 } |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
72 |
306 | 73 VFSFile * |
74 stdio_vfs_fopen_impl(const gchar * path, | |
75 const gchar * mode) | |
76 { | |
77 VFSFile *file; | |
500
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
78 gchar *decpath; |
306 | 79 |
80 if (!path || !mode) | |
81 return NULL; | |
82 | |
500
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
83 decpath = vfs_stdio_urldecode_path(path); |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
84 |
306 | 85 file = g_new(VFSFile, 1); |
86 | |
500
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
87 file->handle = fopen(decpath != NULL ? decpath : path, mode); |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
88 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
89 if (decpath != NULL) |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
90 g_free(decpath); |
306 | 91 |
92 if (file->handle == NULL) { | |
93 g_free(file); | |
94 file = NULL; | |
95 } | |
96 | |
97 return file; | |
98 } | |
99 | |
100 gint | |
101 stdio_vfs_fclose_impl(VFSFile * file) | |
102 { | |
103 gint ret = 0; | |
104 | |
105 if (file == NULL) | |
106 return -1; | |
107 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
108 if (file->handle) |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
109 { |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
110 FILE *handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
111 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
112 if (fclose(handle) != 0) |
306 | 113 ret = -1; |
114 } | |
115 | |
116 return ret; | |
117 } | |
118 | |
119 size_t | |
120 stdio_vfs_fread_impl(gpointer ptr, | |
121 size_t size, | |
122 size_t nmemb, | |
123 VFSFile * file) | |
124 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
125 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
126 |
306 | 127 if (file == NULL) |
128 return 0; | |
129 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
130 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
131 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
132 return fread(ptr, size, nmemb, handle); |
306 | 133 } |
134 | |
135 size_t | |
136 stdio_vfs_fwrite_impl(gconstpointer ptr, | |
137 size_t size, | |
138 size_t nmemb, | |
139 VFSFile * file) | |
140 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
141 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
142 |
306 | 143 if (file == NULL) |
144 return 0; | |
145 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
146 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
147 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
148 return fwrite(ptr, size, nmemb, handle); |
306 | 149 } |
150 | |
151 gint | |
152 stdio_vfs_getc_impl(VFSFile *stream) | |
153 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
154 FILE *handle = (FILE *) stream->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
155 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
156 return getc( handle ); |
306 | 157 } |
158 | |
159 gint | |
160 stdio_vfs_ungetc_impl(gint c, VFSFile *stream) | |
161 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
162 FILE *handle = (FILE *) stream->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
163 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
164 return ungetc( c , handle ); |
306 | 165 } |
166 | |
167 gint | |
168 stdio_vfs_fseek_impl(VFSFile * file, | |
169 glong offset, | |
170 gint whence) | |
171 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
172 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
173 |
306 | 174 if (file == NULL) |
175 return 0; | |
176 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
177 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
178 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
179 return fseek(handle, offset, whence); |
306 | 180 } |
181 | |
182 void | |
183 stdio_vfs_rewind_impl(VFSFile * file) | |
184 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
185 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
186 |
306 | 187 if (file == NULL) |
188 return; | |
189 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
190 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
191 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
192 rewind(handle); |
306 | 193 } |
194 | |
195 glong | |
196 stdio_vfs_ftell_impl(VFSFile * file) | |
197 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
198 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
199 |
306 | 200 if (file == NULL) |
201 return 0; | |
202 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
203 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
204 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
205 return ftell(handle); |
306 | 206 } |
207 | |
208 gboolean | |
209 stdio_vfs_feof_impl(VFSFile * file) | |
210 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
211 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
212 |
306 | 213 if (file == NULL) |
214 return FALSE; | |
215 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
216 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
217 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
218 return (gboolean) feof(handle); |
306 | 219 } |
220 | |
221 gint | |
222 stdio_vfs_truncate_impl(VFSFile * file, glong size) | |
223 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
224 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
225 |
306 | 226 if (file == NULL) |
227 return -1; | |
228 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
229 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
230 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
231 return ftruncate(fileno(handle), size); |
306 | 232 } |
233 | |
965 | 234 off_t |
235 stdio_vfs_fsize_impl(VFSFile * file) | |
236 { | |
237 FILE *handle; | |
238 struct stat s; | |
239 | |
240 if (file == NULL) | |
241 return -1; | |
242 | |
243 handle = (FILE *) file->handle; | |
244 | |
245 if (-1 == fstat(fileno(handle), &s)) | |
246 return -1; | |
247 | |
248 return s.st_size; | |
249 } | |
250 | |
306 | 251 VFSConstructor file_const = { |
252 "file://", | |
253 stdio_vfs_fopen_impl, | |
254 stdio_vfs_fclose_impl, | |
255 stdio_vfs_fread_impl, | |
256 stdio_vfs_fwrite_impl, | |
257 stdio_vfs_getc_impl, | |
258 stdio_vfs_ungetc_impl, | |
259 stdio_vfs_fseek_impl, | |
260 stdio_vfs_rewind_impl, | |
261 stdio_vfs_ftell_impl, | |
262 stdio_vfs_feof_impl, | |
965 | 263 stdio_vfs_truncate_impl, |
264 stdio_vfs_fsize_impl | |
306 | 265 }; |
266 | |
267 VFSConstructor default_const = { | |
268 "/", | |
269 stdio_vfs_fopen_impl, | |
270 stdio_vfs_fclose_impl, | |
271 stdio_vfs_fread_impl, | |
272 stdio_vfs_fwrite_impl, | |
273 stdio_vfs_getc_impl, | |
274 stdio_vfs_ungetc_impl, | |
275 stdio_vfs_fseek_impl, | |
276 stdio_vfs_rewind_impl, | |
277 stdio_vfs_ftell_impl, | |
278 stdio_vfs_feof_impl, | |
965 | 279 stdio_vfs_truncate_impl, |
280 stdio_vfs_fsize_impl | |
306 | 281 }; |
282 | |
283 static void init(void) | |
284 { | |
285 vfs_register_transport(&default_const); | |
286 vfs_register_transport(&file_const); | |
287 } | |
288 | |
289 static void cleanup(void) | |
290 { | |
291 #if 0 | |
292 vfs_unregister_transport(&default_const); | |
293 vfs_unregister_transport(&file_const); | |
294 #endif | |
295 } | |
296 | |
1146
183d03932c9e
[svn] - curl, stdio: convert to plugin2 architecture
nenolod
parents:
965
diff
changeset
|
297 DECLARE_PLUGIN(stdio, init, cleanup, NULL, NULL, NULL, NULL, NULL); |