File: reader.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 (220 lines) | stat: -rw-r--r-- 6,989 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright (c) 2011 Peter Ohler. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license details.

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#ifdef NEEDS_UIO
#if NEEDS_UIO
#include <sys/uio.h>
#endif
#endif
#include <time.h>
#include <unistd.h>

#include "mem.h"
#include "oj.h"
#include "reader.h"
#include "ruby.h"

#define BUF_PAD 4

static VALUE rescue_cb(VALUE rdr, VALUE err);
static VALUE io_cb(VALUE rdr);
static VALUE partial_io_cb(VALUE rdr);
static int   read_from_io(Reader reader);
static int   read_from_fd(Reader reader);
static int   read_from_io_partial(Reader reader);
// static int		read_from_str(Reader reader);

void oj_reader_init(Reader reader, VALUE io, int fd, bool to_s) {
    VALUE io_class = rb_obj_class(io);
    VALUE stat;
    VALUE ftype;

    reader->head            = reader->base;
    *((char *)reader->head) = '\0';
    reader->end             = reader->head + sizeof(reader->base) - BUF_PAD;
    reader->tail            = reader->head;
    reader->read_end        = reader->head;
    reader->pro             = 0;
    reader->str             = 0;
    reader->pos             = 0;
    reader->line            = 1;
    reader->col             = 0;
    reader->free_head       = 0;

    if (0 != fd) {
        reader->read_func = read_from_fd;
        reader->fd        = fd;
    } else if (rb_cString == io_class) {
        reader->read_func = 0;
        reader->in_str    = StringValuePtr(io);
        reader->head      = (char *)reader->in_str;
        reader->tail      = reader->head;
        reader->read_end  = reader->head + RSTRING_LEN(io);
    } else if (oj_stringio_class == io_class) {
        VALUE s = rb_funcall2(io, oj_string_id, 0, 0);

        reader->read_func = 0;
        reader->in_str    = StringValuePtr(s);
        reader->head      = (char *)reader->in_str;
        reader->tail      = reader->head;
        reader->read_end  = reader->head + RSTRING_LEN(s);
    } else if (rb_cFile == io_class && Qnil != (stat = rb_funcall(io, oj_stat_id, 0)) &&
               Qnil != (ftype = rb_funcall(stat, oj_ftype_id, 0)) && 0 == strcmp("file", StringValuePtr(ftype)) &&
               0 == FIX2INT(rb_funcall(io, oj_pos_id, 0))) {
        reader->read_func = read_from_fd;
        reader->fd        = FIX2INT(rb_funcall(io, oj_fileno_id, 0));
    } else if (rb_respond_to(io, oj_readpartial_id)) {
        reader->read_func = read_from_io_partial;
        reader->io        = io;
    } else if (rb_respond_to(io, oj_read_id)) {
        reader->read_func = read_from_io;
        reader->io        = io;
    } else if (to_s) {
        volatile VALUE rstr = oj_safe_string_convert(io);

        reader->read_func = 0;
        reader->in_str    = StringValuePtr(rstr);
        reader->head      = (char *)reader->in_str;
        reader->tail      = reader->head;
        reader->read_end  = reader->head + RSTRING_LEN(rstr);
    } else {
        rb_raise(rb_eArgError, "parser io argument must be a String or respond to readpartial() or read().\n");
    }
}

int oj_reader_read(Reader reader) {
    int    err;
    size_t shift = 0;

    if (0 == reader->read_func) {
        return -1;
    }
    // if there is not much room to read into, shift or realloc a larger buffer.
    if (reader->head < reader->tail && 4096 > reader->end - reader->tail) {
        if (0 == reader->pro) {
            shift = reader->tail - reader->head;
        } else {
            shift = reader->pro - reader->head - 1;  // leave one character so we can backup one
        }
        if (0 >= shift) {                            /* no space left so allocate more */
            const char *old  = reader->head;
            size_t      size = reader->end - reader->head + BUF_PAD;

            if (reader->head == reader->base) {
                reader->head = OJ_R_ALLOC_N(char, size * 2);
                memcpy((char *)reader->head, old, size);
            } else {
                OJ_R_REALLOC_N(reader->head, char, size * 2);
            }
            reader->free_head = 1;
            reader->end       = reader->head + size * 2 - BUF_PAD;
            reader->tail      = reader->head + (reader->tail - old);
            reader->read_end  = reader->head + (reader->read_end - old);
            if (0 != reader->pro) {
                reader->pro = reader->head + (reader->pro - old);
            }
            if (0 != reader->str) {
                reader->str = reader->head + (reader->str - old);
            }
        } else {
            memmove((char *)reader->head, reader->head + shift, reader->read_end - (reader->head + shift));
            reader->tail -= shift;
            reader->read_end -= shift;
            if (0 != reader->pro) {
                reader->pro -= shift;
            }
            if (0 != reader->str) {
                reader->str -= shift;
            }
        }
    }
    err                       = reader->read_func(reader);
    *(char *)reader->read_end = '\0';

    return err;
}

static VALUE rescue_cb(VALUE rbuf, VALUE err) {
    VALUE clas = rb_obj_class(err);

    if (rb_eTypeError != clas && rb_eEOFError != clas) {
        Reader reader = (Reader)rbuf;

        rb_raise(clas, "at line %d, column %d\n", reader->line, reader->col);
    }
    return Qfalse;
}

static VALUE partial_io_cb(VALUE rbuf) {
    Reader reader = (Reader)rbuf;
    VALUE  args[1];
    VALUE  rstr;
    char  *str;
    size_t cnt;

    args[0] = ULONG2NUM(reader->end - reader->tail);
    rstr    = rb_funcall2(reader->io, oj_readpartial_id, 1, args);
    if (Qnil == rstr) {
        return Qfalse;
    }
    str = StringValuePtr(rstr);
    cnt = RSTRING_LEN(rstr);
    strcpy(reader->tail, str);
    reader->read_end = reader->tail + cnt;

    return Qtrue;
}

static VALUE io_cb(VALUE rbuf) {
    Reader reader = (Reader)rbuf;
    VALUE  args[1];
    VALUE  rstr;
    char  *str;
    size_t cnt;

    args[0] = ULONG2NUM(reader->end - reader->tail);
    rstr    = rb_funcall2(reader->io, oj_read_id, 1, args);
    if (Qnil == rstr) {
        return Qfalse;
    }
    str = StringValuePtr(rstr);
    cnt = RSTRING_LEN(rstr);
    strcpy(reader->tail, str);
    reader->read_end = reader->tail + cnt;

    return Qtrue;
}

static int read_from_io_partial(Reader reader) {
    return (Qfalse == rb_rescue(partial_io_cb, (VALUE)reader, rescue_cb, (VALUE)reader));
}

static int read_from_io(Reader reader) {
    return (Qfalse == rb_rescue(io_cb, (VALUE)reader, rescue_cb, (VALUE)reader));
}

static int read_from_fd(Reader reader) {
    ssize_t cnt;
    size_t  max = reader->end - reader->tail;

    cnt = read(reader->fd, reader->tail, max);
    if (cnt <= 0) {
        return -1;
    } else if (0 != cnt) {
        reader->read_end = reader->tail + cnt;
    }
    return 0;
}

// This is only called when the end of the string is reached so just return -1.
/*
static int
read_from_str(Reader reader) {
    return -1;
}
*/