event_buffer learned readline and cleaned fn-list.

Fri, 07 Sep 2007 00:10:08 -0400

author
Thomas Harning Jr <harningt@gmail.com>
date
Fri, 07 Sep 2007 00:10:08 -0400
changeset 31
1956a957c613
parent 30
d5b6c8abaaeb
child 32
22976b4e5ffd

event_buffer learned readline and cleaned fn-list.

CHANGELOG file | annotate | diff | comparison | revisions
src/event_buffer.c file | annotate | diff | comparison | revisions
--- a/CHANGELOG	Fri Sep 07 00:00:59 2007 -0400
+++ b/CHANGELOG	Fri Sep 07 00:10:08 2007 -0400
@@ -6,6 +6,7 @@
  + Added event_buffer object
   * Can 'add' a sequence of strings/event_buffers
   * Can 'get_data', 'length','drain','close' and create new instances
+  * Can 'readline'
 ======
 0.1.2 - 2007-08-18
 + Setup system to use new coro management as described in COROUTINE_MANAGEMENT
--- a/src/event_buffer.c	Fri Sep 07 00:00:59 2007 -0400
+++ b/src/event_buffer.c	Fri Sep 07 00:10:08 2007 -0400
@@ -3,6 +3,7 @@
 
 #include "event_buffer.h"
 #include <lauxlib.h>
+#include <malloc.h>
 
 #define EVENT_BUFFER_MT "EVENT_BUFFER_MT"
 
@@ -141,6 +142,21 @@
 	return 1;
 }
 
+/* LUA: buffer:readline()
+	Returns a line terminated by either '\r\n','\n\r' or '\r' or '\n'
+	Returns nil and leaves data alone if no terminator is found
+	TODO: Evaluate whether or not the newline is included
+*/
+static int event_buffer_readline(lua_State* L) {
+	le_buffer* buf = event_buffer_check(L, 1);
+	char* line = evbuffer_readline(buf->buffer);
+	if(!line)
+		return 0;
+	lua_pushstring(L, line);
+	free(line);
+	return 1;
+}
+
 /* LUA: buffer:drain(amt)
 	Drains 'amt' bytes from the buffer
 */
@@ -152,15 +168,16 @@
 }
 
 static luaL_Reg buffer_funcs[] = {
-	{"add",event_buffer_add},
-	{"length",event_buffer_get_length},
-	{"get_data",event_buffer_get_data},
-	{"drain",event_buffer_drain},
-	{"close",event_buffer_gc},
+	{"add", event_buffer_add},
+	{"length", event_buffer_get_length},
+	{"get_data", event_buffer_get_data},
+	{"readline", event_buffer_readline},
+	{"drain", event_buffer_drain},
+	{"close", event_buffer_gc},
 	{NULL, NULL}
 };
 static luaL_Reg funcs[] = {
-	{"new",event_buffer_push_new},
+	{"new", event_buffer_push_new},
 	{NULL, NULL}
 };
  

mercurial