From 6cefc948d37cd856d23caf00970ad3565672b016 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 21 Aug 2021 16:14:30 +0200 Subject: Removed stream dependency from buffer --- buffer.c | 24 ++++++++++++------------ buffer.h | 6 ++---- stream.c | 2 +- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/buffer.c b/buffer.c index cadfb7d..ad6ca25 100644 --- a/buffer.c +++ b/buffer.c @@ -31,23 +31,23 @@ void grow(lua_State * L, struct buffer * b) b->allocated = allocated; } -int read_more(lua_State * L, struct stream * s, int minimum_length, lua_KContext ctx) +int read_more(lua_State * L, int fd, struct buffer * in, int minimum_length, lua_KContext ctx) { - const int free_space = s->in.allocated + s->in.offset - s->in.length - 1; + const int free_space = in->allocated + in->offset - in->length - 1; while (free_space < minimum_length) { - grow(L, &s->in); + grow(L, in); } - if (0 < s->in.offset) + if (0 < in->offset) { - memmove(s->in.data, s->in.data + s->in.offset, s->in.length - s->in.offset); - s->in.offset = 0; - s->in.length -= s->in.offset; + memmove(in->data, in->data + in->offset, in->length - in->offset); + in->offset = 0; + in->length -= in->offset; } - int length = read(s->fd, s->in.data + s->in.length, free_space); + int length = read(fd, in->data + in->length, free_space); if (-1 == length) { @@ -62,18 +62,18 @@ int read_more(lua_State * L, struct stream * s, int minimum_length, lua_KContext } } - s->in.length += length; + in->length += length; return length; } -int prepare_at_least(lua_State * L, struct stream * s, int minimum_length, lua_KContext ctx) +int prepare_at_least(lua_State * L, int fd, struct buffer * in, int minimum_length, lua_KContext ctx) { - const int remaining_bytes = s->in.length - s->in.next; + const int remaining_bytes = in->length - in->next; if (remaining_bytes < minimum_length) { - return read_more(L, s, minimum_length, ctx); + return read_more(L, fd, in, minimum_length, ctx); } return remaining_bytes; diff --git a/buffer.h b/buffer.h index d4fafa6..66f8f02 100644 --- a/buffer.h +++ b/buffer.h @@ -2,8 +2,6 @@ #include -struct stream; // TODO: Remove buffer to stream dependency. - struct buffer { char * data; @@ -14,6 +12,6 @@ struct buffer }; void grow(lua_State *, struct buffer *); -int prepare_at_least(lua_State *, struct stream *, const int, lua_KContext); -int read_more(lua_State *, struct stream *, const int, lua_KContext); +int read_more(lua_State *, const int, struct buffer *, const int, lua_KContext); +int prepare_at_least(lua_State *, const int, struct buffer *, const int, lua_KContext); int until(struct buffer *, const char *, const int); diff --git a/stream.c b/stream.c index f04806e..cb5eff5 100644 --- a/stream.c +++ b/stream.c @@ -155,7 +155,7 @@ int stream_readk(lua_State * L, int status, lua_KContext ctx) int remaining_bytes; do { - remaining_bytes = prepare_at_least(L, s, (int) pattern_length, ctx); + remaining_bytes = prepare_at_least(L, s->fd, &s->in, (int) pattern_length, ctx); offset = until(&s->in, pattern, (int) pattern_length); } while (-1 == offset && 0 < remaining_bytes); -- cgit v1.1