Update Troubleshooting.md
[RRG-proxmark3.git] / client / deps / jansson / error.c
blob06dc837ec0dcdbfad643b4ed5b6ad84c2bf46edf
1 #include <string.h>
2 #include "jansson_private.h"
4 void jsonp_error_init(json_error_t *error, const char *source) {
5 if (error) {
6 error->text[0] = '\0';
7 error->line = -1;
8 error->column = -1;
9 error->position = 0;
10 if (source)
11 jsonp_error_set_source(error, source);
12 else
13 error->source[0] = '\0';
17 void jsonp_error_set_source(json_error_t *error, const char *source) {
18 size_t length;
20 if (!error || !source)
21 return;
23 length = strlen(source);
24 if (length < JSON_ERROR_SOURCE_LENGTH) {
25 strncpy(error->source, source, JSON_ERROR_SOURCE_LENGTH - 1);
26 } else {
27 size_t extra = length - JSON_ERROR_SOURCE_LENGTH + 4;
28 memcpy(error->source, "...", 3);
29 strncpy(error->source + 3, source + extra, length - extra + 1);
33 void jsonp_error_set(json_error_t *error, int line, int column,
34 size_t position, enum json_error_code code,
35 const char *msg, ...) {
36 va_list ap;
37 va_start(ap, msg);
38 jsonp_error_vset(error, line, column, position, code, msg, ap);
39 va_end(ap);
42 void jsonp_error_vset(json_error_t *error, int line, int column,
43 size_t position, enum json_error_code code,
44 const char *msg, va_list ap) {
45 if (!error)
46 return;
48 if (error->text[0] != '\0') {
49 /* error already set */
50 return;
53 error->line = line;
54 error->column = column;
55 error->position = (int)position;
57 vsnprintf(error->text, JSON_ERROR_TEXT_LENGTH - 1, msg, ap);
58 error->text[JSON_ERROR_TEXT_LENGTH - 2] = '\0';
59 error->text[JSON_ERROR_TEXT_LENGTH - 1] = code;