Mercurial > audlegacy
annotate src/audacious/vfs_buffer.c @ 3700:81026451b455
fix tuple_get_int declaration
author | William Pitcock <nenolod@atheme.org> |
---|---|
date | Sun, 07 Oct 2007 00:28:00 -0500 |
parents | f1c756f39e6c |
children |
rev | line source |
---|---|
2896
51dda959be4d
Backed out changeset 7d3beedf1db8677dab2327bc8d85af4746344f6e
William Pitcock <nenolod@atheme.org>
parents:
2861
diff
changeset
|
1 /* Audacious |
51dda959be4d
Backed out changeset 7d3beedf1db8677dab2327bc8d85af4746344f6e
William Pitcock <nenolod@atheme.org>
parents:
2861
diff
changeset
|
2 * Copyright (c) 2006-2007 William Pitcock |
2313 | 3 * |
2896
51dda959be4d
Backed out changeset 7d3beedf1db8677dab2327bc8d85af4746344f6e
William Pitcock <nenolod@atheme.org>
parents:
2861
diff
changeset
|
4 * This program is free software; you can redistribute it and/or modify |
51dda959be4d
Backed out changeset 7d3beedf1db8677dab2327bc8d85af4746344f6e
William Pitcock <nenolod@atheme.org>
parents:
2861
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
3121
3b6d316f8b09
GPL3 relicensing.
William Pitcock <nenolod@atheme-project.org>
parents:
2896
diff
changeset
|
6 * the Free Software Foundation; under version 3 of the License. |
2313 | 7 * |
2896
51dda959be4d
Backed out changeset 7d3beedf1db8677dab2327bc8d85af4746344f6e
William Pitcock <nenolod@atheme.org>
parents:
2861
diff
changeset
|
8 * This program is distributed in the hope that it will be useful, |
51dda959be4d
Backed out changeset 7d3beedf1db8677dab2327bc8d85af4746344f6e
William Pitcock <nenolod@atheme.org>
parents:
2861
diff
changeset
|
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
51dda959be4d
Backed out changeset 7d3beedf1db8677dab2327bc8d85af4746344f6e
William Pitcock <nenolod@atheme.org>
parents:
2861
diff
changeset
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
51dda959be4d
Backed out changeset 7d3beedf1db8677dab2327bc8d85af4746344f6e
William Pitcock <nenolod@atheme.org>
parents:
2861
diff
changeset
|
11 * GNU General Public License for more details. |
2313 | 12 * |
2896
51dda959be4d
Backed out changeset 7d3beedf1db8677dab2327bc8d85af4746344f6e
William Pitcock <nenolod@atheme.org>
parents:
2861
diff
changeset
|
13 * You should have received a copy of the GNU General Public License |
3121
3b6d316f8b09
GPL3 relicensing.
William Pitcock <nenolod@atheme-project.org>
parents:
2896
diff
changeset
|
14 * along with this program. If not, see <http://www.gnu.org/licenses>. |
3123
f1c756f39e6c
Invoke "Plugins are not derived work" clause provided by GPL3.
William Pitcock <nenolod@atheme-project.org>
parents:
3121
diff
changeset
|
15 * |
f1c756f39e6c
Invoke "Plugins are not derived work" clause provided by GPL3.
William Pitcock <nenolod@atheme-project.org>
parents:
3121
diff
changeset
|
16 * The Audacious team does not consider modular code linking to |
f1c756f39e6c
Invoke "Plugins are not derived work" clause provided by GPL3.
William Pitcock <nenolod@atheme-project.org>
parents:
3121
diff
changeset
|
17 * Audacious or using our public API to be a derived work. |
2313 | 18 */ |
19 | |
20 #include <glib.h> | |
21 #include <string.h> | |
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 | |
2518
4c9910af4cc5
[svn] - fix signed vs unsigned comparisons and missing initializer warnings
nenolod
parents:
2344
diff
changeset
|
59 for (i = ptr; (gsize) (i - ptr) < nmemb * size && |
4c9910af4cc5
[svn] - fix signed vs unsigned comparisons and missing initializer warnings
nenolod
parents:
2344
diff
changeset
|
60 (gsize) (i - ptr) <= handle->size; |
4c9910af4cc5
[svn] - fix signed vs unsigned comparisons and missing initializer warnings
nenolod
parents:
2344
diff
changeset
|
61 i++, handle->iter++) |
2313 | 62 { |
63 *i = *handle->iter; | |
64 read++; | |
65 } | |
66 | |
2342
f140d0a27093
[svn] - use vfs_rewind() instead of vfs_fseek(fd, 0, seek_set) which was wrong
nenolod
parents:
2332
diff
changeset
|
67 return (read / size); |
2313 | 68 } |
69 | |
70 size_t | |
71 buffer_vfs_fwrite_impl(gconstpointer i_ptr, | |
72 size_t size, | |
73 size_t nmemb, | |
74 VFSFile * file) | |
75 { | |
76 VFSBuffer *handle; | |
77 const guchar *i; | |
78 size_t written = 0; | |
79 const guchar *ptr = (const guchar *) i_ptr; | |
80 | |
81 if (file == NULL) | |
82 return 0; | |
83 | |
84 handle = (VFSBuffer *) file->handle; | |
85 | |
2518
4c9910af4cc5
[svn] - fix signed vs unsigned comparisons and missing initializer warnings
nenolod
parents:
2344
diff
changeset
|
86 for (i = ptr; (gsize) (i - ptr) < nmemb * size && |
4c9910af4cc5
[svn] - fix signed vs unsigned comparisons and missing initializer warnings
nenolod
parents:
2344
diff
changeset
|
87 (gsize) (i - ptr) <= handle->size; |
4c9910af4cc5
[svn] - fix signed vs unsigned comparisons and missing initializer warnings
nenolod
parents:
2344
diff
changeset
|
88 i++, handle->iter++) |
2313 | 89 { |
90 *handle->iter = *i; | |
91 written++; | |
92 } | |
93 | |
2342
f140d0a27093
[svn] - use vfs_rewind() instead of vfs_fseek(fd, 0, seek_set) which was wrong
nenolod
parents:
2332
diff
changeset
|
94 return (written / size); |
2313 | 95 } |
96 | |
97 gint | |
98 buffer_vfs_getc_impl(VFSFile *stream) | |
99 { | |
100 VFSBuffer *handle = (VFSBuffer *) stream->handle; | |
101 gint c; | |
102 | |
103 c = *handle->iter; | |
104 handle->iter++; | |
105 | |
106 return c; | |
107 } | |
108 | |
109 gint | |
110 buffer_vfs_ungetc_impl(gint c, VFSFile *stream) | |
111 { | |
112 return -1; | |
113 } | |
114 | |
115 gint | |
116 buffer_vfs_fseek_impl(VFSFile * file, | |
117 glong offset, | |
118 gint whence) | |
119 { | |
120 VFSBuffer *handle; | |
121 | |
122 if (file == NULL) | |
123 return 0; | |
124 | |
125 handle = (VFSBuffer *) file->handle; | |
126 | |
127 switch(whence) | |
128 { | |
129 case SEEK_CUR: | |
130 handle->iter = handle->iter + offset; | |
131 break; | |
132 case SEEK_END: | |
133 handle->iter = handle->end; | |
134 break; | |
135 case SEEK_SET: | |
136 default: | |
137 handle->iter = handle->data + offset; | |
138 break; | |
139 } | |
140 | |
141 return 0; | |
142 } | |
143 | |
144 void | |
145 buffer_vfs_rewind_impl(VFSFile * file) | |
146 { | |
147 VFSBuffer *handle; | |
148 | |
149 if (file == NULL) | |
150 return; | |
151 | |
152 handle = (VFSBuffer *) file->handle; | |
153 | |
154 handle->iter = handle->data; | |
155 } | |
156 | |
157 glong | |
158 buffer_vfs_ftell_impl(VFSFile * file) | |
159 { | |
160 VFSBuffer *handle; | |
161 | |
162 if (file == NULL) | |
163 return 0; | |
164 | |
165 handle = (VFSBuffer *) file->handle; | |
166 | |
167 return handle->iter - handle->data; | |
168 } | |
169 | |
170 gboolean | |
171 buffer_vfs_feof_impl(VFSFile * file) | |
172 { | |
173 VFSBuffer *handle; | |
174 | |
175 if (file == NULL) | |
176 return FALSE; | |
177 | |
178 handle = (VFSBuffer *) file->handle; | |
179 | |
180 return (gboolean) (handle->iter == handle->end); | |
181 } | |
182 | |
183 gint | |
184 buffer_vfs_truncate_impl(VFSFile * file, glong size) | |
185 { | |
186 return 0; | |
187 } | |
188 | |
2688 | 189 off_t |
190 buffer_vfs_fsize_impl(VFSFile * file) | |
191 { | |
192 VFSBuffer *handle; | |
193 | |
194 if (file == NULL) | |
195 return -1; | |
196 | |
197 handle = (VFSBuffer *) file->handle; | |
198 | |
199 return (off_t)handle->end; | |
200 } | |
201 | |
2313 | 202 VFSConstructor buffer_const = { |
203 NULL, // not a normal VFS class | |
204 buffer_vfs_fopen_impl, | |
205 buffer_vfs_fclose_impl, | |
206 buffer_vfs_fread_impl, | |
207 buffer_vfs_fwrite_impl, | |
208 buffer_vfs_getc_impl, | |
209 buffer_vfs_ungetc_impl, | |
210 buffer_vfs_fseek_impl, | |
211 buffer_vfs_rewind_impl, | |
212 buffer_vfs_ftell_impl, | |
213 buffer_vfs_feof_impl, | |
2518
4c9910af4cc5
[svn] - fix signed vs unsigned comparisons and missing initializer warnings
nenolod
parents:
2344
diff
changeset
|
214 buffer_vfs_truncate_impl, |
2688 | 215 buffer_vfs_fsize_impl, |
2518
4c9910af4cc5
[svn] - fix signed vs unsigned comparisons and missing initializer warnings
nenolod
parents:
2344
diff
changeset
|
216 NULL |
2313 | 217 }; |
218 | |
219 VFSFile * | |
220 vfs_buffer_new(gpointer data, gsize size) | |
221 { | |
222 VFSFile *handle; | |
223 VFSBuffer *buffer; | |
224 | |
225 g_return_val_if_fail(data != NULL, NULL); | |
226 g_return_val_if_fail(size > 0, NULL); | |
227 | |
228 handle = g_new0(VFSFile, 1); | |
229 | |
230 buffer = g_new0(VFSBuffer, 1); | |
231 buffer->data = data; | |
232 buffer->iter = data; | |
2519
793ede082399
[svn] - fix: vfs_buffer.c:218: warning: pointer of type 'void *' used in arithmetic
nenolod
parents:
2518
diff
changeset
|
233 buffer->end = (guchar *) data + size; |
2313 | 234 buffer->size = size; |
235 | |
236 handle->handle = buffer; | |
237 handle->base = &buffer_const; | |
2562
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2519
diff
changeset
|
238 handle->ref = 1; |
2313 | 239 |
240 return handle; | |
241 } | |
242 | |
243 VFSFile * | |
244 vfs_buffer_new_from_string(gchar *str) | |
245 { | |
246 g_return_val_if_fail(str != NULL, NULL); | |
247 | |
248 return vfs_buffer_new(str, strlen(str)); | |
249 } |