Mercurial > audlegacy-plugins
annotate src/stdio/stdio.c @ 2872:469f7e596a20
hide widgets before performing skin cleanup
author | Tomasz Mon <desowin@gmail.com> |
---|---|
date | Thu, 07 Aug 2008 15:14:09 +0200 |
parents | bd3a24b39058 |
children | 30d30cfe6b47 |
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> |
21 #include <stdio.h> | |
22 | |
23 #include <unistd.h> | |
24 #include <sys/stat.h> | |
25 #include <sys/types.h> | |
26 | |
500
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
27 #include <string.h> |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
28 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
29 static gchar * |
1978 | 30 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
|
31 { |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
32 const gchar *cur, *ext; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
33 gchar *path, *tmp; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
34 gint realchar; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
35 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
36 if (!encoded_path) |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
37 return NULL; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
38 |
2050
2ffc6a69fcd1
string API calls -> vtable
William Pitcock <nenolod@atheme.org>
parents:
1980
diff
changeset
|
39 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
|
40 return NULL; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
41 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
42 cur = encoded_path + 5; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
43 |
2050
2ffc6a69fcd1
string API calls -> vtable
William Pitcock <nenolod@atheme.org>
parents:
1980
diff
changeset
|
44 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
|
45 cur += 11; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
46 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
47 if (*cur == '/') |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
48 while (cur[1] == '/') |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
49 cur++; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
50 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
51 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
|
52 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
53 while ((ext = strchr(cur, '%')) != NULL) { |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
54 strncat(tmp, cur, ext - cur); |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
55 ext++; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
56 cur = ext + 2; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
57 if (!sscanf(ext, "%2x", &realchar)) { |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
58 /* 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
|
59 * 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
|
60 * and drop. */ |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
61 realchar = '%'; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
62 cur -= 2; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
63 } |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
64 tmp[strlen(tmp)] = realchar; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
65 } |
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 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
|
68 g_free(tmp); |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
69 return path; |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
70 } |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
71 |
306 | 72 VFSFile * |
1978 | 73 stdio_aud_vfs_fopen_impl(const gchar * path, |
306 | 74 const gchar * mode) |
75 { | |
76 VFSFile *file; | |
500
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
77 gchar *decpath; |
306 | 78 |
79 if (!path || !mode) | |
80 return NULL; | |
81 | |
1978 | 82 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
|
83 |
306 | 84 file = g_new(VFSFile, 1); |
85 | |
500
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
86 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
|
87 |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
88 if (decpath != NULL) |
1d81ea250dce
[svn] - URIs are now passed naked instead of mangled by the VFS subsystem.
nenolod
parents:
326
diff
changeset
|
89 g_free(decpath); |
306 | 90 |
91 if (file->handle == NULL) { | |
92 g_free(file); | |
93 file = NULL; | |
94 } | |
95 | |
96 return file; | |
97 } | |
98 | |
99 gint | |
1978 | 100 stdio_aud_vfs_fclose_impl(VFSFile * file) |
306 | 101 { |
102 gint ret = 0; | |
103 | |
104 if (file == NULL) | |
105 return -1; | |
106 | |
326
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
107 if (file->handle) |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
108 { |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
109 FILE *handle = (FILE *) file->handle; |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
110 |
90a843d02970
[svn] - some OSes use macros for some f*() functions, therefore make sure we
nenolod
parents:
306
diff
changeset
|
111 if (fclose(handle) != 0) |
306 | 112 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
|
113 file->handle = NULL; |
306 | 114 } |
115 | |
116 return ret; | |
117 } | |
118 | |
119 size_t | |
1978 | 120 stdio_aud_vfs_fread_impl(gpointer ptr, |
306 | 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 | |
1978 | 136 stdio_aud_vfs_fwrite_impl(gconstpointer ptr, |
306 | 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 | |
1978 | 152 stdio_aud_vfs_getc_impl(VFSFile *stream) |
306 | 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 | |
1978 | 160 stdio_aud_vfs_ungetc_impl(gint c, VFSFile *stream) |
306 | 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 | |
1978 | 168 stdio_aud_vfs_fseek_impl(VFSFile * file, |
306 | 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 | |
1978 | 183 stdio_aud_vfs_rewind_impl(VFSFile * file) |
306 | 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 | |
1978 | 196 stdio_aud_vfs_ftell_impl(VFSFile * file) |
306 | 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 | |
1978 | 209 stdio_aud_vfs_feof_impl(VFSFile * file) |
306 | 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 | |
1978 | 222 stdio_aud_vfs_truncate_impl(VFSFile * file, glong size) |
306 | 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 |
1978 | 235 stdio_aud_vfs_fsize_impl(VFSFile * file) |
965 | 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 = { |
1957 | 252 .uri_id = "file://", |
1980 | 253 .vfs_fopen_impl = stdio_aud_vfs_fopen_impl, |
254 .vfs_fclose_impl = stdio_aud_vfs_fclose_impl, | |
255 .vfs_fread_impl = stdio_aud_vfs_fread_impl, | |
256 .vfs_fwrite_impl = stdio_aud_vfs_fwrite_impl, | |
257 .vfs_getc_impl = stdio_aud_vfs_getc_impl, | |
258 .vfs_ungetc_impl = stdio_aud_vfs_ungetc_impl, | |
259 .vfs_fseek_impl = stdio_aud_vfs_fseek_impl, | |
260 .vfs_rewind_impl = stdio_aud_vfs_rewind_impl, | |
261 .vfs_ftell_impl = stdio_aud_vfs_ftell_impl, | |
262 .vfs_feof_impl = stdio_aud_vfs_feof_impl, | |
263 .vfs_truncate_impl = stdio_aud_vfs_truncate_impl, | |
264 .vfs_fsize_impl = stdio_aud_vfs_fsize_impl | |
306 | 265 }; |
266 | |
267 static void init(void) | |
268 { | |
1978 | 269 aud_vfs_register_transport(&file_const); |
306 | 270 } |
271 | |
272 static void cleanup(void) | |
273 { | |
274 #if 0 | |
1978 | 275 aud_vfs_unregister_transport(&file_const); |
306 | 276 #endif |
277 } | |
278 | |
1395
761e17b23e0c
added Discovery plugin type
Cristi Magherusan <majeru@atheme-project.org>
parents:
1228
diff
changeset
|
279 DECLARE_PLUGIN(stdio, init, cleanup, NULL, NULL, NULL, NULL, NULL, NULL); |