bl/textfileutils.c
author ali <ali@juiblex.co.uk>
Fri Jan 27 10:30:16 2012 +0000 (2012-01-27)
changeset 5 f600b0d1fc5d
parent 0 gclib/textfileutils.c@c2f4c0285180
child 6 faab25d520dd
permissions -rw-r--r--
Rename to bookloupe
     1 #include <stdlib.h>
     2 #include <stdio.h>
     3 #include <bl/bl.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 }