comparison src/audacious/vfs_buffer.c @ 2313:3149d4b1a9a9 trunk

[svn] - objective-make autodepend fixes - move all sourcecode into src/ and adjust Makefiles accordingly
author nenolod
date Fri, 12 Jan 2007 11:43:40 -0800
parents
children 593fd166af00
comparison
equal deleted inserted replaced
2312:e1a5a66fb9cc 2313:3149d4b1a9a9
1 /* Audacious
2 * Copyright (c) 2006-2007 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; under version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA.
17 */
18
19 #include <glib.h>
20 #include <string.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 return 0;
40 }
41
42 size_t
43 buffer_vfs_fread_impl(gpointer i_ptr,
44 size_t size,
45 size_t nmemb,
46 VFSFile * file)
47 {
48 VFSBuffer *handle;
49 guchar *i;
50 size_t read = 0;
51 guchar *ptr = (guchar *) i_ptr;
52
53 if (file == NULL)
54 return 0;
55
56 handle = (VFSBuffer *) file->handle;
57
58 for (i = ptr; i - ptr <= nmemb * size && i - ptr <= handle->size; i++, handle->iter++)
59 {
60 *i = *handle->iter;
61 read++;
62 }
63
64 return (read / nmemb);
65 }
66
67 size_t
68 buffer_vfs_fwrite_impl(gconstpointer i_ptr,
69 size_t size,
70 size_t nmemb,
71 VFSFile * file)
72 {
73 VFSBuffer *handle;
74 const guchar *i;
75 size_t written = 0;
76 const guchar *ptr = (const guchar *) i_ptr;
77
78 if (file == NULL)
79 return 0;
80
81 handle = (VFSBuffer *) file->handle;
82
83 for (i = ptr; (i - ptr) <= nmemb * size && (i - ptr) <= handle->size; i++, handle->iter++)
84 {
85 *handle->iter = *i;
86 written++;
87 }
88
89 return (written / nmemb);
90 }
91
92 gint
93 buffer_vfs_getc_impl(VFSFile *stream)
94 {
95 VFSBuffer *handle = (VFSBuffer *) stream->handle;
96 gint c;
97
98 c = *handle->iter;
99 handle->iter++;
100
101 return c;
102 }
103
104 gint
105 buffer_vfs_ungetc_impl(gint c, VFSFile *stream)
106 {
107 return -1;
108 }
109
110 gint
111 buffer_vfs_fseek_impl(VFSFile * file,
112 glong offset,
113 gint whence)
114 {
115 VFSBuffer *handle;
116
117 if (file == NULL)
118 return 0;
119
120 handle = (VFSBuffer *) file->handle;
121
122 switch(whence)
123 {
124 case SEEK_CUR:
125 handle->iter = handle->iter + offset;
126 break;
127 case SEEK_END:
128 handle->iter = handle->end;
129 break;
130 case SEEK_SET:
131 default:
132 handle->iter = handle->data + offset;
133 break;
134 }
135
136 return 0;
137 }
138
139 void
140 buffer_vfs_rewind_impl(VFSFile * file)
141 {
142 VFSBuffer *handle;
143
144 if (file == NULL)
145 return;
146
147 handle = (VFSBuffer *) file->handle;
148
149 handle->iter = handle->data;
150 }
151
152 glong
153 buffer_vfs_ftell_impl(VFSFile * file)
154 {
155 VFSBuffer *handle;
156
157 if (file == NULL)
158 return 0;
159
160 handle = (VFSBuffer *) file->handle;
161
162 return handle->iter - handle->data;
163 }
164
165 gboolean
166 buffer_vfs_feof_impl(VFSFile * file)
167 {
168 VFSBuffer *handle;
169
170 if (file == NULL)
171 return FALSE;
172
173 handle = (VFSBuffer *) file->handle;
174
175 return (gboolean) (handle->iter == handle->end);
176 }
177
178 gint
179 buffer_vfs_truncate_impl(VFSFile * file, glong size)
180 {
181 return 0;
182 }
183
184 VFSConstructor buffer_const = {
185 NULL, // not a normal VFS class
186 buffer_vfs_fopen_impl,
187 buffer_vfs_fclose_impl,
188 buffer_vfs_fread_impl,
189 buffer_vfs_fwrite_impl,
190 buffer_vfs_getc_impl,
191 buffer_vfs_ungetc_impl,
192 buffer_vfs_fseek_impl,
193 buffer_vfs_rewind_impl,
194 buffer_vfs_ftell_impl,
195 buffer_vfs_feof_impl,
196 buffer_vfs_truncate_impl
197 };
198
199 VFSFile *
200 vfs_buffer_new(gpointer data, gsize size)
201 {
202 VFSFile *handle;
203 VFSBuffer *buffer;
204
205 g_return_val_if_fail(data != NULL, NULL);
206 g_return_val_if_fail(size > 0, NULL);
207
208 handle = g_new0(VFSFile, 1);
209 handle->uri == NULL;
210
211 buffer = g_new0(VFSBuffer, 1);
212 buffer->data = data;
213 buffer->iter = data;
214 buffer->end = data + size;
215 buffer->size = size;
216
217 handle->handle = buffer;
218 handle->base = &buffer_const;
219
220 return handle;
221 }
222
223 VFSFile *
224 vfs_buffer_new_from_string(gchar *str)
225 {
226 g_return_val_if_fail(str != NULL, NULL);
227
228 return vfs_buffer_new(str, strlen(str));
229 }