2006-08-03 02:50:00 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: readutil.c *
|
|
|
|
* Last rev: 2/8/06 *
|
|
|
|
* mods: *
|
|
|
|
* comments: readutil library support *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "%W% %G%";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
#include "Yatom.h"
|
2009-10-23 14:22:17 +01:00
|
|
|
#include "YapHeap.h"
|
2006-08-03 02:50:00 +01:00
|
|
|
#include "yapio.h"
|
|
|
|
#include "iopreds.h"
|
|
|
|
|
|
|
|
static Int
|
|
|
|
rl_to_codes(Term TEnd, int do_as_binary, int arity)
|
|
|
|
{
|
|
|
|
int sno = Yap_CheckStream (ARG1, Input_Stream_f, "read_line_to_codes/2");
|
|
|
|
Int status;
|
|
|
|
UInt max_inp, buf_sz, sz;
|
|
|
|
char *buf;
|
|
|
|
int binary_stream;
|
|
|
|
|
|
|
|
if (sno < 0)
|
|
|
|
return FALSE;
|
|
|
|
status = Stream[sno].status;
|
|
|
|
binary_stream = Stream[sno].status & Binary_Stream_f;
|
|
|
|
if (status & Eof_Stream_f) {
|
|
|
|
UNLOCK(Stream[sno].streamlock);
|
2010-08-04 02:45:31 +01:00
|
|
|
return Yap_unify_constant(ARG2, MkAtomTerm (AtomEof));
|
2006-08-03 02:50:00 +01:00
|
|
|
}
|
|
|
|
max_inp = (ASP-H)/2-1024;
|
|
|
|
buf = (char *)TR;
|
|
|
|
buf_sz = (char *)Yap_TrailTop-buf;
|
|
|
|
while (TRUE) {
|
|
|
|
if ( buf_sz > max_inp ) {
|
|
|
|
buf_sz = max_inp;
|
|
|
|
}
|
|
|
|
if (do_as_binary && !binary_stream)
|
|
|
|
Stream[sno].status |= Binary_Stream_f;
|
|
|
|
sz = Stream[sno].stream_gets(sno, buf_sz, buf);
|
|
|
|
if (do_as_binary && !binary_stream)
|
|
|
|
Stream[sno].status &= ~Binary_Stream_f;
|
|
|
|
if (sz == -1 || sz == 0) {
|
|
|
|
if (Stream[sno].status & Eof_Stream_f) {
|
2010-08-03 21:05:53 +01:00
|
|
|
UNLOCK(Stream[sno].streamlock);
|
2006-08-03 02:50:00 +01:00
|
|
|
return Yap_unify_constant(ARG2, MkAtomTerm (AtomEof));
|
|
|
|
}
|
2010-08-03 21:05:53 +01:00
|
|
|
UNLOCK(Stream[sno].streamlock);
|
2006-08-03 02:50:00 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (Stream[sno].status & Eof_Stream_f || buf[sz-1] == 10) {
|
|
|
|
/* we're done */
|
|
|
|
Term end;
|
|
|
|
if (!(do_as_binary || Stream[sno].status & Eof_Stream_f)) {
|
2010-08-03 21:05:53 +01:00
|
|
|
UNLOCK(Stream[sno].streamlock);
|
2008-04-15 12:08:10 +01:00
|
|
|
/* handle CR before NL */
|
|
|
|
if (sz-2 >= 0 && buf[sz-2] == 13)
|
|
|
|
buf[sz-2] = '\0';
|
|
|
|
else
|
|
|
|
buf[sz-1] = '\0';
|
2010-08-03 21:05:53 +01:00
|
|
|
} else {
|
|
|
|
UNLOCK(Stream[sno].streamlock);
|
2006-08-03 02:50:00 +01:00
|
|
|
}
|
|
|
|
if (arity == 2)
|
|
|
|
end = TermNil;
|
|
|
|
else
|
|
|
|
end = Deref(XREGS[arity]);
|
|
|
|
return Yap_unify(ARG2, Yap_StringToDiffList((char *)TR, end)) ;
|
|
|
|
}
|
|
|
|
buf += (buf_sz-1);
|
|
|
|
max_inp -= (buf_sz-1);
|
|
|
|
if (max_inp <= 0) {
|
|
|
|
UNLOCK(Stream[sno].streamlock);
|
|
|
|
Yap_Error(OUT_OF_STACK_ERROR, ARG1, "read_line_to_codes/%d", arity);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_rl_to_codes(void)
|
|
|
|
{
|
|
|
|
return rl_to_codes(TermNil, FALSE, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_rl_to_codes2(void)
|
|
|
|
{
|
|
|
|
return rl_to_codes(TermNil, TRUE, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_stream_to_codes(void)
|
|
|
|
{
|
|
|
|
int sno = Yap_CheckStream (ARG1, Input_Stream_f, "read_line_to_codes/2");
|
|
|
|
CELL *HBASE = H;
|
2006-08-04 05:06:33 +01:00
|
|
|
CELL *h0 = &ARG4;
|
2006-08-03 02:50:00 +01:00
|
|
|
|
|
|
|
if (sno < 0)
|
|
|
|
return FALSE;
|
|
|
|
while (!(Stream[sno].status & Eof_Stream_f)) {
|
|
|
|
/* skip errors */
|
|
|
|
Int ch = Stream[sno].stream_getc(sno);
|
2006-08-04 05:06:33 +01:00
|
|
|
Term t;
|
2006-08-03 02:50:00 +01:00
|
|
|
if (ch == EOFCHAR)
|
|
|
|
break;
|
2006-08-04 05:06:33 +01:00
|
|
|
t = MkIntegerTerm(ch);
|
|
|
|
h0[0] = AbsPair(H);
|
|
|
|
*H = t;
|
|
|
|
H+=2;
|
|
|
|
h0 = H-1;
|
|
|
|
if (H >= ASP-1024) {
|
|
|
|
RESET_VARIABLE(h0);
|
|
|
|
ARG4 = AbsPair(HBASE);
|
|
|
|
ARG5 = (CELL)h0;
|
2008-08-28 04:43:00 +01:00
|
|
|
if (!Yap_gcl((ASP-HBASE)*sizeof(CELL), 5, ENV, gc_P(P,CP))) {
|
2006-08-04 05:06:33 +01:00
|
|
|
Yap_Error(OUT_OF_STACK_ERROR, ARG1, "read_stream_to_codes/3");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* build a legal term again */
|
|
|
|
h0 = (CELL *)ARG5;
|
|
|
|
HBASE = RepPair(ARG4);
|
2006-08-03 02:50:00 +01:00
|
|
|
}
|
|
|
|
}
|
2010-08-03 21:05:53 +01:00
|
|
|
UNLOCK(Stream[sno].streamlock);
|
2006-08-03 02:50:00 +01:00
|
|
|
if (H == HBASE)
|
|
|
|
return Yap_unify(ARG2,ARG3);
|
2006-08-04 05:06:33 +01:00
|
|
|
RESET_VARIABLE(H-1);
|
|
|
|
Yap_unify(H[-1],ARG3);
|
2006-08-03 02:50:00 +01:00
|
|
|
return Yap_unify(AbsPair(HBASE),ARG2);
|
2006-08-04 05:06:33 +01:00
|
|
|
|
2006-08-03 02:50:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_stream_to_terms(void)
|
|
|
|
{
|
|
|
|
int sno = Yap_CheckStream (ARG1, Input_Stream_f, "read_line_to_codes/2");
|
2008-10-23 22:17:45 +01:00
|
|
|
Term t = Deref(ARG3), tpos = TermNil;
|
2006-08-03 02:50:00 +01:00
|
|
|
|
|
|
|
if (sno < 0)
|
|
|
|
return FALSE;
|
|
|
|
while (!(Stream[sno].status & Eof_Stream_f)) {
|
|
|
|
/* skip errors */
|
2008-10-23 22:17:45 +01:00
|
|
|
TokEntry *tokstart = Yap_tokptr = Yap_toktide = Yap_tokenizer(sno, &tpos);
|
2006-08-03 02:50:00 +01:00
|
|
|
if (!Yap_ErrorMessage)
|
|
|
|
{
|
|
|
|
Term th = Yap_Parse();
|
|
|
|
if (H >= ASP-1024) {
|
2010-08-03 21:05:53 +01:00
|
|
|
UNLOCK(Stream[sno].streamlock);
|
2006-08-03 02:50:00 +01:00
|
|
|
Yap_Error(OUT_OF_STACK_ERROR, ARG1, "read_stream_to_terms/3");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!th || Yap_ErrorMessage)
|
|
|
|
break;
|
|
|
|
if (th == MkAtomTerm (AtomEof)) {
|
2010-08-03 21:05:53 +01:00
|
|
|
UNLOCK(Stream[sno].streamlock);
|
2006-08-03 02:50:00 +01:00
|
|
|
Yap_clean_tokenizer(tokstart, Yap_VarTable, Yap_AnonVarTable);
|
|
|
|
return Yap_unify(t,ARG2);
|
|
|
|
} else {
|
|
|
|
t = MkPairTerm(th,t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Yap_clean_tokenizer(tokstart, Yap_VarTable, Yap_AnonVarTable);
|
|
|
|
}
|
2010-08-03 21:05:53 +01:00
|
|
|
UNLOCK(Stream[sno].streamlock);
|
2006-08-03 02:50:00 +01:00
|
|
|
return Yap_unify(t,ARG2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Yap_InitReadUtil(void)
|
|
|
|
{
|
|
|
|
Term cm = CurrentModule;
|
|
|
|
CurrentModule = READUTIL_MODULE;
|
2010-07-19 14:42:22 +01:00
|
|
|
Yap_InitCPred("read_line_to_codes", 2, p_rl_to_codes, SyncPredFlag);
|
|
|
|
Yap_InitCPred("read_line_to_codes", 3, p_rl_to_codes2, SyncPredFlag);
|
2006-08-04 05:06:33 +01:00
|
|
|
Yap_InitCPred("read_stream_to_codes", 3, p_stream_to_codes, SyncPredFlag);
|
2010-07-19 14:42:22 +01:00
|
|
|
Yap_InitCPred("read_stream_to_terms", 3, p_stream_to_terms, SyncPredFlag);
|
2006-08-03 02:50:00 +01:00
|
|
|
CurrentModule = cm;
|
|
|
|
}
|
|
|
|
|