From: Petri Lehtinen Date: Tue, 22 Feb 2011 11:24:15 +0000 (+0200) Subject: Truncate error source from start, not end, if it's too long to fit X-Git-Tag: v2.0~11 X-Git-Url: http://www.project-moonshot.org/gitweb/?p=jansson.git;a=commitdiff_plain;h=50dc64a7af3664ca55ac74b393459a6d54e6958f Truncate error source from start, not end, if it's too long to fit It's more helpful to see "...bar/baz.json" instead of "/long/path/to". --- diff --git a/src/error.c b/src/error.c index b950c2c..d113b8f 100644 --- a/src/error.c +++ b/src/error.c @@ -5,13 +5,21 @@ void jsonp_error_init(json_error_t *error, const char *source) { if(error) { + size_t length; + error->text[0] = '\0'; error->line = -1; error->column = -1; error->position = 0; - strncpy(error->source, source, JSON_ERROR_SOURCE_LENGTH); - error->source[JSON_ERROR_SOURCE_LENGTH - 1] = '\0'; + length = strlen(source); + if(length < JSON_ERROR_SOURCE_LENGTH) + strcpy(error->source, source); + else { + size_t extra = length - JSON_ERROR_SOURCE_LENGTH + 4; + strcpy(error->source, "..."); + strcpy(error->source + 3, source + extra); + } } }