Mercurial > audlegacy
annotate src/audacious/vfs_buffer.c @ 2760:c2613269f913 trunk
[svn] - auddrct: more calls implemented (part 6)
author | giacomo |
---|---|
date | Sat, 12 May 2007 05:20:40 -0700 |
parents | ac22b2cb6013 |
children | 7d3beedf1db8 |
rev | line source |
---|---|
2313 | 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 | |
2518
4c9910af4cc5
[svn] - fix signed vs unsigned comparisons and missing initializer warnings
nenolod
parents:
2344
diff
changeset
|
58 for (i = ptr; (gsize) (i - ptr) < nmemb * size && |
4c9910af4cc5
[svn] - fix signed vs unsigned comparisons and missing initializer warnings
nenolod
parents:
2344
diff
changeset
|
59 (gsize) (i - ptr) <= handle->size; |
4c9910af4cc5
[svn] - fix signed vs unsigned comparisons and missing initializer warnings
nenolod
parents:
2344
diff
changeset
|
60 i++, handle->iter++) |
2313 | 61 { |
62 *i = *handle->iter; | |
63 read++; | |
64 } | |
65 | |
2342
f140d0a27093
[svn] - use vfs_rewind() instead of vfs_fseek(fd, 0, seek_set) which was wrong
nenolod
parents:
2332
diff
changeset
|
66 return (read / size); |
2313 | 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 | |
2518
4c9910af4cc5
[svn] - fix signed vs unsigned comparisons and missing initializer warnings
nenolod
parents:
2344
diff
changeset
|
85 for (i = ptr; (gsize) (i - ptr) < nmemb * size && |
4c9910af4cc5
[svn] - fix signed vs unsigned comparisons and missing initializer warnings
nenolod
parents:
2344
diff
changeset
|
86 (gsize) (i - ptr) <= handle->size; |
4c9910af4cc5
[svn] - fix signed vs unsigned comparisons and missing initializer warnings
nenolod
parents:
2344
diff
changeset
|
87 i++, handle->iter++) |
2313 | 88 { |
89 *handle->iter = *i; | |
90 written++; | |
91 } | |
92 | |
2342
f140d0a27093
[svn] - use vfs_rewind() instead of vfs_fseek(fd, 0, seek_set) which was wrong
nenolod
parents:
2332
diff
changeset
|
93 return (written / size); |
2313 | 94 } |
95 | |
96 gint | |
97 buffer_vfs_getc_impl(VFSFile *stream) | |
98 { | |
99 VFSBuffer *handle = (VFSBuffer *) stream->handle; | |
100 gint c; | |
101 | |
102 c = *handle->iter; | |
103 handle->iter++; | |
104 | |
105 return c; | |
106 } | |
107 | |
108 gint | |
109 buffer_vfs_ungetc_impl(gint c, VFSFile *stream) | |
110 { | |
111 return -1; | |
112 } | |
113 | |
114 gint | |
115 buffer_vfs_fseek_impl(VFSFile * file, | |
116 glong offset, | |
117 gint whence) | |
118 { | |
119 VFSBuffer *handle; | |
120 | |
121 if (file == NULL) | |
122 return 0; | |
123 | |
124 handle = (VFSBuffer *) file->handle; | |
125 | |
126 switch(whence) | |
127 { | |
128 case SEEK_CUR: | |
129 handle->iter = handle->iter + offset; | |
130 break; | |
131 case SEEK_END: | |
132 handle->iter = handle->end; | |
133 break; | |
134 case SEEK_SET: | |
135 default: | |
136 handle->iter = handle->data + offset; | |
137 break; | |
138 } | |
139 | |
140 return 0; | |
141 } | |
142 | |
143 void | |
144 buffer_vfs_rewind_impl(VFSFile * file) | |
145 { | |
146 VFSBuffer *handle; | |
147 | |
148 if (file == NULL) | |
149 return; | |
150 | |
151 handle = (VFSBuffer *) file->handle; | |
152 | |
153 handle->iter = handle->data; | |
154 } | |
155 | |
156 glong | |
157 buffer_vfs_ftell_impl(VFSFile * file) | |
158 { | |
159 VFSBuffer *handle; | |
160 | |
161 if (file == NULL) | |
162 return 0; | |
163 | |
164 handle = (VFSBuffer *) file->handle; | |
165 | |
166 return handle->iter - handle->data; | |
167 } | |
168 | |
169 gboolean | |
170 buffer_vfs_feof_impl(VFSFile * file) | |
171 { | |
172 VFSBuffer *handle; | |
173 | |
174 if (file == NULL) | |
175 return FALSE; | |
176 | |
177 handle = (VFSBuffer *) file->handle; | |
178 | |
179 return (gboolean) (handle->iter == handle->end); | |
180 } | |
181 | |
182 gint | |
183 buffer_vfs_truncate_impl(VFSFile * file, glong size) | |
184 { | |
185 return 0; | |
186 } | |
187 | |
2688 | 188 off_t |
189 buffer_vfs_fsize_impl(VFSFile * file) | |
190 { | |
191 VFSBuffer *handle; | |
192 | |
193 if (file == NULL) | |
194 return -1; | |
195 | |
196 handle = (VFSBuffer *) file->handle; | |
197 | |
198 return (off_t)handle->end; | |
199 } | |
200 | |
2313 | 201 VFSConstructor buffer_const = { |
202 NULL, // not a normal VFS class | |
203 buffer_vfs_fopen_impl, | |
204 buffer_vfs_fclose_impl, | |
205 buffer_vfs_fread_impl, | |
206 buffer_vfs_fwrite_impl, | |
207 buffer_vfs_getc_impl, | |
208 buffer_vfs_ungetc_impl, | |
209 buffer_vfs_fseek_impl, | |
210 buffer_vfs_rewind_impl, | |
211 buffer_vfs_ftell_impl, | |
212 buffer_vfs_feof_impl, | |
2518
4c9910af4cc5
[svn] - fix signed vs unsigned comparisons and missing initializer warnings
nenolod
parents:
2344
diff
changeset
|
213 buffer_vfs_truncate_impl, |
2688 | 214 buffer_vfs_fsize_impl, |
2518
4c9910af4cc5
[svn] - fix signed vs unsigned comparisons and missing initializer warnings
nenolod
parents:
2344
diff
changeset
|
215 NULL |
2313 | 216 }; |
217 | |
218 VFSFile * | |
219 vfs_buffer_new(gpointer data, gsize size) | |
220 { | |
221 VFSFile *handle; | |
222 VFSBuffer *buffer; | |
223 | |
224 g_return_val_if_fail(data != NULL, NULL); | |
225 g_return_val_if_fail(size > 0, NULL); | |
226 | |
227 handle = g_new0(VFSFile, 1); | |
228 | |
229 buffer = g_new0(VFSBuffer, 1); | |
230 buffer->data = data; | |
231 buffer->iter = data; | |
2519
793ede082399
[svn] - fix: vfs_buffer.c:218: warning: pointer of type 'void *' used in arithmetic
nenolod
parents:
2518
diff
changeset
|
232 buffer->end = (guchar *) data + size; |
2313 | 233 buffer->size = size; |
234 | |
235 handle->handle = buffer; | |
236 handle->base = &buffer_const; | |
2562
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2519
diff
changeset
|
237 handle->ref = 1; |
2313 | 238 |
239 return handle; | |
240 } | |
241 | |
242 VFSFile * | |
243 vfs_buffer_new_from_string(gchar *str) | |
244 { | |
245 g_return_val_if_fail(str != NULL, NULL); | |
246 | |
247 return vfs_buffer_new(str, strlen(str)); | |
248 } |