error.c 1.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <string.h>
#include "jansson_private.h"

void jsonp_error_init(json_error_t *error, const char *source)
{
    if(error)
    {
        error->text[0] = '\0';
        error->line = -1;
        error->column = -1;
        error->position = 0;
        if(source)
            jsonp_error_set_source(error, source);
        else
            error->source[0] = '\0';
    }
}

void jsonp_error_set_source(json_error_t *error, const char *source)
{
21 22
    size_t length;

23 24 25
    if(!error || !source)
        return;

26
    length = strlen(source);
27
    if(length < JSON_ERROR_SOURCE_LENGTH)
28
        strncpy(error->source, source, length + 1);
29 30
    else {
        size_t extra = length - JSON_ERROR_SOURCE_LENGTH + 4;
31 32
        strncpy(error->source, "...", 3);
        strncpy(error->source + 3, source + extra, length - extra + 1);
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    }
}

void jsonp_error_set(json_error_t *error, int line, int column,
                     size_t position, const char *msg, ...)
{
    va_list ap;

    va_start(ap, msg);
    jsonp_error_vset(error, line, column, position, msg, ap);
    va_end(ap);
}

void jsonp_error_vset(json_error_t *error, int line, int column,
                      size_t position, const char *msg, va_list ap)
{
    if(!error)
        return;

    if(error->text[0] != '\0') {
        /* error already set */
        return;
    }

    error->line = line;
    error->column = column;
59
    error->position = (int)position;
60 61

    vsnprintf(error->text, JSON_ERROR_TEXT_LENGTH, msg, ap);
62
    error->text[JSON_ERROR_TEXT_LENGTH - 1] = '\0';
63
}