Audacious $Id:Doxyfile42802007-03-2104:39:00Znenolod$
|
00001 /* 00002 * vfs_common.c 00003 * Copyright 2006-2010 Tony Vroon, William Pitcock, Maciej Grela, 00004 * Matti Hämäläinen, and John Lindgren 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; under version 3 of the License. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program. If not, see <http://www.gnu.org/licenses>. 00017 * 00018 * The Audacious team does not consider modular code linking to 00019 * Audacious or using our public API to be a derived work. 00020 */ 00021 00022 #include <glib.h> 00023 #include <glib/gprintf.h> 00024 #include <stdio.h> 00025 #include <stdlib.h> 00026 #include <string.h> 00027 00028 #include "config.h" 00029 #include "vfs.h" 00030 00046 EXPORT int vfs_fputc(int c, VFSFile *stream) 00047 { 00048 unsigned char uc = (unsigned char) c; 00049 00050 if (!vfs_fwrite(&uc, 1, 1, stream)) { 00051 return EOF; 00052 } 00053 00054 return uc; 00055 } 00056 00065 EXPORT char *vfs_fgets(char *s, int n, VFSFile *stream) 00066 { 00067 int c; 00068 register char *p; 00069 00070 if (n <= 0) return NULL; 00071 00072 p = s; 00073 00074 while (--n) { 00075 if ((c = vfs_getc(stream))== EOF) { 00076 break; 00077 } 00078 if ((*p++ = c) == '\n') { 00079 break; 00080 } 00081 } 00082 if (p > s) { 00083 *p = 0; 00084 return s; 00085 } 00086 00087 return NULL; 00088 } 00089 00097 EXPORT int vfs_fputs(const char *s, VFSFile *stream) 00098 { 00099 gsize n = strlen(s); 00100 00101 return ((vfs_fwrite(s, 1, n, stream) == n) ? n : EOF); 00102 } 00103 00112 EXPORT int vfs_vfprintf(VFSFile *stream, char const *format, va_list args) 00113 { 00114 char *string; 00115 int rv = g_vasprintf(&string, format, args); 00116 if (rv < 0) return rv; 00117 rv = vfs_fputs(string, stream); 00118 g_free(string); 00119 return rv; 00120 } 00121 00130 EXPORT int vfs_fprintf(VFSFile *stream, char const *format, ...) 00131 { 00132 va_list arg; 00133 int rv; 00134 00135 va_start(arg, format); 00136 rv = vfs_vfprintf(stream, format, arg); 00137 va_end(arg); 00138 00139 return rv; 00140 } 00141 00151 EXPORT void vfs_file_get_contents (const char * filename, void * * buf, int64_t * size) 00152 { 00153 * buf = NULL; 00154 * size = 0; 00155 00156 VFSFile *fd; 00157 gsize filled_size = 0, buf_size = 4096; 00158 unsigned char * ptr; 00159 00160 if ((fd = vfs_fopen(filename, "rb")) == NULL) 00161 return; 00162 00163 if ((* size = vfs_fsize (fd)) >= 0) 00164 { 00165 * buf = g_malloc (* size); 00166 * size = vfs_fread (* buf, 1, * size, fd); 00167 goto close_handle; 00168 } 00169 00170 if ((*buf = g_malloc(buf_size)) == NULL) 00171 goto close_handle; 00172 00173 ptr = *buf; 00174 while (TRUE) { 00175 gsize read_size = vfs_fread(ptr, 1, buf_size - filled_size, fd); 00176 if (read_size == 0) break; 00177 00178 filled_size += read_size; 00179 ptr += read_size; 00180 00181 if (filled_size == buf_size) { 00182 buf_size += 4096; 00183 00184 *buf = g_realloc(*buf, buf_size); 00185 00186 if (*buf == NULL) 00187 goto close_handle; 00188 00189 ptr = (unsigned char *) (* buf) + filled_size; 00190 } 00191 } 00192 00193 *size = filled_size; 00194 00195 close_handle: 00196 vfs_fclose(fd); 00197 } 00198 00199 00208 EXPORT bool_t vfs_fget_le16(uint16_t *value, VFSFile *stream) 00209 { 00210 uint16_t tmp; 00211 if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1) 00212 return FALSE; 00213 *value = GUINT16_FROM_LE(tmp); 00214 return TRUE; 00215 } 00216 00224 EXPORT bool_t vfs_fget_le32(uint32_t *value, VFSFile *stream) 00225 { 00226 uint32_t tmp; 00227 if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1) 00228 return FALSE; 00229 *value = GUINT32_FROM_LE(tmp); 00230 return TRUE; 00231 } 00232 00240 EXPORT bool_t vfs_fget_le64(uint64_t *value, VFSFile *stream) 00241 { 00242 uint64_t tmp; 00243 if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1) 00244 return FALSE; 00245 *value = GUINT64_FROM_LE(tmp); 00246 return TRUE; 00247 } 00248 00249 00257 EXPORT bool_t vfs_fget_be16(uint16_t *value, VFSFile *stream) 00258 { 00259 uint16_t tmp; 00260 if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1) 00261 return FALSE; 00262 *value = GUINT16_FROM_BE(tmp); 00263 return TRUE; 00264 } 00265 00273 EXPORT bool_t vfs_fget_be32(uint32_t *value, VFSFile *stream) 00274 { 00275 uint32_t tmp; 00276 if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1) 00277 return FALSE; 00278 *value = GUINT32_FROM_BE(tmp); 00279 return TRUE; 00280 } 00281 00289 EXPORT bool_t vfs_fget_be64(uint64_t *value, VFSFile *stream) 00290 { 00291 uint64_t tmp; 00292 if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1) 00293 return FALSE; 00294 *value = GUINT64_FROM_BE(tmp); 00295 return TRUE; 00296 } 00297 00306 EXPORT bool_t vfs_fput_le16(uint16_t value, VFSFile *stream) 00307 { 00308 uint16_t tmp = GUINT16_TO_LE(value); 00309 return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1; 00310 } 00311 00320 EXPORT bool_t vfs_fput_le32(uint32_t value, VFSFile *stream) 00321 { 00322 uint32_t tmp = GUINT32_TO_LE(value); 00323 return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1; 00324 } 00325 00334 EXPORT bool_t vfs_fput_le64(uint64_t value, VFSFile *stream) 00335 { 00336 uint64_t tmp = GUINT64_TO_LE(value); 00337 return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1; 00338 } 00339 00348 EXPORT bool_t vfs_fput_be16(uint16_t value, VFSFile *stream) 00349 { 00350 uint16_t tmp = GUINT16_TO_BE(value); 00351 return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1; 00352 } 00353 00362 EXPORT bool_t vfs_fput_be32(uint32_t value, VFSFile *stream) 00363 { 00364 uint32_t tmp = GUINT32_TO_BE(value); 00365 return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1; 00366 } 00367 00376 EXPORT bool_t vfs_fput_be64(uint64_t value, VFSFile *stream) 00377 { 00378 uint64_t tmp = GUINT64_TO_BE(value); 00379 return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1; 00380 }