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@6
|
12 |
gboolean 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@6
|
18 |
GString *string;
|
ali@6
|
19 |
GError *error=NULL;
|
ali@6
|
20 |
if (!g_file_get_contents(filename,&raw,&raw_length,&error))
|
ali@6
|
21 |
{
|
ali@6
|
22 |
fprintf(stderr,"%s: %s\n",filename,error->message);
|
ali@6
|
23 |
g_error_free(error);
|
ali@0
|
24 |
return FALSE;
|
ali@6
|
25 |
}
|
ali@6
|
26 |
string=g_string_new(NULL);
|
ali@0
|
27 |
for(i=0;i<raw_length;i++)
|
ali@0
|
28 |
if (raw[i]!='\r')
|
ali@6
|
29 |
g_string_append_c(string,raw[i]);
|
ali@6
|
30 |
g_free(raw);
|
ali@0
|
31 |
if (length)
|
ali@0
|
32 |
*length=string->len;
|
ali@0
|
33 |
if (contents)
|
ali@6
|
34 |
*contents=g_string_free(string,FALSE);
|
ali@0
|
35 |
else
|
ali@6
|
36 |
g_string_free(string,TRUE);
|
ali@0
|
37 |
return TRUE;
|
ali@0
|
38 |
}
|