Mercurial > audlegacy
annotate libaudacious/vfs.c @ 1999:dcea7c329ad6 trunk
[svn] - don't memory leak the uri
author | nenolod |
---|---|
date | Thu, 23 Nov 2006 20:10:03 -0800 |
parents | 1e0503521702 |
children | ebce0d5efac1 |
rev | line source |
---|---|
1997 | 1 /* Audacious |
2 * Copyright (c) 2006 William Pitcock | |
3 * | |
4 * This program is free software; you can redistribute it and/or modify | |
1460 | 5 * it under the terms of the GNU General Public License as published by |
0 | 6 * the Free Software Foundation; either version 2 of the License, or |
7 * (at your option) any later version. | |
8 * | |
9 * This program is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
15 * along with this program; if not, write to the Free Software | |
1459 | 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
0 | 17 */ |
18 | |
19 #include "vfs.h" | |
20 #include <stdio.h> | |
21 | |
22 #include <unistd.h> | |
23 #include <sys/stat.h> | |
24 #include <sys/types.h> | |
25 | |
1997 | 26 static GList *vfs_transports = NULL; |
27 | |
28 #ifdef VFS_DEBUG | |
29 # define DBG(x, args...) g_print(x, ## args); | |
30 #else | |
31 # define DBG(x, args...) | |
32 #endif | |
33 | |
0 | 34 gboolean |
1997 | 35 vfs_register_transport(VFSConstructor *vtable) |
0 | 36 { |
1997 | 37 vfs_transports = g_list_append(vfs_transports, vtable); |
38 | |
0 | 39 return TRUE; |
40 } | |
41 | |
42 VFSFile * | |
43 vfs_fopen(const gchar * path, | |
44 const gchar * mode) | |
45 { | |
46 VFSFile *file; | |
1997 | 47 gchar **vec; |
48 VFSConstructor *vtable = NULL; | |
49 GList *node; | |
0 | 50 |
550 | 51 if (!path || !mode) |
52 return NULL; | |
53 | |
1997 | 54 vec = g_strsplit(path, "://", 2); |
55 | |
56 /* special case: no transport specified, look for the "/" transport */ | |
57 if (vec[1] == NULL) | |
58 { | |
59 for (node = vfs_transports; node != NULL; node = g_list_next(node)) | |
60 { | |
61 vtable = (VFSConstructor *) node->data; | |
62 | |
63 if (*vtable->uri_id == '/') | |
64 break; | |
65 } | |
66 } | |
67 else | |
68 { | |
69 for (node = vfs_transports; node != NULL; node = g_list_next(node)) | |
70 { | |
71 vtable = (VFSConstructor *) node->data; | |
0 | 72 |
1997 | 73 if (!g_strcasecmp(vec[0], vtable->uri_id)) |
74 break; | |
75 } | |
76 } | |
77 | |
78 /* no transport vtable has been registered, bail. */ | |
79 if (vtable == NULL) | |
80 { | |
81 return NULL; | |
0 | 82 } |
83 | |
1997 | 84 file = vtable->vfs_fopen_impl(vec[1] ? vec[1] : vec[0], mode); |
85 | |
86 if (file == NULL) | |
87 { | |
88 return NULL; | |
89 } | |
90 | |
91 file->uri = g_strdup(path); | |
92 file->base = vtable; | |
93 | |
0 | 94 return file; |
95 } | |
96 | |
97 gint | |
98 vfs_fclose(VFSFile * file) | |
99 { | |
100 gint ret = 0; | |
101 | |
821 | 102 if (file == NULL) |
103 return -1; | |
104 | |
1997 | 105 if (file->base->vfs_fclose_impl(file) != 0) |
106 ret = -1; | |
0 | 107 |
1999 | 108 if (file->uri != NULL) |
109 g_free(file->uri); | |
110 | |
0 | 111 g_free(file); |
112 | |
113 return ret; | |
114 } | |
115 | |
116 size_t | |
117 vfs_fread(gpointer ptr, | |
118 size_t size, | |
119 size_t nmemb, | |
120 VFSFile * file) | |
121 { | |
821 | 122 if (file == NULL) |
123 return 0; | |
124 | |
1997 | 125 return file->base->vfs_fread_impl(ptr, size, nmemb, file); |
0 | 126 } |
127 | |
128 size_t | |
129 vfs_fwrite(gconstpointer ptr, | |
130 size_t size, | |
131 size_t nmemb, | |
132 VFSFile * file) | |
133 { | |
821 | 134 if (file == NULL) |
135 return 0; | |
136 | |
1997 | 137 return file->base->vfs_fwrite_impl(ptr, size, nmemb, file); |
0 | 138 } |
139 | |
140 gint | |
1683
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
141 vfs_getc(VFSFile *stream) |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
142 { |
1997 | 143 if (stream == NULL) |
144 return -1; | |
145 | |
146 return stream->base->vfs_getc_impl(stream); | |
1683
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
147 } |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
148 |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
149 gint |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
150 vfs_ungetc(gint c, VFSFile *stream) |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
151 { |
1997 | 152 if (stream == NULL) |
153 return -1; | |
154 | |
155 return stream->base->vfs_ungetc_impl(c, stream); | |
1683
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
156 } |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
157 |
e9c24e35bd76
[svn] - File stream API for audacious vfs; uses real getc/ungetc functions for vfs_stdio and emulated functions for vfs_gnome
giacomo
parents:
1460
diff
changeset
|
158 gint |
0 | 159 vfs_fseek(VFSFile * file, |
160 glong offset, | |
161 gint whence) | |
162 { | |
821 | 163 if (file == NULL) |
164 return 0; | |
165 | |
1997 | 166 return file->base->vfs_fseek_impl(file, offset, whence); |
0 | 167 } |
168 | |
169 void | |
170 vfs_rewind(VFSFile * file) | |
171 { | |
821 | 172 if (file == NULL) |
173 return; | |
174 | |
1997 | 175 file->base->vfs_rewind_impl(file); |
0 | 176 } |
177 | |
178 glong | |
179 vfs_ftell(VFSFile * file) | |
180 { | |
821 | 181 if (file == NULL) |
182 return 0; | |
183 | |
1997 | 184 return file->base->vfs_ftell_impl(file); |
0 | 185 } |
186 | |
187 gboolean | |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
188 vfs_feof(VFSFile * file) |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
189 { |
821 | 190 if (file == NULL) |
191 return FALSE; | |
192 | |
1997 | 193 return (gboolean) file->base->vfs_feof_impl(file); |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
194 } |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
195 |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
196 gboolean |
0 | 197 vfs_file_test(const gchar * path, GFileTest test) |
198 { | |
199 return g_file_test(path, test); | |
200 } | |
201 | |
202 /* NOTE: stat() is not part of stdio */ | |
203 gboolean | |
204 vfs_is_writeable(const gchar * path) | |
205 { | |
206 struct stat info; | |
207 | |
208 if (stat(path, &info) == -1) | |
209 return FALSE; | |
210 | |
211 return (info.st_mode & S_IWUSR); | |
212 } | |
213 | |
214 gint | |
215 vfs_truncate(VFSFile * file, glong size) | |
216 { | |
821 | 217 if (file == NULL) |
218 return -1; | |
219 | |
1997 | 220 return file->base->vfs_truncate_impl(file, size); |
0 | 221 } |