gclib/textfileutils.c
changeset 4 218904410231
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gclib/textfileutils.c	Fri Jan 27 00:28:11 2012 +0000
     1.3 @@ -0,0 +1,33 @@
     1.4 +#include <stdlib.h>
     1.5 +#include <stdio.h>
     1.6 +#include <gclib/gclib.h>
     1.7 +
     1.8 +/*
     1.9 + * Read a file into memory (which should be freed with mem_free when no
    1.10 + * longer required). Returns NULL on error and outputs a suitable error
    1.11 + * message to stderr.
    1.12 + * DOS-style line endings are handled transparently even on platforms which
    1.13 + * don't normally use this format.
    1.14 + */
    1.15 +boolean file_get_contents_text(const char *filename,char **contents,
    1.16 +  size_t *length)
    1.17 +{
    1.18 +    int i;
    1.19 +    char *raw;
    1.20 +    size_t raw_length;
    1.21 +    String *string;
    1.22 +    if (!file_get_contents(filename,&raw,&raw_length))
    1.23 +	return FALSE;
    1.24 +    string=string_new(NULL);
    1.25 +    for(i=0;i<raw_length;i++)
    1.26 +	if (raw[i]!='\r')
    1.27 +	    string_append_c(string,raw[i]);
    1.28 +    mem_free(raw);
    1.29 +    if (length)
    1.30 +	*length=string->len;
    1.31 +    if (contents)
    1.32 +	*contents=string_free(string,FALSE);
    1.33 +    else
    1.34 +	string_free(string,TRUE);
    1.35 +    return TRUE;
    1.36 +}