aboutsummaryrefslogtreecommitdiff
path: root/src/buffer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/buffer.c')
-rw-r--r--src/buffer.c25
1 files changed, 13 insertions, 12 deletions
diff --git a/src/buffer.c b/src/buffer.c
index 423d804..fff1634 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -4,6 +4,7 @@
4\*=========================================================================*/ 4\*=========================================================================*/
5#include "lua.h" 5#include "lua.h"
6#include "lauxlib.h" 6#include "lauxlib.h"
7#include "compat.h"
7 8
8#include "buffer.h" 9#include "buffer.h"
9 10
@@ -37,7 +38,7 @@ int buffer_open(lua_State *L) {
37} 38}
38 39
39/*-------------------------------------------------------------------------*\ 40/*-------------------------------------------------------------------------*\
40* Initializes C structure 41* Initializes C structure
41\*-------------------------------------------------------------------------*/ 42\*-------------------------------------------------------------------------*/
42void buffer_init(p_buffer buf, p_io io, p_timeout tm) { 43void buffer_init(p_buffer buf, p_io io, p_timeout tm) {
43 buf->first = buf->last = 0; 44 buf->first = buf->last = 0;
@@ -61,8 +62,8 @@ int buffer_meth_getstats(lua_State *L, p_buffer buf) {
61* object:setstats() interface 62* object:setstats() interface
62\*-------------------------------------------------------------------------*/ 63\*-------------------------------------------------------------------------*/
63int buffer_meth_setstats(lua_State *L, p_buffer buf) { 64int buffer_meth_setstats(lua_State *L, p_buffer buf) {
64 buf->received = (long) luaL_optnumber(L, 2, (lua_Number) buf->received); 65 buf->received = (long) luaL_optnumber(L, 2, (lua_Number) buf->received);
65 buf->sent = (long) luaL_optnumber(L, 3, (lua_Number) buf->sent); 66 buf->sent = (long) luaL_optnumber(L, 3, (lua_Number) buf->sent);
66 if (lua_isnumber(L, 4)) buf->birthday = timeout_gettime() - lua_tonumber(L, 4); 67 if (lua_isnumber(L, 4)) buf->birthday = timeout_gettime() - lua_tonumber(L, 4);
67 lua_pushnumber(L, 1); 68 lua_pushnumber(L, 1);
68 return 1; 69 return 1;
@@ -87,7 +88,7 @@ int buffer_meth_send(lua_State *L, p_buffer buf) {
87 /* check if there was an error */ 88 /* check if there was an error */
88 if (err != IO_DONE) { 89 if (err != IO_DONE) {
89 lua_pushnil(L); 90 lua_pushnil(L);
90 lua_pushstring(L, buf->io->error(buf->io->ctx, err)); 91 lua_pushstring(L, buf->io->error(buf->io->ctx, err));
91 lua_pushnumber(L, (lua_Number) (sent+start-1)); 92 lua_pushnumber(L, (lua_Number) (sent+start-1));
92 } else { 93 } else {
93 lua_pushnumber(L, (lua_Number) (sent+start-1)); 94 lua_pushnumber(L, (lua_Number) (sent+start-1));
@@ -110,7 +111,7 @@ int buffer_meth_receive(lua_State *L, p_buffer buf) {
110 size_t size; 111 size_t size;
111 const char *part = luaL_optlstring(L, 3, "", &size); 112 const char *part = luaL_optlstring(L, 3, "", &size);
112 timeout_markstart(buf->tm); 113 timeout_markstart(buf->tm);
113 /* initialize buffer with optional extra prefix 114 /* initialize buffer with optional extra prefix
114 * (useful for concatenating previous partial results) */ 115 * (useful for concatenating previous partial results) */
115 luaL_buffinit(L, &b); 116 luaL_buffinit(L, &b);
116 luaL_addlstring(&b, part, size); 117 luaL_addlstring(&b, part, size);
@@ -118,12 +119,12 @@ int buffer_meth_receive(lua_State *L, p_buffer buf) {
118 if (!lua_isnumber(L, 2)) { 119 if (!lua_isnumber(L, 2)) {
119 const char *p= luaL_optstring(L, 2, "*l"); 120 const char *p= luaL_optstring(L, 2, "*l");
120 if (p[0] == '*' && p[1] == 'l') err = recvline(buf, &b); 121 if (p[0] == '*' && p[1] == 'l') err = recvline(buf, &b);
121 else if (p[0] == '*' && p[1] == 'a') err = recvall(buf, &b); 122 else if (p[0] == '*' && p[1] == 'a') err = recvall(buf, &b);
122 else luaL_argcheck(L, 0, 2, "invalid receive pattern"); 123 else luaL_argcheck(L, 0, 2, "invalid receive pattern");
123 /* get a fixed number of bytes (minus what was already partially 124 /* get a fixed number of bytes (minus what was already partially
124 * received) */ 125 * received) */
125 } else { 126 } else {
126 double n = lua_tonumber(L, 2); 127 double n = lua_tonumber(L, 2);
127 size_t wanted = (size_t) n; 128 size_t wanted = (size_t) n;
128 luaL_argcheck(L, n >= 0, 2, "invalid receive pattern"); 129 luaL_argcheck(L, n >= 0, 2, "invalid receive pattern");
129 if (size == 0 || wanted > size) 130 if (size == 0 || wanted > size)
@@ -134,8 +135,8 @@ int buffer_meth_receive(lua_State *L, p_buffer buf) {
134 /* we can't push anyting in the stack before pushing the 135 /* we can't push anyting in the stack before pushing the
135 * contents of the buffer. this is the reason for the complication */ 136 * contents of the buffer. this is the reason for the complication */
136 luaL_pushresult(&b); 137 luaL_pushresult(&b);
137 lua_pushstring(L, buf->io->error(buf->io->ctx, err)); 138 lua_pushstring(L, buf->io->error(buf->io->ctx, err));
138 lua_pushvalue(L, -2); 139 lua_pushvalue(L, -2);
139 lua_pushnil(L); 140 lua_pushnil(L);
140 lua_replace(L, -4); 141 lua_replace(L, -4);
141 } else { 142 } else {
@@ -218,7 +219,7 @@ static int recvall(p_buffer buf, luaL_Buffer *b) {
218} 219}
219 220
220/*-------------------------------------------------------------------------*\ 221/*-------------------------------------------------------------------------*\
221* Reads a line terminated by a CR LF pair or just by a LF. The CR and LF 222* Reads a line terminated by a CR LF pair or just by a LF. The CR and LF
222* are not returned by the function and are discarded from the buffer 223* are not returned by the function and are discarded from the buffer
223\*-------------------------------------------------------------------------*/ 224\*-------------------------------------------------------------------------*/
224static int recvline(p_buffer buf, luaL_Buffer *b) { 225static int recvline(p_buffer buf, luaL_Buffer *b) {
@@ -248,7 +249,7 @@ static int recvline(p_buffer buf, luaL_Buffer *b) {
248static void buffer_skip(p_buffer buf, size_t count) { 249static void buffer_skip(p_buffer buf, size_t count) {
249 buf->received += count; 250 buf->received += count;
250 buf->first += count; 251 buf->first += count;
251 if (buffer_isempty(buf)) 252 if (buffer_isempty(buf))
252 buf->first = buf->last = 0; 253 buf->first = buf->last = 0;
253} 254}
254 255