event_buffer:add learned not to append to itself...
[luaevent.git] / src / event_buffer.c
blob546a5965956b9598b63b677568a79e1b037de25c
1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com>
2 * Licensed as LGPL - See doc/COPYING for details */
4 #include "event_buffer.h"
5 #include <lauxlib.h>
6 #include <malloc.h>
8 #define EVENT_BUFFER_MT "EVENT_BUFFER_MT"
10 #define BUFFER_ADD_CHECK_INPUT_FIRST 1
12 /* Obtains an le_buffer structure from a given index */
13 static le_buffer* event_buffer_get(lua_State* L, int idx) {
14 return (le_buffer*)luaL_checkudata(L, idx, EVENT_BUFFER_MT);
17 /* Obtains an le_buffer structure from a given index
18 AND checks that it hadn't been prematurely freed
20 static le_buffer* event_buffer_check(lua_State* L, int idx) {
21 le_buffer* buf = (le_buffer*)luaL_checkudata(L, idx, EVENT_BUFFER_MT);
22 if(!buf->buffer)
23 luaL_argerror(L, idx, "Attempt to use closed event_buffer object");
24 return buf;
27 /* Checks if the given index contains an le_buffer object */
28 static int is_event_buffer(lua_State* L, int idx) {
29 int ret;
30 lua_getmetatable(L, idx);
31 luaL_getmetatable(L, EVENT_BUFFER_MT);
32 ret = lua_rawequal(L, -2, -1);
33 lua_pop(L, 2);
34 return ret;
37 /* TODO: Use lightuserdata mapping to locate hanging object instances */
38 /* Pushes the specified evbuffer object onto the stack, attaching a metatable to it */
39 static int event_buffer_push(lua_State* L, struct evbuffer* buffer) {
40 le_buffer *buf = (le_buffer*)lua_newuserdata(L, sizeof(le_buffer));
41 buf->buffer = buffer;
42 luaL_getmetatable(L, EVENT_BUFFER_MT);
43 lua_setmetatable(L, -2);
44 return 1;
47 /* LUA: new()
48 Pushes a new evbuffer instance on the stack
50 static int event_buffer_push_new(lua_State* L) {
51 return event_buffer_push(L, evbuffer_new());
54 /* LUA: __gc and buffer:close()
55 Releases the buffer resources
57 static int event_buffer_gc(lua_State* L) {
58 le_buffer* buf = event_buffer_get(L, 1);
59 if(buf->buffer) {
60 evbuffer_free(buf->buffer);
61 buf->buffer = NULL;
63 return 0;
66 /* LUA: buffer:add(...)
67 progressively adds items to the buffer
68 if arg[*] is string, treat as a string:format call
69 if arg[*] is a buffer, perform event_add_buffer
70 returns number of bytes added
72 static int event_buffer_add(lua_State* L) {
73 le_buffer* buf = event_buffer_check(L, 1);
74 struct evbuffer* buffer = buf->buffer;
75 int oldLength = EVBUFFER_LENGTH(buffer);
76 int last = lua_gettop(L);
77 int i;
78 for(i = 2; i <= last; i++) {
79 if(!lua_isstring(L, i) && !is_event_buffer(L, i))
80 luaL_argerror(L, i, "Argument is not a string or buffer object");
81 if(lua_equal(L, 1, i))
82 luaL_argerror(L, i, "Cannot add buffer to itself");
83 /* Optionally perform checks and data loading separately to avoid overfilling the buffer */
84 #if BUFFER_ADD_CHECK_INPUT_FIRST
86 for(i = 2; i <= last; i++) {
87 #endif
88 if(lua_isstring(L, i)) {
89 size_t len;
90 const char* data = lua_tolstring(L, i, &len);
91 if(0 != evbuffer_add(buffer, data, len))
92 luaL_error(L, "Failed to add data to the buffer");
93 } else {
94 le_buffer* buf2 = event_buffer_check(L, i);
95 if(0 != evbuffer_add_buffer(buffer, buf2->buffer))
96 luaL_error(L, "Failed to move buffer-data to the buffer");
99 lua_pushinteger(L, EVBUFFER_LENGTH(buffer) - oldLength);
100 return 1;
103 /* LUA: buffer:length()
104 Returns the length of the buffer contents
106 static int event_buffer_get_length(lua_State* L) {
107 le_buffer* buf = event_buffer_check(L, 1);
108 lua_pushinteger(L, EVBUFFER_LENGTH(buf->buffer));
109 return 1;
112 /* MAYBE: Could add caching */
113 /* LUA: buffer:get_data
114 () - Returns all data in buffer
115 (len) - Returns data up to 'len' bytes long
116 (begin,len) - Returns data beginning at 'begin' up to 'len' bytes long
118 static int event_buffer_get_data(lua_State* L) {
119 le_buffer* buf = event_buffer_check(L, 1);
120 int begin, len;
121 switch(lua_gettop(L)) {
122 case 1:
123 /* Obtain full data */
124 begin = 0;
125 len = EVBUFFER_LENGTH(buf->buffer);
126 break;
127 case 2:
128 begin = 0;
129 len = luaL_checkinteger(L, 2);
130 if(len > EVBUFFER_LENGTH(buf->buffer))
131 len = EVBUFFER_LENGTH(buf->buffer);
132 break;
133 case 3:
134 default:
135 begin = luaL_checkinteger(L, 2);
136 len = luaL_checkinteger(L, 3);
137 if(begin > EVBUFFER_LENGTH(buf->buffer))
138 begin = EVBUFFER_LENGTH(buf->buffer);
139 if(begin + len > EVBUFFER_LENGTH(buf->buffer))
140 len = EVBUFFER_LENGTH(buf->buffer) - begin;
141 break;
143 lua_pushlstring(L, (const char*)EVBUFFER_DATA(buf->buffer) + begin, len);
144 return 1;
147 /* LUA: buffer:readline()
148 Returns a line terminated by either '\r\n','\n\r' or '\r' or '\n'
149 Returns nil and leaves data alone if no terminator is found
150 TODO: Evaluate whether or not the newline is included
152 static int event_buffer_readline(lua_State* L) {
153 le_buffer* buf = event_buffer_check(L, 1);
154 char* line = evbuffer_readline(buf->buffer);
155 if(!line)
156 return 0;
157 lua_pushstring(L, line);
158 free(line);
159 return 1;
162 /* LUA: buffer:drain(amt)
163 Drains 'amt' bytes from the buffer
165 static int event_buffer_drain(lua_State* L) {
166 le_buffer* buf = event_buffer_check(L, 1);
167 size_t len = luaL_checkinteger(L, 2);
168 evbuffer_drain(buf->buffer, len);
169 return 0;
172 static luaL_Reg buffer_funcs[] = {
173 {"add", event_buffer_add},
174 {"length", event_buffer_get_length},
175 {"get_data", event_buffer_get_data},
176 {"readline", event_buffer_readline},
177 {"drain", event_buffer_drain},
178 {"close", event_buffer_gc},
179 {NULL, NULL}
181 static luaL_Reg funcs[] = {
182 {"new", event_buffer_push_new},
183 {NULL, NULL}
186 int event_buffer_register(lua_State* L) {
187 luaL_newmetatable(L, EVENT_BUFFER_MT);
188 lua_pushcfunction(L, event_buffer_gc);
189 lua_setfield(L, -2, "__gc");
190 lua_newtable(L);
191 luaL_register(L, NULL, buffer_funcs);
192 lua_setfield(L, -2, "__index");
193 lua_pop(L, 1);
195 luaL_register(L, "luaevent.core.buffer", funcs);
196 return 0;