title: How much safe to use read_pending_codes/3 to see end-of-data
Dear SWI-Prologers,
I have written a predicate read_codes/2 which reads codes from the
standard input up to a specified end_of_data codes. The end_of_data
codes must appear at the current end of the standard input. In other
words, only when the end_of_data is at the end of the *pending codes*
(read by read_pending_code/3),
there is currently nothing more to read from the standard input. Note
that the end_of_data codes may appear as data (atom name or string
data) from the standard input.
Although I am not really sure whether my use of the
read_pending_codes/3 matches its intended uses, it works
so far so good withtout any problem even for reading
data which has the end_of_data codes in the middle.
I will appreciate if someone tell me any simple alternative
to my read_codes/2 for any (utf-8 and ascii) text.
%%%% definition of read_codes/2 %%%%%
% ?- read_codes(X, `yz`).
% |:abyzcyz
read_codes(X, End):- get_code(C),
read_codes(C, End, End, [], X, []).
%
read_codes(X):- read_codes(X, `\000\\n`).
%
read_codes(C, [C|R], End, Q, X, Y):- !, read_codes(R, End, [C|Q], X, Y).
read_codes(C, _, End, [], [C|X], Y):- !, read_codes(End, End, [], X, Y).
read_codes(C, _, End, Q, X, Y):- reverse(Q, Z, X),
read_codes(C, End, End, [], Z, Y).
%
read_codes([], End, Q, X, Y):- !,
read_pending_codes(user_input, P, []),
( P==[] -> X = Y
; reverse(Q, Z, X),
read_codes_suspended(P, End, Z, Y)
).
read_codes(R, End, Q, X, Y):- get_code(C),
read_codes(C, R, End, Q, X, Y).
%
read_codes_suspended(S, End, X, Y):-
( append(A, End, S) -> append(A, Y, X)
; append(S, Z, X),
read_pending_codes(user_input, P, []),
read_codes_suspended(P, End, Z, Y)
).
reverse([A|B], X, Y):- reverse(B, [A|X], Y).
reverse([], X, X).
Kuniaki Mukai