summaryrefslogtreecommitdiffstats
path: root/lem/parsers/core.c
blob: 0b0bb107e6fdf25e8ef931abcb9126ee51baac2d (plain) (blame)
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
/*
 * This file is part of LEM, a Lua Event Machine.
 * Copyright 2011-2013 Emil Renner Berthing
 *
 * LEM is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * LEM is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with LEM.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <lem-parsers.h>

/*
 * read available data
 */
static int
parse_available_process(lua_State *T, struct lem_inputbuf *b)
{
	size_t size = b->end - b->start;

	if (size == 0)
		return LEM_PMORE;

	lua_pushlstring(T, b->buf + b->start, size);
	b->start = b->end = 0;
	return 1;
}

static const struct lem_parser parser_available = {
	.process = parse_available_process,
};

/*
 * read a specified number of bytes
 */
static void
parse_target_init(lua_State *T, struct lem_inputbuf *b)
{
	b->u = luaL_checknumber(T, 3);
	b->parts = 0;
	lua_settop(T, 2);
}

static int
parse_target_process(lua_State *T, struct lem_inputbuf *b)
{
	unsigned long target = b->u;
	unsigned int size = b->end - b->start;

	if ((unsigned long)size >= target) {
		lua_pushlstring(T, b->buf + b->start, target);
		lua_concat(T, b->parts + 1);
		b->start += target;
		if (b->start == b->end)
			b->start = b->end = 0;

		return 1;
	}

	if (b->end == LEM_INPUTBUF_SIZE) {
		lua_pushlstring(T, b->buf + b->start, size);
		b->parts++;
		if (b->parts == LUA_MINSTACK-2) {
			lua_concat(T, LUA_MINSTACK-2);
			b->parts = 1;
		}
		b->start = b->end = 0;
		b->u = target - size;
	}

	return LEM_PMORE;
}

static const struct lem_parser parser_target = {
	.init    = parse_target_init,
	.process = parse_target_process,
};

/*
 * read all data until stream closes
 */
static void
parse_all_init(lua_State *T, struct lem_inputbuf *b)
{
	b->parts = 0;
	lua_settop(T, 2);
}

static int
parse_all_process(lua_State *T, struct lem_inputbuf *b)
{
	if (b->end == LEM_INPUTBUF_SIZE) {
		lua_pushlstring(T, b->buf + b->start,
				LEM_INPUTBUF_SIZE - b->start);
		b->parts++;
		if (b->parts == LUA_MINSTACK-2) {
			lua_concat(T, LUA_MINSTACK-2);
			b->parts = 1;
		}
		b->start = b->end = 0;
	}

	return LEM_PMORE;
}

static int
parse_all_destroy(lua_State *T, struct lem_inputbuf *b, enum lem_preason reason)
{
	unsigned int size;

	if (reason != LEM_PCLOSED)
		return LEM_PMORE;

	size = b->end - b->start;
	if (size > 0) {
		lua_pushlstring(T, b->buf + b->start, size);
		b->parts++;
		b->start = b->end = 0;
	}

	lua_concat(T, b->parts);
	return 1;
}

static const struct lem_parser parser_all = {
	.init    = parse_all_init,
	.process = parse_all_process,
	.destroy = parse_all_destroy,
};

/*
 * read a line
 */
static void
parse_line_init(lua_State *T, struct lem_inputbuf *b)
{
	const char *stopbyte = luaL_optstring(T, 3, "\n");

	b->u = (b->start << 8) | stopbyte[0];
	b->parts = 0;
	lua_settop(T, 2);
}

static int
parse_line_process(lua_State *T, struct lem_inputbuf *b)
{
	unsigned int i;
	unsigned char stopbyte = b->u & 0xFF;

	for (i = b->u >> 8; i < b->end; i++) {
		if (b->buf[i] == stopbyte) {
			lua_pushlstring(T, b->buf + b->start, i - b->start);
			lua_concat(T, b->parts + 1);
			i++;
			if (i == b->end)
				b->start = b->end = 0;
			else
				b->start = i;
			return 1;
		}
	}

	if (b->end == LEM_INPUTBUF_SIZE) {
		lua_pushlstring(T, b->buf + b->start, b->end - b->start);
		b->parts++;
		if (b->parts == LUA_MINSTACK-2) {
			lua_concat(T, LUA_MINSTACK-2);
			b->parts = 1;
		}
		b->start = b->end = 0;
		b->u = stopbyte;
	} else
		b->u = (i << 8) | stopbyte;

	return LEM_PMORE;
}

static const struct lem_parser parser_line = {
	.init    = parse_line_init,
	.process = parse_line_process,
};

int
luaopen_lem_parsers_core(lua_State *L)
{
	/* create module table */
	lua_newtable(L);

	/* create lookup table */
	lua_createtable(L, 0, 4);
	/* push parser_line */
	lua_pushlightuserdata(L, (void *)&parser_available);
	lua_setfield(L, -2, "available");
	/* push parser_target */
	lua_pushlightuserdata(L, (void *)&parser_target);
	lua_setfield(L, -2, "target");
	/* push parser_all */
	lua_pushlightuserdata(L, (void *)&parser_all);
	lua_setfield(L, -2, "*a");
	/* push parser_line */
	lua_pushlightuserdata(L, (void *)&parser_line);
	lua_setfield(L, -2, "*l");
	/* insert lookup table */
	lua_setfield(L, -2, "lookup");

	return 1;
}