summaryrefslogtreecommitdiffhomepage
path: root/stream.c
diff options
context:
space:
mode:
Diffstat (limited to 'stream.c')
-rw-r--r--stream.c94
1 files changed, 2 insertions, 92 deletions
diff --git a/stream.c b/stream.c
index 5dbddb5..f04806e 100644
--- a/stream.c
+++ b/stream.c
@@ -9,6 +9,8 @@
#include <lauxlib.h>
#include <lua.h>
+#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