2018-11-12 21:00:35 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include "jansson_private.h"
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void jsonp_error_init(json_error_t *error, const char *source) {
|
2019-02-25 02:32:14 +08:00
|
|
|
if (error) {
|
2018-11-12 21:00:35 +08:00
|
|
|
error->text[0] = '\0';
|
|
|
|
error->line = -1;
|
|
|
|
error->column = -1;
|
|
|
|
error->position = 0;
|
2019-03-10 07:00:59 +08:00
|
|
|
if (source)
|
2018-11-12 21:00:35 +08:00
|
|
|
jsonp_error_set_source(error, source);
|
|
|
|
else
|
|
|
|
error->source[0] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void jsonp_error_set_source(json_error_t *error, const char *source) {
|
2018-11-12 21:00:35 +08:00
|
|
|
size_t length;
|
|
|
|
|
2019-02-25 02:32:14 +08:00
|
|
|
if (!error || !source)
|
2018-11-12 21:00:35 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
length = strlen(source);
|
2019-02-25 02:32:14 +08:00
|
|
|
if (length < JSON_ERROR_SOURCE_LENGTH) {
|
2019-04-17 22:53:28 +08:00
|
|
|
strncpy(error->source, source, JSON_ERROR_SOURCE_LENGTH - 1);
|
2019-03-10 06:35:06 +08:00
|
|
|
} else {
|
2018-11-12 21:00:35 +08:00
|
|
|
size_t extra = length - JSON_ERROR_SOURCE_LENGTH + 4;
|
|
|
|
memcpy(error->source, "...", 3);
|
|
|
|
strncpy(error->source + 3, source + extra, length - extra + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void jsonp_error_set(json_error_t *error, int line, int column,
|
|
|
|
size_t position, enum json_error_code code,
|
2019-03-10 18:20:22 +08:00
|
|
|
const char *msg, ...) {
|
2018-11-12 21:00:35 +08:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, msg);
|
|
|
|
jsonp_error_vset(error, line, column, position, code, msg, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void jsonp_error_vset(json_error_t *error, int line, int column,
|
|
|
|
size_t position, enum json_error_code code,
|
2019-03-10 18:20:22 +08:00
|
|
|
const char *msg, va_list ap) {
|
2019-02-25 02:32:14 +08:00
|
|
|
if (!error)
|
2018-11-12 21:00:35 +08:00
|
|
|
return;
|
|
|
|
|
2019-02-25 02:32:14 +08:00
|
|
|
if (error->text[0] != '\0') {
|
2018-11-12 21:00:35 +08:00
|
|
|
/* error already set */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
error->line = line;
|
|
|
|
error->column = column;
|
|
|
|
error->position = (int)position;
|
|
|
|
|
|
|
|
vsnprintf(error->text, JSON_ERROR_TEXT_LENGTH - 1, msg, ap);
|
|
|
|
error->text[JSON_ERROR_TEXT_LENGTH - 2] = '\0';
|
|
|
|
error->text[JSON_ERROR_TEXT_LENGTH - 1] = code;
|
|
|
|
}
|