2 * lmo - Lua Machine Objects - Lua binding
4 * Copyright (C) 2009-2012 Jo-Philipp Wich <xm@subsignal.org>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #include "lmo_lualib.h"
21 extern char _lmo_error[1024];
24 static int lmo_L_open(lua_State *L) {
25 const char *filename = luaL_checklstring(L, 1, NULL);
26 lmo_archive_t *ar, **udata;
28 if( (ar = lmo_open(filename)) != NULL )
30 if( (udata = lua_newuserdata(L, sizeof(lmo_archive_t *))) != NULL )
33 luaL_getmetatable(L, LMO_ARCHIVE_META);
34 lua_setmetatable(L, -2);
40 lua_pushstring(L, "out of memory");
45 lua_pushstring(L, lmo_error());
49 static uint32_t _lmo_hash_string(lua_State *L, int n) {
51 const char *str = luaL_checklstring(L, n, &len);
55 if (!str || len >= sizeof(res))
58 for (prev = ' ', ptr = res; *str; prev = *str, str++)
71 if ((ptr > res) && isspace(*(ptr-1)))
74 return sfh_hash(res, ptr - res);
77 static int lmo_L_hash(lua_State *L) {
78 uint32_t hash = _lmo_hash_string(L, 1);
79 lua_pushinteger(L, (lua_Integer)hash);
83 static lmo_luaentry_t *_lmo_push_entry(lua_State *L) {
86 if( (le = lua_newuserdata(L, sizeof(lmo_luaentry_t))) != NULL )
88 luaL_getmetatable(L, LMO_ENTRY_META);
89 lua_setmetatable(L, -2);
97 static int _lmo_lookup(lua_State *L, lmo_archive_t *ar, uint32_t hash) {
98 lmo_entry_t *e = ar->index;
99 lmo_luaentry_t *le = NULL;
103 if( e->key_id == hash )
105 if( (le = _lmo_push_entry(L)) != NULL )
114 lua_pushstring(L, "out of memory");
126 static int lmo_L_get(lua_State *L) {
127 lmo_archive_t **ar = luaL_checkudata(L, 1, LMO_ARCHIVE_META);
128 uint32_t hash = (uint32_t) luaL_checkinteger(L, 2);
129 return _lmo_lookup(L, *ar, hash);
132 static int lmo_L_lookup(lua_State *L) {
133 lmo_archive_t **ar = luaL_checkudata(L, 1, LMO_ARCHIVE_META);
134 uint32_t hash = _lmo_hash_string(L, 2);
135 return _lmo_lookup(L, *ar, hash);
138 static int lmo_L_foreach(lua_State *L) {
139 lmo_archive_t **ar = luaL_checkudata(L, 1, LMO_ARCHIVE_META);
140 lmo_entry_t *e = (*ar)->index;
142 if( lua_isfunction(L, 2) )
147 lua_pushinteger(L, e->key_id);
148 lua_pushlstring(L, &(*ar)->mmap[e->offset], e->length);
149 lua_pcall(L, 2, 0, 0);
157 static int lmo_L__gc(lua_State *L) {
158 lmo_archive_t **ar = luaL_checkudata(L, 1, LMO_ARCHIVE_META);
168 static int lmo_L__tostring(lua_State *L) {
169 lmo_archive_t **ar = luaL_checkudata(L, 1, LMO_ARCHIVE_META);
170 lua_pushfstring(L, "LMO Archive (%d bytes)", (*ar)->length);
175 static int _lmo_convert_entry(lua_State *L, int idx) {
176 lmo_luaentry_t *le = luaL_checkudata(L, idx, LMO_ENTRY_META);
179 &le->archive->mmap[le->entry->offset],
186 static int lmo_L_entry__tostring(lua_State *L) {
187 return _lmo_convert_entry(L, 1);
190 static int lmo_L_entry__concat(lua_State *L) {
191 if( lua_isuserdata(L, 1) )
192 _lmo_convert_entry(L, 1);
194 lua_pushstring(L, lua_tostring(L, 1));
196 if( lua_isuserdata(L, 2) )
197 _lmo_convert_entry(L, 2);
199 lua_pushstring(L, lua_tostring(L, 2));
206 static int lmo_L_entry__len(lua_State *L) {
207 lmo_luaentry_t *le = luaL_checkudata(L, 1, LMO_ENTRY_META);
208 lua_pushinteger(L, le->entry->length);
212 static int lmo_L_entry__gc(lua_State *L) {
213 lmo_luaentry_t *le = luaL_checkudata(L, 1, LMO_ENTRY_META);
220 /* lmo method table */
221 static const luaL_reg M[] = {
222 {"close", lmo_L__gc},
224 {"lookup", lmo_L_lookup},
225 {"foreach", lmo_L_foreach},
226 {"__tostring", lmo_L__tostring},
231 /* lmo.entry method table */
232 static const luaL_reg E[] = {
233 {"__tostring", lmo_L_entry__tostring},
234 {"__concat", lmo_L_entry__concat},
235 {"__len", lmo_L_entry__len},
236 {"__gc", lmo_L_entry__gc},
241 static const luaL_reg R[] = {
242 {"open", lmo_L_open},
243 {"hash", lmo_L_hash},
247 LUALIB_API int luaopen_lmo(lua_State *L) {
248 luaL_newmetatable(L, LMO_ARCHIVE_META);
249 luaL_register(L, NULL, M);
250 lua_pushvalue(L, -1);
251 lua_setfield(L, -2, "__index");
252 lua_setglobal(L, LMO_ARCHIVE_META);
254 luaL_newmetatable(L, LMO_ENTRY_META);
255 luaL_register(L, NULL, E);
256 lua_pushvalue(L, -1);
257 lua_setfield(L, -2, "__index");
258 lua_setglobal(L, LMO_ENTRY_META);
260 luaL_register(L, LMO_LUALIB_META, R);