diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2014-07-19 09:09:13 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2014-07-19 09:09:13 -0700 |
commit | 74880de58e4f3ae6e0a84187f92e6853c37aa64e (patch) | |
tree | 64904f35f9d7279cb437866bdfe9deb501ef798f | |
parent | 614ec6640e3232edfea2614ef290ffcb7ffbae7f (diff) | |
download | txr-74880de58e4f3ae6e0a84187f92e6853c37aa64e.tar.gz txr-74880de58e4f3ae6e0a84187f92e6853c37aa64e.tar.bz2 txr-74880de58e4f3ae6e0a84187f92e6853c37aa64e.zip |
* eval.c (eval_init): Register get-lines as a synonym for lazy-stream-cons.
* stream.c (get_string): New function.
* stream.h (get_string): Declared.
(stream_init): get_string registered as get-string intrinsic.
* txr.1: Documented get-string, and get-lines as a synonym for
lazy-stream-cons. Documented the behavioral difference of
lazy-stream-cons lazy lists for real-time and non-real-time streams.
-rw-r--r-- | ChangeLog | 13 | ||||
-rw-r--r-- | eval.c | 1 | ||||
-rw-r--r-- | stream.c | 19 | ||||
-rw-r--r-- | stream.h | 1 | ||||
-rw-r--r-- | txr.1 | 39 |
5 files changed, 72 insertions, 1 deletions
@@ -1,3 +1,16 @@ +2014-07-19 Kaz Kylheku <kaz@kylheku.com> + + * eval.c (eval_init): Register get-lines as a synonym for lazy-stream-cons. + + * stream.c (get_string): New function. + + * stream.h (get_string): Declared. + (stream_init): get_string registered as get-string intrinsic. + + * txr.1: Documented get-string, and get-lines as a synonym for + lazy-stream-cons. Documented the behavioral difference of + lazy-stream-cons lazy lists for real-time and non-real-time streams. + 2014-07-18 Kaz Kylheku <kaz@kylheku.com> * Makefile (conftest, conftest2): Link math @@ -3774,6 +3774,7 @@ void eval_init(void) reg_fun(intern(lit("break-str"), user_package), func_n2(break_str)); reg_fun(intern(lit("lazy-stream-cons"), user_package), func_n1(lazy_stream_cons)); + reg_fun(intern(lit("get-lines"), user_package), func_n1(lazy_stream_cons)); reg_fun(intern(lit("lazy-str"), user_package), func_n3o(lazy_str, 1)); reg_fun(intern(lit("lazy-stringp"), user_package), func_n1(lazy_stringp)); reg_fun(intern(lit("lazy-str-force-upto"), user_package), func_n2(lazy_str_force_upto)); @@ -2004,6 +2004,24 @@ val seek_stream(val stream, val offset, val whence) } } +val get_string(val stream, val nchars) +{ + val strstream = make_string_output_stream(); + nchars = default_bool_arg(nchars); + val ch; + + if (nchars) { + for (; gt(nchars, zero) && (ch = get_char(stream)); + nchars = minus(nchars, one)) + put_char(ch, strstream); + } else { + while ((ch = get_char(stream))) + put_char(ch, strstream); + } + + return get_string_from_stream(strstream); +} + #if HAVE_SYS_STAT static int w_stat(const wchar_t *wpath, struct stat *buf) { @@ -2835,6 +2853,7 @@ void stream_init(void) reg_fun(intern(lit("get-line"), user_package), func_n1o(get_line, 0)); reg_fun(intern(lit("get-char"), user_package), func_n1o(get_char, 0)); reg_fun(intern(lit("get-byte"), user_package), func_n1o(get_byte, 0)); + reg_fun(intern(lit("get-string"), user_package), func_n2o(get_string, 0)); reg_fun(intern(lit("put-string"), user_package), func_n2o(put_string, 1)); reg_fun(intern(lit("put-line"), user_package), func_n2o(put_line, 0)); reg_fun(intern(lit("put-char"), user_package), func_n2o(put_char, 1)); @@ -94,6 +94,7 @@ val put_char(val ch, val stream); val put_byte(val byte, val stream); val flush_stream(val stream); val seek_stream(val stream, val offset, val whence); +val get_string(val stream, val nchars); val statf(val path); val open_directory(val path); val open_file(val path, val mode_str); @@ -8804,16 +8804,20 @@ executing, it is still accessible. This allows the update function to retrieve a reference to itself and propagate itself into another lazy cons (as in the example under make-lazy-cons). -.SS Function lazy-stream-cons +.SS Functions lazy-stream-cons and get-lines .TP Syntax: (lazy-stream-cons <stream>) + (get-lines <stream>) .TP Description: +The lazy-stream-cons and get-lines functions are synonyms. Thus, the following +description of lazy-stream-cons also applies to get-lines. + The lazy-stream-cons returns a lazy cons which generates a lazy list based on reading lines of text from input stream <stream>, which form the elements of the list. The get-line function is called on demand to add elements to the @@ -8828,6 +8832,16 @@ cdr field. the string returned by get-line, and whose cdr contains the lazy function. +The lazy-stream-cons function inspects the real-time property of a stream +as if by the real-time-stream-p function. This determines which of two +styles of lazy list are returned. For an ordinary (non-real-time) stream, +the lazy list treats the end-of-file condition accurately: an empty +file turns into the empty list nil, a one line file into a one-element +list which contains that line and so on. This accuracy requries one +line of lookahead which is not acceptable in real-time streams. For real-time +streams, the lazy list will generate an extra nil item after the last +line, and so an empty input source translates to the list (nil). + .SS Macro delay .TP @@ -12952,6 +12966,29 @@ the two operations will interfere with the UTF-8 decoding. These functions return nil when the end of data is reached. Errors are represented as exceptions. +See also: get-lines + +.SS Function get-string + +.TP +Syntax: + + (get-string [<stream> [<count>]]) + +.TP +Description: + +The get-string function reads characters from a stream, and assembles them into +a string, which is returned. If the <stream> argument is omitted, then the +*stdin* stream is used. + +If the <count> argument is missing, then all of the characters from the +stream are read and assembled into a string. + +If present, the <count> argument should be a positive integer indicating +a limit on how many characters to read. The returned string will be no +longer than <count>, but may be shorter. + .SS Functions unget-char and unget-byte .TP |