From 2f4cc718e4588701c02ab98beb17a1adc86431f8 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 10 Dec 2011 21:50:46 +0000 Subject: Tolerate invalid characters in string literals by replacing with the Unicode replacement character. git-svn-id: http://svn.drobilla.net/serd/trunk@232 490d8e77-9747-427b-9fa3-0b8f29cee8a0 --- src/reader.c | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/reader.c b/src/reader.c index 70bdd1a1..9059236f 100644 --- a/src/reader.c +++ b/src/reader.c @@ -95,14 +95,32 @@ struct SerdReaderImpl { #endif }; +static int +msg(SerdReader* reader, const char* prefix, const char* fmt, va_list args) +{ + fprintf(stderr, "%s: %s:%u:%u: ", + prefix, reader->cur.filename, reader->cur.line, reader->cur.col); + vfprintf(stderr, fmt, args); + return 0; +} + static int error(SerdReader* reader, const char* fmt, ...) { va_list args; va_start(args, fmt); - fprintf(stderr, "error: %s:%u:%u: ", - reader->cur.filename, reader->cur.line, reader->cur.col); - vfprintf(stderr, fmt, args); + msg(reader, "error", fmt, args); + va_end(args); + return 0; +} + +static int +warn(SerdReader* reader, const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + msg(reader, "warning", fmt, args); + va_end(args); return 0; } @@ -493,8 +511,13 @@ read_character(SerdReader* reader, Ref dest) } else if ((c & 0xF8) == 0xF0) { // Starts with `11110' size = 4; } else { - error(reader, "invalid character\n"); - return SERD_ERR_BAD_SYNTAX; + warn(reader, "invalid character\n"); + // Push replacement character + push_byte(reader, dest, 0xEF); + push_byte(reader, dest, 0xBF); + push_byte(reader, dest, 0xBD); + eat_byte(reader, c); + return SERD_SUCCESS; } for (unsigned i = 0; i < size; ++i) { push_byte(reader, dest, eat_byte(reader, peek_byte(reader))); -- cgit v1.2.1