Mercurial > audlegacy-plugins
comparison src/gio/gio.c @ 3081:4e3712e142b4
gio: merge experimental GIO plugin. not everything is implemented yet (getc/ungetc).
| author | William Pitcock <nenolod@atheme.org> |
|---|---|
| date | Wed, 29 Apr 2009 19:26:49 -0500 |
| parents | |
| children | abb8604cb718 |
comparison
equal
deleted
inserted
replaced
| 3080:1f13c07c4d80 | 3081:4e3712e142b4 |
|---|---|
| 1 /* Audacious | |
| 2 * Copyright (c) 2009 William Pitcock | |
| 3 * | |
| 4 * This program is free software; you can redistribute it and/or modify | |
| 5 * it under the terms of the GNU General Public License as published by | |
| 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 | |
| 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
| 17 */ | |
| 18 | |
| 19 #include "config.h" | |
| 20 #include <audacious/plugin.h> | |
| 21 #include <stdio.h> | |
| 22 #include <gio/gio.h> | |
| 23 | |
| 24 #include <unistd.h> | |
| 25 #include <sys/stat.h> | |
| 26 #include <sys/types.h> | |
| 27 | |
| 28 #include <string.h> | |
| 29 | |
| 30 typedef struct { | |
| 31 GFile *file; | |
| 32 GFileInputStream *istream; | |
| 33 GFileOutputStream *ostream; | |
| 34 GSeekable *seekable; | |
| 35 } VFSGIOHandle; | |
| 36 | |
| 37 VFSFile * | |
| 38 gio_aud_vfs_fopen_impl(const gchar *path, const gchar *mode) | |
| 39 { | |
| 40 VFSFile *file; | |
| 41 VFSGIOHandle *handle; | |
| 42 GError *error = NULL; | |
| 43 | |
| 44 if (path == NULL || mode == NULL) | |
| 45 return NULL; | |
| 46 | |
| 47 handle = g_slice_new0(VFSGIOHandle); | |
| 48 handle->file = g_file_new_for_uri(path); | |
| 49 | |
| 50 if (*mode == 'r') | |
| 51 { | |
| 52 handle->istream = g_file_read(handle->file, NULL, &error); | |
| 53 handle->seekable = G_SEEKABLE(handle->istream); | |
| 54 } | |
| 55 else if (*mode == 'w') | |
| 56 { | |
| 57 handle->ostream = g_file_replace(handle->file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error); | |
| 58 handle->seekable = G_SEEKABLE(handle->ostream); | |
| 59 } | |
| 60 else | |
| 61 { | |
| 62 g_warning("UNSUPPORTED ACCESS MODE: %s", mode); | |
| 63 g_object_unref(handle->file); | |
| 64 g_slice_free(VFSGIOHandle, handle); | |
| 65 return NULL; | |
| 66 } | |
| 67 | |
| 68 if (handle->istream == NULL && handle->ostream == NULL) | |
| 69 { | |
| 70 g_warning("Could not open %s for reading or writing: %s", path, error->message); | |
| 71 g_object_unref(handle->file); | |
| 72 g_slice_free(VFSGIOHandle, handle); | |
| 73 g_error_free(error); | |
| 74 return NULL; | |
| 75 } | |
| 76 | |
| 77 file = g_new(VFSFile, 1); | |
| 78 file->handle = handle; | |
| 79 | |
| 80 return file; | |
| 81 } | |
| 82 | |
| 83 gint | |
| 84 gio_aud_vfs_fclose_impl(VFSFile * file) | |
| 85 { | |
| 86 gint ret = 0; | |
| 87 | |
| 88 if (file == NULL) | |
| 89 return -1; | |
| 90 | |
| 91 if (file->handle) | |
| 92 { | |
| 93 VFSGIOHandle *handle = (VFSGIOHandle *) file->handle; | |
| 94 | |
| 95 g_object_unref(handle->file); | |
| 96 g_slice_free(VFSGIOHandle, handle); | |
| 97 | |
| 98 file->handle = NULL; | |
| 99 } | |
| 100 | |
| 101 return ret; | |
| 102 } | |
| 103 | |
| 104 size_t | |
| 105 gio_aud_vfs_fread_impl(gpointer ptr, | |
| 106 size_t size, | |
| 107 size_t nmemb, | |
| 108 VFSFile * file) | |
| 109 { | |
| 110 VFSGIOHandle *handle; | |
| 111 | |
| 112 if (file == NULL) | |
| 113 return 0; | |
| 114 | |
| 115 handle = (VFSGIOHandle *) file->handle; | |
| 116 | |
| 117 return g_input_stream_read(G_INPUT_STREAM(handle->istream), ptr, size * nmemb, NULL, NULL); | |
| 118 } | |
| 119 | |
| 120 size_t | |
| 121 gio_aud_vfs_fwrite_impl(gconstpointer ptr, | |
| 122 size_t size, | |
| 123 size_t nmemb, | |
| 124 VFSFile * file) | |
| 125 { | |
| 126 VFSGIOHandle *handle; | |
| 127 | |
| 128 if (file == NULL) | |
| 129 return 0; | |
| 130 | |
| 131 handle = (VFSGIOHandle *) file->handle; | |
| 132 | |
| 133 return g_output_stream_write(G_OUTPUT_STREAM(handle->ostream), ptr, size * nmemb, NULL, NULL); | |
| 134 } | |
| 135 | |
| 136 gint | |
| 137 gio_aud_vfs_getc_impl(VFSFile *file) | |
| 138 { | |
| 139 guchar buf[1]; | |
| 140 VFSGIOHandle *handle; | |
| 141 | |
| 142 if (file == NULL) | |
| 143 return -1; | |
| 144 | |
| 145 handle = (VFSGIOHandle *) file->handle; | |
| 146 | |
| 147 g_input_stream_read(G_INPUT_STREAM(handle->istream), &buf, 1, NULL, NULL); | |
| 148 | |
| 149 return *buf; | |
| 150 } | |
| 151 | |
| 152 gint | |
| 153 gio_aud_vfs_ungetc_impl(gint c, VFSFile * file) | |
| 154 { | |
| 155 g_print("ungetc(): unimplemented function!\n"); | |
| 156 return 0; | |
| 157 #if 0 | |
| 158 VFSGIOHandle *handle; | |
| 159 | |
| 160 if (file == NULL) | |
| 161 return -1; | |
| 162 | |
| 163 handle = (VFSGIOHandle *) file->handle; | |
| 164 | |
| 165 return ungetc(c, handle); | |
| 166 #endif | |
| 167 } | |
| 168 | |
| 169 gint | |
| 170 gio_aud_vfs_fseek_impl(VFSFile * file, | |
| 171 glong offset, | |
| 172 gint whence) | |
| 173 { | |
| 174 VFSGIOHandle *handle; | |
| 175 GSeekType seektype; | |
| 176 | |
| 177 if (file == NULL) | |
| 178 return 0; | |
| 179 | |
| 180 handle = (VFSGIOHandle *) file->handle; | |
| 181 | |
| 182 if (!g_seekable_can_seek(handle->seekable)) | |
| 183 return 0; | |
| 184 | |
| 185 switch (whence) | |
| 186 { | |
| 187 case SEEK_CUR: | |
| 188 seektype = G_SEEK_CUR; | |
| 189 break; | |
| 190 case SEEK_END: | |
| 191 seektype = G_SEEK_END; | |
| 192 break; | |
| 193 default: | |
| 194 seektype = G_SEEK_SET; | |
| 195 break; | |
| 196 } | |
| 197 | |
| 198 return g_seekable_seek(handle->seekable, seektype, offset, NULL, NULL); | |
| 199 } | |
| 200 | |
| 201 void | |
| 202 gio_aud_vfs_rewind_impl(VFSFile * file) | |
| 203 { | |
| 204 if (file == NULL) | |
| 205 return; | |
| 206 | |
| 207 file->base->vfs_fseek_impl(file, 0, SEEK_SET); | |
| 208 } | |
| 209 | |
| 210 glong | |
| 211 gio_aud_vfs_ftell_impl(VFSFile * file) | |
| 212 { | |
| 213 VFSGIOHandle *handle; | |
| 214 | |
| 215 if (file == NULL) | |
| 216 return 0; | |
| 217 | |
| 218 handle = (VFSGIOHandle *) file->handle; | |
| 219 | |
| 220 return (glong) g_seekable_tell(handle->seekable); | |
| 221 } | |
| 222 | |
| 223 gboolean | |
| 224 gio_aud_vfs_feof_impl(VFSFile * file) | |
| 225 { | |
| 226 return (file->base->vfs_ftell_impl(file) == file->base->vfs_fsize_impl(file)); | |
| 227 } | |
| 228 | |
| 229 gint | |
| 230 gio_aud_vfs_truncate_impl(VFSFile * file, glong size) | |
| 231 { | |
| 232 VFSGIOHandle *handle; | |
| 233 | |
| 234 if (file == NULL) | |
| 235 return -1; | |
| 236 | |
| 237 handle = (VFSGIOHandle *) file->handle; | |
| 238 | |
| 239 return g_seekable_truncate(handle->seekable, size, NULL, NULL); | |
| 240 } | |
| 241 | |
| 242 off_t | |
| 243 gio_aud_vfs_fsize_impl(VFSFile * file) | |
| 244 { | |
| 245 GFileInfo *info; | |
| 246 VFSGIOHandle *handle; | |
| 247 GError *error = NULL; | |
| 248 goffset size; | |
| 249 | |
| 250 if (file == NULL) | |
| 251 return -1; | |
| 252 | |
| 253 handle = (VFSGIOHandle *) file->handle; | |
| 254 info = g_file_query_info(handle->file, G_FILE_ATTRIBUTE_STANDARD_SIZE, G_FILE_QUERY_INFO_NONE, NULL, &error); | |
| 255 | |
| 256 if (info == NULL) | |
| 257 { | |
| 258 g_warning("gio fsize(): error: %s", error->message); | |
| 259 g_error_free(error); | |
| 260 return -1; | |
| 261 } | |
| 262 | |
| 263 size = g_file_info_get_attribute_uint64(info, G_FILE_ATTRIBUTE_STANDARD_SIZE); | |
| 264 g_object_unref(info); | |
| 265 | |
| 266 return size; | |
| 267 } | |
| 268 | |
| 269 VFSConstructor file_const = { | |
| 270 .uri_id = "file://", | |
| 271 .vfs_fopen_impl = gio_aud_vfs_fopen_impl, | |
| 272 .vfs_fclose_impl = gio_aud_vfs_fclose_impl, | |
| 273 .vfs_fread_impl = gio_aud_vfs_fread_impl, | |
| 274 .vfs_fwrite_impl = gio_aud_vfs_fwrite_impl, | |
| 275 .vfs_getc_impl = gio_aud_vfs_getc_impl, | |
| 276 .vfs_ungetc_impl = gio_aud_vfs_ungetc_impl, | |
| 277 .vfs_fseek_impl = gio_aud_vfs_fseek_impl, | |
| 278 .vfs_rewind_impl = gio_aud_vfs_rewind_impl, | |
| 279 .vfs_ftell_impl = gio_aud_vfs_ftell_impl, | |
| 280 .vfs_feof_impl = gio_aud_vfs_feof_impl, | |
| 281 .vfs_truncate_impl = gio_aud_vfs_truncate_impl, | |
| 282 .vfs_fsize_impl = gio_aud_vfs_fsize_impl | |
| 283 }; | |
| 284 | |
| 285 static void init(void) | |
| 286 { | |
| 287 aud_vfs_register_transport(&file_const); | |
| 288 } | |
| 289 | |
| 290 static void cleanup(void) | |
| 291 { | |
| 292 #if 0 | |
| 293 aud_vfs_unregister_transport(&file_const); | |
| 294 #endif | |
| 295 } | |
| 296 | |
| 297 DECLARE_PLUGIN(gio, init, cleanup, NULL, NULL, NULL, NULL, NULL, NULL); |
