From 5edadcdff0d80ceb5639dc31bbe7c2b43a1d2782 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Mon, 21 Aug 2017 20:09:11 -0700 Subject: Default the length argument of truncate-stream. * stream.c (truncate_stream): If the len argument is missing, default to the current position, obtained by using the seek operation. (stream_init): Fix up registration of truncate-stream for one optional argument. * txr.1: Documentation of truncate-stream updated. --- stream.c | 4 +++- txr.1 | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/stream.c b/stream.c index 9f92dd97..2af67db2 100644 --- a/stream.c +++ b/stream.c @@ -3600,6 +3600,8 @@ val seek_stream(val stream, val offset, val whence) val truncate_stream(val stream, val len) { struct strm_ops *ops = coerce(struct strm_ops *, cobj_ops(stream, stream_s)); + if (missingp(len)) + len = ops->seek(stream, zero, strm_cur); return ops->truncate(stream, len); } @@ -4503,7 +4505,7 @@ void stream_init(void) reg_fun(intern(lit("fill-buf"), user_package), func_n3o(fill_buf, 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)); - reg_fun(intern(lit("truncate-stream"), user_package), func_n2(truncate_stream)); + reg_fun(intern(lit("truncate-stream"), user_package), func_n2o(truncate_stream, 1)); reg_fun(intern(lit("streamp"), user_package), func_n1(streamp)); reg_fun(intern(lit("real-time-stream-p"), user_package), func_n1(real_time_stream_p)); reg_fun(intern(lit("stream-set-prop"), user_package), func_n3(stream_set_prop)); diff --git a/txr.1 b/txr.1 index b228334f..911143dd 100644 --- a/txr.1 +++ b/txr.1 @@ -42537,7 +42537,7 @@ On failure, it throws an exception of type .coNP Function @ truncate-stream .synb -.mets (truncate-stream < stream << length ) +.mets (truncate-stream < stream <> [ length ]) .syne .desc The @@ -42550,6 +42550,21 @@ bytes. The stream must be a file stream, and must be open for writing. +If +.meta length +is omitted, then it defaults to the current position, retrieved +as if by invoking the +.code seek-stream +with an +.meta offset +argument of zero and +.meta whence +argument of +.codn :from-current . +Hence, after the +.code truncate-stream +operation, that position is one byte past the end of the file. + .coNP Functions @ stream-get-prop and @ stream-set-prop .synb .mets (stream-get-prop < stream << indicator ) -- cgit v1.2.3