changeset 2068:31b5c59ed31b trunk

[svn] - add VFSBuffer, equivilant to fmemopen().
author nenolod
date Fri, 08 Dec 2006 02:25:20 -0800
parents 0b0a12ea9dd9
children 5bc48557acbb
files ChangeLog libaudacious/Makefile libaudacious/vfs_buffer.c libaudacious/vfs_buffer.h
diffstat 4 files changed, 313 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Thu Dec 07 22:56:55 2006 -0800
+++ b/ChangeLog	Fri Dec 08 02:25:20 2006 -0800
@@ -1,3 +1,12 @@
+2006-12-08 06:56:55 +0000  William Pitcock <nenolod@nenolod.net>
+  revision [3137]
+  - allow the output plugin to be changed while playing.
+  
+  trunk/audacious/output.c   |   76 ++++++++++++++++++++++++---------------------
+  trunk/audacious/prefswin.c |    4 --
+  2 files changed, 41 insertions(+), 39 deletions(-)
+
+
 2006-12-08 06:42:22 +0000  Yoshiki Yazawa <yaz@cc.rim.or.jp>
   revision [3135]
   Do not install signal handler for SIGSEGV if environmental variable
--- a/libaudacious/Makefile	Thu Dec 07 22:56:55 2006 -0800
+++ b/libaudacious/Makefile	Fri Dec 08 02:25:20 2006 -0800
@@ -20,7 +20,7 @@
 	-I../intl
 
 CONF_SRC = configdb_$(CONFIGDB_BACKEND).c
-VFS_SRC = vfs.c vfs_common.c
+VFS_SRC = vfs.c vfs_common.c vfs_buffer.c
 
 SOURCES = \
 	$(CONF_SRC) \
@@ -36,7 +36,7 @@
 OBJECTS = ${SOURCES:.c=.o}
 
 HEADERS = \
-	vfs.h rcfile.h configdb.h \
+	vfs.h vfs_buffer.h rcfile.h configdb.h \
 	beepctrl.h dirbrowser.h \
 	formatter.h titlestring.h xconvert.h
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libaudacious/vfs_buffer.c	Fri Dec 08 02:25:20 2006 -0800
@@ -0,0 +1,231 @@
+/*  Audacious
+ *  Copyright (c) 2006 William Pitcock
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
+ *  02110-1301, USA.
+ */
+
+#include <glib.h>
+#include "vfs.h"
+#include "vfs_buffer.h"
+
+VFSFile *
+buffer_vfs_fopen_impl(const gchar * path,
+          const gchar * mode)
+{
+    return NULL;
+}
+
+gint
+buffer_vfs_fclose_impl(VFSFile * file)
+{
+    g_return_val_if_fail(file != NULL, -1);
+
+    if (file->handle)
+        g_free(file->handle);
+
+    g_free(file);
+
+    return 0;
+}
+
+size_t
+buffer_vfs_fread_impl(gpointer i_ptr,
+          size_t size,
+          size_t nmemb,
+          VFSFile * file)
+{
+    VFSBuffer *handle;
+    guchar *i;
+    size_t read = 0;
+    guchar *ptr = (guchar *) i_ptr;
+
+    if (file == NULL)
+        return 0;
+
+    handle = (VFSBuffer *) file->handle;
+
+    for (i = ptr; i - ptr <= nmemb * size && i - ptr <= handle->size; i++, handle->iter++)
+    {
+       *i = *handle->iter;
+       read++;
+    }
+
+    return (read / nmemb);
+}
+
+size_t
+buffer_vfs_fwrite_impl(gconstpointer i_ptr,
+           size_t size,
+           size_t nmemb,
+           VFSFile * file)
+{
+    VFSBuffer *handle;
+    const guchar *i;
+    size_t written = 0;
+    const guchar *ptr = (const guchar *) i_ptr;
+
+    if (file == NULL)
+        return 0;
+
+    handle = (VFSBuffer *) file->handle;
+
+    for (i = ptr; (i - ptr) <= nmemb * size && (i - ptr) <= handle->size; i++, handle->iter++)
+    {
+       *handle->iter = *i;
+       written++;
+    }
+
+    return (written / nmemb);
+}
+
+gint
+buffer_vfs_getc_impl(VFSFile *stream)
+{
+  VFSBuffer *handle = (VFSBuffer *) stream->handle;
+  gint c;
+
+  c = *handle->iter;
+  handle->iter++;
+
+  return c;
+}
+
+gint
+buffer_vfs_ungetc_impl(gint c, VFSFile *stream)
+{
+  return -1;
+}
+
+gint
+buffer_vfs_fseek_impl(VFSFile * file,
+          glong offset,
+          gint whence)
+{
+    VFSBuffer *handle;
+
+    if (file == NULL)
+        return 0;
+
+    handle = (VFSBuffer *) file->handle;
+
+    switch(whence)
+    {
+       case SEEK_CUR:
+          handle->iter = handle->iter + offset;
+          break;
+       case SEEK_END:
+          handle->iter = handle->end;
+          break;
+       case SEEK_SET:
+       default:
+          handle->iter = handle->data + offset;
+          break;
+    }
+
+    return 0;
+}
+
+void
+buffer_vfs_rewind_impl(VFSFile * file)
+{
+    VFSBuffer *handle;
+
+    if (file == NULL)
+        return;
+
+    handle = (VFSBuffer *) file->handle;
+
+    handle->iter = handle->data;
+}
+
+glong
+buffer_vfs_ftell_impl(VFSFile * file)
+{
+    VFSBuffer *handle;
+
+    if (file == NULL)
+        return 0;
+
+    handle = (VFSBuffer *) file->handle;
+
+    return handle->iter - handle->data;
+}
+
+gboolean
+buffer_vfs_feof_impl(VFSFile * file)
+{
+    VFSBuffer *handle;
+
+    if (file == NULL)
+        return FALSE;
+
+    handle = (VFSBuffer *) file->handle;
+
+    return (gboolean) (handle->iter == handle->end);
+}
+
+gint
+buffer_vfs_truncate_impl(VFSFile * file, glong size)
+{
+    return 0;
+}
+
+VFSConstructor buffer_const = {
+	NULL,			// not a normal VFS class
+	buffer_vfs_fopen_impl,
+	buffer_vfs_fclose_impl,
+	buffer_vfs_fread_impl,
+	buffer_vfs_fwrite_impl,
+	buffer_vfs_getc_impl,
+	buffer_vfs_ungetc_impl,
+	buffer_vfs_fseek_impl,
+	buffer_vfs_rewind_impl,
+	buffer_vfs_ftell_impl,
+	buffer_vfs_feof_impl,
+	buffer_vfs_truncate_impl
+};
+
+VFSFile *
+vfs_buffer_new(gpointer data, gsize size)
+{
+    VFSFile *handle;
+    VFSBuffer *buffer;
+
+    g_return_val_if_fail(data != NULL, NULL);
+    g_return_val_if_fail(size > 0, NULL);
+
+    handle = g_new0(VFSFile, 1);
+    handle->uri == NULL;
+
+    buffer = g_new0(VFSBuffer, 1);
+    buffer->data = data;
+    buffer->iter = data;
+    buffer->end = data + size;
+    buffer->size = size;
+
+    handle->handle = buffer;
+    handle->base = buffer_const;
+
+    return handle;
+}
+
+VFSFile *
+vfs_buffer_new_from_string(gchar *str)
+{
+    g_return_val_if_fail(str != NULL, NULL);
+
+    return vfs_buffer_new(str, strlen(str));
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libaudacious/vfs_buffer.h	Fri Dec 08 02:25:20 2006 -0800
@@ -0,0 +1,71 @@
+/*  Audacious
+ *  Copyright (c) 2006 William Pitcock
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
+ *  02110-1301, USA.
+ */
+
+#ifndef AUDACIOUS_VFS_BUFFER_H
+#define AUDACIOUS_VFS_BUFFER_H
+
+#include <glib.h>
+#include <audacious/vfs.h>
+
+G_BEGIN_DECLS
+
+/**
+ * VFSBuffer:
+ * @data: The data inside the VFSBuffer.
+ * @iter: The current position of the VFS buffer iterator.
+ * @begin: The beginning of the memory segment that the VFS buffer uses.
+ * @end: The end of the memory segment that the VFS buffer uses.
+ * @size: The size of the memory segment.
+ *
+ * Private data for the VFS memorybuffer class.
+ **/
+
+typedef struct {
+	guchar *data;
+	guchar *iter;
+	guchar *end;
+	gsize   size;
+} VFSBuffer;
+
+/**
+ * vfs_buffer_new:
+ * @data: Pointer to data to use.
+ * @size: Size of data to use.
+ *
+ * Creates a VFS buffer for reading/writing to a memory segment.
+ *
+ * Return value: A VFSFile handle for the memory segment's stream 
+ *               representation.
+ **/
+VFSFile *vfs_buffer_new(gpointer data, gsize size);
+
+/**
+ * vfs_buffer_new_from_string:
+ * @str: String to use.
+ *
+ * Creates a VFS buffer for reading/writing to a string.
+ *
+ * Return value: A VFSFile handle for the memory segment's stream 
+ *               representation.
+ **/
+VFSFile *vfs_buffer_new_from_string(gchar *str);
+
+G_END_DECLS
+
+#endif