changeset 1975:976da06332df trunk

[svn] - newvfs code, part 1
author nenolod
date Thu, 16 Nov 2006 11:20:07 -0800
parents 9f6c17f1cc93
children 8045625b45d3
files ChangeLog libaudacious/Makefile libaudacious/vfs.c libaudacious/vfs.h libaudacious/vfs_stdio.c
diffstat 5 files changed, 243 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Thu Nov 16 10:53:21 2006 -0800
+++ b/ChangeLog	Thu Nov 16 11:20:07 2006 -0800
@@ -1,3 +1,11 @@
+2006-11-16 18:53:21 +0000  William Pitcock <nenolod@nenolod.net>
+  revision [2925]
+  - define VFSConstructor
+  
+  trunk/libaudacious/vfs.h |   36 ++++++++++++++++++++++++++++++++++++
+  1 file changed, 36 insertions(+)
+
+
 2006-11-14 20:37:14 +0000  William Pitcock <nenolod@nenolod.net>
   revision [2923]
   - reseek to 0 on a newvfs FD for each probe pass
--- a/libaudacious/Makefile	Thu Nov 16 10:53:21 2006 -0800
+++ b/libaudacious/Makefile	Thu Nov 16 11:20:07 2006 -0800
@@ -20,7 +20,7 @@
 	-I../intl
 
 CONF_SRC = configdb_$(CONFIGDB_BACKEND).c
-VFS_SRC = vfs_stdio.c vfs_common.c
+VFS_SRC = vfs.c vfs_stdio.c vfs_common.c
 
 SOURCES = \
 	$(CONF_SRC) \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libaudacious/vfs.c	Thu Nov 16 11:20:07 2006 -0800
@@ -0,0 +1,173 @@
+/*  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 "vfs.h"
+#include <stdio.h>
+
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+
+struct _VFSFile {
+    FILE *handle;
+};
+
+
+gboolean
+vfs_init(void)
+{
+    return TRUE;
+}
+
+VFSFile *
+vfs_fopen(const gchar * path,
+          const gchar * mode)
+{
+    VFSFile *file;
+
+    if (!path || !mode)
+	return NULL;
+
+    file = g_new(VFSFile, 1);
+
+    file->handle = fopen(path, mode);
+
+    if (file->handle == NULL) {
+        g_free(file);
+        file = NULL;
+    }
+
+    return file;
+}
+
+gint
+vfs_fclose(VFSFile * file)
+{
+    gint ret = 0;
+
+    if (file == NULL)
+        return -1;
+
+    if (file->handle) {
+        if (fclose(file->handle) != 0)
+            ret = -1;
+    }
+
+    g_free(file);
+
+    return ret;
+}
+
+size_t
+vfs_fread(gpointer ptr,
+          size_t size,
+          size_t nmemb,
+          VFSFile * file)
+{
+    if (file == NULL)
+        return 0;
+
+    return fread(ptr, size, nmemb, file->handle);
+}
+
+size_t
+vfs_fwrite(gconstpointer ptr,
+           size_t size,
+           size_t nmemb,
+           VFSFile * file)
+{
+    if (file == NULL)
+        return 0;
+
+    return fwrite(ptr, size, nmemb, file->handle);
+}
+
+gint
+vfs_getc(VFSFile *stream)
+{
+  return getc( stream->handle );
+}
+
+gint
+vfs_ungetc(gint c, VFSFile *stream)
+{
+  return ungetc( c , stream->handle );
+}
+
+gint
+vfs_fseek(VFSFile * file,
+          glong offset,
+          gint whence)
+{
+    if (file == NULL)
+        return 0;
+
+    return fseek(file->handle, offset, whence);
+}
+
+void
+vfs_rewind(VFSFile * file)
+{
+    if (file == NULL)
+        return;
+
+    rewind(file->handle);
+}
+
+glong
+vfs_ftell(VFSFile * file)
+{
+    if (file == NULL)
+        return 0;
+
+    return ftell(file->handle);
+}
+
+gboolean
+vfs_feof(VFSFile * file)
+{
+    if (file == NULL)
+        return FALSE;
+
+    return (gboolean) feof(file->handle);
+}
+
+gboolean
+vfs_file_test(const gchar * path, GFileTest test)
+{
+    return g_file_test(path, test);
+}
+
+/* NOTE: stat() is not part of stdio */
+gboolean
+vfs_is_writeable(const gchar * path)
+{
+    struct stat info;
+
+    if (stat(path, &info) == -1)
+        return FALSE;
+
+    return (info.st_mode & S_IWUSR);
+}
+
+gint
+vfs_truncate(VFSFile * file, glong size)
+{
+    if (file == NULL)
+        return -1;
+
+    return ftruncate(fileno(file->handle), size);
+}
--- a/libaudacious/vfs.h	Thu Nov 16 10:53:21 2006 -0800
+++ b/libaudacious/vfs.h	Thu Nov 16 11:20:07 2006 -0800
@@ -24,6 +24,13 @@
 #include <stdio.h>
 
 typedef struct _VFSFile VFSFile;
+typedef struct _VFSConstructor VFSConstructor;
+
+struct _VFSFile {
+	gchar *uri;
+	gpointer handle;
+	VFSConstructor *base;
+};
 
 struct _VFSConstructor {
 	gchar *uri_id;
@@ -32,6 +39,9 @@
 	gint (*vfs_fclose_impl)(VFSFile * file);
 	size_t (*vfs_fread_impl)(gpointer ptr, size_t size,
 		size_t nmemb, VFSFile *file);
+	size_t (*vfs_fwrite_impl)(gconstpointer ptr, size_t size,
+		size_t nmemb, VFSFile *file);
+	gint (*vfs_getc_impl)(VFSFile *stream);
 	gint (*vfs_ungetc_impl)(gint c, VFSFile *stream);
 	gint (*vfs_fseek_impl)(VFSFile *file, glong offset, gint whence);
 	void (*vfs_rewind_impl)(VFSFile *file);
@@ -40,8 +50,6 @@
 	gboolean (*vfs_truncate_impl)(VFSFile *file, glong length);
 };
 
-typedef struct _VFSConstructor VFSConstructor;
-
 G_BEGIN_DECLS
 
 /* Reserved for private use by BMP */
--- a/libaudacious/vfs_stdio.c	Thu Nov 16 10:53:21 2006 -0800
+++ b/libaudacious/vfs_stdio.c	Thu Nov 16 11:20:07 2006 -0800
@@ -20,20 +20,8 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 
-
-struct _VFSFile {
-    FILE *handle;
-};
-
-
-gboolean
-vfs_init(void)
-{
-    return TRUE;
-}
-
 VFSFile *
-vfs_fopen(const gchar * path,
+stdio_vfs_fopen_impl(const gchar * path,
           const gchar * mode)
 {
     VFSFile *file;
@@ -54,7 +42,7 @@
 }
 
 gint
-vfs_fclose(VFSFile * file)
+stdio_vfs_fclose_impl(VFSFile * file)
 {
     gint ret = 0;
 
@@ -72,7 +60,7 @@
 }
 
 size_t
-vfs_fread(gpointer ptr,
+stdio_vfs_fread_impl(gpointer ptr,
           size_t size,
           size_t nmemb,
           VFSFile * file)
@@ -84,7 +72,7 @@
 }
 
 size_t
-vfs_fwrite(gconstpointer ptr,
+stdio_vfs_fwrite_impl(gconstpointer ptr,
            size_t size,
            size_t nmemb,
            VFSFile * file)
@@ -96,19 +84,19 @@
 }
 
 gint
-vfs_getc(VFSFile *stream)
+stdio_vfs_getc_impl(VFSFile *stream)
 {
   return getc( stream->handle );
 }
 
 gint
-vfs_ungetc(gint c, VFSFile *stream)
+stdio_vfs_ungetc_impl(gint c, VFSFile *stream)
 {
   return ungetc( c , stream->handle );
 }
 
 gint
-vfs_fseek(VFSFile * file,
+stdio_vfs_fseek_impl(VFSFile * file,
           glong offset,
           gint whence)
 {
@@ -119,7 +107,7 @@
 }
 
 void
-vfs_rewind(VFSFile * file)
+stdio_vfs_rewind_impl(VFSFile * file)
 {
     if (file == NULL)
         return;
@@ -128,7 +116,7 @@
 }
 
 glong
-vfs_ftell(VFSFile * file)
+stdio_vfs_ftell_impl(VFSFile * file)
 {
     if (file == NULL)
         return 0;
@@ -137,7 +125,7 @@
 }
 
 gboolean
-vfs_feof(VFSFile * file)
+stdio_vfs_feof_impl(VFSFile * file)
 {
     if (file == NULL)
         return FALSE;
@@ -145,29 +133,51 @@
     return (gboolean) feof(file->handle);
 }
 
-gboolean
-vfs_file_test(const gchar * path, GFileTest test)
-{
-    return g_file_test(path, test);
-}
-
-/* NOTE: stat() is not part of stdio */
-gboolean
-vfs_is_writeable(const gchar * path)
-{
-    struct stat info;
-
-    if (stat(path, &info) == -1)
-        return FALSE;
-
-    return (info.st_mode & S_IWUSR);
-}
-
 gint
-vfs_truncate(VFSFile * file, glong size)
+stdio_vfs_truncate_impl(VFSFile * file, glong size)
 {
     if (file == NULL)
         return -1;
 
     return ftruncate(fileno(file->handle), size);
 }
+
+VFSConstructor file_const = {
+	"file://",
+	stdio_vfs_fopen_impl,
+	stdio_vfs_fclose_impl,
+	stdio_vfs_fread_impl,
+	stdio_vfs_fwrite_impl,
+	stdio_vfs_getc_impl,
+	stdio_vfs_ungetc_impl,
+	stdio_vfs_fseek_impl,
+	stdio_vfs_rewind_impl,
+	stdio_vfs_ftell_impl,
+	stdio_vfs_feof_impl,
+	stdio_vfs_truncate_impl
+};
+
+VFSConstructor default_const = {
+	"/",
+	stdio_vfs_fopen_impl,
+	stdio_vfs_fclose_impl,
+	stdio_vfs_fread_impl,
+	stdio_vfs_fwrite_impl,
+	stdio_vfs_getc_impl,
+	stdio_vfs_ungetc_impl,
+	stdio_vfs_fseek_impl,
+	stdio_vfs_rewind_impl,
+	stdio_vfs_ftell_impl,
+	stdio_vfs_feof_impl,
+	stdio_vfs_truncate_impl
+};
+
+#if 0
+gboolean
+vfs_init(void)
+{
+    vfs_register_transport(&default_const);
+    vfs_register_transport(&file_const);
+    return TRUE;
+}
+#endif