summaryrefslogtreecommitdiffstats
path: root/stream.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2020-04-19 19:15:48 -0700
committerKaz Kylheku <kaz@kylheku.com>2020-04-19 19:15:48 -0700
commitc99dd869357f2afd0b79dfc24f9e9953d9129837 (patch)
tree5297cdf3c4745d8225e98281071db50aa71f424e /stream.c
parent17725d8ac6a5bbc96e888ae6e346f909fe77f3e5 (diff)
downloadtxr-c99dd869357f2afd0b79dfc24f9e9953d9129837.tar.gz
txr-c99dd869357f2afd0b79dfc24f9e9953d9129837.tar.bz2
txr-c99dd869357f2afd0b79dfc24f9e9953d9129837.zip
New function: get-line-as-buf
* buf.c (buf_put_bytes, buf_get_bytes): static functions become external. * buf.h (buf_put_bytes, buf_get_bytes): Declared. * stream.c (get_line_as_buf): New fucntnion. (stream_init): Registered get-line-as-buf intrinsic. * stream.h (get_line_as_buf): Declared. * txr.1: Documented.
Diffstat (limited to 'stream.c')
-rw-r--r--stream.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/stream.c b/stream.c
index ae8f384e..5d040e5d 100644
--- a/stream.c
+++ b/stream.c
@@ -3038,6 +3038,35 @@ val fill_buf_adjust(val buf, val pos_in, val stream_in)
return readpos;
}
+val get_line_as_buf(val stream_in)
+{
+ val self = lit("get-line-as-buf");
+ val stream = default_arg(stream_in, std_input);
+ struct strm_ops *ops = coerce(struct strm_ops *,
+ cobj_ops(self, stream, stream_s));
+ val buf = make_buf(zero, nil, num_fast(128));
+ unsigned char bytes[128];
+ size_t count = 0;
+
+ for (;;) {
+ val b = ops->get_byte(stream);
+ if (b == nil || b == num('\n'))
+ break;
+ bytes[count++] = c_num(b);
+
+ if (count == sizeof bytes) {
+ buf_put_bytes(buf, length_buf(buf), bytes, count, self);
+ count = 0;
+ }
+ }
+
+ if (count > 0)
+ buf_put_bytes(buf, length_buf(buf), bytes, count, self);
+
+ buf_trim(buf);
+ return buf;
+}
+
struct fmt {
size_t minsize;
const char *dec;
@@ -4964,6 +4993,7 @@ void stream_init(void)
reg_fun(unget_byte_s, func_n2o(unget_byte, 1));
reg_fun(put_buf_s, func_n3o(put_buf, 1));
reg_fun(fill_buf_s, func_n3o(fill_buf, 1));
+ reg_fun(intern(lit("get-line-as-buf"), user_package), func_n1o(get_line_as_buf, 0));
reg_fun(intern(lit("fill-buf-adjust"), user_package), func_n3o(fill_buf_adjust, 1));
reg_fun(intern(lit("flush-stream"), user_package), func_n1o(flush_stream, 0));
reg_fun(intern(lit("seek-stream"), user_package), func_n3(seek_stream));