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 "vfs.h"
28 
44 EXPORT int vfs_fputc(int c, VFSFile *stream)
45 {
46  unsigned char uc = (unsigned char) c;
47 
48  if (!vfs_fwrite(&uc, 1, 1, stream)) {
49  return EOF;
50  }
51 
52  return uc;
53 }
54 
63 EXPORT char *vfs_fgets(char *s, int n, VFSFile *stream)
64 {
65  int c;
66  register char *p;
67 
68  if (n <= 0) return NULL;
69 
70  p = s;
71 
72  while (--n) {
73  if ((c = vfs_getc(stream))== EOF) {
74  break;
75  }
76  if ((*p++ = c) == '\n') {
77  break;
78  }
79  }
80  if (p > s) {
81  *p = 0;
82  return s;
83  }
84 
85  return NULL;
86 }
87 
95 EXPORT int vfs_fputs(const char *s, VFSFile *stream)
96 {
97  gsize n = strlen(s);
98 
99  return ((vfs_fwrite(s, 1, n, stream) == n) ? n : EOF);
100 }
101 
110 EXPORT int vfs_vfprintf(VFSFile *stream, char const *format, va_list args)
111 {
112  char *string;
113  int rv = g_vasprintf(&string, format, args);
114  if (rv < 0) return rv;
115  rv = vfs_fputs(string, stream);
116  g_free(string);
117  return rv;
118 }
119 
128 EXPORT int vfs_fprintf(VFSFile *stream, char const *format, ...)
129 {
130  va_list arg;
131  int rv;
132 
133  va_start(arg, format);
134  rv = vfs_vfprintf(stream, format, arg);
135  va_end(arg);
136 
137  return rv;
138 }
139 
149 EXPORT void vfs_file_get_contents (const char * filename, void * * buf, int64_t * size)
150 {
151  * buf = NULL;
152  * size = 0;
153 
154  VFSFile *fd;
155  gsize filled_size = 0, buf_size = 4096;
156  unsigned char * ptr;
157 
158  if ((fd = vfs_fopen(filename, "rb")) == NULL)
159  return;
160 
161  if ((* size = vfs_fsize (fd)) >= 0)
162  {
163  * buf = g_malloc (* size);
164  * size = vfs_fread (* buf, 1, * size, fd);
165  goto close_handle;
166  }
167 
168  if ((*buf = g_malloc(buf_size)) == NULL)
169  goto close_handle;
170 
171  ptr = *buf;
172  while (TRUE) {
173  gsize read_size = vfs_fread(ptr, 1, buf_size - filled_size, fd);
174  if (read_size == 0) break;
175 
176  filled_size += read_size;
177  ptr += read_size;
178 
179  if (filled_size == buf_size) {
180  buf_size += 4096;
181 
182  *buf = g_realloc(*buf, buf_size);
183 
184  if (*buf == NULL)
185  goto close_handle;
186 
187  ptr = (unsigned char *) (* buf) + filled_size;
188  }
189  }
190 
191  *size = filled_size;
192 
193 close_handle:
194  vfs_fclose(fd);
195 }
196 
197 
206 EXPORT bool_t vfs_fget_le16(uint16_t *value, VFSFile *stream)
207 {
208  uint16_t tmp;
209  if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
210  return FALSE;
211  *value = GUINT16_FROM_LE(tmp);
212  return TRUE;
213 }
214 
222 EXPORT bool_t vfs_fget_le32(uint32_t *value, VFSFile *stream)
223 {
224  uint32_t tmp;
225  if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
226  return FALSE;
227  *value = GUINT32_FROM_LE(tmp);
228  return TRUE;
229 }
230 
238 EXPORT bool_t vfs_fget_le64(uint64_t *value, VFSFile *stream)
239 {
240  uint64_t tmp;
241  if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
242  return FALSE;
243  *value = GUINT64_FROM_LE(tmp);
244  return TRUE;
245 }
246 
247 
255 EXPORT bool_t vfs_fget_be16(uint16_t *value, VFSFile *stream)
256 {
257  uint16_t tmp;
258  if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
259  return FALSE;
260  *value = GUINT16_FROM_BE(tmp);
261  return TRUE;
262 }
263 
271 EXPORT bool_t vfs_fget_be32(uint32_t *value, VFSFile *stream)
272 {
273  uint32_t tmp;
274  if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
275  return FALSE;
276  *value = GUINT32_FROM_BE(tmp);
277  return TRUE;
278 }
279 
287 EXPORT bool_t vfs_fget_be64(uint64_t *value, VFSFile *stream)
288 {
289  uint64_t tmp;
290  if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
291  return FALSE;
292  *value = GUINT64_FROM_BE(tmp);
293  return TRUE;
294 }
295 
304 EXPORT bool_t vfs_fput_le16(uint16_t value, VFSFile *stream)
305 {
306  uint16_t tmp = GUINT16_TO_LE(value);
307  return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
308 }
309 
318 EXPORT bool_t vfs_fput_le32(uint32_t value, VFSFile *stream)
319 {
320  uint32_t tmp = GUINT32_TO_LE(value);
321  return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
322 }
323 
332 EXPORT bool_t vfs_fput_le64(uint64_t value, VFSFile *stream)
333 {
334  uint64_t tmp = GUINT64_TO_LE(value);
335  return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
336 }
337 
346 EXPORT bool_t vfs_fput_be16(uint16_t value, VFSFile *stream)
347 {
348  uint16_t tmp = GUINT16_TO_BE(value);
349  return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
350 }
351 
360 EXPORT bool_t vfs_fput_be32(uint32_t value, VFSFile *stream)
361 {
362  uint32_t tmp = GUINT32_TO_BE(value);
363  return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
364 }
365 
374 EXPORT bool_t vfs_fput_be64(uint64_t value, VFSFile *stream)
375 {
376  uint64_t tmp = GUINT64_TO_BE(value);
377  return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
378 }