2 * lmo - Lua Machine Objects - Lookup utility
4 * Copyright (C) 2009 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);
39 lua_pushstring(L, "out of memory");
44 lua_pushstring(L, lmo_error());
48 static int lmo_L_hash(lua_State *L) {
49 const char *data = luaL_checkstring(L, 1);
50 uint32_t hash = sfh_hash(data, strlen(data));
51 lua_pushnumber(L, hash);
55 static int lmo_L_lookup(lua_State *L) {
56 lmo_archive_t **ar = luaL_checkudata(L, 1, LMO_ARCHIVE_META);
57 lmo_entry_t *e = (*ar)->index;
58 const char *key = luaL_checkstring(L, 2);
59 uint32_t hash = sfh_hash(key, strlen(key));
63 if( e->key_id == hash )
65 lua_pushlstring(L, &(*ar)->mmap[e->offset], e->length);
76 static int lmo_L__gc(lua_State *L) {
77 lmo_archive_t **ar = luaL_checkudata(L, 1, LMO_ARCHIVE_META);
87 static int lmo_L__tostring(lua_State *L) {
88 lmo_archive_t **ar = luaL_checkudata(L, 1, LMO_ARCHIVE_META);
89 lua_pushfstring(L, "LMO Archive (%d bytes)", (*ar)->length);
95 static const luaL_reg M[] = {
97 {"lookup", lmo_L_lookup},
98 {"__tostring", lmo_L__tostring},
104 static const luaL_reg R[] = {
105 {"open", lmo_L_open},
106 {"hash", lmo_L_hash},
110 LUALIB_API int luaopen_lmo(lua_State *L) {
111 luaL_newmetatable(L, LMO_LUALIB_META);
112 luaL_register(L, NULL, R);
113 lua_pushvalue(L, -1);
114 lua_setfield(L, -2, "__index");
115 lua_setglobal(L, LMO_LUALIB_META);
117 luaL_newmetatable(L, LMO_ARCHIVE_META);
118 luaL_register(L, NULL, M);
119 lua_pushvalue(L, -1);
120 lua_setfield(L, -2, "__index");
121 lua_setglobal(L, LMO_ARCHIVE_META);