summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2021-08-21 16:14:30 +0200
committerAki <please@ignore.pl>2021-08-21 16:14:30 +0200
commit6cefc948d37cd856d23caf00970ad3565672b016 (patch)
treebe3b0f370724d9d30a21b4fa72ab6b349b0777c9
parentf41138949b4fb13331391cf3d7570d38f04a2ce8 (diff)
downloadplop-6cefc948d37cd856d23caf00970ad3565672b016.zip
plop-6cefc948d37cd856d23caf00970ad3565672b016.tar.gz
plop-6cefc948d37cd856d23caf00970ad3565672b016.tar.bz2
Removed stream dependency from buffer
-rw-r--r--buffer.c24
-rw-r--r--buffer.h6
-rw-r--r--stream.c2
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 <lua.h>
-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);