Moved apache code into a folder to help prepare for packaging where we dont want...
[httpd-crcsyncproxy.git] / apache / modules / lua / lua_apr.c
blobfb514ebc05a1d4b4923aeed51b65c142dfe30cb1
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 #include "apr.h"
18 #include "apr_tables.h"
20 #include "lua.h"
21 #include "lauxlib.h"
22 #include "lualib.h"
23 #include "lua_apr.h"
25 /**
26 * make a userdata out of a C pointer, and vice versa
27 * instead of using lightuserdata
29 #ifndef lua_boxpointer
30 #define lua_boxpointer(L,u) (*(void **)(lua_newuserdata(L, sizeof(void *))) = (u))
31 #define lua_unboxpointer(L,i) (*(void **)(lua_touserdata(L, i)))
32 #endif
35 apr_table_t *check_apr_table(lua_State *L, int index)
37 apr_table_t *t;
38 luaL_checkudata(L, index, "Apr.Table");
39 t = (apr_table_t *) lua_unboxpointer(L, index);
40 return t;
44 void apl_push_apr_table(lua_State *L, apr_table_t *t)
46 lua_boxpointer(L, t);
47 luaL_getmetatable(L, "Apr.Table");
48 lua_setmetatable(L, -2);
51 static int lua_table_set(lua_State *L)
53 apr_table_t *t = check_apr_table(L, 1);
54 const char *key = luaL_checkstring(L, 2);
55 const char *val = luaL_checkstring(L, 3);
57 apr_table_set(t, key, val);
58 return 0;
61 static int lua_table_get(lua_State *L)
63 apr_table_t *t = check_apr_table(L, 1);
64 const char *key = luaL_checkstring(L, 2);
65 const char *val = apr_table_get(t, key);
66 lua_pushstring(L, val);
67 return 1;
70 static const luaL_reg lua_table_methods[] = {
71 {"set", lua_table_set},
72 {"get", lua_table_get},
73 {0, 0}
77 int apr_lua_init(lua_State *L, apr_pool_t *p)
79 luaL_newmetatable(L, "Apr.Table");
80 luaL_openlib(L, "apr_table", lua_table_methods, 0);
81 lua_pushstring(L, "__index");
82 lua_pushstring(L, "get");
83 lua_gettable(L, 2);
84 lua_settable(L, 1);
86 lua_pushstring(L, "__newindex");
87 lua_pushstring(L, "set");
88 lua_gettable(L, 2);
89 lua_settable(L, 1);
91 return 0;