File: dump_leaf.c

package info (click to toggle)
ruby-oj 3.16.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,852 kB
  • sloc: ansic: 19,402; ruby: 11,451; makefile: 17
file content (162 lines) | stat: -rw-r--r-- 4,809 bytes parent folder | download
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) 2012, 2017 Peter Ohler. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license details.

#include <errno.h>

#include "dump.h"
#include "oj.h"

static void dump_leaf(Leaf leaf, int depth, Out out);

inline static void dump_chars(const char *s, size_t size, Out out) {
    assure_size(out, size);
    APPEND_CHARS(out->cur, s, size);
    *out->cur = '\0';
}

static void dump_leaf_str(Leaf leaf, Out out) {
    switch (leaf->value_type) {
    case STR_VAL: oj_dump_cstr(leaf->str, strlen(leaf->str), 0, 0, out); break;
    case RUBY_VAL: oj_dump_cstr(StringValueCStr(leaf->value), (int)RSTRING_LEN(leaf->value), 0, 0, out); break;
    case COL_VAL:
    default: rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type); break;
    }
}

static void dump_leaf_fixnum(Leaf leaf, Out out) {
    switch (leaf->value_type) {
    case STR_VAL: dump_chars(leaf->str, strlen(leaf->str), out); break;
    case RUBY_VAL:
        if (T_BIGNUM == rb_type(leaf->value)) {
            oj_dump_bignum(leaf->value, 0, out, false);
        } else {
            oj_dump_fixnum(leaf->value, 0, out, false);
        }
        break;
    case COL_VAL:
    default: rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type); break;
    }
}

static void dump_leaf_float(Leaf leaf, Out out) {
    switch (leaf->value_type) {
    case STR_VAL: dump_chars(leaf->str, strlen(leaf->str), out); break;
    case RUBY_VAL: oj_dump_float(leaf->value, 0, out, false); break;
    case COL_VAL:
    default: rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type); break;
    }
}

static void dump_leaf_array(Leaf leaf, int depth, Out out) {
    size_t size;
    int    d2 = depth + 1;

    size = 2;
    assure_size(out, size);
    *out->cur++ = '[';
    if (0 == leaf->elements) {
        *out->cur++ = ']';
    } else {
        Leaf first = leaf->elements->next;
        Leaf e     = first;

        size = d2 * out->indent + 2;
        do {
            assure_size(out, size);
            fill_indent(out, d2);
            dump_leaf(e, d2, out);
            if (e->next != first) {
                *out->cur++ = ',';
            }
            e = e->next;
        } while (e != first);
        size = depth * out->indent + 1;
        assure_size(out, size);
        fill_indent(out, depth);
        *out->cur++ = ']';
    }
    *out->cur = '\0';
}

static void dump_leaf_hash(Leaf leaf, int depth, Out out) {
    size_t size;
    int    d2 = depth + 1;

    size = 2;
    assure_size(out, size);
    *out->cur++ = '{';
    if (0 == leaf->elements) {
        *out->cur++ = '}';
    } else {
        Leaf first = leaf->elements->next;
        Leaf e     = first;

        size = d2 * out->indent + 2;
        do {
            assure_size(out, size);
            fill_indent(out, d2);
            oj_dump_cstr(e->key, strlen(e->key), 0, 0, out);
            *out->cur++ = ':';
            dump_leaf(e, d2, out);
            if (e->next != first) {
                *out->cur++ = ',';
            }
            e = e->next;
        } while (e != first);
        size = depth * out->indent + 1;
        assure_size(out, size);
        fill_indent(out, depth);
        *out->cur++ = '}';
    }
    *out->cur = '\0';
}

static void dump_leaf(Leaf leaf, int depth, Out out) {
    switch (leaf->rtype) {
    case T_NIL: oj_dump_nil(Qnil, 0, out, false); break;
    case T_TRUE: oj_dump_true(Qtrue, 0, out, false); break;
    case T_FALSE: oj_dump_false(Qfalse, 0, out, false); break;
    case T_STRING: dump_leaf_str(leaf, out); break;
    case T_FIXNUM: dump_leaf_fixnum(leaf, out); break;
    case T_FLOAT: dump_leaf_float(leaf, out); break;
    case T_ARRAY: dump_leaf_array(leaf, depth, out); break;
    case T_HASH: dump_leaf_hash(leaf, depth, out); break;
    default: rb_raise(rb_eTypeError, "Unexpected type %02x.\n", leaf->rtype); break;
    }
}

void oj_dump_leaf_to_json(Leaf leaf, Options copts, Out out) {
    if (0 == out->buf) {
        oj_out_init(out);
    }
    out->cur      = out->buf;
    out->circ_cnt = 0;
    out->opts     = copts;
    out->hash_cnt = 0;
    out->indent   = copts->indent;
    dump_leaf(leaf, 0, out);
}

void oj_write_leaf_to_file(Leaf leaf, const char *path, Options copts) {
    struct _out out;
    size_t      size;
    FILE       *f;

    oj_out_init(&out);

    out.omit_nil = copts->dump_opts.omit_nil;
    oj_dump_leaf_to_json(leaf, copts, &out);
    size = out.cur - out.buf;
    if (0 == (f = fopen(path, "w"))) {
        rb_raise(rb_eIOError, "%s\n", strerror(errno));
    }
    if (size != fwrite(out.buf, 1, size, f)) {
        int err = ferror(f);

        rb_raise(rb_eIOError, "Write failed. [%d:%s]\n", err, strerror(err));
    }

    oj_out_free(&out);

    fclose(f);
}