2068
|
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
|
|
17 * 02110-1301, USA.
|
|
18 */
|
|
19
|
|
20 #include <glib.h>
|
2075
|
21 #include <string.h>
|
2068
|
22 #include "vfs.h"
|
|
23 #include "vfs_buffer.h"
|
|
24
|
|
25 VFSFile *
|
|
26 buffer_vfs_fopen_impl(const gchar * path,
|
|
27 const gchar * mode)
|
|
28 {
|
|
29 return NULL;
|
|
30 }
|
|
31
|
|
32 gint
|
|
33 buffer_vfs_fclose_impl(VFSFile * file)
|
|
34 {
|
|
35 g_return_val_if_fail(file != NULL, -1);
|
|
36
|
|
37 if (file->handle)
|
|
38 g_free(file->handle);
|
|
39
|
|
40 return 0;
|
|
41 }
|
|
42
|
|
43 size_t
|
|
44 buffer_vfs_fread_impl(gpointer i_ptr,
|
|
45 size_t size,
|
|
46 size_t nmemb,
|
|
47 VFSFile * file)
|
|
48 {
|
|
49 VFSBuffer *handle;
|
|
50 guchar *i;
|
|
51 size_t read = 0;
|
|
52 guchar *ptr = (guchar *) i_ptr;
|
|
53
|
|
54 if (file == NULL)
|
|
55 return 0;
|
|
56
|
|
57 handle = (VFSBuffer *) file->handle;
|
|
58
|
|
59 for (i = ptr; i - ptr <= nmemb * size && i - ptr <= handle->size; i++, handle->iter++)
|
|
60 {
|
|
61 *i = *handle->iter;
|
|
62 read++;
|
|
63 }
|
|
64
|
|
65 return (read / nmemb);
|
|
66 }
|
|
67
|
|
68 size_t
|
|
69 buffer_vfs_fwrite_impl(gconstpointer i_ptr,
|
|
70 size_t size,
|
|
71 size_t nmemb,
|
|
72 VFSFile * file)
|
|
73 {
|
|
74 VFSBuffer *handle;
|
|
75 const guchar *i;
|
|
76 size_t written = 0;
|
|
77 const guchar *ptr = (const guchar *) i_ptr;
|
|
78
|
|
79 if (file == NULL)
|
|
80 return 0;
|
|
81
|
|
82 handle = (VFSBuffer *) file->handle;
|
|
83
|
|
84 for (i = ptr; (i - ptr) <= nmemb * size && (i - ptr) <= handle->size; i++, handle->iter++)
|
|
85 {
|
|
86 *handle->iter = *i;
|
|
87 written++;
|
|
88 }
|
|
89
|
|
90 return (written / nmemb);
|
|
91 }
|
|
92
|
|
93 gint
|
|
94 buffer_vfs_getc_impl(VFSFile *stream)
|
|
95 {
|
|
96 VFSBuffer *handle = (VFSBuffer *) stream->handle;
|
|
97 gint c;
|
|
98
|
|
99 c = *handle->iter;
|
|
100 handle->iter++;
|
|
101
|
|
102 return c;
|
|
103 }
|
|
104
|
|
105 gint
|
|
106 buffer_vfs_ungetc_impl(gint c, VFSFile *stream)
|
|
107 {
|
|
108 return -1;
|
|
109 }
|
|
110
|
|
111 gint
|
|
112 buffer_vfs_fseek_impl(VFSFile * file,
|
|
113 glong offset,
|
|
114 gint whence)
|
|
115 {
|
|
116 VFSBuffer *handle;
|
|
117
|
|
118 if (file == NULL)
|
|
119 return 0;
|
|
120
|
|
121 handle = (VFSBuffer *) file->handle;
|
|
122
|
|
123 switch(whence)
|
|
124 {
|
|
125 case SEEK_CUR:
|
|
126 handle->iter = handle->iter + offset;
|
|
127 break;
|
|
128 case SEEK_END:
|
|
129 handle->iter = handle->end;
|
|
130 break;
|
|
131 case SEEK_SET:
|
|
132 default:
|
|
133 handle->iter = handle->data + offset;
|
|
134 break;
|
|
135 }
|
|
136
|
|
137 return 0;
|
|
138 }
|
|
139
|
|
140 void
|
|
141 buffer_vfs_rewind_impl(VFSFile * file)
|
|
142 {
|
|
143 VFSBuffer *handle;
|
|
144
|
|
145 if (file == NULL)
|
|
146 return;
|
|
147
|
|
148 handle = (VFSBuffer *) file->handle;
|
|
149
|
|
150 handle->iter = handle->data;
|
|
151 }
|
|
152
|
|
153 glong
|
|
154 buffer_vfs_ftell_impl(VFSFile * file)
|
|
155 {
|
|
156 VFSBuffer *handle;
|
|
157
|
|
158 if (file == NULL)
|
|
159 return 0;
|
|
160
|
|
161 handle = (VFSBuffer *) file->handle;
|
|
162
|
|
163 return handle->iter - handle->data;
|
|
164 }
|
|
165
|
|
166 gboolean
|
|
167 buffer_vfs_feof_impl(VFSFile * file)
|
|
168 {
|
|
169 VFSBuffer *handle;
|
|
170
|
|
171 if (file == NULL)
|
|
172 return FALSE;
|
|
173
|
|
174 handle = (VFSBuffer *) file->handle;
|
|
175
|
|
176 return (gboolean) (handle->iter == handle->end);
|
|
177 }
|
|
178
|
|
179 gint
|
|
180 buffer_vfs_truncate_impl(VFSFile * file, glong size)
|
|
181 {
|
|
182 return 0;
|
|
183 }
|
|
184
|
|
185 VFSConstructor buffer_const = {
|
|
186 NULL, // not a normal VFS class
|
|
187 buffer_vfs_fopen_impl,
|
|
188 buffer_vfs_fclose_impl,
|
|
189 buffer_vfs_fread_impl,
|
|
190 buffer_vfs_fwrite_impl,
|
|
191 buffer_vfs_getc_impl,
|
|
192 buffer_vfs_ungetc_impl,
|
|
193 buffer_vfs_fseek_impl,
|
|
194 buffer_vfs_rewind_impl,
|
|
195 buffer_vfs_ftell_impl,
|
|
196 buffer_vfs_feof_impl,
|
|
197 buffer_vfs_truncate_impl
|
|
198 };
|
|
199
|
|
200 VFSFile *
|
|
201 vfs_buffer_new(gpointer data, gsize size)
|
|
202 {
|
|
203 VFSFile *handle;
|
|
204 VFSBuffer *buffer;
|
|
205
|
|
206 g_return_val_if_fail(data != NULL, NULL);
|
|
207 g_return_val_if_fail(size > 0, NULL);
|
|
208
|
|
209 handle = g_new0(VFSFile, 1);
|
|
210 handle->uri == NULL;
|
|
211
|
|
212 buffer = g_new0(VFSBuffer, 1);
|
|
213 buffer->data = data;
|
|
214 buffer->iter = data;
|
|
215 buffer->end = data + size;
|
|
216 buffer->size = size;
|
|
217
|
|
218 handle->handle = buffer;
|
2069
|
219 handle->base = &buffer_const;
|
2068
|
220
|
|
221 return handle;
|
|
222 }
|
|
223
|
|
224 VFSFile *
|
|
225 vfs_buffer_new_from_string(gchar *str)
|
|
226 {
|
|
227 g_return_val_if_fail(str != NULL, NULL);
|
|
228
|
|
229 return vfs_buffer_new(str, strlen(str));
|
|
230 }
|