YAP 7.1.0
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
readutil.yap
Go to the documentation of this file.
1/*************************************************************************
2* *
3* YAP Prolog *
4* *
5* Yap Prolog was developed at NCCUP - Universidade do Porto *
6* *
7* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
8* *
9**************************************************************************
10* *
11* File: readutil.yap *
12* Last rev: 5/12/99 *
13* mods: *
14* comments: SWI compatible read utilities *
15* *
16*************************************************************************/
17
18/**
19 * @file readutil.yap
20 * @author VITOR SANTOS COSTA <vsc@VITORs-MBP.lan>
21 * @date Wed Nov 18 00:16:15 2015
22 *
23 * @brief Read full lines and a full file in a single call.
24 *
25 *
26*/
27
28:- module(readutil, [
30 read_line_to_codes/3,
31 read_stream_to_codes/2,
33 read_file_to_codes/2,
34 read_file_to_codes/3,
35 read_file_to_terms/2,
36 read_file_to_terms/3,
37 read_line_to_string/2
38 ]).
39
40/**
41* @defgroup readutil Reading Lines and Files
42* @ingroup YAPLibrary
43* @{
44* Read full lines and a full file in a single call.
45*
46*/
47
48/**
49 read_stream_to_codes( +_Stream_, -_Codes_)
50
51 If _Stream_ is a readable text stream, unify _Codes_ with
52 the sequence of character codess available from the stream.
53
54 If the stream had been emptied before, unify _Codes_ with `end_of_file`.
55 */
56read_stream_to_codes(Stream, Codes) :-
57 read_stream_to_codes(Stream, Codes, []).
58
59/**
60 read_stream_to_codes( +_Stream_, -_Codes_, ?_Tail_)
61
62 If _Stream_ is a readable text stream, unify _Codes_-_Tail with
63 the sequence of character codess available from the stream.
64
65 */
66read_file_to_codes(File, Codes, _) :-
67 open(File, read, Stream),
68 read_stream_to_codes(Stream, Codes, []),
69 close(Stream).
70
71read_file_to_codes(File, Codes) :-
72 open(File, read, Stream),
73 read_stream_to_codes(Stream, Codes, []),
74 close(Stream).
75
76read_file_to_terms(File, Codes, _) :-
77 open(File, read, Stream),
78 prolog_read_stream_to_terms(Stream, Codes, []),
79 close(Stream).
80
81read_file_to_terms(File, Codes) :-
82 open(File, read, Stream),
83 read_stream_to_terms(Stream, Codes, []),
84 close(Stream).
85
86
87prolog_read_stream_to_terms(Stream, Terms, Terms0) :-
88 read(Stream, Term),
89 (Term == end_of_file ->
90 Terms = Terms0
91 ;
92 Terms = [Term|TermsI],
93 prolog_read_stream_to_terms(Stream, TermsI, Terms0)
94 ).
95
96%% @}
97
close(+ S)
open(+ F,+ M,- S)
read(+ Stream, -Term )
read_line_to_codes( +_Stream_, -_String_)
read_stream_to_codes( +_Stream_, -Codes, ?_Tail_)
read_stream_to_terms( +_Stream_, -Terms, ?_Tail_)