Audacious  $Id:Doxyfile42802007-03-2104:39:00Znenolod$
vfs_common.c
Go to the documentation of this file.
1 /*
2  * vfs_common.c
3  * Copyright 2006-2010 Tony Vroon, William Pitcock, Maciej Grela,
4  * Matti Hämäläinen, and John Lindgren
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; under version 3 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses>.
17  *
18  * The Audacious team does not consider modular code linking to
19  * Audacious or using our public API to be a derived work.
20  */
21 
22 #include <glib.h>
23 #include <glib/gprintf.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "config.h"
29 #include "vfs.h"
30 
46 EXPORT int vfs_fputc(int c, VFSFile *stream)
47 {
48  unsigned char uc = (unsigned char) c;
49 
50  if (!vfs_fwrite(&uc, 1, 1, stream)) {
51  return EOF;
52  }
53 
54  return uc;
55 }
56 
65 EXPORT char *vfs_fgets(char *s, int n, VFSFile *stream)
66 {
67  int c;
68  register char *p;
69 
70  if (n <= 0) return NULL;
71 
72  p = s;
73 
74  while (--n) {
75  if ((c = vfs_getc(stream))== EOF) {
76  break;
77  }
78  if ((*p++ = c) == '\n') {
79  break;
80  }
81  }
82  if (p > s) {
83  *p = 0;
84  return s;
85  }
86 
87  return NULL;
88 }
89 
97 EXPORT int vfs_fputs(const char *s, VFSFile *stream)
98 {
99  gsize n = strlen(s);
100 
101  return ((vfs_fwrite(s, 1, n, stream) == n) ? n : EOF);
102 }
103 
112 EXPORT int vfs_vfprintf(VFSFile *stream, char const *format, va_list args)
113 {
114  char *string;
115  int rv = g_vasprintf(&string, format, args);
116  if (rv < 0) return rv;
117  rv = vfs_fputs(string, stream);
118  g_free(string);
119  return rv;
120 }
121 
130 EXPORT int vfs_fprintf(VFSFile *stream, char const *format, ...)
131 {
132  va_list arg;
133  int rv;
134 
135  va_start(arg, format);
136  rv = vfs_vfprintf(stream, format, arg);
137  va_end(arg);
138 
139  return rv;
140 }
141 
151 EXPORT void vfs_file_get_contents (const char * filename, void * * buf, int64_t * size)
152 {
153  * buf = NULL;
154  * size = 0;
155 
156  VFSFile *fd;
157  gsize filled_size = 0, buf_size = 4096;
158  unsigned char * ptr;
159 
160  if ((fd = vfs_fopen(filename, "rb")) == NULL)
161  return;
162 
163  if ((* size = vfs_fsize (fd)) >= 0)
164  {
165  * buf = g_malloc (* size);
166  * size = vfs_fread (* buf, 1, * size, fd);
167  goto close_handle;
168  }
169 
170  if ((*buf = g_malloc(buf_size)) == NULL)
171  goto close_handle;
172 
173  ptr = *buf;
174  while (TRUE) {
175  gsize read_size = vfs_fread(ptr, 1, buf_size - filled_size, fd);
176  if (read_size == 0) break;
177 
178  filled_size += read_size;
179  ptr += read_size;
180 
181  if (filled_size == buf_size) {
182  buf_size += 4096;
183 
184  *buf = g_realloc(*buf, buf_size);
185 
186  if (*buf == NULL)
187  goto close_handle;
188 
189  ptr = (unsigned char *) (* buf) + filled_size;
190  }
191  }
192 
193  *size = filled_size;
194 
195 close_handle:
196  vfs_fclose(fd);
197 }
198 
199 
208 EXPORT bool_t vfs_fget_le16(uint16_t *value, VFSFile *stream)
209 {
210  uint16_t tmp;
211  if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
212  return FALSE;
213  *value = GUINT16_FROM_LE(tmp);
214  return TRUE;
215 }
216 
224 EXPORT bool_t vfs_fget_le32(uint32_t *value, VFSFile *stream)
225 {
226  uint32_t tmp;
227  if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
228  return FALSE;
229  *value = GUINT32_FROM_LE(tmp);
230  return TRUE;
231 }
232 
240 EXPORT bool_t vfs_fget_le64(uint64_t *value, VFSFile *stream)
241 {
242  uint64_t tmp;
243  if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
244  return FALSE;
245  *value = GUINT64_FROM_LE(tmp);
246  return TRUE;
247 }
248 
249 
257 EXPORT bool_t vfs_fget_be16(uint16_t *value, VFSFile *stream)
258 {
259  uint16_t tmp;
260  if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
261  return FALSE;
262  *value = GUINT16_FROM_BE(tmp);
263  return TRUE;
264 }
265 
273 EXPORT bool_t vfs_fget_be32(uint32_t *value, VFSFile *stream)
274 {
275  uint32_t tmp;
276  if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
277  return FALSE;
278  *value = GUINT32_FROM_BE(tmp);
279  return TRUE;
280 }
281 
289 EXPORT bool_t vfs_fget_be64(uint64_t *value, VFSFile *stream)
290 {
291  uint64_t tmp;
292  if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
293  return FALSE;
294  *value = GUINT64_FROM_BE(tmp);
295  return TRUE;
296 }
297 
306 EXPORT bool_t vfs_fput_le16(uint16_t value, VFSFile *stream)
307 {
308  uint16_t tmp = GUINT16_TO_LE(value);
309  return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
310 }
311 
320 EXPORT bool_t vfs_fput_le32(uint32_t value, VFSFile *stream)
321 {
322  uint32_t tmp = GUINT32_TO_LE(value);
323  return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
324 }
325 
334 EXPORT bool_t vfs_fput_le64(uint64_t value, VFSFile *stream)
335 {
336  uint64_t tmp = GUINT64_TO_LE(value);
337  return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
338 }
339 
348 EXPORT bool_t vfs_fput_be16(uint16_t value, VFSFile *stream)
349 {
350  uint16_t tmp = GUINT16_TO_BE(value);
351  return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
352 }
353 
362 EXPORT bool_t vfs_fput_be32(uint32_t value, VFSFile *stream)
363 {
364  uint32_t tmp = GUINT32_TO_BE(value);
365  return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
366 }
367 
376 EXPORT bool_t vfs_fput_be64(uint64_t value, VFSFile *stream)
377 {
378  uint64_t tmp = GUINT64_TO_BE(value);
379  return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
380 }