How to read a stream in Eclipse CLP?

248 Views Asked by At

I am new to Eclipse, and I have been trying to read a file stream without success. The code that I normally use for this in SWI-Prolog is this:

read_until_stop(File, [L|Lines]) :-
    read_line_to_codes(File, Codes),
    Codes \= end_of_file,
    atom_codes(L, Codes),
    L \= stop,
    !,
    read_until_stop(File, Lines).
read_until_stop(_, []).

But read_line_to_codes is not available in Eclipse apparently. What is a good alternative for this?

2

There are 2 best solutions below

0
On

As suggested by the Eclipse manual the Eclipse counterpart would be

read_line(Stream, String) :-
    read_string(Stream, end_of_line, _Length, String).

with the difference that read_string returns the actual string as opposed to the list of codes, i.e., atom_codes is no longer necessary:

?- read_string(input, end_of_line, Length, String).
      abcdefghi
      Length = 9
      String = "abcdefghi"
      yes.
0
On

I think read_line_to_codes/2 can easily be implemented in ECLiPSe, but for efficiency reuse available builtins. You can do with read_line/2.

Try to define

:- use_module(library(util)).
read_line_to_codes(S, L) :- read_line(S, L).

or simply call read_line...