summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEmil Renner Berthing <esmil@mailme.dk>2013-02-01 19:31:19 +0100
committerEmil Renner Berthing <esmil@mailme.dk>2013-02-01 21:14:26 +0100
commit1074e609707f2143450c7101075137249e979ca9 (patch)
treecf3c00f7aeacc9b23a68fdd631b30a7b30587377
parent176c0bde4140fa0c3403d19c11eb32dc456bf7ad (diff)
downloadlem-1074e609707f2143450c7101075137249e979ca9.tar.gz
lem-1074e609707f2143450c7101075137249e979ca9.tar.xz
lem-1074e609707f2143450c7101075137249e979ca9.zip
io: skip empty strings in write methods
-rw-r--r--lem/io/file.c33
-rw-r--r--lem/io/stream.c18
2 files changed, 37 insertions, 14 deletions
diff --git a/lem/io/file.c b/lem/io/file.c
index 5bdce06..e3289a1 100644
--- a/lem/io/file.c
+++ b/lem/io/file.c
@@ -220,6 +220,7 @@ file_write_reap(struct lem_async *a)
{
struct file *f = (struct file *)a;
lua_State *T = f->a.T;
+ int top;
if (f->ret) {
f->a.T = NULL;
@@ -227,14 +228,18 @@ file_write_reap(struct lem_async *a)
return;
}
- if (f->write.idx == lua_gettop(T)) {
- f->a.T = NULL;
- lua_pushboolean(T, 1);
- lem_queue(T, 1);
- return;
- }
+ top = lua_gettop(T);
+ do {
+ if (f->write.idx == top) {
+ f->a.T = NULL;
+ lua_pushboolean(T, 1);
+ lem_queue(T, 1);
+ return;
+ }
+
+ f->write.str = lua_tolstring(T, ++f->write.idx, &f->write.len);
+ } while (f->write.len == 0);
- f->write.str = lua_tolstring(T, ++f->write.idx, &f->write.len);
lem_async_put(&f->a);
}
@@ -244,13 +249,17 @@ file_write(lua_State *T)
struct file *f;
const char *str;
size_t len;
+ int idx;
int i;
int top;
luaL_checktype(T, 1, LUA_TUSERDATA);
- str = luaL_checklstring(T, 2, &len);
top = lua_gettop(T);
- for (i = 3; i <= top; i++)
+ idx = 1;
+ do {
+ str = luaL_checklstring(T, ++idx, &len);
+ } while (len == 0 && idx <= top);
+ for (i = idx+1; i <= top; i++)
(void)luaL_checkstring(T, i);
f = lua_touserdata(T, 1);
@@ -258,10 +267,14 @@ file_write(lua_State *T)
return io_closed(T);
if (f->a.T != NULL)
return io_busy(T);
+ if (idx > top) {
+ lua_pushboolean(T, 1);
+ return 1;
+ }
f->write.str = str;
f->write.len = len;
- f->write.idx = 2;
+ f->write.idx = idx;
lem_async_do(&f->a, T, file_write_work, file_write_reap);
return lua_yield(T, top);
diff --git a/lem/io/stream.c b/lem/io/stream.c
index 0056233..cfbc1ed 100644
--- a/lem/io/stream.c
+++ b/lem/io/stream.c
@@ -211,9 +211,10 @@ stream__write(lua_State *T, struct stream *s)
int err;
while ((bytes = write(s->w.fd, s->out, s->out_len)) > 0) {
+ lem_debug("wrote %ld bytes to fd %d", bytes, s->w.fd);
s->out += bytes;
s->out_len -= bytes;
- if (s->out_len == 0) {
+ while (s->out_len == 0) {
if (s->idx == lua_gettop(T)) {
lua_pushboolean(T, 1);
return 1;
@@ -222,6 +223,7 @@ stream__write(lua_State *T, struct stream *s)
}
}
err = errno;
+ lem_debug("wrote %ld bytes to fd %d", bytes, s->w.fd);
if (bytes < 0 && (err == EAGAIN || err == EINTR))
return 0;
@@ -263,14 +265,18 @@ stream_write(lua_State *T)
struct stream *s;
const char *out;
size_t out_len;
+ int idx;
int i;
int top;
int ret;
luaL_checktype(T, 1, LUA_TUSERDATA);
- out = luaL_checklstring(T, 2, &out_len);
top = lua_gettop(T);
- for (i = 3; i <= top; i++)
+ idx = 1;
+ do {
+ out = luaL_checklstring(T, ++idx, &out_len);
+ } while (out_len == 0 && idx <= top);
+ for (i = idx+1; i <= top; i++)
(void)luaL_checkstring(T, i);
s = lua_touserdata(T, 1);
@@ -278,10 +284,14 @@ stream_write(lua_State *T)
return io_closed(T);
if (s->w.data != NULL)
return io_busy(T);
+ if (idx > top) {
+ lua_pushboolean(T, 1);
+ return 1;
+ }
s->out = out;
s->out_len = out_len;
- s->idx = 2;
+ s->idx = idx;
ret = stream__write(T, s);
if (ret > 0)
return ret;