lua binding module interface stdlize with new and ip2long method access by . support
This commit is contained in:
parent
01ab0a0373
commit
a589b79e6c
|
|
@ -46,9 +46,10 @@ construct method
|
||||||
@param obj
|
@param obj
|
||||||
@return Ip2region object
|
@return Ip2region object
|
||||||
--]]
|
--]]
|
||||||
function _M:new(obj)
|
function _M.new(dbFile)
|
||||||
obj = obj or {};
|
obj = {};
|
||||||
setmetatable(obj, _M);
|
setmetatable(obj, _M);
|
||||||
|
obj.dbFile = dbFile;
|
||||||
return obj;
|
return obj;
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -80,7 +81,13 @@ internal function to convert the string ip to a long value
|
||||||
@param ip
|
@param ip
|
||||||
@return Integer
|
@return Integer
|
||||||
]]--
|
]]--
|
||||||
function _M:ip2long(ip)
|
function _M.ip2long(self, ip)
|
||||||
|
-- dynamic arguments checking
|
||||||
|
-- to support object.ip2long and object:ip2long access
|
||||||
|
if ( type(self) == "string") then
|
||||||
|
ip = self;
|
||||||
|
end
|
||||||
|
|
||||||
local ini = 1;
|
local ini = 1;
|
||||||
local iip = 0;
|
local iip = 0;
|
||||||
local off = 24;
|
local off = 24;
|
||||||
|
|
|
||||||
|
|
@ -27,11 +27,12 @@ ip2region>>
|
||||||
* 2, 通过如下流程在你的lua程序中使用
|
* 2, 通过如下流程在你的lua程序中使用
|
||||||
```lua
|
```lua
|
||||||
-- 包含模块
|
-- 包含模块
|
||||||
require "Ip2region";
|
local Ip2region = require "Ip2region";
|
||||||
|
|
||||||
-- 创建查询对象
|
-- 创建查询对象
|
||||||
-- 设置ip2region.db的文件地址,dbFile表示ip2region.db数据库文件的地址
|
-- 设置ip2region.db的文件地址,dbFile表示ip2region.db数据库文件的地址
|
||||||
local ip2region = Ip2region:new({dbFile = "ip2region.db file path"});
|
-- 注意new方法是通过“.”调用,而不是“:”
|
||||||
|
local ip2region = Ip2region.new("ip2region.db file path");
|
||||||
|
|
||||||
-- 也可以通过如下方式设置dbFile
|
-- 也可以通过如下方式设置dbFile
|
||||||
-- ip2region.dbFile = "ip2region.db file path";
|
-- ip2region.dbFile = "ip2region.db file path";
|
||||||
|
|
|
||||||
|
|
@ -12,14 +12,14 @@ Usage: lua testSearcher.lua [ip2region db file] [algorithm]
|
||||||
os.exit();
|
os.exit();
|
||||||
end
|
end
|
||||||
|
|
||||||
require "Ip2region";
|
local Ip2region = require "Ip2region";
|
||||||
-- local cjson = require "cjson";
|
-- local cjson = require "cjson";
|
||||||
-- local socket = require "socket";
|
-- local socket = require "socket";
|
||||||
|
|
||||||
|
|
||||||
-- check and parse the dbFile and the method algorithm
|
-- check and parse the dbFile and the method algorithm
|
||||||
-- Create a new ip2region object by the new interface
|
-- Create a new ip2region object by the new interface
|
||||||
local ip2region = Ip2region:new({dbFile = arg[1]});
|
local ip2region = Ip2region.new(arg[1]);
|
||||||
|
|
||||||
-- reset the dbFile by the follow two ways:
|
-- reset the dbFile by the follow two ways:
|
||||||
-- ip2region.dbFile = arg[1];
|
-- ip2region.dbFile = arg[1];
|
||||||
|
|
@ -58,7 +58,7 @@ while ( true ) do
|
||||||
break;
|
break;
|
||||||
elseif ( line == "quit" ) then
|
elseif ( line == "quit" ) then
|
||||||
break;
|
break;
|
||||||
elseif ( ip2region:ip2long(line) == nil ) then
|
elseif ( ip2region.ip2long(line) == nil ) then
|
||||||
print("Invalid ip address=", line);
|
print("Invalid ip address=", line);
|
||||||
else
|
else
|
||||||
local data;
|
local data;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue