Audacious $Id:Doxyfile42802007-03-2104:39:00Znenolod$

vfs_common.c

Go to the documentation of this file.
00001 /*  This program is free software; you can redistribute it and/or modify
00002  *  it under the terms of the GNU General Public License as published by
00003  *  the Free Software Foundation; under version 3 of the License.
00004  *
00005  *  This program is distributed in the hope that it will be useful,
00006  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00007  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00008  *  GNU General Public License for more details.
00009  *
00010  *  You should have received a copy of the GNU General Public License
00011  *  along with this program.  If not, see <http://www.gnu.org/licenses>.
00012  *
00013  *  The Audacious team does not consider modular code linking to
00014  *  Audacious or using our public API to be a derived work.
00015  */
00016 #include "vfs.h"
00017 #include <string.h>
00018 #include <stdlib.h>
00019 #include <glib/gprintf.h>
00035 gint vfs_fputc(gint c, VFSFile *stream)
00036 {
00037     guchar uc = (guchar) c;
00038 
00039     if (!vfs_fwrite(&uc, 1, 1, stream)) {
00040         return EOF;
00041     }
00042 
00043     return uc;
00044 }
00045 
00054 gchar *vfs_fgets(gchar *s, gint n, VFSFile *stream)
00055 {
00056     gint c;
00057     register gchar *p;
00058 
00059     if (n <= 0) return NULL;
00060 
00061     p = s;
00062 
00063     while (--n) {
00064         if ((c = vfs_getc(stream))== EOF) {
00065             break;
00066         }
00067         if ((*p++ = c) == '\n') {
00068             break;
00069         }
00070     }
00071     if (p > s) {
00072         *p = 0;
00073         return s;
00074     }
00075 
00076     return NULL;
00077 }
00078 
00086 gint vfs_fputs(const gchar *s, VFSFile *stream)
00087 {
00088     gsize n = strlen(s);
00089 
00090     return ((vfs_fwrite(s, 1, n, stream) == n) ? n : EOF);
00091 }
00092 
00101 gint vfs_vfprintf(VFSFile *stream, gchar const *format, va_list args)
00102 {
00103     gchar *string;
00104     gint rv = g_vasprintf(&string, format, args);
00105     if (rv < 0) return rv;
00106     rv = vfs_fputs(string, stream);
00107     g_free(string);
00108     return rv;
00109 }
00110 
00119 gint vfs_fprintf(VFSFile *stream, gchar const *format, ...)
00120 {
00121     va_list arg;
00122     gint rv;
00123 
00124     va_start(arg, format);
00125     rv = vfs_vfprintf(stream, format, arg);
00126     va_end(arg);
00127 
00128     return rv;
00129 }
00130 
00140 void vfs_file_get_contents (const gchar * filename, void * * buf, gint64 * size)
00141 {
00142     * buf = NULL;
00143     * size = 0;
00144 
00145     VFSFile *fd;
00146     gsize filled_size = 0, buf_size = 4096;
00147     guchar * ptr;
00148 
00149     if ((fd = vfs_fopen(filename, "rb")) == NULL)
00150         return;
00151 
00152     if ((* size = vfs_fsize (fd)) >= 0)
00153     {
00154         * buf = g_malloc (* size);
00155         * size = vfs_fread (* buf, 1, * size, fd);
00156         goto close_handle;
00157     }
00158 
00159     if ((*buf = g_malloc(buf_size)) == NULL)
00160         goto close_handle;
00161 
00162     ptr = *buf;
00163     while (TRUE) {
00164         gsize read_size = vfs_fread(ptr, 1, buf_size - filled_size, fd);
00165         if (read_size == 0) break;
00166 
00167         filled_size += read_size;
00168         ptr += read_size;
00169 
00170         if (filled_size == buf_size) {
00171             buf_size += 4096;
00172 
00173             *buf = g_realloc(*buf, buf_size);
00174 
00175             if (*buf == NULL)
00176                 goto close_handle;
00177 
00178             ptr = *buf + filled_size;
00179         }
00180     }
00181 
00182     *size = filled_size;
00183 
00184 close_handle:
00185     vfs_fclose(fd);
00186 }
00187 
00188 
00197 gboolean vfs_fget_le16(guint16 *value, VFSFile *stream)
00198 {
00199     guint16 tmp;
00200     if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
00201         return FALSE;
00202     *value = GUINT16_FROM_LE(tmp);
00203     return TRUE;
00204 }
00205 
00213 gboolean vfs_fget_le32(guint32 *value, VFSFile *stream)
00214 {
00215     guint32 tmp;
00216     if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
00217         return FALSE;
00218     *value = GUINT32_FROM_LE(tmp);
00219     return TRUE;
00220 }
00221 
00229 gboolean vfs_fget_le64(guint64 *value, VFSFile *stream)
00230 {
00231     guint64 tmp;
00232     if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
00233         return FALSE;
00234     *value = GUINT64_FROM_LE(tmp);
00235     return TRUE;
00236 }
00237 
00238 
00246 gboolean vfs_fget_be16(guint16 *value, VFSFile *stream)
00247 {
00248     guint16 tmp;
00249     if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
00250         return FALSE;
00251     *value = GUINT16_FROM_BE(tmp);
00252     return TRUE;
00253 }
00254 
00262 gboolean vfs_fget_be32(guint32 *value, VFSFile *stream)
00263 {
00264     guint32 tmp;
00265     if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
00266         return FALSE;
00267     *value = GUINT32_FROM_BE(tmp);
00268     return TRUE;
00269 }
00270 
00278 gboolean vfs_fget_be64(guint64 *value, VFSFile *stream)
00279 {
00280     guint64 tmp;
00281     if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
00282         return FALSE;
00283     *value = GUINT64_FROM_BE(tmp);
00284     return TRUE;
00285 }
00286 
00295 gboolean vfs_fput_le16(guint16 value, VFSFile *stream)
00296 {
00297     guint16 tmp = GUINT16_TO_LE(value);
00298     return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
00299 }
00300 
00309 gboolean vfs_fput_le32(guint32 value, VFSFile *stream)
00310 {
00311     guint32 tmp = GUINT32_TO_LE(value);
00312     return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
00313 }
00314 
00323 gboolean vfs_fput_le64(guint64 value, VFSFile *stream)
00324 {
00325     guint64 tmp = GUINT64_TO_LE(value);
00326     return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
00327 }
00328 
00337 gboolean vfs_fput_be16(guint16 value, VFSFile *stream)
00338 {
00339     guint16 tmp = GUINT16_TO_BE(value);
00340     return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
00341 }
00342 
00351 gboolean vfs_fput_be32(guint32 value, VFSFile *stream)
00352 {
00353     guint32 tmp = GUINT32_TO_BE(value);
00354     return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
00355 }
00356 
00365 gboolean vfs_fput_be64(guint64 value, VFSFile *stream)
00366 {
00367     guint64 tmp = GUINT64_TO_BE(value);
00368     return vfs_fwrite(&tmp, sizeof(tmp), 1, stream) == 1;
00369 }