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>
|
|
21 #include <stdio.h>
|
|
22
|
|
23 #include <unistd.h>
|
|
24 #include <sys/stat.h>
|
|
25 #include <sys/types.h>
|
|
26
|
|
27 VFSFile *
|
|
28 stdio_vfs_fopen_impl(const gchar * path,
|
|
29 const gchar * mode)
|
|
30 {
|
|
31 VFSFile *file;
|
|
32
|
|
33 if (!path || !mode)
|
|
34 return NULL;
|
|
35
|
|
36 file = g_new(VFSFile, 1);
|
|
37
|
|
38 file->handle = fopen(path, mode);
|
|
39
|
|
40 if (file->handle == NULL) {
|
|
41 g_free(file);
|
|
42 file = NULL;
|
|
43 }
|
|
44
|
|
45 return file;
|
|
46 }
|
|
47
|
|
48 gint
|
|
49 stdio_vfs_fclose_impl(VFSFile * file)
|
|
50 {
|
|
51 gint ret = 0;
|
|
52
|
|
53 if (file == NULL)
|
|
54 return -1;
|
|
55
|
|
56 if (file->handle) {
|
|
57 if (fclose(file->handle) != 0)
|
|
58 ret = -1;
|
|
59 }
|
|
60
|
|
61 return ret;
|
|
62 }
|
|
63
|
|
64 size_t
|
|
65 stdio_vfs_fread_impl(gpointer ptr,
|
|
66 size_t size,
|
|
67 size_t nmemb,
|
|
68 VFSFile * file)
|
|
69 {
|
|
70 if (file == NULL)
|
|
71 return 0;
|
|
72
|
|
73 return fread(ptr, size, nmemb, file->handle);
|
|
74 }
|
|
75
|
|
76 size_t
|
|
77 stdio_vfs_fwrite_impl(gconstpointer ptr,
|
|
78 size_t size,
|
|
79 size_t nmemb,
|
|
80 VFSFile * file)
|
|
81 {
|
|
82 if (file == NULL)
|
|
83 return 0;
|
|
84
|
|
85 return fwrite(ptr, size, nmemb, file->handle);
|
|
86 }
|
|
87
|
|
88 gint
|
|
89 stdio_vfs_getc_impl(VFSFile *stream)
|
|
90 {
|
|
91 return getc( stream->handle );
|
|
92 }
|
|
93
|
|
94 gint
|
|
95 stdio_vfs_ungetc_impl(gint c, VFSFile *stream)
|
|
96 {
|
|
97 return ungetc( c , stream->handle );
|
|
98 }
|
|
99
|
|
100 gint
|
|
101 stdio_vfs_fseek_impl(VFSFile * file,
|
|
102 glong offset,
|
|
103 gint whence)
|
|
104 {
|
|
105 if (file == NULL)
|
|
106 return 0;
|
|
107
|
|
108 return fseek(file->handle, offset, whence);
|
|
109 }
|
|
110
|
|
111 void
|
|
112 stdio_vfs_rewind_impl(VFSFile * file)
|
|
113 {
|
|
114 if (file == NULL)
|
|
115 return;
|
|
116
|
|
117 rewind(file->handle);
|
|
118 }
|
|
119
|
|
120 glong
|
|
121 stdio_vfs_ftell_impl(VFSFile * file)
|
|
122 {
|
|
123 if (file == NULL)
|
|
124 return 0;
|
|
125
|
|
126 return ftell(file->handle);
|
|
127 }
|
|
128
|
|
129 gboolean
|
|
130 stdio_vfs_feof_impl(VFSFile * file)
|
|
131 {
|
|
132 if (file == NULL)
|
|
133 return FALSE;
|
|
134
|
|
135 return (gboolean) feof(file->handle);
|
|
136 }
|
|
137
|
|
138 gint
|
|
139 stdio_vfs_truncate_impl(VFSFile * file, glong size)
|
|
140 {
|
|
141 if (file == NULL)
|
|
142 return -1;
|
|
143
|
|
144 return ftruncate(fileno(file->handle), size);
|
|
145 }
|
|
146
|
|
147 VFSConstructor file_const = {
|
|
148 "file://",
|
|
149 stdio_vfs_fopen_impl,
|
|
150 stdio_vfs_fclose_impl,
|
|
151 stdio_vfs_fread_impl,
|
|
152 stdio_vfs_fwrite_impl,
|
|
153 stdio_vfs_getc_impl,
|
|
154 stdio_vfs_ungetc_impl,
|
|
155 stdio_vfs_fseek_impl,
|
|
156 stdio_vfs_rewind_impl,
|
|
157 stdio_vfs_ftell_impl,
|
|
158 stdio_vfs_feof_impl,
|
|
159 stdio_vfs_truncate_impl
|
|
160 };
|
|
161
|
|
162 VFSConstructor default_const = {
|
|
163 "/",
|
|
164 stdio_vfs_fopen_impl,
|
|
165 stdio_vfs_fclose_impl,
|
|
166 stdio_vfs_fread_impl,
|
|
167 stdio_vfs_fwrite_impl,
|
|
168 stdio_vfs_getc_impl,
|
|
169 stdio_vfs_ungetc_impl,
|
|
170 stdio_vfs_fseek_impl,
|
|
171 stdio_vfs_rewind_impl,
|
|
172 stdio_vfs_ftell_impl,
|
|
173 stdio_vfs_feof_impl,
|
|
174 stdio_vfs_truncate_impl
|
|
175 };
|
|
176
|
|
177 static void init(void)
|
|
178 {
|
|
179 vfs_register_transport(&default_const);
|
|
180 vfs_register_transport(&file_const);
|
|
181 }
|
|
182
|
|
183 static void cleanup(void)
|
|
184 {
|
|
185 #if 0
|
|
186 vfs_unregister_transport(&default_const);
|
|
187 vfs_unregister_transport(&file_const);
|
|
188 #endif
|
|
189 }
|
|
190
|
|
191 LowlevelPlugin llp_stdio = {
|
|
192 NULL,
|
|
193 NULL,
|
|
194 "file:// URI Transport",
|
|
195 init,
|
|
196 cleanup,
|
|
197 };
|
|
198
|
|
199 LowlevelPlugin *get_lplugin_info(void)
|
|
200 {
|
|
201 return &llp_stdio;
|
|
202 }
|
|
203
|