Simon Josefsson <[EMAIL PROTECTED]> writes: > I like this, and I also like your fread_file function. I will create > a module read-file with two functions fread_file and read_file.
Here's the module. Only lighted tested, mostly to get feedback on the approach, but if you agree with it, detailed review would be useful. Index: lib/read-file.c =================================================================== RCS file: lib/read-file.c diff -N lib/read-file.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lib/read-file.c 31 May 2006 13:25:27 -0000 @@ -0,0 +1,141 @@ +/* read-file.c -- read file contents into a string + Copyright (C) 2006 Free Software Foundation, Inc. + Written by Simon Josefsson and Bruno Haible. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "read-file.h" + +/* Get realloc, free. */ +#include <stdlib.h> + +/* Get errno. */ +#include <errno.h> + +/* Read a STREAM and return a newly allocated string with the content, + and set LENGTH to the length of the string. The string is + zero-terminated, but the terminating zero character is not counted + in the LENGTH variable. On errors, return NULL, sets errno and + LENGTH is undefined. */ +char * +fread_file (FILE * stream, size_t * length) +{ + char *buf = NULL; + size_t alloc = 0; + size_t size = 0; + + while (!feof (stream)) + { + size_t count; + + if (size + BUFSIZ + 1 > alloc) + { + char *tmp; + + alloc += alloc / 2; + if (alloc < size + BUFSIZ + 1) + alloc = size + BUFSIZ + 1; + + tmp = realloc (buf, alloc); + if (!tmp) + { + int save_errno = errno; + if (buf) + free (buf); + errno = save_errno; + return NULL; + } + + buf = tmp; + } + + count = fread (buf + size, 1, BUFSIZ, stream); + size += count; + + if (ferror (stream)) + { + int save_errno = errno; + free (buf); + errno = save_errno; + return NULL; + } + } + + buf[size] = '\0'; + + *length = size; + + return buf; +} + +static char * +internal_read_file (const char *filename, size_t * length, const char *mode) +{ + FILE *stream = fopen (filename, mode); + char *out = NULL; + int rc; + + if (!stream) + return NULL; + + out = fread_file (stream, length); + + if (out) + rc = fclose (stream); + else + { + /* On failure, preserve the original errno value. */ + int save_errno = errno; + rc = fclose (stream); + errno = save_errno; + } + + if (rc != 0) + { + int save_errno = errno; + if (out) + free (out); + errno = save_errno; + return NULL; + } + + return out; +} + +/* Open and read the contents of FILENAME, and return a newly + allocated string with the content, and set LENGTH to the length of + the string. The string is zero-terminated, but the terminating + zero character is not counted in the LENGTH variable. On errors, + return NULL and sets errno. */ +char * +read_file (const char *filename, size_t * length) +{ + return internal_read_file (filename, length, "r"); +} + +/* Open (on non-POSIX systems, in binary mode) and read the contents + of FILENAME, and return a newly allocated string with the content, + and set LENGTH to the length of the string. The string is + zero-terminated, but the terminating zero character is not counted + in the LENGTH variable. On errors, return NULL and sets errno. */ +char * +read_binary_file (const char *filename, size_t * length) +{ + return internal_read_file (filename, length, "rb"); +} Index: lib/read-file.h =================================================================== RCS file: lib/read-file.h diff -N lib/read-file.h --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lib/read-file.h 31 May 2006 13:25:27 -0000 @@ -0,0 +1,34 @@ +/* read-file.h -- read file contents into a string + Copyright (C) 2006 Free Software Foundation, Inc. + Written by Simon Josefsson. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#ifndef READ_FILE_H +#define READ_FILE_H + +/* Get size_t. */ +#include <stddef.h> + +/* Get FILE. */ +#include <stdio.h> + +extern char *fread_file (FILE * stream, size_t * length); + +extern char *read_file (const char *filename, size_t * length); + +extern char *read_binary_file (const char *filename, size_t * length); + +#endif /* READ_FILE_H */ Index: m4/read-file.m4 =================================================================== RCS file: m4/read-file.m4 diff -N m4/read-file.m4 --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ m4/read-file.m4 31 May 2006 13:25:27 -0000 @@ -0,0 +1,15 @@ +# read-file.m4 serial 1 +dnl Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_FUNC_READ_FILE], +[ + AC_LIBSOURCES([read-file.c, read-file.h]) + AC_LIBOBJ([read-file]) + gl_PREREQ_READ_FILE +]) + +# Prerequisites of lib/read-file.c. +AC_DEFUN([gl_PREREQ_READ_FILE], [:]) Index: modules/read-file =================================================================== RCS file: modules/read-file diff -N modules/read-file --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/read-file 31 May 2006 13:25:27 -0000 @@ -0,0 +1,24 @@ +Description: +read_file() function: read the contents of a file into a string + +Files: +lib/read-file.h +lib/read-file.c +m4/read-file.m4 + +Depends-on: +realloc + +configure.ac: +gl_FUNC_READ_FILE + +Makefile.am: + +Include: +"read-file.h" + +License: +LGPL + +Maintainer: +Simon Josefsson