more stream stuff.

This commit is contained in:
ubu32 2011-02-14 12:14:31 -08:00
parent c8aee540ce
commit 38fa886e18
3 changed files with 19 additions and 204 deletions

View File

@ -113,8 +113,6 @@ STATIC_PROTO (Int p_past_eof, (void));
STATIC_PROTO (Int p_skip, (void));
STATIC_PROTO (Int p_write_depth, (void));
STATIC_PROTO (Int p_user_file_name, (void));
STATIC_PROTO (Int p_show_stream_position, (void));
STATIC_PROTO (Int p_set_stream_position, (void));
STATIC_PROTO (Int p_format, (void));
STATIC_PROTO (Int p_startline, (void));
STATIC_PROTO (Int p_change_type_of_char, (void));
@ -2359,140 +2357,19 @@ p_user_file_name (void)
return (Yap_unify_constant (ARG2, tout));
}
static Term
StreamPosition(int sno)
{
Term sargs[5];
Int cpos;
cpos = Stream[sno].charcount;
if (Stream[sno].status & SWI_Stream_f) {
return Yap_get_stream_position(Stream[sno].u.swi_stream.swi_ptr);
}
if (Stream[sno].stream_getc == PlUnGetc) {
cpos--;
}
sargs[0] = MkIntegerTerm (cpos);
sargs[1] = MkIntegerTerm (StartLine = Stream[sno].linecount);
sargs[2] = MkIntegerTerm (Stream[sno].linepos);
sargs[3] = sargs[4] = MkIntTerm (0);
return Yap_MkApplTerm (FunctorStreamPos, 5, sargs);
return TermNil;
}
Term
Yap_StreamPosition(int sno)
{
return StreamPosition(sno);
}
static Int
p_show_stream_position (void)
{ /* '$show_stream_position'(+Stream,Pos) */
Term tout;
int sno =
CheckStream (ARG1, Input_Stream_f | Output_Stream_f | Append_Stream_f, "stream_position/2");
if (sno < 0)
return (FALSE);
tout = StreamPosition(sno);
UNLOCK(Stream[sno].streamlock);
return Yap_unify (ARG2, tout);
}
static Int
p_set_stream_position (void)
{ /* '$set_stream_position'(+Stream,Pos) */
Term tin, tp;
Int char_pos;
int sno = CheckStream (ARG1, Input_Stream_f | Output_Stream_f | Append_Stream_f, "set_stream_position/2");
if (sno < 0) {
return (FALSE);
}
tin = Deref (ARG2);
if (IsVarTerm (tin)) {
UNLOCK(Stream[sno].streamlock);
Yap_Error(INSTANTIATION_ERROR, tin, "set_stream_position/2");
return (FALSE);
} else if (!(IsApplTerm (tin))) {
UNLOCK(Stream[sno].streamlock);
Yap_Error(DOMAIN_ERROR_STREAM_POSITION, tin, "set_stream_position/2");
return (FALSE);
}
if (FunctorOfTerm (tin) == FunctorStreamPos) {
if (IsVarTerm (tp = ArgOfTerm (1, tin))) {
UNLOCK(Stream[sno].streamlock);
Yap_Error(INSTANTIATION_ERROR, tp, "set_stream_position/2");
return (FALSE);
} else if (!IsIntTerm (tp)) {
UNLOCK(Stream[sno].streamlock);
Yap_Error(DOMAIN_ERROR_STREAM_POSITION, tin, "set_stream_position/2");
return (FALSE);
}
if (!(Stream[sno].status & Seekable_Stream_f) ) {
UNLOCK(Stream[sno].streamlock);
Yap_Error(PERMISSION_ERROR_REPOSITION_STREAM, ARG1,"set_stream_position/2");
return(FALSE);
}
char_pos = IntOfTerm (tp);
if (IsVarTerm (tp = ArgOfTerm (2, tin))) {
UNLOCK(Stream[sno].streamlock);
Yap_Error(INSTANTIATION_ERROR, tp, "set_stream_position/2");
return (FALSE);
} else if (!IsIntTerm (tp)) {
UNLOCK(Stream[sno].streamlock);
Yap_Error(DOMAIN_ERROR_STREAM_POSITION, tin, "set_stream_position/2");
return (FALSE);
}
Stream[sno].charcount = char_pos;
Stream[sno].linecount = IntOfTerm (tp);
if (IsVarTerm (tp = ArgOfTerm (3, tin))) {
UNLOCK(Stream[sno].streamlock);
Yap_Error(INSTANTIATION_ERROR, tp, "set_stream_position/2");
return (FALSE);
} else if (!IsIntTerm (tp)) {
UNLOCK(Stream[sno].streamlock);
Yap_Error(DOMAIN_ERROR_STREAM_POSITION, tin, "set_stream_position/2");
return (FALSE);
}
Stream[sno].linepos = IntOfTerm (tp);
if (YP_fseek (Stream[sno].u.file.file, (long) (char_pos), 0) == -1) {
UNLOCK(Stream[sno].streamlock);
Yap_Error(SYSTEM_ERROR, tp,
"fseek failed for set_stream_position/2");
return(FALSE);
}
Stream[sno].stream_getc = PlGetc;
Stream[sno].stream_gets = PlGetsFunc();
} else if (FunctorOfTerm (tin) == FunctorStreamEOS) {
if (IsVarTerm (tp = ArgOfTerm (1, tin))) {
UNLOCK(Stream[sno].streamlock);
Yap_Error(INSTANTIATION_ERROR, tp, "set_stream_position/2");
return (FALSE);
} else if (tp != MkAtomTerm(AtomAt)) {
UNLOCK(Stream[sno].streamlock);
Yap_Error(DOMAIN_ERROR_STREAM_POSITION, tin, "set_stream_position/2");
return (FALSE);
}
if (!(Stream[sno].status & Seekable_Stream_f) ) {
UNLOCK(Stream[sno].streamlock);
Yap_Error(PERMISSION_ERROR_REPOSITION_STREAM, ARG1,"set_stream_position/2");
return(FALSE);
}
if (YP_fseek (Stream[sno].u.file.file, 0L, SEEK_END) == -1) {
UNLOCK(Stream[sno].streamlock);
Yap_Error(SYSTEM_ERROR, tp,
"fseek failed for set_stream_position/2");
return(FALSE);
}
Stream[sno].stream_getc = PlGetc;
Stream[sno].stream_gets = PlGetsFunc();
/* reset the counters */
Stream[sno].linepos = 0;
Stream[sno].linecount = 1;
Stream[sno].charcount = 0;
}
UNLOCK(Stream[sno].streamlock);
return (TRUE);
}
static Term
read_line(int sno)
@ -4051,8 +3928,6 @@ Yap_InitIOPreds(void)
Yap_InitCPred ("format", 2, p_format, SyncPredFlag);
Yap_InitCPred ("format", 3, p_format2, SyncPredFlag);
Yap_InitCPred ("$start_line", 1, p_startline, SafePredFlag|SyncPredFlag|HiddenPredFlag);
Yap_InitCPred ("$show_stream_position", 2, p_show_stream_position, SafePredFlag|SyncPredFlag|HiddenPredFlag);
Yap_InitCPred ("$set_stream_position", 2, p_set_stream_position, SafePredFlag|SyncPredFlag|HiddenPredFlag);
Yap_InitCPred ("$user_file_name", 2, p_user_file_name, SafePredFlag|SyncPredFlag),
Yap_InitCPred ("$past_eof", 1, p_past_eof, SafePredFlag|SyncPredFlag),
Yap_InitCPred ("$has_bom", 1, p_has_bom, SafePredFlag);

View File

@ -1403,6 +1403,10 @@ term_to_atom(Term, Atom) :-
with_output_to(Output, G) :-
swi_with_output_to(Output, G).
at_end_of_stream :-
swi_at_end_of_stream.
at_end_of_stream(Stream) :-
swi_at_end_of_stream(Stream).
byte_count(Stream, Count) :-
% format('~w~n',byte_count(Stream, Count)),
swi_byte_count(Stream, Count).
@ -1426,6 +1430,9 @@ set_stream(Stream, Property) :-
stream_property(Stream, Property) :-
% format('~w~n',stream_property(Stream,Property)),
swi_stream_property(Stream, Property).
set_stream_position(Stream, Position) :-
% format('~w~n',stream_property(Stream,Property)),
swi_set_stream_position(Stream, Position).
prompt1(X) :-
swi_prompt1(X).
@ -1499,3 +1506,7 @@ put_code(C) :-
put_code(Stream, C) :-
swi_put_code(Stream, C).
nl :-
swi_nl.
nl(Stream) :-
swi_nl(Stream).

View File

@ -268,11 +268,6 @@ read_term(Stream, T, Options) :-
'$add_singleton_if_no_underscore'(Na,V2,NSs,[(Name=V2)|NSs]) :-
atom_codes(Name, Na).
nl(Stream) :- put(Stream,10).
nl :- current_output(Stream), put(Stream,10), fail.
nl.
/* meaning of flags for '$write' is
1 quote illegal atoms
2 ignore operator declarations
@ -444,77 +439,11 @@ current_line_number(N) :-
current_line_number(Stream,N) :-
line_count(Stream, N).
stream_position(user,N) :- !,
'$show_stream_position'(user_input,N).
stream_position(A,N) :-
atom(A),
'$current_stream'(_,_,S), '$user_file_name'(S,A), !,
'$show_stream_position'(S,N).
stream_position(S,N) :-
'$show_stream_position'(S,N).
stream_position(user,N,M) :- !,
'$stream_position'(user_input,N,M).
stream_position(A,N,M) :-
atom(A),
'$current_stream'(_,_,S), '$user_file_name'(S,A), !,
'$stream_position'(S,N,M).
stream_position(S,N,M) :-
'$stream_position'(S,N,M).
'$stream_position'(S,N,M) :-
var(M), !,
'$show_stream_position'(S,N),
M = N.
'$stream_position'(S,N,M) :-
'$show_stream_position'(S,N),
'$set_stream_position'(S,M).
set_stream_position(S,N) :- var(S), !,
'$do_error'(instantiation_error, set_stream_position(S, N)).
set_stream_position(user,N) :- !,
'$set_stream_position'(user_input,N).
set_stream_position(A,N) :-
atom(A),
'$current_stream'(_,_,S), '$user_file_name'(S,A), !,
'$set_stream_position'(S,N).
set_stream_position(S,N) :-
'$set_stream_position'(S,N).
'$show_stream_eof'(Stream, past) :-
'$past_eof'(Stream), !.
'$show_stream_eof'(Stream, at) :-
'$peek'(Stream,N), N = -1, !.
'$show_stream_eof'(_, not).
'$show_stream_eof_action'(Fl, error) :-
Fl /\ 0x0200 =:= 0x0200, !.
'$show_stream_eof_action'(Fl, reset) :-
Fl /\ 0x0400 =:= 0x0400, !.
'$show_stream_eof_action'(_, eof_code).
'$show_stream_reposition'(Fl, true) :-
Fl /\ 0x2000 =:= 0x2000, !.
'$show_stream_reposition'(_, false).
'$show_stream_bom'(Fl, true) :-
'$has_bom'(Fl), !.
'$show_stream_bom'(_, false).
'$show_stream_type'(Fl, binary) :-
Fl /\ 0x0100 =:= 0x0100, !.
'$show_stream_type'(_, text).
at_end_of_stream :-
current_input(S),
at_end_of_stream(S).
at_end_of_stream(S) :-
'$past_eof'(S), !.
at_end_of_stream(S) :-
'$peek'(S,N), N = -1.
stream_position(Stream, Position) :-
stream_property(Stream, position(Position)).
stream_position(Stream, Position, NewPosition) :-
stream_property(Stream, position(Position)),
set_stream_position(Stream, NewPosition).
at_end_of_line :-
current_input(S),
@ -523,7 +452,7 @@ at_end_of_line :-
at_end_of_line(S) :-
'$past_eof'(S), !.
at_end_of_line(S) :-
'$peek'(S,N), ( N = 10 -> true ; N = -1).
peek(S,N), ( N = 10 -> true ; N = -1).
consult_depth(LV) :- '$show_consult_level'(LV).