Mercurial > audlegacy
comparison libaudacious/vfs.c @ 1997:93c59698f5fd trunk
[svn] - NewVFS lives ;)
author | nenolod |
---|---|
date | Thu, 23 Nov 2006 20:06:19 -0800 |
parents | 8045625b45d3 |
children | 1e0503521702 |
comparison
equal
deleted
inserted
replaced
1996:1abdcfc557d8 | 1997:93c59698f5fd |
---|---|
1 /* This program is free software; you can redistribute it and/or modify | 1 /* Audacious |
2 * Copyright (c) 2006 William Pitcock | |
3 * | |
4 * This program is free software; you can redistribute it and/or modify | |
2 * it under the terms of the GNU General Public License as published by | 5 * it under the terms of the GNU General Public License as published by |
3 * the Free Software Foundation; either version 2 of the License, or | 6 * the Free Software Foundation; either version 2 of the License, or |
4 * (at your option) any later version. | 7 * (at your option) any later version. |
5 * | 8 * |
6 * This program is distributed in the hope that it will be useful, | 9 * This program is distributed in the hope that it will be useful, |
18 | 21 |
19 #include <unistd.h> | 22 #include <unistd.h> |
20 #include <sys/stat.h> | 23 #include <sys/stat.h> |
21 #include <sys/types.h> | 24 #include <sys/types.h> |
22 | 25 |
23 gboolean | 26 static GList *vfs_transports = NULL; |
24 vfs_init(void) | 27 |
25 { | 28 #define VFS_DEBUG |
29 | |
30 #ifdef VFS_DEBUG | |
31 # define DBG(x, args...) g_print(x, ## args); | |
32 #else | |
33 # define DBG(x, args...) | |
34 #endif | |
35 | |
36 gboolean | |
37 vfs_register_transport(VFSConstructor *vtable) | |
38 { | |
39 DBG("registering transport [%s]\n", vtable->uri_id); | |
40 | |
41 vfs_transports = g_list_append(vfs_transports, vtable); | |
42 | |
26 return TRUE; | 43 return TRUE; |
27 } | 44 } |
28 | 45 |
29 VFSFile * | 46 VFSFile * |
30 vfs_fopen(const gchar * path, | 47 vfs_fopen(const gchar * path, |
31 const gchar * mode) | 48 const gchar * mode) |
32 { | 49 { |
33 VFSFile *file; | 50 VFSFile *file; |
51 gchar **vec; | |
52 VFSConstructor *vtable = NULL; | |
53 GList *node; | |
34 | 54 |
35 if (!path || !mode) | 55 if (!path || !mode) |
36 return NULL; | 56 return NULL; |
37 | 57 |
38 file = g_new(VFSFile, 1); | 58 vec = g_strsplit(path, "://", 2); |
39 | 59 |
40 file->handle = fopen(path, mode); | 60 DBG("vec[0]: %s, vec[1]: %s\n", vec[0], vec[1]); |
41 | 61 |
42 if (file->handle == NULL) { | 62 /* special case: no transport specified, look for the "/" transport */ |
43 g_free(file); | 63 if (vec[1] == NULL) |
44 file = NULL; | 64 { |
45 } | 65 for (node = vfs_transports; node != NULL; node = g_list_next(node)) |
66 { | |
67 vtable = (VFSConstructor *) node->data; | |
68 | |
69 if (*vtable->uri_id == '/') | |
70 break; | |
71 } | |
72 } | |
73 else | |
74 { | |
75 for (node = vfs_transports; node != NULL; node = g_list_next(node)) | |
76 { | |
77 vtable = (VFSConstructor *) node->data; | |
78 | |
79 if (!g_strcasecmp(vec[0], vtable->uri_id)) | |
80 break; | |
81 } | |
82 } | |
83 | |
84 /* no transport vtable has been registered, bail. */ | |
85 if (vtable == NULL) | |
86 { | |
87 return NULL; | |
88 } | |
89 | |
90 file = vtable->vfs_fopen_impl(vec[1] ? vec[1] : vec[0], mode); | |
91 | |
92 if (file == NULL) | |
93 { | |
94 return NULL; | |
95 } | |
96 | |
97 file->uri = g_strdup(path); | |
98 file->base = vtable; | |
99 | |
100 DBG("returning %p", file); | |
46 | 101 |
47 return file; | 102 return file; |
48 } | 103 } |
49 | 104 |
50 gint | 105 gint |
53 gint ret = 0; | 108 gint ret = 0; |
54 | 109 |
55 if (file == NULL) | 110 if (file == NULL) |
56 return -1; | 111 return -1; |
57 | 112 |
58 if (file->handle) { | 113 if (file->base->vfs_fclose_impl(file) != 0) |
59 if (fclose(file->handle) != 0) | 114 ret = -1; |
60 ret = -1; | |
61 } | |
62 | 115 |
63 g_free(file); | 116 g_free(file); |
64 | 117 |
65 return ret; | 118 return ret; |
66 } | 119 } |
72 VFSFile * file) | 125 VFSFile * file) |
73 { | 126 { |
74 if (file == NULL) | 127 if (file == NULL) |
75 return 0; | 128 return 0; |
76 | 129 |
77 return fread(ptr, size, nmemb, file->handle); | 130 return file->base->vfs_fread_impl(ptr, size, nmemb, file); |
78 } | 131 } |
79 | 132 |
80 size_t | 133 size_t |
81 vfs_fwrite(gconstpointer ptr, | 134 vfs_fwrite(gconstpointer ptr, |
82 size_t size, | 135 size_t size, |
84 VFSFile * file) | 137 VFSFile * file) |
85 { | 138 { |
86 if (file == NULL) | 139 if (file == NULL) |
87 return 0; | 140 return 0; |
88 | 141 |
89 return fwrite(ptr, size, nmemb, file->handle); | 142 return file->base->vfs_fwrite_impl(ptr, size, nmemb, file); |
90 } | 143 } |
91 | 144 |
92 gint | 145 gint |
93 vfs_getc(VFSFile *stream) | 146 vfs_getc(VFSFile *stream) |
94 { | 147 { |
95 return getc( stream->handle ); | 148 if (stream == NULL) |
149 return -1; | |
150 | |
151 return stream->base->vfs_getc_impl(stream); | |
96 } | 152 } |
97 | 153 |
98 gint | 154 gint |
99 vfs_ungetc(gint c, VFSFile *stream) | 155 vfs_ungetc(gint c, VFSFile *stream) |
100 { | 156 { |
101 return ungetc( c , stream->handle ); | 157 if (stream == NULL) |
158 return -1; | |
159 | |
160 return stream->base->vfs_ungetc_impl(c, stream); | |
102 } | 161 } |
103 | 162 |
104 gint | 163 gint |
105 vfs_fseek(VFSFile * file, | 164 vfs_fseek(VFSFile * file, |
106 glong offset, | 165 glong offset, |
107 gint whence) | 166 gint whence) |
108 { | 167 { |
109 if (file == NULL) | 168 if (file == NULL) |
110 return 0; | 169 return 0; |
111 | 170 |
112 return fseek(file->handle, offset, whence); | 171 return file->base->vfs_fseek_impl(file, offset, whence); |
113 } | 172 } |
114 | 173 |
115 void | 174 void |
116 vfs_rewind(VFSFile * file) | 175 vfs_rewind(VFSFile * file) |
117 { | 176 { |
118 if (file == NULL) | 177 if (file == NULL) |
119 return; | 178 return; |
120 | 179 |
121 rewind(file->handle); | 180 file->base->vfs_rewind_impl(file); |
122 } | 181 } |
123 | 182 |
124 glong | 183 glong |
125 vfs_ftell(VFSFile * file) | 184 vfs_ftell(VFSFile * file) |
126 { | 185 { |
127 if (file == NULL) | 186 if (file == NULL) |
128 return 0; | 187 return 0; |
129 | 188 |
130 return ftell(file->handle); | 189 return file->base->vfs_ftell_impl(file); |
131 } | 190 } |
132 | 191 |
133 gboolean | 192 gboolean |
134 vfs_feof(VFSFile * file) | 193 vfs_feof(VFSFile * file) |
135 { | 194 { |
136 if (file == NULL) | 195 if (file == NULL) |
137 return FALSE; | 196 return FALSE; |
138 | 197 |
139 return (gboolean) feof(file->handle); | 198 return (gboolean) file->base->vfs_feof_impl(file); |
140 } | 199 } |
141 | 200 |
142 gboolean | 201 gboolean |
143 vfs_file_test(const gchar * path, GFileTest test) | 202 vfs_file_test(const gchar * path, GFileTest test) |
144 { | 203 { |
161 vfs_truncate(VFSFile * file, glong size) | 220 vfs_truncate(VFSFile * file, glong size) |
162 { | 221 { |
163 if (file == NULL) | 222 if (file == NULL) |
164 return -1; | 223 return -1; |
165 | 224 |
166 return ftruncate(fileno(file->handle), size); | 225 return file->base->vfs_truncate_impl(file, size); |
167 } | 226 } |