comparison libaudacious/vfs_buffer.c @ 2068:31b5c59ed31b trunk

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