1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
5 * Copyright (C) 2007 Dom Lachowicz <cinamod@hotmail.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2.1 of the GNU Lesser General Public
9 * License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22 #include <gsf-config.h>
23 #include <gsf/gsf-input-gio.h>
24 #include <gsf/gsf-input-memory.h>
25 #include <gsf/gsf-output-memory.h>
26 #include <gsf/gsf-input-impl.h>
27 #include <gsf/gsf-impl-utils.h>
39 GsfInputClass input_class;
43 can_seek (GInputStream *stream)
45 if (!G_IS_SEEKABLE (stream))
48 return g_seekable_can_seek (G_SEEKABLE (stream));
52 make_local_copy (GFile *file, GInputStream *stream)
58 out = gsf_output_memory_new ();
64 nread = g_input_stream_read (stream, buf, sizeof(buf), NULL, NULL);
67 if (!gsf_output_write (out, nread, buf)) {
69 goto cleanup_and_exit;
76 goto cleanup_and_exit;
80 copy = gsf_input_memory_new_clone
81 (gsf_output_memory_get_bytes (GSF_OUTPUT_MEMORY (out)),
82 gsf_output_size (out));
85 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, NULL);
87 gsf_input_set_name (GSF_INPUT (copy), g_file_info_get_name (info));
88 g_object_unref (info);
94 gsf_output_close (out);
97 g_input_stream_close (stream, NULL, NULL);
98 g_object_unref (stream);
105 * @err: optionally NULL.
107 * Returns: A new #GsfInputGio or NULL
110 gsf_input_gio_new (GFile *file, GError **err)
113 GInputStream *stream;
116 g_return_val_if_fail (file != NULL, NULL);
118 stream = (GInputStream *)g_file_read (file, NULL, err);
122 if (!can_seek (stream))
123 return make_local_copy (file, stream);
125 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_SIZE, 0, NULL, NULL);
127 return make_local_copy (file, stream);
129 input = g_object_new (GSF_INPUT_GIO_TYPE, NULL);
130 if (G_UNLIKELY (NULL == input)) {
131 g_input_stream_close (stream, NULL, NULL);
132 g_object_unref (stream);
136 gsf_input_set_size (GSF_INPUT (input), g_file_info_get_size (info));
137 g_object_unref (info);
139 g_object_ref (G_OBJECT (file));
141 input->stream = stream;
146 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, NULL);
148 gsf_input_set_name (GSF_INPUT (input), g_file_info_get_name (info));
149 g_object_unref (info);
152 return GSF_INPUT (input);
156 * gsf_input_gio_new_for_path:
158 * @err: optionally NULL.
160 * Returns: A new #GsfInputGio or NULL
163 gsf_input_gio_new_for_path (char const *path, GError **err)
168 g_return_val_if_fail (path != NULL, NULL);
170 file = g_file_new_for_path (path);
171 input = gsf_input_gio_new (file, err);
172 g_object_unref (file);
178 * gsf_input_gio_new_for_uri:
180 * @err: optionally NULL.
182 * Returns: A new #GsfInputGio or NULL
185 gsf_input_gio_new_for_uri (char const *uri, GError **err)
190 g_return_val_if_fail (uri != NULL, NULL);
192 file = g_file_new_for_uri (uri);
193 input = gsf_input_gio_new (file, err);
194 g_object_unref (file);
200 gsf_input_gio_finalize (GObject *obj)
202 GObjectClass *parent_class;
203 GsfInputGio *input = (GsfInputGio *)obj;
205 g_input_stream_close (input->stream, NULL, NULL);
206 g_object_unref (input->stream);
207 input->stream = NULL;
209 g_object_unref (input->file);
212 if (input->buf != NULL) {
218 parent_class = g_type_class_peek (GSF_INPUT_TYPE);
219 if (parent_class && parent_class->finalize)
220 parent_class->finalize (obj);
224 gsf_input_gio_dup (GsfInput *src_input, GError **err)
226 GsfInputGio *src = (GsfInputGio *)src_input;
229 g_return_val_if_fail (src_input != NULL, NULL);
230 g_return_val_if_fail (src->file != NULL, NULL);
232 clone = g_file_dup (src->file);
234 GsfInput *dst = gsf_input_gio_new (clone, err);
237 * gsf_input_gio_new() adds a ref, or fails to create a new
238 * file. in any case, we need to unref the clone
240 g_object_unref (clone);
248 static guint8 const *
249 gsf_input_gio_read (GsfInput *input, size_t num_bytes, guint8 *buffer)
251 GsfInputGio *gio = GSF_INPUT_GIO (input);
252 size_t total_read = 0;
254 g_return_val_if_fail (gio != NULL, NULL);
255 g_return_val_if_fail (gio->stream != NULL, NULL);
257 if (buffer == NULL) {
258 if (gio->buf_size < num_bytes) {
259 gio->buf_size = num_bytes;
261 gio->buf = g_new (guint8, gio->buf_size);
269 nread = g_input_stream_read (gio->stream, (buffer + total_read), (num_bytes - total_read), NULL, NULL);
273 if ((size_t) total_read == num_bytes) {
284 gsf_input_gio_seek (GsfInput *input, gsf_off_t offset, GSeekType whence)
286 GsfInputGio *gio = GSF_INPUT_GIO (input);
288 g_return_val_if_fail (gio != NULL, TRUE);
289 g_return_val_if_fail (gio->stream != NULL, TRUE);
290 g_return_val_if_fail (can_seek (gio->stream), TRUE);
292 return (g_seekable_seek (G_SEEKABLE (gio->stream), offset, whence, NULL, NULL) ? FALSE : TRUE);
296 gsf_input_gio_init (GObject *obj)
298 GsfInputGio *gio = GSF_INPUT_GIO (obj);
307 gsf_input_gio_class_init (GObjectClass *gobject_class)
309 GsfInputClass *input_class = GSF_INPUT_CLASS (gobject_class);
311 gobject_class->finalize = gsf_input_gio_finalize;
312 input_class->Dup = gsf_input_gio_dup;
313 input_class->Read = gsf_input_gio_read;
314 input_class->Seek = gsf_input_gio_seek;
317 GSF_CLASS (GsfInputGio, gsf_input_gio,
318 gsf_input_gio_class_init, gsf_input_gio_init, GSF_INPUT_TYPE)
320 /***************************************************************************/
321 /***************************************************************************/