peter@peterodding.comhttp://peterodding.com/code/lua/apr/
local apr = require 'apr.core'
apr._VERSION = '0.9.29'
function apr.md5(input, binary)
assert(type(input) == 'string', "bad argument #1 to apr.md5() (string expected)")
local context, digest, status, errmsg, errcode
context, errmsg, errcode = apr.md5_init()
if context then
status, errmsg, errcode = context:update(input)
if status then
digest, errmsg, errcode = context:digest(binary)
if digest then return digest end
end
end
return nil, errmsg, errcode
end
function apr.sha1(input, binary)
assert(type(input) == 'string', "bad argument #1 to apr.sha1() (string expected)")
local context, digest, status, errmsg, errcode
context, errmsg, errcode = apr.sha1_init()
if context then
status, errmsg, errcode = context:update(input)
if status then
digest, errmsg, errcode = context:digest(binary)
if digest then return digest end
end
end
return nil, errmsg, errcode
end
http://en.wikipedia.org/wiki/PATH_%28variable%29
function apr.filepath_which(program, find_all)
local split = apr.filepath_list_split
local is_windows = apr.platform_get() == 'WIN32'
local extensions = is_windows and split(apr.env_get 'PATHEXT')
local results = find_all and {}
for _, directory in ipairs(split(apr.env_get 'PATH')) do
local candidate = apr.filepath_merge(directory, program)
if apr.stat(candidate, 'type') == 'file' then
TODO if not find_all then return candidate end
results[#results + 1] = candidate
end
if is_windows and #extensions >= 1 then
for _, extension in ipairs(extensions) do
candidate = apr.filepath_merge(directory, program .. '.' .. extension)
if apr.stat(candidate, 'type') == 'file' then
if not find_all then return candidate end
results[#results + 1] = candidate
end
end
end
end
return results
end
function apr.glob(pattern, ignorecase)
local fnmatch = apr.fnmatch
local yield = coroutine.yield
local directory, pattern = apr.filepath_parent(pattern)
local handle = assert(apr.dir_open(directory))
return coroutine.wrap(function()
for path, name in handle:entries('path', 'name') do
if fnmatch(pattern, name, ignorecase) then
yield(path)
end
end
handle:close()
end)
end
http://en.wikipedia.org/wiki/Percent-encoding
function apr.uri_encode(s)
local byte = string.byte
local format = string.format
return (s:gsub('[^A-Za-z0-9_.-]', function(c)
if c == ' ' then
return '+'
else
return format('%%%02x', byte(c))
end
end))
end
http://en.wikipedia.org/wiki/Percent-encoding
function apr.uri_decode(s)
local char = string.char
local tonumber = tonumber
s = s:gsub('+', ' ')
return (s:gsub('%%(%x%x?)', function(code)
return char(tonumber(code, 16))
end))
end
return apr