File: err.c

package info (click to toggle)
ruby-oj 3.16.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,192 kB
  • sloc: ansic: 19,659; ruby: 11,750; sh: 70; makefile: 17
file content (57 lines) | stat: -rw-r--r-- 1,626 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
// Copyright (c) 2011 Peter Ohler. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license details.

#include "err.h"

#include <stdarg.h>

void oj_err_set(Err e, VALUE clas, const char *format, ...) {
    va_list ap;

    va_start(ap, format);
    e->clas = clas;
    vsnprintf(e->msg, sizeof(e->msg) - 1, format, ap);
    va_end(ap);
}

void oj_err_raise(Err e) {
    rb_raise(e->clas, "%s", e->msg);
}

void _oj_err_set_with_location(Err         err,
                               VALUE       eclas,
                               const char *msg,
                               const char *json,
                               const char *current,
                               const char *file,
                               int         line) {
    int n   = 1;
    int col = 1;

    for (; json < current && '\n' != *current; current--) {
        col++;
    }
    for (; json < current; current--) {
        if ('\n' == *current) {
            n++;
        }
    }
    oj_err_set(err, eclas, "%s at line %d, column %d [%s:%d]", msg, n, col, file, line);
}

void _oj_raise_error(const char *msg, const char *json, const char *current, const char *file, int line) {
    struct _err err;
    int         n   = 1;
    int         col = 1;

    for (; json < current && '\n' != *current; current--) {
        col++;
    }
    for (; json < current; current--) {
        if ('\n' == *current) {
            n++;
        }
    }
    oj_err_set(&err, oj_parse_error_class, "%s at line %d, column %d [%s:%d]", msg, n, col, file, line);
    rb_raise(err.clas, "%s", err.msg);
}