bl/textfileutils.c
author ali <ali@juiblex.co.uk>
Sun Feb 19 10:06:41 2012 +0000 (2012-02-19)
changeset 30 980add74a280
parent 6 faab25d520dd
child 69 1016349e619f
permissions -rw-r--r--
Add testcase for words that "can't" come before a comma
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@13
     9
 * DOS-style line endings and UTF-8 BOM are handled transparently even
ali@13
    10
 * on platforms which don't normally use these formats.
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@13
    16
    unsigned char *raw;
ali@0
    17
    size_t raw_length;
ali@6
    18
    GString *string;
ali@6
    19
    GError *error=NULL;
ali@13
    20
    if (!g_file_get_contents(filename,(char *)&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@13
    27
    i=0;
ali@13
    28
    if (raw_length>=3 && raw[0]==0xEF && raw[1]==0xBB && raw[2]==0xBF)
ali@13
    29
	i+=3;			/* Skip BOM (U+FEFF) */
ali@13
    30
    for(;i<raw_length;i++)
ali@0
    31
	if (raw[i]!='\r')
ali@6
    32
	    g_string_append_c(string,raw[i]);
ali@6
    33
    g_free(raw);
ali@0
    34
    if (length)
ali@0
    35
	*length=string->len;
ali@0
    36
    if (contents)
ali@6
    37
	*contents=g_string_free(string,FALSE);
ali@0
    38
    else
ali@6
    39
	g_string_free(string,TRUE);
ali@0
    40
    return TRUE;
ali@0
    41
}