1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com> |
1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com> |
2 * Licensed as LGPL - See doc/COPYING for details */ |
2 * Licensed as LGPL - See doc/COPYING for details */ |
3 |
3 |
4 #include "event_buffer.h" |
4 #include "event_buffer.h" |
5 #include <lauxlib.h> |
5 #include <lauxlib.h> |
|
6 #include <malloc.h> |
6 |
7 |
7 #define EVENT_BUFFER_MT "EVENT_BUFFER_MT" |
8 #define EVENT_BUFFER_MT "EVENT_BUFFER_MT" |
8 |
9 |
9 #define BUFFER_ADD_CHECK_INPUT_FIRST 1 |
10 #define BUFFER_ADD_CHECK_INPUT_FIRST 1 |
10 |
11 |
139 } |
140 } |
140 lua_pushlstring(L, (const char*)EVBUFFER_DATA(buf->buffer) + begin, len); |
141 lua_pushlstring(L, (const char*)EVBUFFER_DATA(buf->buffer) + begin, len); |
141 return 1; |
142 return 1; |
142 } |
143 } |
143 |
144 |
|
145 /* LUA: buffer:readline() |
|
146 Returns a line terminated by either '\r\n','\n\r' or '\r' or '\n' |
|
147 Returns nil and leaves data alone if no terminator is found |
|
148 TODO: Evaluate whether or not the newline is included |
|
149 */ |
|
150 static int event_buffer_readline(lua_State* L) { |
|
151 le_buffer* buf = event_buffer_check(L, 1); |
|
152 char* line = evbuffer_readline(buf->buffer); |
|
153 if(!line) |
|
154 return 0; |
|
155 lua_pushstring(L, line); |
|
156 free(line); |
|
157 return 1; |
|
158 } |
|
159 |
144 /* LUA: buffer:drain(amt) |
160 /* LUA: buffer:drain(amt) |
145 Drains 'amt' bytes from the buffer |
161 Drains 'amt' bytes from the buffer |
146 */ |
162 */ |
147 static int event_buffer_drain(lua_State* L) { |
163 static int event_buffer_drain(lua_State* L) { |
148 le_buffer* buf = event_buffer_check(L, 1); |
164 le_buffer* buf = event_buffer_check(L, 1); |
150 evbuffer_drain(buf->buffer, len); |
166 evbuffer_drain(buf->buffer, len); |
151 return 0; |
167 return 0; |
152 } |
168 } |
153 |
169 |
154 static luaL_Reg buffer_funcs[] = { |
170 static luaL_Reg buffer_funcs[] = { |
155 {"add",event_buffer_add}, |
171 {"add", event_buffer_add}, |
156 {"length",event_buffer_get_length}, |
172 {"length", event_buffer_get_length}, |
157 {"get_data",event_buffer_get_data}, |
173 {"get_data", event_buffer_get_data}, |
158 {"drain",event_buffer_drain}, |
174 {"readline", event_buffer_readline}, |
159 {"close",event_buffer_gc}, |
175 {"drain", event_buffer_drain}, |
|
176 {"close", event_buffer_gc}, |
160 {NULL, NULL} |
177 {NULL, NULL} |
161 }; |
178 }; |
162 static luaL_Reg funcs[] = { |
179 static luaL_Reg funcs[] = { |
163 {"new",event_buffer_push_new}, |
180 {"new", event_buffer_push_new}, |
164 {NULL, NULL} |
181 {NULL, NULL} |
165 }; |
182 }; |
166 |
183 |
167 int event_buffer_register(lua_State* L) { |
184 int event_buffer_register(lua_State* L) { |
168 luaL_newmetatable(L, EVENT_BUFFER_MT); |
185 luaL_newmetatable(L, EVENT_BUFFER_MT); |