% This LaTeX document was generated using the LaTeX backend of PlDoc, % The SWI-Prolog documentation system \section{library(readutil): Read utilities} \label{sec:readutil} \begin{tags} \mtag{See also}- \file{library(pure_input)} allows for processing files with DCGs. \\- \file{library(lazy_lists)} for creating lazy lists from input. \end{tags} This library provides some commonly used reading predicates. As these predicates have proven to be time-critical in some applications we moved them to C. For compatibility as well as to reduce system dependency, we link the foreign code at runtime and fallback to the Prolog implementation if the shared object cannot be found.\vspace{0.7cm} \begin{description} \predicate[det]{read_line_to_codes}{2}{+Stream, -Line:codes} Read the next line of input from \arg{Stream}. Unify content of the lines as a list of character codes with \arg{Line} \textit{after} the line has been read. A line is ended by a newline character or end-of-file. Unlike \predref{read_line_to_codes}{3}, this predicate removes a trailing newline character. \predicate[det]{read_line_to_codes}{3}{+Stream, -Line, ?Tail} Difference-list version to read an input line to a list of character codes. Reading stops at the newline or end-of-file character, but unlike \predref{read_line_to_codes}{2}, the newline is retained in the output. This predicate is especially useful for reading a block of lines up to some delimiter. The following example reads an HTTP header ended by a blank line: \begin{code} read_header_data(Stream, Header) :- read_line_to_codes(Stream, Header, Tail), read_header_data(Header, Stream, Tail). read_header_data("\r\n", _, _) :- !. read_header_data("\n", _, _) :- !. read_header_data("", _, _) :- !. read_header_data(_, Stream, Tail) :- read_line_to_codes(Stream, Tail, NewTail), read_header_data(Tail, Stream, NewTail). \end{code} \predicate[det]{read_line_to_string}{2}{+Stream, -String} Read the next line from \arg{Stream} into \arg{String}. \arg{String} does not contain the line terminator. \arg{String} is unified with the \textit{atom} \verb$end_of_file$ if the end of the file is reached. \begin{tags} \tag{See also} \predref{read_string}{5} can be used to read lines with separated records without creating intermediate strings. \end{tags} \predicate[det]{read_stream_to_codes}{2}{+Stream, -Codes} \nodescription \predicate[det]{read_stream_to_codes}{3}{+Stream, -Codes, ?Tail} Read input from \arg{Stream} to a list of character codes. The version \predref{read_stream_to_codes}{3} creates a difference-list. \predicate[det]{read_file_to_codes}{3}{+Spec, -Codes, +Options} Read the file \arg{Spec} into a list of \arg{Codes}. \arg{Options} is split into options for \predref{absolute_file_name}{3} and \predref{open}{4}. In addition, the following option is provided: \begin{description} \termitem{tail}{?Tail} Read the data into a \textit{difference list} \arg{Codes}\Sneg{}\arg{Tail}. \end{description} \begin{tags} \tag{See also} \predref{phrase_from_file}{3} and \predref{read_file_to_string}{3}. \end{tags} \predicate[det]{read_file_to_string}{3}{+Spec, -String, +Options} Read the file \arg{Spec} into a the string \arg{String}. \arg{Options} is split into options for \predref{absolute_file_name}{3} and \predref{open}{4}. \begin{tags} \tag{See also} \predref{phrase_from_file}{3} and \predref{read_file_to_codes}{3}. \end{tags} \predicate[det]{read_file_to_terms}{3}{+Spec, -Terms, +Options} Read the file \arg{Spec} into a list of terms. \arg{Options} is split over \predref{absolute_file_name}{3}, \predref{open}{4} and \predref{read_term}{3}. In addition, the following option is processed: \begin{description} \termitem{tail}{?Tail} If present, \arg{Terms}\Sneg{}\arg{Tail} forms a \textit{difference list}. \end{description} Note that the \textit{output} options of \predref{read_term}{3}, such as \verb$variable_names$ or \verb$subterm_positions$ will cause \predref{read_file_to_terms}{3} to fail if \arg{Spec} contains multiple terms because the values for the different terms will not unify. \end{description}