gclib/textfileutils.c
author ali <ali@juiblex.co.uk>
Fri Jan 27 00:28:11 2012 +0000 (2012-01-27)
changeset 4 218904410231
permissions -rw-r--r--
Add workaround for missing -framework Carbon
     1 #include <stdlib.h>
     2 #include <stdio.h>
     3 #include <gclib/gclib.h>
     4 
     5 /*
     6  * Read a file into memory (which should be freed with mem_free when no
     7  * longer required). Returns NULL on error and outputs a suitable error
     8  * message to stderr.
     9  * DOS-style line endings are handled transparently even on platforms which
    10  * don't normally use this format.
    11  */
    12 boolean file_get_contents_text(const char *filename,char **contents,
    13   size_t *length)
    14 {
    15     int i;
    16     char *raw;
    17     size_t raw_length;
    18     String *string;
    19     if (!file_get_contents(filename,&raw,&raw_length))
    20 	return FALSE;
    21     string=string_new(NULL);
    22     for(i=0;i<raw_length;i++)
    23 	if (raw[i]!='\r')
    24 	    string_append_c(string,raw[i]);
    25     mem_free(raw);
    26     if (length)
    27 	*length=string->len;
    28     if (contents)
    29 	*contents=string_free(string,FALSE);
    30     else
    31 	string_free(string,TRUE);
    32     return TRUE;
    33 }