Support building on Windows
[jansson.git] / src / error.c
index b950c2c..a544a59 100644 (file)
@@ -9,9 +9,27 @@ void jsonp_error_init(json_error_t *error, const char *source)
         error->line = -1;
         error->column = -1;
         error->position = 0;
+        if(source)
+            jsonp_error_set_source(error, source);
+        else
+            error->source[0] = '\0';
+    }
+}
+
+void jsonp_error_set_source(json_error_t *error, const char *source)
+{
+    size_t length;
+
+    if(!error || !source)
+        return;
 
-        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);
     }
 }
 
@@ -41,4 +59,5 @@ void jsonp_error_vset(json_error_t *error, int line, int column,
     error->position = position;
 
     vsnprintf(error->text, JSON_ERROR_TEXT_LENGTH, msg, ap);
+    error->text[JSON_ERROR_TEXT_LENGTH - 1] = '\0';
 }