:- module('io',[]).  %% converted djd
:- style_check(-singleton). % added djd
%% :- module('iO', []).

:- discontiguous 'iO.writeCharList.p2'/2. %% added djd
:- discontiguous '~iO.writeCharList.p2'/2. %% added djd


:- op(500, yfx, and).
:- op(400, yfx, or).

'iO.newLine.p1'(A) :-
        user:goedel_freeze(ground([A]), 'iO':'iO.newLine.p1.0'(A)).
'~iO.newLine.p1'(A) :-
        user:goedel_freeze(ground([A]), 'iO':'~iO.newLine.p1.0'(A)).
'iO.newLine.p1.0'(A) :-
        'iO.put.p2'(A, 10).
'~iO.newLine.p1.0'(A) :-
        '~iO.put.p2'(A, 10).
'iO.writeCharList.p2'([], _).
'~iO.writeCharList.p2'([], _).
'iO.writeCharList.p2'([A|B], C) :-
        'iO.put.p2'(C, A),
        'iO.writeCharList.p2'(B, C).
'~iO.writeCharList.p2'([A|B], C) :-
        '~iO.put.p2'(C, A),
        '~iO.writeCharList.p2'(B, C).
'iO.writeString.p2'(A, B) :-
        user:goedel_freeze(ground([A,B]), 'iO':'iO.writeString.p2.0'(A,B)).
'~iO.writeString.p2'(A, B) :-
        user:goedel_freeze(ground([A,B]), 'iO':'~iO.writeString.p2.0'(A,B)).
'iO.writeString.p2.0'(A, B) :-
        'strings':'strings.stringInts.p2'(B, C),
        'iO.writeCharList.p2'(C, A).
'~iO.writeString.p2.0'(A, B) :-
        'strings':'~strings.stringInts.p2'(B, C),
        '~iO.writeCharList.p2'(C, A).
/*
File:      iO.sup
Subject:   the system module iO.
Author:	   Jiwei Wang
Date:      17 September 1991

================================================================================
*/

%% '$$module'('@(#)iO.sup 1.13 last updated 94/02/11 17:07:48 by jiwei').

module(iO_sup).

?- dynamic '$end_of_stream'/1.


'iO.get.p2'(X, Y) :-
   nonvar(X), !,
   'iO.get.p2.1'(X,Y).

'iO.get.p2'(X, Y) :-
   user:goedel_freeze(ground([X]), 'iO':'iO.get.p2.1'(X,Y)).


'iO.get.p2.1'('iO.stdIn.c0', Char) :-
   !,
   prompt(Old, ''),
   get0(user_input, Char),
   prompt(_, Old).

'iO.get.p2.1'(ResultOfFind, Char) :-
   '$end_of_stream'(ResultOfFind), !,
   Char = -1.

'iO.get.p2.1'(ResultOfFind, Char) :-
   translate_stream(ResultOfFind, Stream),
   get0(Stream, Char),
   check_for_end_of_stream(ResultOfFind, Char).

'~iO.get.p2'(ResultOfFind, Char) :-
   'iO.get.p2'(ResultOfFind, Char).

%------------------------------------------------------------------------------

'iO.readChar.p2'(X, Y) :-
   nonvar(X), !,
   'iO.readChar.p2.1'(X,Y).

'iO.readChar.p2'(X, Y) :-
   user:goedel_freeze(ground([X]), 'iO':'iO.readChar.p2.1'(X,Y)).


'iO.readChar.p2.1'('iO.stdIn.c0', String) :-
   !,
   prompt(Old, ''),
   get0(user_input, Char),
   name(String, [34, Char]) ,  % 34 for "
   prompt(_, Old).

'iO.readChar.p2.1'(ResultOfFind, String) :-
   '$end_of_stream'(ResultOfFind), !,
   name(String, [34]).   % return empty string    % 34 for "

'iO.readChar.p2.1'(ResultOfFind, String) :-
   translate_stream(ResultOfFind, Stream),
   get0(Stream, Char),
   ( Char == -1
     -> assert('$end_of_stream'(ResultOfFind)),
        name(String, [34])   % return empty string    % 34 for "
     ;  name(String, [34, Char])    % 34 for "
   ).

'~iO.readChar.p2'(ResultOfFind, String) :-
   'iO.readChar.p2'(ResultOfFind, String).

%------------------------------------------------------------------------------

'iO.put.p2'(X, Y) :-
   nonvar(X), integer(Y), !,
   'iO.put.p2.1'(X,Y).

'iO.put.p2'(X, Y) :-
   user:goedel_freeze(ground([X,Y]), 'iO':'iO.put.p2.1'(X,Y)).


'iO.put.p2.1'(ResultOfFind, Char) :-
   Char >= 0, Char =< 127,
   translate_stream(ResultOfFind, Stream),
   put(Stream, Char).

'~iO.put.p2'(ResultOfFind, Char) :-
   'iO.put.p2'(ResultOfFind, Char).

%------------------------------------------------------------------------------

check_for_end_of_stream(ResultOfFind, C) :-
   ( C == -1
     -> assert('$end_of_stream'(ResultOfFind))
     ;  true
   ).

%------------------------------------------------------------------------------

'iO.findInput.p2'(X, Y) :-
   user:goedel_freeze(ground([X]), 'iO':'iO.findInput.p2.1'(X,Y)).

'iO.findInput.p2.1'(FileString, ResultOfFind) :-
   user:gstring2string(FileString, FileName),
   ( open(FileName, read, Stream)
     -> translate_stream('iO.inputStreamDescriptor.f1'(List), Stream),
	ResultOfFind = 'iO.in.f1'('iO.inputStreamDescriptor.f1'(List))
     ;  ResultOfFind = 'iO.notFound.c0'
   ).

'~iO.findInput.p2'(FileString, ResultOfFind) :-
   'iO.findInput.p2'(FileString, ResultOfFind).

%------------------------------------------------------------------------------

'iO.findOutput.p2'(X, Y) :-
   user:goedel_freeze(ground([X]), 'iO':'iO.findOutput.p2.1'(X,Y)).

'iO.findOutput.p2.1'(FileString, ResultOfFind) :-
   user:gstring2string(FileString, FileName),
   ( open(FileName, write, Stream)
     -> translate_stream('iO.outputStreamDescriptor.f1'(List), Stream),
	ResultOfFind = 'iO.out.f1'('iO.outputStreamDescriptor.f1'(List))
     ;  ResultOfFind = 'iO.notFound.c0'
   ).

'~iO.findOutput.p2'(FileString, ResultOfFind) :-
   'iO.findOutput.p2'(FileString, ResultOfFind).

%------------------------------------------------------------------------------

'iO.findUpdate.p2'(X, Y) :-
   user:goedel_freeze(ground([X]), 'iO':'iO.findUpdate.p2.1'(X,Y)).

'iO.findUpdate.p2.1'(FileString, ResultOfFind) :-
   user:gstring2string(FileString, FileName),
   ( open(FileName, append, Stream)
     -> translate_stream('iO.outputStreamDescriptor.f1'(List), Stream),
	ResultOfFind = 'iO.out.f1'('iO.outputStreamDescriptor.f1'(List))
     ;  ResultOfFind = 'iO.notFound.c0'
   ).

'~iO.findUpdate.p2'(FileString, ResultOfFind) :-
   'iO.findUpdate.p2'(FileString, ResultOfFind).

%------------------------------------------------------------------------------

'iO.endInput.p1'(X) :-
   user:goedel_freeze(ground([X]), 'iO':'iO.endInput.p1.1'(X)).

'iO.endInput.p1.1'(GoedelStream) :-
   retractall('$end_of_stream'(GoedelStream)),
   translate_stream(GoedelStream, Stream),
   close(Stream).

'~iO.endInput.p1'(GoedelStream) :-
   'iO.endInput.p1'(GoedelStream).

%------------------------------------------------------------------------------

'iO.endOutput.p1'(X) :-
   user:goedel_freeze(ground([X]), 'iO':'iO.endOutput.p1.1'(X)).

'iO.endOutput.p1'(GoedelStream) :-
   translate_stream(GoedelStream, Stream),
   close(Stream).

'~iO.endOutput.p1'(GoedelStream) :-
   'iO.endOutput.p1'(GoedelStream).

%------------------------------------------------------------------------------

'iO.flush.p1'(X) :-
   nonvar(X), !,
   'iO.flush.p1.1'(X).

'iO.flush.p1'(X) :-
   user:goedel_freeze(ground([X]), 'iO':'iO.flush.p1.1'(X)).

'iO.flush.p1.1'(GoedelStream) :-
   translate_stream(GoedelStream, Stream),
   flush_output(Stream).

'~iO.flush.p1'(GoedelStream) :-
   'iO.flush.p1'(GoedelStream).

%------------------------------------------------------------------------------

translate_stream('iO.inputStreamDescriptor.f1'(List), Stream) :-
   Stream =.. ['$stream'|List].
translate_stream('iO.outputStreamDescriptor.f1'(List), Stream) :-
   Stream =.. ['$stream'|List].
translate_stream('iO.stdIn.c0', user_input).
translate_stream('iO.stdOut.c0', user_output).
translate_stream('iO.stdErr.c0', user_error).
