Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Completeness of :- use_module(library(prolog_stream)). #4

Open
TeamSPoon opened this issue Aug 21, 2015 · 4 comments
Open

Feature/Completeness of :- use_module(library(prolog_stream)). #4

TeamSPoon opened this issue Aug 21, 2015 · 4 comments

Comments

@TeamSPoon
Copy link



:- use_module(library(prolog_stream)).

% Writeq/1s a term the user_error and flushes
dmsg(M):-format(user_error,'~N~n% dmsg: ~q.~n',[M]),flush_output(user_error).


/** <module> Tests the Prolog stream callbacks
This library defines a Prolog  stream   that  realises its low-level I/O
with callbacks to Prolog.  The  library   was  developed  to bind normal
Prolog I/O to Pengines I/O. This type of I/O redirection is probably the
primary use case.
*/

%%  open_prolog_stream(+Module, +Mode, -Stream, +Options)
%
%   Create  a  new  stream  that  implements   its  I/O  by  calling
%   predicates in Module.  The called predicates are:
%
%   The current implementation only  deals   with  text streams. The
%   stream uses the =wchar_t= encoding. The   buffer  size must be a
%   multiple of =wchar_t=, i.e., a multiple of four for portability.
%   The _newline_ mode of the stream   is  =posix= on all platforms,
%   disabling the translation `"\n" --> "\r\n"`.
%
%   @arg Options is currently ignored.
%   @bug    Futher versions might require additional callbacks.  As we
%       demand all callbacks to be defined, existing code needs
%       to implement the new callbacks.


%     - Module:stream_close(+Stream)
%     Called when the stream is closed.  This predicate must
%     succeed.  The callback can be used to cleanup associated
%     resources.

:- thread_local(tl_with_prolog_streams:stream_close/1).

%     - Module:stream_write(+Stream, +String)
%     Called for a `Mode = write` stream if data is available.
%     String contains the (textual) data that is written
%     to Stream.  The callback is called if the buffer of
%     Stream overflows, the user calls flush_output(Stream)
%     or Stream is closed and there is buffered data.

:- thread_local(tl_with_prolog_streams:stream_write/2).

:- meta_predicate(with_output_to_pred(1,0)).

with_output_to_pred(Callback,Goal):-
 open_prolog_stream(tl_with_prolog_streams, write, Stream, []),
   asserta(((tl_with_prolog_streams:stream_write(Stream,Data):- (ignore(call(Callback,Data))))),Ref),
   asserta(((tl_with_prolog_streams:stream_close(Stream):- (ignore(call(Callback,end_of_file))))),Ref2),
    % catch so we will not exception on a closed stream   
    call_cleanup(with_output_to(Stream,call_cleanup(Goal,catch(flush_output(Stream),_,true))),(erase(Ref),erase(Ref2))).
     % Not decided that a with_output_to_pred/2 should call close of not (flush gets the job done equally as well as closing)

:- dynamic(received_chars/1).

% test predciate to receive char codes
buffer_chars(end_of_file):-!,assertz(received_chars(end_of_file)).
buffer_chars(N):-number(N),!,char_code(C,N),assertz(received_chars(C)).
buffer_chars(C):-name(C,Chars),maplist(buffer_chars,Chars).


:- with_output_to_pred(buffer_chars,write('hello. ')).
:- with_output_to_pred(buffer_chars,write('World.\n')).
% lets just be nasy to ourselves here (confirms we handle closing of current output)
:- with_output_to_pred(buffer_chars,told).
:- with_output_to_pred(buffer_chars,(current_output(Out),close(Out))).
% Not bad !

:- listing(received_chars/1).

/*
received_chars(h).
received_chars(e).
received_chars(l).
received_chars(l).
received_chars(o).
received_chars('.').
received_chars(' ').
received_chars('W').
received_chars(o).
received_chars(r).
received_chars(l).
received_chars(d).
received_chars('.').
received_chars('\n').
received_chars(end_of_file).
received_chars(end_of_file).

Looks good !
*/


:- with_output_to_pred(dmsg, (current_output(Out),forall(stream_property(Out,Prop),dmsg(stream_property(Out,Prop))))).

% dmsg: stream_property(<stream>(0x232b8a0),mode(write)).
% dmsg: stream_property(<stream>(0x232b8a0),output).
% dmsg: stream_property(<stream>(0x232b8a0),position('$stream_position'(0,1,0,0))).
% dmsg: stream_property(<stream>(0x232b8a0),eof_action(eof_code)).
% dmsg: stream_property(<stream>(0x232b8a0),reposition(false)).
% dmsg: stream_property(<stream>(0x232b8a0),type(text)).
% dmsg: stream_property(<stream>(0x232b8a0),buffer(full)).
% dmsg: stream_property(<stream>(0x232b8a0),buffer_size(0)).
% dmsg: stream_property(<stream>(0x232b8a0),close_on_abort(true)).
% dmsg: stream_property(<stream>(0x232b8a0),encoding(wchar_t)).
% dmsg: stream_property(<stream>(0x232b8a0),locale(default)).
% dmsg: stream_property(<stream>(0x232b8a0),newline(posix)).
% dmsg: stream_property(<stream>(0x232b8a0),representation_errors(error)).



%     - Module:stream_read(+Stream, -Term)
%     Called for a `Mode == read` stream to get new data.  On
%     success the stream extracts text from the provided Term.
%     Term is typically a string, atom, code or character list.
%     If term is not one of the above, it is handed to writeq/1.
%     To signal end-of-file, unify stream with an empty text,
%     e.g., `stream_read(Stream, "")`.

:- thread_local(tl_with_prolog_streams:stream_read/2).

:- meta_predicate(with_input_from_pred(1,0)).

with_input_from_pred(Callback,Goal):-
 current_input(Old),
 call_cleanup((
   open_prolog_stream(tl_with_prolog_streams, read, Stream, []),
   asserta(((tl_with_prolog_streams:stream_read(Stream,Data):- ((call(Callback,Data))))),Ref),
   asserta(((tl_with_prolog_streams:stream_close(Stream):- (ignore(call(Callback,end_of_file))))),Ref2),
   setup_call_cleanup(see(Stream), Goal,(seen,erase(Ref2),erase(Ref)))),
 set_input(Old)).

% our test callback
read_received(A):- A == end_of_file,!. %  Dmiles was lazy
read_received(C):- retract(received_chars(A)), (A==end_of_file -> C="" ; C =A).
read_received("").

% Test wtih read/1 works awesome
:- with_input_from_pred(read_received, (read(X), writeln(read(X)))).
% dmsg: read(hello)

% This test is just for deciding the scope .. (about asking callback to understand peeking or not)
:- with_input_from_pred(read_received, (peek_char(X), writeln(peek_char(X)))).
% dmsg: peek_char('W')

:- listing(received_chars).

/*
:- dynamic received_chars/1.

received_chars(o).
received_chars(r).
received_chars(l).
received_chars(d).
received_chars('.').
received_chars('\n').
received_chars(end_of_file).
received_chars(end_of_file).
*/

:- with_input_from_pred(=(""), (current_input(In),forall(stream_property(In,Prop),dmsg(stream_property(In,Prop))))).

% dmsg: stream_property(<stream>(0x9cfd70),mode(read)).
% dmsg: stream_property(<stream>(0x9cfd70),input).
% dmsg: stream_property(<stream>(0x9cfd70),position('$stream_position'(0,1,0,0))).
% dmsg: stream_property(<stream>(0x9cfd70),end_of_stream(not)).
% dmsg: stream_property(<stream>(0x9cfd70),eof_action(eof_code)).
% dmsg: stream_property(<stream>(0x9cfd70),reposition(false)).
% dmsg: stream_property(<stream>(0x9cfd70),type(text)).
% dmsg: stream_property(<stream>(0x9cfd70),buffer(full)).
% dmsg: stream_property(<stream>(0x9cfd70),buffer_size(0)).
% dmsg: stream_property(<stream>(0x9cfd70),close_on_abort(true)).
% dmsg: stream_property(<stream>(0x9cfd70),encoding(wchar_t)).
% dmsg: stream_property(<stream>(0x9cfd70),locale(default)).
% dmsg: stream_property(<stream>(0x9cfd70),newline(posix)).
% dmsg: stream_property(<stream>(0x9cfd70),representation_errors(error)).
% dmsg: stream_property(<stream>(0x9cfd70),timeout(infinite)).


% Passes
:- \+ with_input_from_pred(=(""), \+ at_end_of_stream(current_input)).

% Passes
:- with_input_from_pred(=(hi), \+ at_end_of_stream(current_input)).



% Test 1
test1(In) :-
        repeat,
               get_char(In, Char),
               dmsg(getch(In,Char)),
               Char == end_of_file.


% Passes
:- with_input_from_pred(read_received, test1(current_input)).

% dmsg: getch(current_input,o).
% dmsg: getch(current_input,r).
% dmsg: getch(current_input,l).
% dmsg: getch(current_input,d).
% dmsg: getch(current_input,'.').
% dmsg: getch(current_input,'\n').


% setup for test 2 
:- with_output_to_pred(buffer_chars, format('Read it all\n',[])).
:- listing(received_chars/1).


% Test 2 is indeed asks much, but still is reasonable
test2(In) :-
        repeat,
            (   at_end_of_stream(In)
            ->  !
            ;   read_pending_input(In, Chars, []),
                dmsg(read_pending_input(In, Chars, [])),
                fail
            ).

% TODO
:- with_input_from_pred(read_received, test2(current_input)).



'''
@JanWielemaker
Copy link
Member

Lots of code, but what is the question?

@TeamSPoon
Copy link
Author

Sorry got distracted making it fair :)

Very last line I am thinking either my test is in error or read_pending_input does not like the stream yet.

The other stuff above it just because I was making some tests to make sure I understood things

@JanWielemaker
Copy link
Member

If it is not urgent, I'll leave it ...

@TeamSPoon
Copy link
Author

Not urgent at all.. I currently able to do the things I wanted!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants