Mercurial > audlegacy
comparison src/audlegacy/vfs_common.c @ 4811:7bf7f83a217e
rename src/audacious src/audlegacy so that both audlegacy and audacious can coexist.
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Wed, 26 Nov 2008 00:44:56 +0900 |
parents | src/audacious/vfs_common.c@9d5e6bfca672 |
children |
comparison
equal
deleted
inserted
replaced
4810:c10e53092037 | 4811:7bf7f83a217e |
---|---|
1 /* This program is free software; you can redistribute it and/or modify | |
2 * it under the terms of the GNU General Public License as published by | |
3 * the Free Software Foundation; under version 3 of the License. | |
4 * | |
5 * This program is distributed in the hope that it will be useful, | |
6 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
7 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
8 * GNU General Public License for more details. | |
9 * | |
10 * You should have received a copy of the GNU General Public License | |
11 * along with this program. If not, see <http://www.gnu.org/licenses>. | |
12 * | |
13 * The Audacious team does not consider modular code linking to | |
14 * Audacious or using our public API to be a derived work. | |
15 */ | |
16 | |
17 #include "vfs.h" | |
18 #include <string.h> | |
19 #include <stdlib.h> | |
20 #include <glib/gprintf.h> | |
21 | |
22 /** | |
23 * vfs_fputc: | |
24 * @c: A character to write to the stream. | |
25 * @stream: A #VFSFile object representing the stream. | |
26 * | |
27 * Writes a character to a stream. | |
28 * | |
29 * Return value: The character on success, or EOF. | |
30 **/ | |
31 gint vfs_fputc(gint c, VFSFile *stream) | |
32 { | |
33 guchar uc = (guchar) c; | |
34 | |
35 if (! vfs_fwrite(&uc, 1, 1, stream)) { | |
36 return EOF; | |
37 } | |
38 | |
39 return uc; | |
40 } | |
41 | |
42 /** | |
43 * vfs_fgets: | |
44 * @s: A buffer to put the string in. | |
45 * @n: The amount of characters to read. | |
46 * @stream: A #VFSFile object representing the stream. | |
47 * | |
48 * Reads a set of characters from a stream. | |
49 * | |
50 * Return value: The string on success, or NULL. | |
51 **/ | |
52 gchar *vfs_fgets(gchar *s, gint n, VFSFile *stream) | |
53 { | |
54 gint c; | |
55 register gchar *p; | |
56 | |
57 if(n<=0) return NULL; | |
58 | |
59 p = s; | |
60 | |
61 while (--n) { | |
62 if ((c = vfs_getc(stream))== EOF) { | |
63 break; | |
64 } | |
65 if ((*p++ = c) == '\n') { | |
66 break; | |
67 } | |
68 } | |
69 if (p > s) { | |
70 *p = 0; | |
71 return s; | |
72 } | |
73 | |
74 return NULL; | |
75 } | |
76 | |
77 /** | |
78 * vfs_fputc: | |
79 * @s: A string to write to the stream. | |
80 * @stream: A #VFSFile object representing the stream. | |
81 * | |
82 * Writes a string to a VFS stream. | |
83 * | |
84 * Return value: The amount of bytes written. | |
85 **/ | |
86 gint vfs_fputs(const gchar *s, VFSFile *stream) | |
87 { | |
88 size_t n = strlen(s); | |
89 | |
90 return ((vfs_fwrite(s, 1, n, stream) == n) ? n : EOF); | |
91 } | |
92 | |
93 /** | |
94 * vfs_vfprintf: | |
95 * @stream: A #VFSFile object representing the stream. | |
96 * @format: A printf-style format string. | |
97 * @args: A va_list of args to use. | |
98 * | |
99 * Writes a formatted string to a VFS stream via a va_list of args. | |
100 * | |
101 * Return value: The amount of bytes written. | |
102 **/ | |
103 gint vfs_vfprintf(VFSFile *stream, gchar const *format, va_list args) | |
104 { | |
105 gchar *string; | |
106 gint rv = g_vasprintf(&string, format, args); | |
107 if (rv<0) return rv; | |
108 rv = vfs_fputs(string, stream); | |
109 free (string); | |
110 return rv; | |
111 } | |
112 | |
113 /** | |
114 * vfs_fprintf: | |
115 * @stream: A #VFSFile object representing the stream. | |
116 * @format: A printf-style format string. | |
117 * @...: A list of args to use. | |
118 * | |
119 * Writes a formatted string to a VFS stream. | |
120 * | |
121 * Return value: The amount of bytes written. | |
122 **/ | |
123 gint vfs_fprintf(VFSFile *stream, gchar const *format, ...) | |
124 { | |
125 va_list arg; | |
126 gint rv; | |
127 | |
128 va_start(arg, format); | |
129 rv = vfs_vfprintf(stream, format, arg); | |
130 va_end(arg); | |
131 | |
132 return rv; | |
133 } | |
134 | |
135 /** | |
136 * vfs_file_get_contents | |
137 * @filename: the filename to read in | |
138 * @buf: pointer to pointer of buffer | |
139 * @sz: pointer to integer that is the size | |
140 **/ | |
141 void | |
142 vfs_file_get_contents(const gchar *filename, gchar **buf, gsize *size) | |
143 { | |
144 VFSFile *fd; | |
145 size_t filled_size = 0; | |
146 size_t buf_size = 4096; | |
147 gchar *ptr; | |
148 | |
149 fd = vfs_fopen(filename, "rb"); | |
150 | |
151 if (fd == NULL) | |
152 return; | |
153 | |
154 if ( vfs_fseek(fd, 0, SEEK_END) == 0) { // seeking supported by VFS backend | |
155 *size = vfs_ftell(fd); | |
156 | |
157 *buf = g_new(gchar, *size); | |
158 | |
159 if (*buf == NULL) | |
160 goto close_handle; | |
161 | |
162 vfs_fseek(fd, 0, SEEK_SET); | |
163 vfs_fread(*buf, 1, *size, fd); | |
164 | |
165 goto close_handle; | |
166 } | |
167 | |
168 | |
169 *buf = g_new(gchar, buf_size); | |
170 | |
171 if (*buf == NULL) | |
172 goto close_handle; | |
173 | |
174 ptr=*buf; | |
175 while ( 1 ) { | |
176 size_t read_size = vfs_fread(ptr, 1, buf_size - filled_size, fd); | |
177 if ( read_size == 0 ) break; | |
178 | |
179 filled_size+=read_size; | |
180 ptr+=read_size; | |
181 | |
182 if ( filled_size == buf_size ) { | |
183 buf_size+=4096; | |
184 | |
185 *buf = g_realloc(*buf, buf_size); | |
186 | |
187 if ( *buf == NULL ) | |
188 goto close_handle; | |
189 | |
190 ptr=*buf + filled_size; | |
191 } | |
192 } | |
193 | |
194 *size = filled_size; | |
195 | |
196 close_handle: | |
197 vfs_fclose(fd); | |
198 } | |
199 | |
200 | |
201 /** | |
202 * vfs_fget_le16: | |
203 * @value: Pointer to the variable to read the value into. | |
204 * @stream: A #VFSFile object representing the stream. | |
205 * | |
206 * Reads an unsigned 16-bit Little Endian value from the stream into native endian format. | |
207 * | |
208 * Return value: TRUE if read was succesful, FALSE if there was an error. | |
209 **/ | |
210 gboolean vfs_fget_le16(guint16 *value, VFSFile *stream) | |
211 { | |
212 guint16 tmp; | |
213 if (vfs_fread(&tmp, sizeof(guint16), 1, stream) != 1) | |
214 return FALSE; | |
215 *value = GUINT16_FROM_LE(tmp); | |
216 return TRUE; | |
217 } | |
218 | |
219 /** | |
220 * vfs_fget_le32: | |
221 * @value: Pointer to the variable to read the value into. | |
222 * @stream: A #VFSFile object representing the stream. | |
223 * | |
224 * Reads an unsigned 32-bit Little Endian value from the stream into native endian format. | |
225 * | |
226 * Return value: TRUE if read was succesful, FALSE if there was an error. | |
227 **/ | |
228 gboolean vfs_fget_le32(guint32 *value, VFSFile *stream) | |
229 { | |
230 guint32 tmp; | |
231 if (vfs_fread(&tmp, sizeof(guint32), 1, stream) != 1) | |
232 return FALSE; | |
233 *value = GUINT32_FROM_LE(tmp); | |
234 return TRUE; | |
235 } | |
236 | |
237 /** | |
238 * vfs_fget_le64: | |
239 * @value: Pointer to the variable to read the value into. | |
240 * @stream: A #VFSFile object representing the stream. | |
241 * | |
242 * Reads an unsigned 64-bit Little Endian value from the stream into native endian format. | |
243 * | |
244 * Return value: TRUE if read was succesful, FALSE if there was an error. | |
245 **/ | |
246 gboolean vfs_fget_le64(guint64 *value, VFSFile *stream) | |
247 { | |
248 guint64 tmp; | |
249 if (vfs_fread(&tmp, sizeof(guint64), 1, stream) != 1) | |
250 return FALSE; | |
251 *value = GUINT64_FROM_LE(tmp); | |
252 return TRUE; | |
253 } | |
254 | |
255 | |
256 /** | |
257 * vfs_fget_be16: | |
258 * @value: Pointer to the variable to read the value into. | |
259 * @stream: A #VFSFile object representing the stream. | |
260 * | |
261 * Reads an unsigned 16-bit Big Endian value from the stream into native endian format. | |
262 * | |
263 * Return value: TRUE if read was succesful, FALSE if there was an error. | |
264 **/ | |
265 gboolean vfs_fget_be16(guint16 *value, VFSFile *stream) | |
266 { | |
267 guint16 tmp; | |
268 if (vfs_fread(&tmp, sizeof(guint16), 1, stream) != 1) | |
269 return FALSE; | |
270 *value = GUINT16_FROM_BE(tmp); | |
271 return TRUE; | |
272 } | |
273 | |
274 /** | |
275 * vfs_fget_be32: | |
276 * @value: Pointer to the variable to read the value into. | |
277 * @stream: A #VFSFile object representing the stream. | |
278 * | |
279 * Reads an unsigned 32-bit Big Endian value from the stream into native endian format. | |
280 * | |
281 * Return value: TRUE if read was succesful, FALSE if there was an error. | |
282 **/ | |
283 gboolean vfs_fget_be32(guint32 *value, VFSFile *stream) | |
284 { | |
285 guint32 tmp; | |
286 if (vfs_fread(&tmp, sizeof(guint32), 1, stream) != 1) | |
287 return FALSE; | |
288 *value = GUINT32_FROM_BE(tmp); | |
289 return TRUE; | |
290 } | |
291 | |
292 /** | |
293 * vfs_fget_be64: | |
294 * @value: Pointer to the variable to read the value into. | |
295 * @stream: A #VFSFile object representing the stream. | |
296 * | |
297 * Reads an unsigned 64-bit Big Endian value from the stream into native endian format. | |
298 * | |
299 * Return value: TRUE if read was succesful, FALSE if there was an error. | |
300 **/ | |
301 gboolean vfs_fget_be64(guint64 *value, VFSFile *stream) | |
302 { | |
303 guint64 tmp; | |
304 if (vfs_fread(&tmp, sizeof(guint64), 1, stream) != 1) | |
305 return FALSE; | |
306 *value = GUINT64_FROM_BE(tmp); | |
307 return TRUE; | |
308 } | |
309 | |
310 |