From 09e886db0be24faacaf7183fa6ccb3d439df1cad Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Thu, 4 Nov 2021 18:36:27 +0100 Subject: [PATCH] ADDED: read_n_chars/3, reading N characters from a stream. --- src/lib/charsio.pl | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/lib/charsio.pl b/src/lib/charsio.pl index d186eaf4..5a71ce1b 100644 --- a/src/lib/charsio.pl +++ b/src/lib/charsio.pl @@ -1,6 +1,7 @@ :- module(charsio, [char_type/2, chars_utf8bytes/2, get_single_char/1, + read_n_chars/3, read_line_to_chars/3, read_term_from_chars/2, write_term_to_chars/3, @@ -199,6 +200,29 @@ read_line_to_chars(Stream, Cs0, Cs) :- ) ). +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + Read N characters from Stream. + + If N is a variable, read until EOF, unifying N with the number of + characters read. +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +read_n_chars(Stream, N, Cs) :- + can_be(integer, N), + ( var(N) -> + read_to_eof(Stream, Cs), + length(Cs, N) + ; N >= 0, + '$get_n_chars'(Stream, N, Cs) + ). + +read_to_eof(Stream, Cs) :- + '$get_n_chars'(Stream, 512, Cs0), + ( Cs0 == [] -> Cs = [] + ; partial_string(Cs0, Cs, Rest), + read_to_eof(Stream, Rest) + ). + /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Relation between a list of characters Cs and its Base64 encoding Bs, also a list of characters. -- 2.54.0