Mercurial > audlegacy-plugins
annotate src/stdio/stdio.c @ 2050:2ffc6a69fcd1
string API calls -> vtable
author | William Pitcock <nenolod@atheme.org> |
---|---|
date | Sat, 13 Oct 2007 19:55:04 -0500 |
parents | 6e2070ea35e7 |
children | bd3a24b39058 |
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 | |
1957 | 19 #include "config.h" |
306 | 20 #include <audacious/plugin.h> |
1950
2ebeb7816c5e
Change from "" to <>-style includes. With thanks to ccr for the correct sed voodoo.
chainsaw@localhost
parents:
1935
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 * |
1978 | 31 aud_vfs_stdio_urldecode_path(const gchar * encoded_path) |
500
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 |
2050
2ffc6a69fcd1
string API calls -> vtable
William Pitcock <nenolod@atheme.org>
parents:
1980
diff
changeset
|
40 if (!aud_str_has_prefix_nocase(encoded_path, "file:")) |
500
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 |
2050
2ffc6a69fcd1
string API calls -> vtable
William Pitcock <nenolod@atheme.org>
parents:
1980
diff
changeset
|
45 if (aud_str_has_prefix_nocase(cur, "//localhost")) |
500
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 * |
1978 | 74 stdio_aud_vfs_fopen_impl(const gchar * path, |
306 | 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 | |
1978 | 83 decpath = aud_vfs_stdio_urldecode_path(path); |
500
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 | |
1978 | 101 stdio_aud_vfs_fclose_impl(VFSFile * file) |
306 | 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; |
1228
01efa0e5b554
file->handle should be cleared on close, otherwise it can lead to double free.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
1210
diff
changeset
|
114 file->handle = NULL; |
306 | 115 } |
116 | |
117 return ret; | |
118 } | |
119 | |
120 size_t | |
1978 | 121 stdio_aud_vfs_fread_impl(gpointer ptr, |
306 | 122 size_t size, |
123 size_t nmemb, | |
124 VFSFile * file) | |
125 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
126 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
127 |
306 | 128 if (file == NULL) |
129 return 0; | |
130 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
131 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
132 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
133 return fread(ptr, size, nmemb, handle); |
306 | 134 } |
135 | |
136 size_t | |
1978 | 137 stdio_aud_vfs_fwrite_impl(gconstpointer ptr, |
306 | 138 size_t size, |
139 size_t nmemb, | |
140 VFSFile * file) | |
141 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
142 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
143 |
306 | 144 if (file == NULL) |
145 return 0; | |
146 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
147 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
148 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
149 return fwrite(ptr, size, nmemb, handle); |
306 | 150 } |
151 | |
152 gint | |
1978 | 153 stdio_aud_vfs_getc_impl(VFSFile *stream) |
306 | 154 { |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
155 FILE *handle = (FILE *) stream->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
156 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
157 return getc( handle ); |
306 | 158 } |
159 | |
160 gint | |
1978 | 161 stdio_aud_vfs_ungetc_impl(gint c, VFSFile *stream) |
306 | 162 { |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
163 FILE *handle = (FILE *) stream->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
164 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
165 return ungetc( c , handle ); |
306 | 166 } |
167 | |
168 gint | |
1978 | 169 stdio_aud_vfs_fseek_impl(VFSFile * file, |
306 | 170 glong offset, |
171 gint whence) | |
172 { | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
173 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
174 |
306 | 175 if (file == NULL) |
176 return 0; | |
177 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
178 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
179 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
180 return fseek(handle, offset, whence); |
306 | 181 } |
182 | |
183 void | |
1978 | 184 stdio_aud_vfs_rewind_impl(VFSFile * file) |
306 | 185 { |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
186 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
187 |
306 | 188 if (file == NULL) |
189 return; | |
190 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
191 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
192 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
193 rewind(handle); |
306 | 194 } |
195 | |
196 glong | |
1978 | 197 stdio_aud_vfs_ftell_impl(VFSFile * file) |
306 | 198 { |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
199 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
200 |
306 | 201 if (file == NULL) |
202 return 0; | |
203 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
204 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
205 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
206 return ftell(handle); |
306 | 207 } |
208 | |
209 gboolean | |
1978 | 210 stdio_aud_vfs_feof_impl(VFSFile * file) |
306 | 211 { |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
212 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
213 |
306 | 214 if (file == NULL) |
215 return FALSE; | |
216 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
217 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
218 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
219 return (gboolean) feof(handle); |
306 | 220 } |
221 | |
222 gint | |
1978 | 223 stdio_aud_vfs_truncate_impl(VFSFile * file, glong size) |
306 | 224 { |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
225 FILE *handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
226 |
306 | 227 if (file == NULL) |
228 return -1; | |
229 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
230 handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
231 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
232 return ftruncate(fileno(handle), size); |
306 | 233 } |
234 | |
965 | 235 off_t |
1978 | 236 stdio_aud_vfs_fsize_impl(VFSFile * file) |
965 | 237 { |
238 FILE *handle; | |
239 struct stat s; | |
240 | |
241 if (file == NULL) | |
242 return -1; | |
243 | |
244 handle = (FILE *) file->handle; | |
245 | |
246 if (-1 == fstat(fileno(handle), &s)) | |
247 return -1; | |
248 | |
249 return s.st_size; | |
250 } | |
251 | |
306 | 252 VFSConstructor file_const = { |
1957 | 253 .uri_id = "file://", |
1980 | 254 .vfs_fopen_impl = stdio_aud_vfs_fopen_impl, |
255 .vfs_fclose_impl = stdio_aud_vfs_fclose_impl, | |
256 .vfs_fread_impl = stdio_aud_vfs_fread_impl, | |
257 .vfs_fwrite_impl = stdio_aud_vfs_fwrite_impl, | |
258 .vfs_getc_impl = stdio_aud_vfs_getc_impl, | |
259 .vfs_ungetc_impl = stdio_aud_vfs_ungetc_impl, | |
260 .vfs_fseek_impl = stdio_aud_vfs_fseek_impl, | |
261 .vfs_rewind_impl = stdio_aud_vfs_rewind_impl, | |
262 .vfs_ftell_impl = stdio_aud_vfs_ftell_impl, | |
263 .vfs_feof_impl = stdio_aud_vfs_feof_impl, | |
264 .vfs_truncate_impl = stdio_aud_vfs_truncate_impl, | |
265 .vfs_fsize_impl = stdio_aud_vfs_fsize_impl | |
306 | 266 }; |
267 | |
268 static void init(void) | |
269 { | |
1978 | 270 aud_vfs_register_transport(&file_const); |
306 | 271 } |
272 | |
273 static void cleanup(void) | |
274 { | |
275 #if 0 | |
1978 | 276 aud_vfs_unregister_transport(&file_const); |
306 | 277 #endif |
278 } | |
279 | |
1395
761e17b23e0c
added Discovery plugin type
Cristi Magherusan <majeru@atheme-project.org>
parents:
1228
diff
changeset
|
280 DECLARE_PLUGIN(stdio, init, cleanup, NULL, NULL, NULL, NULL, NULL, NULL); |