Enhance error reporting
[jansson.git] / src / error.c
1 #include <string.h>
2 #include <stdarg.h>
3
4 #include "jansson_private.h"
5
6 void jsonp_error_init(json_error_t *error, const char *source)
7 {
8     if(error)
9     {
10         error->text[0] = '\0';
11         error->line = -1;
12         error->column = -1;
13
14         strncpy(error->source, source, JSON_ERROR_SOURCE_LENGTH);
15         error->source[JSON_ERROR_SOURCE_LENGTH - 1] = '\0';
16     }
17 }
18
19 void jsonp_error_set(json_error_t *error, int line, int column,
20                      const char *msg, ...)
21 {
22     va_list ap;
23
24     if(!error)
25         return;
26
27     if(error->text[0] != '\0') {
28         /* error already set */
29         return;
30     }
31
32     error->line = line;
33     error->column = column;
34
35     va_start(ap, msg);
36     vsnprintf(error->text, JSON_ERROR_TEXT_LENGTH, msg, ap);
37     va_end(ap);
38 }