summaryrefslogtreecommitdiffhomepage
path: root/libs/lmo/src/lmo_lualib.c
blob: ade8e01f72321a0c210788239f3dc633fc89e3b9 (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * lmo - Lua Machine Objects - Lua binding
 *
 *   Copyright (C) 2009-2012 Jo-Philipp Wich <xm@subsignal.org>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include "lmo_lualib.h"

extern char _lmo_error[1024];


static int lmo_L_open(lua_State *L) {
	const char *filename = luaL_checklstring(L, 1, NULL);
	lmo_archive_t *ar, **udata;

	if( (ar = lmo_open(filename)) != NULL )
	{
		if( (udata = lua_newuserdata(L, sizeof(lmo_archive_t *))) != NULL )
		{
			*udata = ar;
			luaL_getmetatable(L, LMO_ARCHIVE_META);
			lua_setmetatable(L, -2);
			return 1;
		}

		lmo_close(ar);
		lua_pushnil(L);
		lua_pushstring(L, "out of memory");
		return 2;
	}

	lua_pushnil(L);
	lua_pushstring(L, lmo_error());
	return 2;
}

static uint32_t _lmo_hash_string(lua_State *L, int n) {
	size_t len;
	const char *str = luaL_checklstring(L, n, &len);
	char res[4096];
	char *ptr, prev;

	if (!str || len >= sizeof(res))
		return 0;

	while (*str && isspace(*str))
		str++;

	for (prev = 0, ptr = res; *str; prev = *str, str++)
	{
		if (isspace(*str))
		{
			if (isspace(prev))
				continue;

			*ptr++ = ' ';
		}
		else
		{
			*ptr++ = *str;
		}
	}

	while ((ptr > res) && isspace(*ptr))
		ptr--;

	return sfh_hash(res, ptr - res);
}

static int lmo_L_hash(lua_State *L) {
	uint32_t hash = _lmo_hash_string(L, 1);
	lua_pushinteger(L, (lua_Integer)hash);
	return 1;
}

static lmo_luaentry_t *_lmo_push_entry(lua_State *L) {
	lmo_luaentry_t *le;

	if( (le = lua_newuserdata(L, sizeof(lmo_luaentry_t))) != NULL )
	{
		luaL_getmetatable(L, LMO_ENTRY_META);
		lua_setmetatable(L, -2);

		return le;
	}

	return NULL;
}

static int _lmo_lookup(lua_State *L, lmo_archive_t *ar, uint32_t hash) {
	lmo_entry_t *e = ar->index;
	lmo_luaentry_t *le = NULL;

	while( e != NULL )
	{
		if( e->key_id == hash )
		{
			if( (le = _lmo_push_entry(L)) != NULL )
			{
				le->archive = ar;
				le->entry   = e;
				return 1;
			}
			else
			{
				lua_pushnil(L);
				lua_pushstring(L, "out of memory");
				return 2;
			}
		}

		e = e->next;
	}

	lua_pushnil(L);
	return 1;
}

static int lmo_L_get(lua_State *L) {
	lmo_archive_t **ar = luaL_checkudata(L, 1, LMO_ARCHIVE_META);
	uint32_t hash = (uint32_t) luaL_checkinteger(L, 2);
	return _lmo_lookup(L, *ar, hash);
}

static int lmo_L_lookup(lua_State *L) {
	lmo_archive_t **ar = luaL_checkudata(L, 1, LMO_ARCHIVE_META);
	uint32_t hash = _lmo_hash_string(L, 2);
	return _lmo_lookup(L, *ar, hash);
}

static int lmo_L_foreach(lua_State *L) {
	lmo_archive_t **ar = luaL_checkudata(L, 1, LMO_ARCHIVE_META);
	lmo_entry_t *e = (*ar)->index;

	if( lua_isfunction(L, 2) )
	{
		while( e != NULL )
		{
			lua_pushvalue(L, 2);
			lua_pushinteger(L, e->key_id);
			lua_pushlstring(L, &(*ar)->mmap[e->offset], e->length);
			lua_pcall(L, 2, 0, 0);
			e = e->next;
		}
	}

	return 0;
}

static int lmo_L__gc(lua_State *L) {
	lmo_archive_t **ar = luaL_checkudata(L, 1, LMO_ARCHIVE_META);

	if( (*ar) != NULL )
		lmo_close(*ar);

	*ar = NULL;

	return 0;
}

static int lmo_L__tostring(lua_State *L) {
	lmo_archive_t **ar = luaL_checkudata(L, 1, LMO_ARCHIVE_META);
	lua_pushfstring(L, "LMO Archive (%d bytes)", (*ar)->length);
	return 1;
}


static int _lmo_convert_entry(lua_State *L, int idx) {
	lmo_luaentry_t *le = luaL_checkudata(L, idx, LMO_ENTRY_META);

	lua_pushlstring(L,
		&le->archive->mmap[le->entry->offset],
		le->entry->length
	);

	return 1;
}

static int lmo_L_entry__tostring(lua_State *L) {
	return _lmo_convert_entry(L, 1);
}

static int lmo_L_entry__concat(lua_State *L) {
	if( lua_isuserdata(L, 1) )
		_lmo_convert_entry(L, 1);
	else
		lua_pushstring(L, lua_tostring(L, 1));

	if( lua_isuserdata(L, 2) )
		_lmo_convert_entry(L, 2);
	else
		lua_pushstring(L, lua_tostring(L, 2));

	lua_concat(L, 2);

	return 1;
}

static int lmo_L_entry__len(lua_State *L) {
	lmo_luaentry_t *le = luaL_checkudata(L, 1, LMO_ENTRY_META);
	lua_pushinteger(L, le->entry->length);
	return 1;
}

static int lmo_L_entry__gc(lua_State *L) {
	lmo_luaentry_t *le = luaL_checkudata(L, 1, LMO_ENTRY_META);
	le->archive = NULL;
	le->entry   = NULL;
	return 0;
}


/* lmo method table */
static const luaL_reg M[] = {
	{"close",		lmo_L__gc},
	{"get",			lmo_L_get},
	{"lookup",		lmo_L_lookup},
	{"foreach",		lmo_L_foreach},
	{"__tostring",	lmo_L__tostring},
	{"__gc",		lmo_L__gc},
	{NULL,			NULL}
};

/* lmo.entry method table */
static const luaL_reg E[] = {
	{"__tostring",	lmo_L_entry__tostring},
	{"__concat",	lmo_L_entry__concat},
	{"__len",		lmo_L_entry__len},
	{"__gc",		lmo_L_entry__gc},
	{NULL,			NULL}
};

/* module table */
static const luaL_reg R[] = {
	{"open",	lmo_L_open},
	{"hash",	lmo_L_hash},
	{NULL,		NULL}
};

LUALIB_API int luaopen_lmo(lua_State *L) {
	luaL_newmetatable(L, LMO_ARCHIVE_META);
	luaL_register(L, NULL, M);
	lua_pushvalue(L, -1);
	lua_setfield(L, -2, "__index");
	lua_setglobal(L, LMO_ARCHIVE_META);

	luaL_newmetatable(L, LMO_ENTRY_META);
	luaL_register(L, NULL, E);
	lua_pushvalue(L, -1);
	lua_setfield(L, -2, "__index");
	lua_setglobal(L, LMO_ENTRY_META);	

	luaL_register(L, LMO_LUALIB_META, R);

	return 1;
}