projects
/
jansson.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Enhance error reporting
[jansson.git]
/
src
/
load.c
diff --git
a/src/load.c
b/src/load.c
index
2a360ff
..
f05230b
100644
(file)
--- a/
src/load.c
+++ b/
src/load.c
@@
-60,54
+60,46
@@
typedef struct {
/*** error reporting ***/
/*** error reporting ***/
-static void error_init(json_error_t *error)
-{
- if(error)
- {
- error->text[0] = '\0';
- error->line = -1;
- }
-}
-
static void error_set(json_error_t *error, const lex_t *lex,
const char *msg, ...)
{
va_list ap;
static void error_set(json_error_t *error, const lex_t *lex,
const char *msg, ...)
{
va_list ap;
- char text[JSON_ERROR_TEXT_LENGTH];
+ char
msg_
text[JSON_ERROR_TEXT_LENGTH];
- if(!error || error->text[0] != '\0') {
- /* error already set */
+ int line = -1, col = -1;
+ const char *result = msg_text;
+
+ if(!error)
return;
return;
- }
va_start(ap, msg);
va_start(ap, msg);
- vsnprintf(text, JSON_ERROR_TEXT_LENGTH, msg, ap);
+ vsnprintf(
msg_
text, JSON_ERROR_TEXT_LENGTH, msg, ap);
va_end(ap);
if(lex)
{
const char *saved_text = strbuffer_value(&lex->saved_text);
va_end(ap);
if(lex)
{
const char *saved_text = strbuffer_value(&lex->saved_text);
- error->line = lex->line;
+ char msg_with_context[JSON_ERROR_TEXT_LENGTH];
+
+ line = lex->line;
+
if(saved_text && saved_text[0])
{
if(lex->saved_text.length <= 20) {
if(saved_text && saved_text[0])
{
if(lex->saved_text.length <= 20) {
- snprintf(error->text, JSON_ERROR_TEXT_LENGTH,
- "%s near '%s'", text, saved_text);
+ snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH,
+ "%s near '%s'", msg_text, saved_text);
+ result = msg_with_context;
}
}
- else
- snprintf(error->text, JSON_ERROR_TEXT_LENGTH, "%s", text);
}
else
{
}
else
{
- snprintf(error->text, JSON_ERROR_TEXT_LENGTH,
- "%s near end of file", text);
+ snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH,
+ "%s near end of file", msg_text);
+ result = msg_with_context;
}
}
}
}
- else
- {
- error->line = -1;
- snprintf(error->text, JSON_ERROR_TEXT_LENGTH, "%s", text);
- }
+
+ jsonp_error_set(error, line, col, "%s", result);
}
}
@@
-401,7
+393,7
@@
out:
free(lex->value.string);
}
free(lex->value.string);
}
-#if
def
JSON_INTEGER_IS_LONG_LONG
+#if JSON_INTEGER_IS_LONG_LONG
#define json_strtoint strtoll
#else
#define json_strtoint strtol
#define json_strtoint strtoll
#else
#define json_strtoint strtol
@@
-774,8
+766,6
@@
static json_t *parse_value(lex_t *lex, json_error_t *error)
static json_t *parse_json(lex_t *lex, json_error_t *error)
{
static json_t *parse_json(lex_t *lex, json_error_t *error)
{
- error_init(error);
-
lex_scan(lex, error);
if(lex->token != '[' && lex->token != '{') {
error_set(error, lex, "'[' or '{' expected");
lex_scan(lex, error);
if(lex->token != '[' && lex->token != '{') {
error_set(error, lex, "'[' or '{' expected");
@@
-811,19
+801,19
@@
static int string_eof(void *data)
return (stream->data[stream->pos] == '\0');
}
return (stream->data[stream->pos] == '\0');
}
-json_t *json_loads(const char *string, json_error_t *error)
+json_t *json_loads(const char *string,
size_t flags,
json_error_t *error)
{
lex_t lex;
json_t *result;
{
lex_t lex;
json_t *result;
+ (void)flags; /* unused */
- string_data_t stream_data = {
- .data = string,
- .pos = 0
- };
+ string_data_t stream_data = {string, 0};
if(lex_init(&lex, string_get, string_eof, (void *)&stream_data))
return NULL;
if(lex_init(&lex, string_get, string_eof, (void *)&stream_data))
return NULL;
+ jsonp_error_init(error, "<string>");
+
result = parse_json(&lex, error);
if(!result)
goto out;
result = parse_json(&lex, error);
if(!result)
goto out;
@@
-840,14
+830,23
@@
out:
return result;
}
return result;
}
-json_t *json_loadf(FILE *input, json_error_t *error)
+json_t *json_loadf(FILE *input,
size_t flags,
json_error_t *error)
{
lex_t lex;
{
lex_t lex;
+ const char *source;
json_t *result;
json_t *result;
+ (void)flags; /* unused */
if(lex_init(&lex, (get_func)fgetc, (eof_func)feof, input))
return NULL;
if(lex_init(&lex, (get_func)fgetc, (eof_func)feof, input))
return NULL;
+ if(input == stdin)
+ source = "<stdin>";
+ else
+ source = "<stream>";
+
+ jsonp_error_init(error, source);
+
result = parse_json(&lex, error);
if(!result)
goto out;
result = parse_json(&lex, error);
if(!result)
goto out;
@@
-864,12
+863,12
@@
out:
return result;
}
return result;
}
-json_t *json_load_file(const char *path, json_error_t *error)
+json_t *json_load_file(const char *path,
size_t flags,
json_error_t *error)
{
json_t *result;
FILE *fp;
{
json_t *result;
FILE *fp;
-
error_init(error
);
+
jsonp_error_init(error, path
);
fp = fopen(path, "r");
if(!fp)
fp = fopen(path, "r");
if(!fp)
@@
-879,7
+878,7
@@
json_t *json_load_file(const char *path, json_error_t *error)
return NULL;
}
return NULL;
}
- result = json_loadf(fp, error);
+ result = json_loadf(fp,
flags,
error);
fclose(fp);
return result;
fclose(fp);
return result;