lua ip2region c module forward with release version published

This commit is contained in:
lionsoul 2018-10-06 11:05:01 +08:00
parent a589b79e6c
commit 08eea15172
3 changed files with 38 additions and 36 deletions

View File

@ -55,18 +55,23 @@ IP2R_API uint_t ip2region_create(ip2region_t ip2rObj, const char *dbFile)
*/ */
IP2R_API uint_t ip2region_destroy(ip2region_t ip2rObj) IP2R_API uint_t ip2region_destroy(ip2region_t ip2rObj)
{ {
if ( ip2rObj->HeaderSip != NULL ) {
IP2R_FREE(ip2rObj->HeaderSip); IP2R_FREE(ip2rObj->HeaderSip);
ip2rObj->HeaderSip = NULL; ip2rObj->HeaderSip = NULL;
}
if ( ip2rObj->HeaderPtr != NULL ) {
IP2R_FREE(ip2rObj->HeaderPtr); IP2R_FREE(ip2rObj->HeaderPtr);
ip2rObj->HeaderPtr = NULL; ip2rObj->HeaderPtr = NULL;
}
//close the db file resource // close the db file resource
if ( ip2rObj->dbHandler != NULL ) { if ( ip2rObj->dbHandler != NULL ) {
fclose(ip2rObj->dbHandler); fclose(ip2rObj->dbHandler);
ip2rObj->dbHandler = NULL; ip2rObj->dbHandler = NULL;
} }
//free the db binary string // free the db binary string
if ( ip2rObj->dbBinStr != NULL ) { if ( ip2rObj->dbBinStr != NULL ) {
IP2R_FREE(ip2rObj->dbBinStr); IP2R_FREE(ip2rObj->dbBinStr);
ip2rObj->dbBinStr = NULL; ip2rObj->dbBinStr = NULL;

View File

@ -51,18 +51,21 @@ static int lua_ip2region_destroy(lua_State *L)
#define get_search_args(entry, ip) \ #define get_search_args(L, entry, ip) \
do { \ do { \
luaL_argcheck(L, lua_gettop(L) == 2, 2, "Object and ip address needed"); \
entry = (ip2region_entry *) luaL_checkudata(L, 1, L_METATABLE_NAME); \ entry = (ip2region_entry *) luaL_checkudata(L, 1, L_METATABLE_NAME); \
ip = luaL_checkstring(L, 2); \ ip = luaL_checkstring(L, 2); \
} while (0); \ } while (0); \
#define set_search_result(data, rptr) \ #define set_search_result(L, data) \
do { \ do { \
rptr = (datablock_entry *) lua_newuserdata(L, sizeof(datablock_entry)); \ lua_newtable(L); \
rptr->city_id = data.city_id; \ lua_pushinteger(L, data.city_id); \
memcpy(rptr->region, data.region, strlen(data.region)); \ lua_setfield(L, -2, "city_id"); \
lua_pushfstring(L, "%s", data.region); \
lua_setfield(L, -2, "region"); \
} while (0); \ } while (0); \
@ -71,12 +74,13 @@ static int lua_ip2region_memory_search(lua_State *L)
{ {
ip2region_entry *self; ip2region_entry *self;
const char *addr; const char *addr;
datablock_entry data, *rptr; datablock_entry data;
// luaL_argcheck(L, lua_gettop(L) == 2, 2, "object and ip address needed");
// self = (ip2region_entry *) luaL_checkudata(L, 1, L_METATABLE_NAME); // self = (ip2region_entry *) luaL_checkudata(L, 1, L_METATABLE_NAME);
// addr = luaL_checkstring(L, 2); // addr = luaL_checkstring(L, 2);
/* Check and get the search parameters */ /* Check and get the search parameters */
get_search_args(self, addr); get_search_args(L, self, addr);
/* do the memory search */ /* do the memory search */
if ( ip2region_memory_search(self, ip2long(addr), &data) == 0 ) { if ( ip2region_memory_search(self, ip2long(addr), &data) == 0 ) {
@ -84,11 +88,12 @@ static int lua_ip2region_memory_search(lua_State *L)
return 1; return 1;
} }
// rptr = (datablock_entry *) lua_newuserdata(L, sizeof(datablock_entry)); // lua_newtable(L);
// rptr->city_id = data.city_id; // lua_pushinteger(L, data.city_id);
// memcpy(rptr->data.region, data.region, strlen(data.region)); // lua_setfield(L, -2, "city_id");
/* Set the return value */ // lua_pushfstring(L, "%s", data.region);
set_search_result(data, rptr); // lua_setfield(L, -2, "region");
set_search_result(L, data);
return 1; return 1;
} }
@ -98,10 +103,10 @@ static int lua_ip2region_binary_search(lua_State *L)
{ {
ip2region_entry *self; ip2region_entry *self;
const char *addr; const char *addr;
datablock_entry data, *rptr; datablock_entry data;
/* Check and get the search parameters */ /* Check and get the search parameters */
get_search_args(self, addr); get_search_args(L, self, addr);
/* Do the binary search */ /* Do the binary search */
if ( ip2region_binary_search(self, ip2long(addr), &data) == 0 ) { if ( ip2region_binary_search(self, ip2long(addr), &data) == 0 ) {
@ -110,7 +115,7 @@ static int lua_ip2region_binary_search(lua_State *L)
} }
/* Set the return value */ /* Set the return value */
set_search_result(data, rptr); set_search_result(L, data);
return 1; return 1;
} }
@ -120,10 +125,10 @@ static int lua_ip2region_btree_search(lua_State *L)
{ {
ip2region_entry *self; ip2region_entry *self;
const char *addr; const char *addr;
datablock_entry data, *rptr; datablock_entry data;
/* Check and get the search parameters */ /* Check and get the search parameters */
get_search_args(self, addr); get_search_args(L, self, addr);
/* Do the btree search */ /* Do the btree search */
if ( ip2region_btree_search(self, ip2long(addr), &data) == 0 ) { if ( ip2region_btree_search(self, ip2long(addr), &data) == 0 ) {
@ -132,7 +137,7 @@ static int lua_ip2region_btree_search(lua_State *L)
} }
/* Set the return value */ /* Set the return value */
set_search_result(data, rptr); set_search_result(L, data);
return 1; return 1;
} }
@ -179,25 +184,19 @@ static int lua_ip2region_tostring(lua_State *L)
} }
/** module method array */ /** module method array */
static const struct luaL_Reg ip2region_methods[] = { static const struct luaL_Reg ip2region_methods[] = {
{ "new", lua_ip2region_new },
{ "ip2long", lua_ip2long }, { "ip2long", lua_ip2long },
{ "memorySearch", lua_ip2region_memory_search }, { "memorySearch", lua_ip2region_memory_search },
{ "binarySearch", lua_ip2region_binary_search }, { "binarySearch", lua_ip2region_binary_search },
{ "btreeSearch", lua_ip2region_btree_search }, { "btreeSearch", lua_ip2region_btree_search },
{ "close", lua_ip2region_destroy },
{ "__gc", lua_ip2region_destroy }, { "__gc", lua_ip2region_destroy },
{ "__tostring", lua_ip2region_tostring }, { "__tostring", lua_ip2region_tostring },
{ NULL, NULL }, { NULL, NULL },
}; };
/** module function array */
static const struct luaL_Reg ip2region_functions[] = {
{ "new", lua_ip2region_new },
{ NULL, NULL }
};
/** module open function interface */ /** module open function interface */
int luaopen_Ip2region(lua_State *L) int luaopen_Ip2region(lua_State *L)
{ {
@ -218,9 +217,5 @@ int luaopen_Ip2region(lua_State *L)
*/ */
luaL_setfuncs(L, ip2region_methods, 0); luaL_setfuncs(L, ip2region_methods, 0);
/* Finally register the object.func functions
* into the table witch at the top of the stack */
luaL_newlib(L, ip2region_functions);
return 1; return 1;
} }

View File

@ -75,11 +75,13 @@ while ( true ) do
if ( data == nil ) then if ( data == nil ) then
io.write("Failed for ip=", line, " is it a valid ip address ?"); io.write("Failed for ip=", line, " is it a valid ip address ?");
else else
print(ip2region);
io.write(string.format("%u|%s in %5f millseconds\n", data.city_id, data.region, cost_time)); io.write(string.format("%u|%s in %5f millseconds\n", data.city_id, data.region, cost_time));
end end
end end
end end
-- close the object -- close the object
-- Also the lua gc will invoke it automatically
-- print(ip2region); -- print(ip2region);
ip2region:close(); ip2region:close();