• Main Page
  • Related Pages
  • Classes
  • Files
  • File List
  • File Members

probe-buffer.c

Go to the documentation of this file.
00001 /*
00002  * probe-buffer.c
00003  * Copyright 2010 John Lindgren
00004  *
00005  * This file is part of Audacious.
00006  *
00007  * Audacious is free software: you can redistribute it and/or modify it under
00008  * the terms of the GNU General Public License as published by the Free Software
00009  * Foundation, version 2 or version 3 of the License.
00010  *
00011  * Audacious is distributed in the hope that it will be useful, but WITHOUT ANY
00012  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00013  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License along with
00016  * Audacious. If not, see <http://www.gnu.org/licenses/>.
00017  *
00018  * The Audacious team does not consider modular code linking to Audacious or
00019  * using our public API to be a derived work.
00020  */
00021 
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 
00026 #include "probe-buffer.h"
00027 
00028 typedef struct
00029 {
00030     const gchar * filename, * decoder;
00031     VFSFile * file;
00032     guchar buffer[16384];
00033     gint filled, at;
00034     const gchar * read_warned, * seek_warned;
00035 }
00036 ProbeBuffer;
00037 
00038 static gint probe_buffer_fclose (VFSFile * file)
00039 {
00040     gint ret = vfs_fclose (((ProbeBuffer *) file->handle)->file);
00041     g_free (file->handle);
00042     return ret;
00043 }
00044 
00045 static void increase_buffer (ProbeBuffer * p, gint64 size)
00046 {
00047     size = (size + 0xFF) & ~0xFF;
00048 
00049     if (size > sizeof p->buffer)
00050     {
00051         if (p->read_warned != p->decoder)
00052         {
00053             fprintf (stderr, "%s tried to read past end of buffer while "
00054              "probing %s.\n", p->decoder, p->filename);
00055             p->read_warned = p->decoder;
00056         }
00057 
00058         size = sizeof p->buffer;
00059     }
00060 
00061     if (p->filled < size)
00062         p->filled += vfs_fread (p->buffer + p->at, 1, size - p->filled, p->file);
00063 }
00064 
00065 static gint64 probe_buffer_fread (void * buffer, gint64 size, gint64 count,
00066  VFSFile * file)
00067 {
00068     ProbeBuffer * p = file->handle;
00069 
00070     increase_buffer (p, p->at + size * count);
00071     gint readed = (size > 0) ? MIN (count, (p->filled - p->at) / size) : 0;
00072     memcpy (buffer, p->buffer + p->at, size * readed);
00073 
00074     p->at += size * readed;
00075     return readed;
00076 }
00077 
00078 static gint64 probe_buffer_fwrite (const void * data, gint64 size, gint64 count,
00079  VFSFile * file)
00080 {
00081     /* not implemented */
00082     return 0;
00083 }
00084 
00085 static gint probe_buffer_getc (VFSFile * file)
00086 {
00087     guchar c;
00088     return (probe_buffer_fread (& c, 1, 1, file) == 1) ? c : EOF;
00089 }
00090 
00091 static gint probe_buffer_fseek (VFSFile * file, gint64 offset, gint whence)
00092 {
00093     ProbeBuffer * p = file->handle;
00094 
00095     if (whence == SEEK_END)
00096     {
00097         if (p->seek_warned != p->decoder)
00098         {
00099             fprintf (stderr, "%s tried to seek to end of file while probing "
00100              "%s.\n", p->decoder, p->filename);
00101             p->seek_warned = p->decoder;
00102         }
00103 
00104         return -1;
00105     }
00106 
00107     if (whence == SEEK_CUR)
00108         offset += p->at;
00109 
00110     g_return_val_if_fail (offset >= 0, -1);
00111     increase_buffer (p, offset);
00112 
00113     if (offset > p->filled)
00114         return -1;
00115 
00116     p->at = offset;
00117     return 0;
00118 }
00119 
00120 static gint probe_buffer_ungetc (gint c, VFSFile * file)
00121 {
00122     return (! probe_buffer_fseek (file, -1, SEEK_CUR)) ? c : EOF;
00123 }
00124 
00125 static void probe_buffer_rewind (VFSFile * file)
00126 {
00127     probe_buffer_fseek (file, 0, SEEK_SET);
00128 }
00129 
00130 static gint64 probe_buffer_ftell (VFSFile * file)
00131 {
00132     return ((ProbeBuffer *) file->handle)->at;
00133 }
00134 
00135 static gboolean probe_buffer_feof (VFSFile * file)
00136 {
00137     ProbeBuffer * p = file->handle;
00138     return (p->at < p->filled) ? FALSE : vfs_feof (p->file);
00139 }
00140 
00141 static gint probe_buffer_ftruncate (VFSFile * file, gint64 size)
00142 {
00143     /* not implemented */
00144     return -1;
00145 }
00146 
00147 static gint64 probe_buffer_fsize (VFSFile * file)
00148 {
00149     return vfs_fsize (((ProbeBuffer *) file->handle)->file);
00150 }
00151 
00152 static gchar * probe_buffer_get_metadata (VFSFile * file, const gchar * field)
00153 {
00154     return vfs_get_metadata (((ProbeBuffer *) file->handle)->file, field);
00155 }
00156 
00157 static VFSConstructor probe_buffer_table =
00158 {
00159         .uri_id = NULL,
00160         .vfs_fopen_impl = NULL,
00161         .vfs_fclose_impl = probe_buffer_fclose,
00162         .vfs_fread_impl = probe_buffer_fread,
00163         .vfs_fwrite_impl = probe_buffer_fwrite,
00164         .vfs_getc_impl = probe_buffer_getc,
00165         .vfs_ungetc_impl = probe_buffer_ungetc,
00166         .vfs_fseek_impl = probe_buffer_fseek,
00167         .vfs_rewind_impl = probe_buffer_rewind,
00168         .vfs_ftell_impl = probe_buffer_ftell,
00169         .vfs_feof_impl = probe_buffer_feof,
00170         .vfs_ftruncate_impl = probe_buffer_ftruncate,
00171         .vfs_fsize_impl = probe_buffer_fsize,
00172         .vfs_get_metadata_impl = probe_buffer_get_metadata,
00173 };
00174 
00175 VFSFile * probe_buffer_new (const gchar * filename)
00176 {
00177     VFSFile * file = vfs_fopen (filename, "r");
00178 
00179     if (! file)
00180         return NULL;
00181 
00182     ProbeBuffer * p = g_malloc (sizeof (ProbeBuffer));
00183     p->decoder = NULL;
00184     p->filename = filename;
00185     p->file = file;
00186     p->filled = 0;
00187     p->at = 0;
00188     p->read_warned = NULL;
00189     p->seek_warned = NULL;
00190 
00191     VFSFile * file2 = g_malloc (sizeof (VFSFile));
00192     file2->base = & probe_buffer_table;
00193     file2->handle = p;
00194     file2->uri = g_strdup (filename);
00195     file2->ref = 1;
00196 
00197     return file2;
00198 }
00199 
00200 void probe_buffer_set_decoder (VFSFile * file, const gchar * decoder)
00201 {
00202     ProbeBuffer * p = file->handle;
00203     p->decoder = decoder;
00204 }

Generated on Wed Apr 6 2011 for Audacious by  doxygen 1.7.1