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