1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * gsf-input-stdio.c: stdio based input
5 * Copyright (C) 2002-2006 Jody Goldberg (jody@gnome.org)
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-stdio.h>
24 #include <gsf/gsf-input-impl.h>
25 #include <gsf/gsf-impl-utils.h>
26 #include <gsf/gsf-utils.h>
27 #include <gsf/gsf-input-memory.h>
28 #include <gsf/gsf-output-memory.h>
29 #include <glib/gstdio.h>
36 #include <sys/types.h>
39 static GObjectClass *parent_class;
41 struct _GsfInputStdio {
52 GsfInputClass input_class;
56 make_local_copy (FILE *stream, const char *filename, GError **err)
59 GsfInput *copy = NULL;
61 out = gsf_output_memory_new ();
67 nread = fread (buf, 1, sizeof(buf), stream);
70 if (!gsf_output_write (out, nread, buf))
72 } else if (nread == 0)
78 copy = gsf_input_memory_new_clone
79 (gsf_output_memory_get_bytes (GSF_OUTPUT_MEMORY (out)),
80 gsf_output_size (out));
82 gsf_output_close (out);
86 gsf_input_set_name_from_filename (GSF_INPUT (copy), filename);
92 char *utf8name = filename
93 ? g_filename_display_name (filename)
95 g_set_error (err, gsf_input_error_id (), 0,
96 "%s: not a regular file",
101 gsf_output_close (out);
102 g_object_unref (out);
108 * gsf_input_stdio_new :
109 * @filename : in utf8.
110 * @err : optionally NULL.
112 * Returns: a new file or NULL.
114 /* coverity[ -tainted_string_sink_content : arg-0 ] */
116 gsf_input_stdio_new (char const *filename, GError **err)
118 GsfInputStdio *input;
123 g_return_val_if_fail (filename != NULL, NULL);
125 file = g_fopen (filename, "rb");
128 int save_errno = errno;
129 char *utf8name = g_filename_display_name (filename);
132 g_file_error_from_errno (save_errno),
134 utf8name, g_strerror (save_errno));
140 if (fstat (fileno (file), &st) < 0 || !S_ISREG (st.st_mode)) {
141 GsfInput *res = make_local_copy (file, filename, err);
147 input = (GsfInputStdio *)g_object_new (GSF_INPUT_STDIO_TYPE, NULL);
148 if (G_UNLIKELY (NULL == input)) {
154 input->filename = g_strdup (filename);
157 input->keep_open = FALSE;
158 gsf_input_set_size (GSF_INPUT (input), size);
159 gsf_input_set_name_from_filename (GSF_INPUT (input), filename);
161 return GSF_INPUT (input);
165 * gsf_input_stdio_new_FILE :
166 * @filename : The filename corresponding to @file.
167 * @file : an existing stdio FILE *
168 * @keep_open : Should @file be closed when the wrapper is closed
170 * Assumes ownership of @file when succeeding. If @keep_open is true,
171 * ownership reverts to caller when the GsfObject is closed.
173 * Returns: a new GsfInput wrapper for @file. Note that if the file is not
174 * seekable, this function will make a local copy of the entire file.
177 gsf_input_stdio_new_FILE (char const *filename, FILE *file, gboolean keep_open)
179 GsfInputStdio *stdio;
183 g_return_val_if_fail (filename != NULL, NULL);
184 g_return_val_if_fail (file != NULL, NULL);
186 if (fstat (fileno (file), &st) < 0 || !S_ISREG (st.st_mode)) {
187 return make_local_copy (file, filename, NULL);
192 stdio = g_object_new (GSF_INPUT_STDIO_TYPE, NULL);
193 if (G_UNLIKELY (NULL == stdio)) return NULL;
195 stdio->keep_open = keep_open;
196 stdio->filename = g_strdup (filename);
197 gsf_input_set_size (GSF_INPUT (stdio), size);
198 gsf_input_set_name_from_filename (GSF_INPUT (stdio), filename);
199 return GSF_INPUT (stdio);
203 gsf_input_stdio_finalize (GObject *obj)
205 GsfInputStdio *input = (GsfInputStdio *)obj;
207 if (input->file != NULL) {
208 if (!input->keep_open)
209 fclose (input->file);
217 g_free (input->filename);
219 parent_class->finalize (obj);
223 gsf_input_stdio_dup (GsfInput *src_input, GError **err)
225 GsfInputStdio const *src = (GsfInputStdio *)src_input;
226 return gsf_input_stdio_new (src->filename, err);
229 static guint8 const *
230 gsf_input_stdio_read (GsfInput *input, size_t num_bytes,
233 GsfInputStdio *stdio = GSF_INPUT_STDIO (input);
234 size_t nread = 0, total_read = 0;
236 g_return_val_if_fail (stdio != NULL, NULL);
237 g_return_val_if_fail (stdio->file != NULL, NULL);
239 if (buffer == NULL) {
240 if (stdio->buf_size < num_bytes) {
241 stdio->buf_size = num_bytes;
243 stdio->buf = g_new (guint8, stdio->buf_size);
248 while (total_read < num_bytes) {
249 nread = fread (buffer + total_read, 1,
250 num_bytes - total_read, stdio->file);
252 if (total_read < num_bytes &&
253 (ferror (stdio->file) || feof (stdio->file)))
261 gsf_input_stdio_seek (GsfInput *input, gsf_off_t offset, GSeekType whence)
263 GsfInputStdio const *stdio = GSF_INPUT_STDIO (input);
264 int stdio_whence = SEEK_SET;
272 if (stdio->file == NULL)
276 if ((gsf_off_t) loffset != offset) { /* Check for overflow */
278 g_warning ("offset too large for fseeko");
280 g_warning ("offset too large for fseek");
285 case G_SEEK_CUR : stdio_whence = SEEK_CUR; break;
286 case G_SEEK_END : stdio_whence = SEEK_END; break;
294 if (0 == fseeko (stdio->file, loffset, stdio_whence))
297 if (0 == fseek (stdio->file, loffset, stdio_whence))
305 gsf_input_stdio_init (GObject *obj)
307 GsfInputStdio *stdio = GSF_INPUT_STDIO (obj);
310 stdio->filename = NULL;
313 stdio->keep_open = FALSE;
317 gsf_input_stdio_class_init (GObjectClass *gobject_class)
319 GsfInputClass *input_class = GSF_INPUT_CLASS (gobject_class);
321 gobject_class->finalize = gsf_input_stdio_finalize;
322 input_class->Dup = gsf_input_stdio_dup;
323 input_class->Read = gsf_input_stdio_read;
324 input_class->Seek = gsf_input_stdio_seek;
326 parent_class = g_type_class_peek_parent (gobject_class);
329 GSF_CLASS (GsfInputStdio, gsf_input_stdio,
330 gsf_input_stdio_class_init, gsf_input_stdio_init,