Closer to release...

This commit is contained in:
Diego Nehab 2003-03-28 21:08:50 +00:00
parent 307603b24d
commit f18d1b7cd0
31 changed files with 163 additions and 77 deletions

View file

@ -1,3 +1,7 @@
-----------------------------------------------------------------------------
-- Little program that checks links in HTML files
-- LuaSocket 1.5 sample files.
-----------------------------------------------------------------------------
socket.http.TIMEOUT = 10
cache = {}
@ -14,7 +18,7 @@ end
function getstatus(url)
local parsed = socket.url.parse(url, { scheme = "file" })
if cache[url] then return cache[url].res end
if cache[url] then return cache[url] end
local res
if parsed.scheme == "http" then
local request = { url = url }
@ -34,7 +38,7 @@ function getstatus(url)
res = nil
else res = error end
else res = string.format("unhandled scheme '%s'", parsed.scheme) end
cache[url] = { res = res }
cache[url] = res
return res
end

View file

@ -1,12 +1,16 @@
-----------------------------------------------------------------------------
-- Little program to download DICT word definitions
-- LuaSocket 1.5 sample files
-----------------------------------------------------------------------------
function get_status(sock, valid)
local line, err = sock:receive()
local code, par
if not line then sock:close() return err end
_, _, code = strfind(line, "^(%d%d%d)")
_, _, code = string.find(line, "^(%d%d%d)")
code = tonumber(code)
if code ~= valid then return code end
if code == 150 then
_,_,_, par = strfind(line, "^(%d%d%d) (%d*)")
_,_,_, par = string.find(line, "^(%d%d%d) (%d*)")
par = tonumber(par)
end
return nil, par
@ -24,7 +28,7 @@ function get_def(sock)
end
function dict_open()
local sock, err = connect("dict.org", 2628)
local sock, err = socket.connect("dict.org", 2628)
if not sock then return nil, err end
sock:timeout(10)
local code, par = get_status(sock, 220)
@ -48,7 +52,7 @@ function dict_define(sock, word, dict)
end
code, par = get_status(sock, 250)
if code then return nil, code end
return gsub(defs, "%s%s$", "")
return string.gsub(defs, "%s%s$", "")
end
function dict_close(sock)
@ -65,3 +69,10 @@ function dict_get(word, dict)
dict_close(sock)
return defs, err
end
if arg and arg[1] then
defs, err = dict_get(arg[1], arg[2])
print(defs or err)
else
io.write("Usage:\n luasocket dict.lua <word> [<dictionary>]\n")
end

View file

@ -1,3 +1,7 @@
-----------------------------------------------------------------------------
-- Little program to download files from URLs
-- LuaSocket 1.5 sample files
-----------------------------------------------------------------------------
-- formats a number of seconds into human readable form
function nicetime(s)
local l = "s"
@ -63,15 +67,15 @@ function receive2disk(file, size)
size = size
}
local receive_cb = function(chunk, err)
local dt = socket._time() - %aux.start -- elapsed time since start
local dt = socket._time() - aux.start -- elapsed time since start
if not chunk or chunk == "" then
io.write("\n")
aux.file:close()
return
end
aux.file:write(chunk)
aux.got = aux.got + string.len(chunk) -- total bytes received
if dt < 0.1 then return 1 end -- not enough time for estimate
aux.got = aux.got + string.len(chunk) -- total bytes received
if dt < 0.1 then return 1 end -- not enough time for estimate
io.write("\r", gauge(aux.got, dt, aux.size))
return 1
end
@ -122,7 +126,7 @@ function getschemeandname(url, name)
return parsed.scheme, name
end
-- gets a file either by http or url, saving as name
-- gets a file either by http or ftp, saving as <name>
function get(url, name)
local scheme
scheme, name = getschemeandname(url, name)