diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2019-04-18 06:01:52 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2019-04-18 06:01:52 -0700 |
commit | 9cfc6030cf1c38e37345d8e85396ee03bffa67b3 (patch) | |
tree | 25fc8ce3a1ed5067f99cb902c04657111fa9a354 /stream.c | |
parent | 6eeb29dc80b218019ee927b69d1260a61f7ff3f8 (diff) | |
download | txr-9cfc6030cf1c38e37345d8e85396ee03bffa67b3.tar.gz txr-9cfc6030cf1c38e37345d8e85396ee03bffa67b3.tar.bz2 txr-9cfc6030cf1c38e37345d8e85396ee03bffa67b3.zip |
Support max length and depth for object printing.
* hash.c (hash_print_op): Implement max length.
* lib.c (lazy_str_put, out_lazy_str): Take struct strm_base *
parameter and implement max length.
(out_quasi_str_sym): Don't use obj_print_impl for symbol's
name string; just put_string. The use of obj_print_impl causes
symbols appearing as variables in quasiliterals to be
truncated when max length is imposed.
(out_quasi_str): Implement max length.
(obj_print_impl): Implement max length and depth. Note that
there is now always a non-null ctx pointer.
(obj_print): Always set up context pointer for obj_print_impl.
Context now has pointer to low-level stream structure, where
we can access max_length and max_depth. It also carries the
recursion depth.
* lib.h (lazy_str_put): Declaration updated.
* stream.c (strm_base_init): Add initializers for max_length
and max_depth.
(put_string): Pass stream structure to lazy_str_put.
(set_max_length, set_max_depth): New functions.
(stream_init): set-max-length and set-max-depth intrinsics
registered.
* stream.h (struct strm_ctx): New members depth and strm.
(struct strm_base): New members max_length and max_depth.
(set_max_length, set_max_depth): Declared.
* txr.1: Documented.
Diffstat (limited to 'stream.c')
-rw-r--r-- | stream.c | 31 |
1 files changed, 27 insertions, 4 deletions
@@ -102,7 +102,7 @@ val shell, shell_arg; void strm_base_init(struct strm_base *s) { - static struct strm_base init = { indent_off, 60, 10, 0, 0, 0, 0 }; + static struct strm_base init = { indent_off, 60, 10, 0, 0, 0, 0, 0, 0 }; *s = init; } @@ -3547,13 +3547,14 @@ static val put_indent(val stream, struct strm_ops *ops, cnum chars) val put_string(val string, val stream_in) { val self = lit("put-string"); + val stream = default_arg(stream_in, std_output); + struct strm_base *s = coerce(struct strm_base *, stream->co.handle); + if (lazy_stringp(string)) { - return lazy_str_put(string, stream_in); + return lazy_str_put(string, stream_in, s); } else { - val stream = default_arg(stream_in, std_output); struct strm_ops *ops = coerce(struct strm_ops *, cobj_ops(self, stream, stream_s)); - struct strm_base *s = coerce(struct strm_base *, stream->co.handle); cnum col = s->column; const wchar_t *str = c_str(string), *p = str; @@ -3810,6 +3811,26 @@ val force_break(val stream) return stream; } +val set_max_length(val stream, val length) +{ + val self = lit("set-max-length"); + struct strm_base *s = coerce(struct strm_base *, + cobj_handle(self, stream, stream_s)); + cnum old_max = s->max_length; + s->max_length = c_num(length); + return num(old_max); +} + +val set_max_depth(val stream, val depth) +{ + val self = lit("set-max-depth"); + struct strm_base *s = coerce(struct strm_base *, + cobj_handle(self, stream, stream_s)); + cnum old_max = s->max_depth; + s->max_depth = c_num(depth); + return num(old_max); +} + struct strm_ctx *get_set_ctx(val stream, struct strm_ctx *ctx) { struct strm_base *s = coerce(struct strm_base *, stream->co.handle); @@ -4702,6 +4723,8 @@ void stream_init(void) reg_fun(intern(lit("inc-indent"), user_package), func_n2(inc_indent)); reg_fun(intern(lit("width-check"), user_package), func_n2(width_check)); reg_fun(intern(lit("force-break"), user_package), func_n1(force_break)); + reg_fun(intern(lit("set-max-length"), user_package), func_n2(set_max_length)); + reg_fun(intern(lit("set-max-depth"), user_package), func_n2(set_max_depth)); reg_varl(intern(lit("indent-off"), user_package), num_fast(indent_off)); reg_varl(intern(lit("indent-data"), user_package), num_fast(indent_data)); reg_varl(intern(lit("indent-code"), user_package), num_fast(indent_code)); |