From f41138949b4fb13331391cf3d7570d38f04a2ce8 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 21 Aug 2021 14:35:46 +0200 Subject: Moved buffer to own set of files --- Makefile | 5 ++-- buffer.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ buffer.h | 19 +++++++++++++ stream.c | 94 ++------------------------------------------------------------ stream.h | 9 +----- 5 files changed, 122 insertions(+), 102 deletions(-) create mode 100644 buffer.c create mode 100644 buffer.h diff --git a/Makefile b/Makefile index 7facfe1..feb7711 100644 --- a/Makefile +++ b/Makefile @@ -9,13 +9,14 @@ PLOP_DEFAULT_HANDLER?=$(SHARE)/plop/default.lua all: plop -plop: connection.o main.o plop.o stream.o +plop: connection.o main.o plop.o stream.o buffer.o main.o plop.o: CFLAGS+=-DPLOP_DEFAULT_HANDLER=\"$(PLOP_DEFAULT_HANDLER)\" +buffer.o: buffer.h main.o: plop.h plop.o: connection.h plop.h stream.h connection.o: connection.h -stream.o: stream.h +stream.o: stream.h buffer.h clean: rm -f plop *.o diff --git a/buffer.c b/buffer.c new file mode 100644 index 0000000..cadfb7d --- /dev/null +++ b/buffer.c @@ -0,0 +1,97 @@ +#include "buffer.h" + +#include +#include +#include +#include + +#include + +#include "stream.h" + +void grow(lua_State * L, struct buffer * b) +{ + int allocated = b->allocated + 1024; + + if (8192 < allocated) + { + lua_pushliteral(L, "Too large buffer"); + lua_error(L); + } + + void * buffer = realloc(b->data, allocated); + + if (NULL == buffer) + { + lua_pushliteral(L, "Could not grow buffer"); + lua_error(L); + } + + b->data = buffer; + b->allocated = allocated; +} + +int read_more(lua_State * L, struct stream * s, int minimum_length, lua_KContext ctx) +{ + const int free_space = s->in.allocated + s->in.offset - s->in.length - 1; + + while (free_space < minimum_length) + { + grow(L, &s->in); + } + + if (0 < s->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; + } + + int length = read(s->fd, s->in.data + s->in.length, free_space); + + if (-1 == length) + { + if (EWOULDBLOCK == errno || EAGAIN == errno) + { + return lua_yieldk(L, 0, ctx, stream_readk); + } + else + { + lua_pushstring(L, strerror(errno)); + return lua_error(L); + } + } + + s->in.length += length; + + return length; +} + +int prepare_at_least(lua_State * L, struct stream * s, int minimum_length, lua_KContext ctx) +{ + const int remaining_bytes = s->in.length - s->in.next; + + if (remaining_bytes < minimum_length) + { + return read_more(L, s, minimum_length, ctx); + } + + return remaining_bytes; +} + +int until(struct buffer * b, const char * pattern, int pattern_length) +{ + while (b->next + pattern_length <= b->length) + { + if (0 == strncmp(&b->data[b->next], pattern, pattern_length)) + { + return b->next; + } + else + { + b->next++; + } + } + + return -1; +} diff --git a/buffer.h b/buffer.h new file mode 100644 index 0000000..d4fafa6 --- /dev/null +++ b/buffer.h @@ -0,0 +1,19 @@ +#pragma once + +#include + +struct stream; // TODO: Remove buffer to stream dependency. + +struct buffer +{ + char * data; + int length; + int offset; + int next; + int allocated; +}; + +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 until(struct buffer *, const char *, const int); diff --git a/stream.c b/stream.c index 5dbddb5..f04806e 100644 --- a/stream.c +++ b/stream.c @@ -9,6 +9,8 @@ #include #include +#include "buffer.h" + /// Creates and pushes new Stream into the Lua stack. /// \param L Lua state to push to /// \param fd File descriptor used by the Stream @@ -134,98 +136,6 @@ int stream_read(lua_State * L) return stream_readk(L, LUA_OK, 2); // Intentionally do not remove arguments from the stack. } -static void grow(lua_State *, struct buffer *); -static int prepare_at_least(lua_State *, struct stream *, const int, lua_KContext); -static int read_more(lua_State *, struct stream *, const int, lua_KContext); -static int until(struct buffer *, const char *, const int); - -static void grow(lua_State * L, struct buffer * b) -{ - int allocated = b->allocated + 1024; - - if (8192 < allocated) - { - lua_pushliteral(L, "Too large buffer"); - lua_error(L); - } - - void * buffer = realloc(b->data, allocated); - - if (NULL == buffer) - { - lua_pushliteral(L, "Could not grow buffer"); - lua_error(L); - } - - b->data = buffer; - b->allocated = allocated; -} - -static int read_more(lua_State * L, struct stream * s, int minimum_length, lua_KContext ctx) -{ - const int free_space = s->in.allocated + s->in.offset - s->in.length - 1; - - while (free_space < minimum_length) - { - grow(L, &s->in); - } - - if (0 < s->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; - } - - int length = read(s->fd, s->in.data + s->in.length, free_space); - - if (-1 == length) - { - if (EWOULDBLOCK == errno || EAGAIN == errno) - { - return lua_yieldk(L, 0, ctx, stream_readk); - } - else - { - lua_pushstring(L, strerror(errno)); - return lua_error(L); - } - } - - s->in.length += length; - - return length; -} - -static int prepare_at_least(lua_State * L, struct stream * s, int minimum_length, lua_KContext ctx) -{ - const int remaining_bytes = s->in.length - s->in.next; - - if (remaining_bytes < minimum_length) - { - return read_more(L, s, minimum_length, ctx); - } - - return remaining_bytes; -} - -static int until(struct buffer * b, const char * pattern, int pattern_length) -{ - while (b->next + pattern_length <= b->length) - { - if (0 == strncmp(&b->data[b->next], pattern, pattern_length)) - { - return b->next; - } - else - { - b->next++; - } - } - - return -1; -} - /// Continuation function and core implementation of the reading operation from a stream. /// \param L Lua state running reading operation /// \param status Unused diff --git a/stream.h b/stream.h index 5119fcc..92df502 100644 --- a/stream.h +++ b/stream.h @@ -2,14 +2,7 @@ #include -struct buffer -{ - char * data; - int length; - int offset; - int next; - int allocated; -}; +#include "buffer.h" struct stream { -- cgit v1.1