1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
3 /* gnome-vfs-utils.c - Private utility functions for the GNOME Virtual
5 gnome_vfs_read_entire_file() extraction for Captive
7 Copyright (C) 1999 Free Software Foundation
8 Copyright (C) 2000, 2001 Eazel, Inc.
10 The Gnome Library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Library General Public License as
12 published by the Free Software Foundation; either version 2 of the
13 License, or (at your option) any later version.
15 The Gnome Library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Library General Public License for more details.
20 You should have received a copy of the GNU Library General Public
21 License along with the Gnome Library; see the file COPYING.LIB. If not,
22 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA.
25 Authors: Ettore Perazzoli <ettore@comm2000.it>
26 John Sullivan <sullivan@eazel.com>
27 Darin Adler <darin@eazel.com>
33 #include <glib/gmem.h>
34 #include <libgnomevfs/gnome-vfs-result.h>
35 #include <libgnomevfs/gnome-vfs-handle.h>
36 #include <libgnomevfs/gnome-vfs-ops.h>
39 #define READ_CHUNK_SIZE 8192
43 * gnome_vfs_read_entire_file:
44 * @uri: URI of the file to read
45 * @file_size: after reading the file, contains the size of the file read
46 * @file_contents: contains the file_size bytes, the contents of the file at @uri.
48 * Reads an entire file into memory for convenience. Beware accidentally
49 * loading large files into memory with this function.
51 * Return value: An integer representing the result of the operation
57 gnome_vfs_read_entire_file (const char *uri,
61 GnomeVFSResult result;
62 GnomeVFSHandle *handle;
64 GnomeVFSFileSize total_bytes_read;
65 GnomeVFSFileSize bytes_read;
68 *file_contents = NULL;
71 result = gnome_vfs_open (&handle, uri, GNOME_VFS_OPEN_READ);
72 if (result != GNOME_VFS_OK) {
76 /* Read the whole thing. */
80 buffer = g_realloc (buffer, total_bytes_read + READ_CHUNK_SIZE);
81 result = gnome_vfs_read (handle,
82 buffer + total_bytes_read,
85 if (result != GNOME_VFS_OK && result != GNOME_VFS_ERROR_EOF) {
87 gnome_vfs_close (handle);
91 /* Check for overflow. */
92 if (total_bytes_read + bytes_read < total_bytes_read) {
94 gnome_vfs_close (handle);
95 return GNOME_VFS_ERROR_TOO_BIG;
98 total_bytes_read += bytes_read;
99 } while (result == GNOME_VFS_OK);
101 /* Close the file. */
102 result = gnome_vfs_close (handle);
103 if (result != GNOME_VFS_OK) {
108 /* Return the file. */
109 *file_size = total_bytes_read;
110 *file_contents = g_realloc (buffer, total_bytes_read);