Truncate error source from start, not end, if it's too long to fit
[jansson.git] / src / error.c
index cd80756..d113b8f 100644 (file)
@@ -5,27 +5,36 @@ 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;
-
-        strncpy(error->source, source, JSON_ERROR_SOURCE_LENGTH);
-        error->source[JSON_ERROR_SOURCE_LENGTH - 1] = '\0';
+        error->position = 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);
+        }
     }
 }
 
 void jsonp_error_set(json_error_t *error, int line, int column,
-                     const char *msg, ...)
+                     size_t position, const char *msg, ...)
 {
     va_list ap;
 
     va_start(ap, msg);
-    jsonp_error_vset(error, line, column, msg, ap);
+    jsonp_error_vset(error, line, column, position, msg, ap);
     va_end(ap);
 }
 
 void jsonp_error_vset(json_error_t *error, int line, int column,
-                      const char *msg, va_list ap)
+                      size_t position, const char *msg, va_list ap)
 {
     if(!error)
         return;
@@ -37,6 +46,7 @@ void jsonp_error_vset(json_error_t *error, int line, int column,
 
     error->line = line;
     error->column = column;
+    error->position = position;
 
     vsnprintf(error->text, JSON_ERROR_TEXT_LENGTH, msg, ap);
 }