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  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions, and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions, and the following disclaimer in the documentation
14  * provided with the distribution.
15  *
16  * This software is provided "as is" and without any warranty, express or
17  * implied. In no event shall the authors be liable for any damages arising from
18  * the use of this software.
19  */
20 
21 #include <glib.h>
22 #include <glib/gprintf.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "config.h"
28 #include "vfs.h"
29 
45 EXPORT int vfs_fputc(int c, VFSFile *stream)
46 {
47  unsigned char uc = (unsigned char) c;
48 
49  if (!vfs_fwrite(&uc, 1, 1, stream)) {
50  return EOF;
51  }
52 
53  return uc;
54 }
55 
64 EXPORT char *vfs_fgets(char *s, int n, VFSFile *stream)
65 {
66  int c;
67  register char *p;
68 
69  if (n <= 0) return NULL;
70 
71  p = s;
72 
73  while (--n) {
74  if ((c = vfs_getc(stream))== EOF) {
75  break;
76  }
77  if ((*p++ = c) == '\n') {
78  break;
79  }
80  }
81  if (p > s) {
82  *p = 0;
83  return s;
84  }
85 
86  return NULL;
87 }
88 
96 EXPORT int vfs_fputs(const char *s, VFSFile *stream)
97 {
98  gsize n = strlen(s);
99 
100  return ((vfs_fwrite(s, 1, n, stream) == n) ? n : EOF);
101 }
102 
111 EXPORT int vfs_vfprintf(VFSFile *stream, char const *format, va_list args)
112 {
113  char *string;
114  int rv = g_vasprintf(&string, format, args);
115  if (rv < 0) return rv;
116  rv = vfs_fputs(string, stream);
117  g_free(string);
118  return rv;
119 }
120 
129 EXPORT int vfs_fprintf(VFSFile *stream, char const *format, ...)
130 {
131  va_list arg;
132  int rv;
133 
134  va_start(arg, format);
135  rv = vfs_vfprintf(stream, format, arg);
136  va_end(arg);
137 
138  return rv;
139 }
140 
150 EXPORT void vfs_file_get_contents (const char * filename, void * * buf, int64_t * size)
151 {
152  * buf = NULL;
153  * size = 0;
154 
155  VFSFile *fd;
156  gsize filled_size = 0, buf_size = 4096;
157  unsigned char * ptr;
158 
159  if ((fd = vfs_fopen(filename, "rb")) == NULL)
160  return;
161 
162  if ((* size = vfs_fsize (fd)) >= 0)
163  {
164  * buf = g_malloc (* size);
165  * size = vfs_fread (* buf, 1, * size, fd);
166  goto close_handle;
167  }
168 
169  if ((*buf = g_malloc(buf_size)) == NULL)
170  goto close_handle;
171 
172  ptr = *buf;
173  while (TRUE) {
174  gsize read_size = vfs_fread(ptr, 1, buf_size - filled_size, fd);
175  if (read_size == 0) break;
176 
177  filled_size += read_size;
178  ptr += read_size;
179 
180  if (filled_size == buf_size) {
181  buf_size += 4096;
182 
183  *buf = g_realloc(*buf, buf_size);
184 
185  if (*buf == NULL)
186  goto close_handle;
187 
188  ptr = (unsigned char *) (* buf) + filled_size;
189  }
190  }
191 
192  *size = filled_size;
193 
194 close_handle:
195  vfs_fclose(fd);
196 }
197 
198 
207 EXPORT bool_t vfs_fget_le16(uint16_t *value, VFSFile *stream)
208 {
209  uint16_t tmp;
210  if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
211  return FALSE;
212  *value = GUINT16_FROM_LE(tmp);
213  return TRUE;
214 }
215 
223 EXPORT bool_t vfs_fget_le32(uint32_t *value, VFSFile *stream)
224 {
225  uint32_t tmp;
226  if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
227  return FALSE;
228  *value = GUINT32_FROM_LE(tmp);
229  return TRUE;
230 }
231 
239 EXPORT bool_t vfs_fget_le64(uint64_t *value, VFSFile *stream)
240 {
241  uint64_t tmp;
242  if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
243  return FALSE;
244  *value = GUINT64_FROM_LE(tmp);
245  return TRUE;
246 }
247 
248 
256 EXPORT bool_t vfs_fget_be16(uint16_t *value, VFSFile *stream)
257 {
258  uint16_t tmp;
259  if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
260  return FALSE;
261  *value = GUINT16_FROM_BE(tmp);
262  return TRUE;
263 }
264 
272 EXPORT bool_t vfs_fget_be32(uint32_t *value, VFSFile *stream)
273 {
274  uint32_t tmp;
275  if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
276  return FALSE;
277  *value = GUINT32_FROM_BE(tmp);
278  return TRUE;
279 }
280 
288 EXPORT bool_t vfs_fget_be64(uint64_t *value, VFSFile *stream)
289 {
290  uint64_t tmp;
291  if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
292  return FALSE;
293  *value = GUINT64_FROM_BE(tmp);
294  return TRUE;
295 }
296 
305 EXPORT bool_t vfs_fput_le16(uint16_t value, VFSFile *stream)
306 {
307  uint16_t tmp = GUINT16_TO_LE(value);
308  return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
309 }
310 
319 EXPORT bool_t vfs_fput_le32(uint32_t value, VFSFile *stream)
320 {
321  uint32_t tmp = GUINT32_TO_LE(value);
322  return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
323 }
324 
333 EXPORT bool_t vfs_fput_le64(uint64_t value, VFSFile *stream)
334 {
335  uint64_t tmp = GUINT64_TO_LE(value);
336  return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
337 }
338 
347 EXPORT bool_t vfs_fput_be16(uint16_t value, VFSFile *stream)
348 {
349  uint16_t tmp = GUINT16_TO_BE(value);
350  return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
351 }
352 
361 EXPORT bool_t vfs_fput_be32(uint32_t value, VFSFile *stream)
362 {
363  uint32_t tmp = GUINT32_TO_BE(value);
364  return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
365 }
366 
375 EXPORT bool_t vfs_fput_be64(uint64_t value, VFSFile *stream)
376 {
377  uint64_t tmp = GUINT64_TO_BE(value);
378  return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
379 }