Fixed message typo.
[captive.git] / gnome_vfs_read_entire_file.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
3 /* gnome-vfs-utils.c - Private utility functions for the GNOME Virtual
4    File System.
5    gnome_vfs_read_entire_file() extraction for Captive
6
7    Copyright (C) 1999 Free Software Foundation
8    Copyright (C) 2000, 2001 Eazel, Inc.
9
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.
14
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.
19
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.
24
25    Authors: Ettore Perazzoli <ettore@comm2000.it>
26             John Sullivan <sullivan@eazel.com> 
27             Darin Adler <darin@eazel.com>
28 */
29
30
31 #include "config.h"
32
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>
37
38
39 #define READ_CHUNK_SIZE 8192
40
41
42 /**
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.
47  * 
48  * Reads an entire file into memory for convenience. Beware accidentally
49  * loading large files into memory with this function.
50  *
51  * Return value: An integer representing the result of the operation
52  *
53  * Since: 2.2
54  */
55
56 GnomeVFSResult
57 gnome_vfs_read_entire_file (const char *uri,
58                             int *file_size,
59                             char **file_contents)
60 {
61         GnomeVFSResult result;
62         GnomeVFSHandle *handle;
63         char *buffer;
64         GnomeVFSFileSize total_bytes_read;
65         GnomeVFSFileSize bytes_read;
66
67         *file_size = 0;
68         *file_contents = NULL;
69
70         /* Open the file. */
71         result = gnome_vfs_open (&handle, uri, GNOME_VFS_OPEN_READ);
72         if (result != GNOME_VFS_OK) {
73                 return result;
74         }
75
76         /* Read the whole thing. */
77         buffer = NULL;
78         total_bytes_read = 0;
79         do {
80                 buffer = g_realloc (buffer, total_bytes_read + READ_CHUNK_SIZE);
81                 result = gnome_vfs_read (handle,
82                                          buffer + total_bytes_read,
83                                          READ_CHUNK_SIZE,
84                                          &bytes_read);
85                 if (result != GNOME_VFS_OK && result != GNOME_VFS_ERROR_EOF) {
86                         g_free (buffer);
87                         gnome_vfs_close (handle);
88                         return result;
89                 }
90
91                 /* Check for overflow. */
92                 if (total_bytes_read + bytes_read < total_bytes_read) {
93                         g_free (buffer);
94                         gnome_vfs_close (handle);
95                         return GNOME_VFS_ERROR_TOO_BIG;
96                 }
97
98                 total_bytes_read += bytes_read;
99         } while (result == GNOME_VFS_OK);
100
101         /* Close the file. */
102         result = gnome_vfs_close (handle);
103         if (result != GNOME_VFS_OK) {
104                 g_free (buffer);
105                 return result;
106         }
107
108         /* Return the file. */
109         *file_size = total_bytes_read;
110         *file_contents = g_realloc (buffer, total_bytes_read);
111         return GNOME_VFS_OK;
112 }