More nixio fixes
[project/luci.git] / libs / nixio / lua / nixio / util.lua
1 --[[
2 nixio - Linux I/O library for lua
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 local table = require "table"
16 local nixio = require "nixio"
17 local setmetatable, assert = setmetatable, assert
18
19 module "nixio.util"
20
21 local BUFFERSIZE = 8096
22 local socket = nixio.socket_meta
23
24 function socket.recvall(self, len)
25         local block, code, msg = self:recv(len)
26
27         if not block then
28                 return "", code, msg, len
29         elseif #block == 0 then
30                 return "", nil, nil, len
31         end
32
33         local data, total = {block}, #block
34
35         while len > total do
36                 block, code, msg = self:recv(len - total)
37
38                 if not block then
39                         return data, code, msg, len - #data
40                 elseif #block == 0 then
41                         return data, nil, nil, len - #data
42                 end
43
44                 data[#data+1], total = block, total + #block
45         end
46
47         return (#data > 1 and table.concat(data) or data[1]), nil, nil, 0
48 end
49
50 function socket.sendall(self, data)
51         local total, block = 0
52         local sent, code, msg = self:send(data)
53
54         if not sent then
55                 return total, code, msg, data
56         end
57
58         while sent < #data do
59                 block, total = data:sub(sent + 1), total + sent
60                 sent, code, msg = self:send(block)
61                 
62                 if not sent then
63                         return total, code, msg, block
64                 end
65         end
66         
67         return total + sent, nil, nil, ""
68 end
69
70 function socket.linesource(self, limit)
71         limit = limit or BUFFERSIZE
72         local buffer = ""
73         local bpos = 0
74         return function(flush)
75                 local line, endp, _
76                 
77                 if flush then
78                         line = buffer:sub(bpos + 1)
79                         buffer = ""
80                         return line
81                 end
82
83                 while not line do
84                         _, endp, line = buffer:find("^(.-)\r?\n", bpos + 1)
85                         if line then
86                                 bpos = endp
87                                 return line
88                         elseif #buffer < limit + bpos then
89                                 local newblock, code = self:recv(limit + bpos - #buffer)
90                                 if not newblock then
91                                         return nil, code
92                                 end
93                                 buffer = buffer:sub(bpos + 1) .. newblock
94                                 bpos = 0
95                         else
96                                 return nil, 0
97                         end
98                 end
99         end
100 end