annotate src/stdio/stdio.c @ 306:b47f02042530 trunk

[svn] - pluginize the transport layer.
author nenolod
date Thu, 23 Nov 2006 20:20:01 -0800
parents
children 90a843d02970
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
306
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
1 /* Audacious
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
2 * Copyright (c) 2006 William Pitcock
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
3 *
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
6 * the Free Software Foundation; either version 2 of the License, or
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
7 * (at your option) any later version.
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
8 *
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
9 * This program is distributed in the hope that it will be useful,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
12 * GNU General Public License for more details.
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
13 *
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
14 * You should have received a copy of the GNU General Public License
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
15 * along with this program; if not, write to the Free Software
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
17 */
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
18
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
19 #include <audacious/vfs.h>
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
20 #include <audacious/plugin.h>
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
21 #include <stdio.h>
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
22
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
23 #include <unistd.h>
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
24 #include <sys/stat.h>
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
25 #include <sys/types.h>
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
26
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
27 VFSFile *
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
28 stdio_vfs_fopen_impl(const gchar * path,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
29 const gchar * mode)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
30 {
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
31 VFSFile *file;
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
32
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
33 if (!path || !mode)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
34 return NULL;
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
35
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
36 file = g_new(VFSFile, 1);
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
37
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
38 file->handle = fopen(path, mode);
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
39
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
40 if (file->handle == NULL) {
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
41 g_free(file);
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
42 file = NULL;
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
43 }
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
44
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
45 return file;
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
46 }
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
47
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
48 gint
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
49 stdio_vfs_fclose_impl(VFSFile * file)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
50 {
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
51 gint ret = 0;
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
52
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
53 if (file == NULL)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
54 return -1;
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
55
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
56 if (file->handle) {
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
57 if (fclose(file->handle) != 0)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
58 ret = -1;
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
59 }
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
60
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
61 return ret;
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
62 }
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
63
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
64 size_t
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
65 stdio_vfs_fread_impl(gpointer ptr,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
66 size_t size,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
67 size_t nmemb,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
68 VFSFile * file)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
69 {
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
70 if (file == NULL)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
71 return 0;
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
72
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
73 return fread(ptr, size, nmemb, file->handle);
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
74 }
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
75
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
76 size_t
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
77 stdio_vfs_fwrite_impl(gconstpointer ptr,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
78 size_t size,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
79 size_t nmemb,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
80 VFSFile * file)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
81 {
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
82 if (file == NULL)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
83 return 0;
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
84
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
85 return fwrite(ptr, size, nmemb, file->handle);
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
86 }
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
87
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
88 gint
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
89 stdio_vfs_getc_impl(VFSFile *stream)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
90 {
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
91 return getc( stream->handle );
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
92 }
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
93
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
94 gint
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
95 stdio_vfs_ungetc_impl(gint c, VFSFile *stream)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
96 {
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
97 return ungetc( c , stream->handle );
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
98 }
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
99
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
100 gint
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
101 stdio_vfs_fseek_impl(VFSFile * file,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
102 glong offset,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
103 gint whence)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
104 {
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
105 if (file == NULL)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
106 return 0;
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
107
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
108 return fseek(file->handle, offset, whence);
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
109 }
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
110
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
111 void
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
112 stdio_vfs_rewind_impl(VFSFile * file)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
113 {
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
114 if (file == NULL)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
115 return;
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
116
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
117 rewind(file->handle);
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
118 }
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
119
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
120 glong
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
121 stdio_vfs_ftell_impl(VFSFile * file)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
122 {
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
123 if (file == NULL)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
124 return 0;
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
125
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
126 return ftell(file->handle);
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
127 }
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
128
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
129 gboolean
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
130 stdio_vfs_feof_impl(VFSFile * file)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
131 {
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
132 if (file == NULL)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
133 return FALSE;
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
134
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
135 return (gboolean) feof(file->handle);
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
136 }
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
137
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
138 gint
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
139 stdio_vfs_truncate_impl(VFSFile * file, glong size)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
140 {
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
141 if (file == NULL)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
142 return -1;
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
143
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
144 return ftruncate(fileno(file->handle), size);
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
145 }
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
146
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
147 VFSConstructor file_const = {
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
148 "file://",
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
149 stdio_vfs_fopen_impl,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
150 stdio_vfs_fclose_impl,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
151 stdio_vfs_fread_impl,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
152 stdio_vfs_fwrite_impl,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
153 stdio_vfs_getc_impl,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
154 stdio_vfs_ungetc_impl,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
155 stdio_vfs_fseek_impl,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
156 stdio_vfs_rewind_impl,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
157 stdio_vfs_ftell_impl,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
158 stdio_vfs_feof_impl,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
159 stdio_vfs_truncate_impl
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
160 };
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
161
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
162 VFSConstructor default_const = {
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
163 "/",
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
164 stdio_vfs_fopen_impl,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
165 stdio_vfs_fclose_impl,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
166 stdio_vfs_fread_impl,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
167 stdio_vfs_fwrite_impl,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
168 stdio_vfs_getc_impl,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
169 stdio_vfs_ungetc_impl,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
170 stdio_vfs_fseek_impl,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
171 stdio_vfs_rewind_impl,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
172 stdio_vfs_ftell_impl,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
173 stdio_vfs_feof_impl,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
174 stdio_vfs_truncate_impl
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
175 };
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
176
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
177 static void init(void)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
178 {
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
179 vfs_register_transport(&default_const);
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
180 vfs_register_transport(&file_const);
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
181 }
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
182
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
183 static void cleanup(void)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
184 {
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
185 #if 0
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
186 vfs_unregister_transport(&default_const);
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
187 vfs_unregister_transport(&file_const);
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
188 #endif
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
189 }
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
190
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
191 LowlevelPlugin llp_stdio = {
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
192 NULL,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
193 NULL,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
194 "file:// URI Transport",
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
195 init,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
196 cleanup,
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
197 };
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
198
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
199 LowlevelPlugin *get_lplugin_info(void)
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
200 {
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
201 return &llp_stdio;
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
202 }
b47f02042530 [svn] - pluginize the transport layer.
nenolod
parents:
diff changeset
203