comparison extern/jansson/src/error.c @ 0:8991989c4708

Initial import
author David Demelier <markand@malikania.fr>
date Tue, 22 Mar 2016 18:26:05 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:8991989c4708
1 #include <string.h>
2 #include "jansson_private.h"
3
4 void jsonp_error_init(json_error_t *error, const char *source)
5 {
6 if(error)
7 {
8 error->text[0] = '\0';
9 error->line = -1;
10 error->column = -1;
11 error->position = 0;
12 if(source)
13 jsonp_error_set_source(error, source);
14 else
15 error->source[0] = '\0';
16 }
17 }
18
19 void jsonp_error_set_source(json_error_t *error, const char *source)
20 {
21 size_t length;
22
23 if(!error || !source)
24 return;
25
26 length = strlen(source);
27 if(length < JSON_ERROR_SOURCE_LENGTH)
28 strcpy(error->source, source);
29 else {
30 size_t extra = length - JSON_ERROR_SOURCE_LENGTH + 4;
31 strcpy(error->source, "...");
32 strcpy(error->source + 3, source + extra);
33 }
34 }
35
36 void jsonp_error_set(json_error_t *error, int line, int column,
37 size_t position, const char *msg, ...)
38 {
39 va_list ap;
40
41 va_start(ap, msg);
42 jsonp_error_vset(error, line, column, position, msg, ap);
43 va_end(ap);
44 }
45
46 void jsonp_error_vset(json_error_t *error, int line, int column,
47 size_t position, const char *msg, va_list ap)
48 {
49 if(!error)
50 return;
51
52 if(error->text[0] != '\0') {
53 /* error already set */
54 return;
55 }
56
57 error->line = line;
58 error->column = column;
59 error->position = position;
60
61 vsnprintf(error->text, JSON_ERROR_TEXT_LENGTH, msg, ap);
62 error->text[JSON_ERROR_TEXT_LENGTH - 1] = '\0';
63 }