4.19 Primitive character I/O
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • Built-in Predicates
        • Primitive character I/O
          • nl/0
          • nl/1
          • put/1
          • put/2
          • put_byte/1
          • put_byte/2
          • put_char/1
          • put_char/2
          • put_code/1
          • put_code/2
          • tab/1
          • tab/2
          • flush_output/0
          • flush_output/1
          • ttyflush/0
          • get_byte/1
          • get_byte/2
          • get_code/1
          • get_code/2
          • get_char/1
          • get_char/2
          • get0/1
          • get0/2
          • get/1
          • get/2
          • peek_byte/1
          • peek_byte/2
          • peek_code/1
          • peek_code/2
          • peek_char/1
          • peek_char/2
          • peek_string/3
          • skip/1
          • skip/2
          • get_single_char/1
          • with_tty_raw/1
          • at_end_of_stream/0
          • at_end_of_stream/1
          • set_end_of_stream/1
          • copy_stream_data/3
          • copy_stream_data/2
          • fill_buffer/1
          • read_pending_codes/3
          • read_pending_chars/3
    • Packages
Availability:built-in
read_pending_codes(+StreamIn, -Codes, ?Tail)
Read input pending in the input buffer of StreamIn and return it in the difference list Codes-Tail. That is, the available characters codes are used to create the list Codes ending in the tail Tail. On encountering end-of-file, both Codes and Tail are unified with the empty list ([]).

This predicate is intended for efficient unbuffered copying and filtering of input coming from network connections or devices. It also enables the library library(pure_input), which processes input from files and streams using a DCG.

The following code fragment realises efficient non-blocking copying of data from an input to an output stream. The at_end_of_stream/1 call checks for end-of-stream and fills the input buffer. Note that the use of a get_code/2 and put_code/2 based loop requires a flush_output/1 call after each put_code/2. The copy_stream_data/2 does not allow for inspection of the copied data and suffers from the same buffering issues.

copy(In, Out) :-
        repeat,
            fill_buffer(In),
            read_pending_codes(In, Chars, Tail),
            \+ \+ ( Tail = [],
                    format(Out, '~s', [Chars]),
                    flush_output(Out)
                  ),
            (   Tail == []
            ->  !
            ;   fail
            ).