Comparison

util-src/ringbuffer.c @ 8544:e7214441523b

util.ringbuffer: Add method for discarding buffered data without returning it to lua
author Kim Alvefur <zash@zash.se>
date Sat, 24 Feb 2018 14:45:06 +0100
parent 8543:0e1d8f2f02bf
child 8735:856a40ec4a0a
comparison
equal deleted inserted replaced
8543:0e1d8f2f02bf 8544:e7214441523b
77 77
78 return 0; 78 return 0;
79 } 79 }
80 80
81 /* 81 /*
82 * Move read position forward without returning the data
83 * (buffer, number) -> boolean
84 */
85 int rb_discard(lua_State *L) {
86 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
87 size_t r = luaL_checkinteger(L, 2);
88
89 if(r > b->blen) {
90 lua_pushboolean(L, 0);
91 return 1;
92 }
93
94 b->blen -= r;
95 b->rpos += r;
96 modpos(b);
97
98 lua_pushboolean(L, 1);
99 return 1;
100 }
101
102 /*
82 * Read bytes from buffer 103 * Read bytes from buffer
83 * (buffer, number, boolean?) -> string 104 * (buffer, number, boolean?) -> string
84 */ 105 */
85 int rb_read(lua_State *L) { 106 int rb_read(lua_State *L) {
86 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); 107 ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt");
208 229
209 lua_createtable(L, 0, 7); /* __index */ 230 lua_createtable(L, 0, 7); /* __index */
210 { 231 {
211 lua_pushcfunction(L, rb_find); 232 lua_pushcfunction(L, rb_find);
212 lua_setfield(L, -2, "find"); 233 lua_setfield(L, -2, "find");
234 lua_pushcfunction(L, rb_discard);
235 lua_setfield(L, -2, "discard");
213 lua_pushcfunction(L, rb_read); 236 lua_pushcfunction(L, rb_read);
214 lua_setfield(L, -2, "read"); 237 lua_setfield(L, -2, "read");
215 lua_pushcfunction(L, rb_readuntil); 238 lua_pushcfunction(L, rb_readuntil);
216 lua_setfield(L, -2, "readuntil"); 239 lua_setfield(L, -2, "readuntil");
217 lua_pushcfunction(L, rb_write); 240 lua_pushcfunction(L, rb_write);