2015-06-18 01:21:20 +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 "YapHeap.h"
|
|
|
|
#include "YapText.h"
|
2016-03-03 23:18:04 +00:00
|
|
|
#include "Yatom.h"
|
2015-09-21 23:05:36 +01:00
|
|
|
#include "encoding.h"
|
2016-03-03 23:18:04 +00:00
|
|
|
#include "iopreds.h"
|
|
|
|
#include "yapio.h"
|
2015-09-21 23:05:36 +01:00
|
|
|
|
2015-11-18 15:06:25 +00:00
|
|
|
/// @addtogroup readutil
|
|
|
|
|
2016-03-03 23:18:04 +00:00
|
|
|
static Int rl_to_codes(Term TEnd, int do_as_binary, int arity USES_REGS) {
|
|
|
|
int sno = Yap_CheckStream(ARG1, Input_Stream_f, "read_line_to_codes/2");
|
|
|
|
StreamDesc *st = GLOBAL_Stream + sno;
|
2015-06-18 01:21:20 +01:00
|
|
|
Int status;
|
|
|
|
UInt max_inp, buf_sz, sz;
|
2016-03-03 23:18:04 +00:00
|
|
|
unsigned char *buf;
|
|
|
|
bool binary_stream;
|
|
|
|
int ch;
|
2015-06-18 01:21:20 +01:00
|
|
|
|
|
|
|
if (sno < 0)
|
2016-03-03 23:18:04 +00:00
|
|
|
return false;
|
2015-06-18 01:21:20 +01:00
|
|
|
status = GLOBAL_Stream[sno].status;
|
|
|
|
binary_stream = GLOBAL_Stream[sno].status & Binary_Stream_f;
|
|
|
|
if (status & Eof_Stream_f) {
|
|
|
|
UNLOCK(GLOBAL_Stream[sno].streamlock);
|
2016-03-03 23:18:04 +00:00
|
|
|
return Yap_unify_constant(ARG2, MkAtomTerm(AtomEof));
|
2015-06-18 01:21:20 +01:00
|
|
|
}
|
2016-03-03 23:18:04 +00:00
|
|
|
max_inp = (ASP - HR) / 2 - 1024;
|
|
|
|
buf = (unsigned char *)TR;
|
|
|
|
buf_sz = (unsigned char *)LOCAL_TrailTop - buf;
|
|
|
|
while (true) {
|
|
|
|
if (buf_sz > max_inp) {
|
2015-06-18 01:21:20 +01:00
|
|
|
buf_sz = max_inp;
|
|
|
|
}
|
2016-02-21 19:06:25 +00:00
|
|
|
if (do_as_binary && !binary_stream) {
|
2015-06-18 01:21:20 +01:00
|
|
|
GLOBAL_Stream[sno].status |= Binary_Stream_f;
|
2016-02-21 19:06:25 +00:00
|
|
|
}
|
|
|
|
if (st->status & Binary_Stream_f) {
|
2016-03-03 23:18:04 +00:00
|
|
|
char *b = (char *)TR;
|
|
|
|
sz = fread(b, 1, buf_sz, GLOBAL_Stream[sno].file);
|
2016-02-21 19:06:25 +00:00
|
|
|
} else {
|
2016-03-03 23:18:04 +00:00
|
|
|
unsigned char *pt = buf;
|
|
|
|
do {
|
|
|
|
ch = st->stream_wgetc_for_read(sno);
|
2016-03-05 23:43:11 +00:00
|
|
|
if (ch < 127) {
|
2016-07-31 15:54:07 +01:00
|
|
|
|
2016-03-05 23:43:11 +00:00
|
|
|
if (ch < 0) {
|
2016-07-31 15:54:07 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
*pt++ = ch;
|
2016-03-29 01:56:38 +01:00
|
|
|
} else {
|
2016-03-05 23:43:11 +00:00
|
|
|
pt += get_utf8(pt, 4, &ch);
|
|
|
|
if (pt + 4 == buf + buf_sz)
|
|
|
|
break;
|
|
|
|
}
|
2016-03-03 23:18:04 +00:00
|
|
|
} while (ch != '\n');
|
|
|
|
sz = pt - buf;
|
|
|
|
}
|
2015-06-18 01:21:20 +01:00
|
|
|
if (do_as_binary && !binary_stream)
|
|
|
|
GLOBAL_Stream[sno].status &= ~Binary_Stream_f;
|
|
|
|
if (sz == -1 || sz == 0) {
|
|
|
|
if (GLOBAL_Stream[sno].status & Eof_Stream_f) {
|
2016-03-03 23:18:04 +00:00
|
|
|
UNLOCK(GLOBAL_Stream[sno].streamlock);
|
|
|
|
return Yap_unify_constant(ARG2, MkAtomTerm(AtomEof));
|
2015-06-18 01:21:20 +01:00
|
|
|
}
|
|
|
|
UNLOCK(GLOBAL_Stream[sno].streamlock);
|
|
|
|
}
|
2016-03-03 23:18:04 +00:00
|
|
|
if (GLOBAL_Stream[sno].status & Eof_Stream_f || buf[sz - 1] == 10) {
|
2015-06-18 01:21:20 +01:00
|
|
|
/* we're done */
|
|
|
|
Term end;
|
|
|
|
if (!(do_as_binary || GLOBAL_Stream[sno].status & Eof_Stream_f)) {
|
2016-03-03 23:18:04 +00:00
|
|
|
UNLOCK(GLOBAL_Stream[sno].streamlock);
|
|
|
|
/* handle CR before NL */
|
|
|
|
if ((Int)sz - 2 >= 0 && buf[sz - 2] == 13)
|
|
|
|
buf[sz - 2] = '\0';
|
|
|
|
else
|
|
|
|
buf[sz - 1] = '\0';
|
2015-06-18 01:21:20 +01:00
|
|
|
} else {
|
2016-03-03 23:18:04 +00:00
|
|
|
UNLOCK(GLOBAL_Stream[sno].streamlock);
|
2015-06-18 01:21:20 +01:00
|
|
|
}
|
|
|
|
if (arity == 2)
|
2016-03-03 23:18:04 +00:00
|
|
|
end = TermNil;
|
2015-06-18 01:21:20 +01:00
|
|
|
else
|
2016-03-03 23:18:04 +00:00
|
|
|
end = Deref(XREGS[arity]);
|
|
|
|
return Yap_unify(
|
|
|
|
ARG2, Yap_UTF8ToDiffListOfCodes((const char *)TR, end PASS_REGS));
|
|
|
|
}
|
|
|
|
buf += (buf_sz - 1);
|
|
|
|
max_inp -= (buf_sz - 1);
|
2015-06-18 01:21:20 +01:00
|
|
|
if (max_inp <= 0) {
|
|
|
|
UNLOCK(GLOBAL_Stream[sno].streamlock);
|
2015-09-25 10:57:26 +01:00
|
|
|
Yap_Error(RESOURCE_ERROR_STACK, ARG1, "read_line_to_codes/%d", arity);
|
2016-03-03 23:18:04 +00:00
|
|
|
return FALSE;
|
2015-06-18 01:21:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-03 23:18:04 +00:00
|
|
|
static Int read_line_to_codes(USES_REGS1) {
|
2015-06-18 01:21:20 +01:00
|
|
|
return rl_to_codes(TermNil, FALSE, 2 PASS_REGS);
|
|
|
|
}
|
|
|
|
|
2016-03-03 23:18:04 +00:00
|
|
|
static Int read_line_to_codes2(USES_REGS1) {
|
2015-06-18 01:21:20 +01:00
|
|
|
return rl_to_codes(TermNil, TRUE, 3 PASS_REGS);
|
|
|
|
}
|
|
|
|
|
2016-03-03 23:18:04 +00:00
|
|
|
static Int read_line_to_string(USES_REGS1) {
|
|
|
|
int sno = Yap_CheckStream(ARG1, Input_Stream_f, "read_line_to_codes/2");
|
2015-11-13 13:19:42 +00:00
|
|
|
Int status;
|
2016-02-28 19:32:55 +00:00
|
|
|
UInt max_inp, buf_sz;
|
2016-03-03 23:18:04 +00:00
|
|
|
unsigned char *buf;
|
|
|
|
size_t sz;
|
|
|
|
StreamDesc *st = GLOBAL_Stream + sno;
|
2016-03-29 01:56:38 +01:00
|
|
|
int ch;
|
2015-11-13 13:19:42 +00:00
|
|
|
|
|
|
|
if (sno < 0)
|
2016-03-03 23:18:04 +00:00
|
|
|
return false;
|
2015-11-13 13:19:42 +00:00
|
|
|
status = GLOBAL_Stream[sno].status;
|
|
|
|
if (status & Eof_Stream_f) {
|
|
|
|
UNLOCK(GLOBAL_Stream[sno].streamlock);
|
2016-03-03 23:18:04 +00:00
|
|
|
return Yap_unify_constant(ARG2, MkAtomTerm(AtomEof));
|
2015-11-13 13:19:42 +00:00
|
|
|
}
|
2016-03-03 23:18:04 +00:00
|
|
|
max_inp = (ASP - HR) / 2 - 1024;
|
|
|
|
buf = (unsigned char *)TR;
|
|
|
|
buf_sz = (unsigned char *)LOCAL_TrailTop - buf;
|
2016-03-29 01:56:38 +01:00
|
|
|
|
2016-03-03 23:18:04 +00:00
|
|
|
if (buf_sz > max_inp) {
|
2015-11-13 13:19:42 +00:00
|
|
|
buf_sz = max_inp;
|
|
|
|
}
|
2016-03-03 23:18:04 +00:00
|
|
|
if (st->status & Binary_Stream_f) {
|
|
|
|
char *b = (char *)TR;
|
|
|
|
sz = fread(b, 1, buf_sz, GLOBAL_Stream[sno].file);
|
2016-02-21 19:06:25 +00:00
|
|
|
} else {
|
2016-03-03 23:18:04 +00:00
|
|
|
unsigned char *pt = buf;
|
|
|
|
do {
|
2016-03-29 01:56:38 +01:00
|
|
|
ch = st->stream_wgetc_for_read(sno);
|
2016-03-05 23:43:11 +00:00
|
|
|
if (ch < 127) {
|
2016-03-29 01:56:38 +01:00
|
|
|
if (ch < 0) {
|
2016-07-31 15:54:07 +01:00
|
|
|
break;
|
2016-03-29 01:56:38 +01:00
|
|
|
}
|
2016-07-31 15:54:07 +01:00
|
|
|
*pt++ = ch;
|
2016-03-05 23:43:11 +00:00
|
|
|
} else {
|
2016-03-29 01:56:38 +01:00
|
|
|
pt += get_utf8(pt, 4, &ch);
|
|
|
|
if (pt + 4 == buf + buf_sz)
|
|
|
|
break;
|
2016-03-05 23:43:11 +00:00
|
|
|
}
|
2016-03-03 23:18:04 +00:00
|
|
|
} while (ch != '\n');
|
|
|
|
sz = pt - buf;
|
|
|
|
}
|
|
|
|
if (sz == -1 || sz == 0) {
|
|
|
|
if (GLOBAL_Stream[sno].status & Eof_Stream_f) {
|
2015-11-13 13:19:42 +00:00
|
|
|
UNLOCK(GLOBAL_Stream[sno].streamlock);
|
2016-03-03 23:18:04 +00:00
|
|
|
return Yap_unify_constant(ARG2, MkAtomTerm(AtomEof));
|
2015-11-13 13:19:42 +00:00
|
|
|
}
|
2016-03-03 23:18:04 +00:00
|
|
|
UNLOCK(GLOBAL_Stream[sno].streamlock);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (GLOBAL_Stream[sno].status & Eof_Stream_f || buf[sz - 1] == 10) {
|
|
|
|
/* we're done */
|
2015-11-13 13:19:42 +00:00
|
|
|
|
2016-03-03 23:18:04 +00:00
|
|
|
if (!(GLOBAL_Stream[sno].status & Eof_Stream_f)) {
|
|
|
|
UNLOCK(GLOBAL_Stream[sno].streamlock);
|
|
|
|
/* handle CR before NL */
|
|
|
|
if ((Int)sz - 2 >= 0 && buf[sz - 2] == 13)
|
|
|
|
buf[sz - 2] = '\0';
|
|
|
|
else {
|
|
|
|
buf[sz - 1] = '\0';
|
2015-11-13 13:19:42 +00:00
|
|
|
}
|
2016-03-03 23:18:04 +00:00
|
|
|
} else {
|
2015-11-13 13:19:42 +00:00
|
|
|
UNLOCK(GLOBAL_Stream[sno].streamlock);
|
|
|
|
}
|
|
|
|
}
|
2016-03-03 23:18:04 +00:00
|
|
|
if (GLOBAL_Stream[sno].encoding == ENC_ISO_UTF8) {
|
|
|
|
return Yap_unify(ARG2, Yap_UTF8ToString((const char *)TR PASS_REGS));
|
|
|
|
} else if (GLOBAL_Stream[sno].encoding == ENC_WCHAR) {
|
|
|
|
return Yap_unify(ARG2, Yap_WCharsToString((const wchar_t *)TR PASS_REGS));
|
|
|
|
} else {
|
|
|
|
return Yap_unify(
|
|
|
|
ARG2, Yap_CharsToString((const char *)TR, ENC_ISO_LATIN1 PASS_REGS));
|
|
|
|
}
|
|
|
|
buf += (buf_sz - 1);
|
|
|
|
max_inp -= (buf_sz - 1);
|
|
|
|
if (max_inp <= 0) {
|
|
|
|
UNLOCK(GLOBAL_Stream[sno].streamlock);
|
|
|
|
Yap_Error(RESOURCE_ERROR_STACK, ARG1, NULL);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2015-11-13 13:19:42 +00:00
|
|
|
}
|
|
|
|
|
2016-03-03 23:18:04 +00:00
|
|
|
|
|
|
|
static Int read_stream_to_codes(USES_REGS1) {
|
|
|
|
int sno = Yap_CheckStream(ARG1, Input_Stream_f,
|
|
|
|
"reaMkAtomTerm (AtomEofd_line_to_codes/2");
|
2015-06-18 01:21:20 +01:00
|
|
|
CELL *HBASE = HR;
|
|
|
|
CELL *h0 = &ARG4;
|
|
|
|
|
|
|
|
if (sno < 0)
|
|
|
|
return FALSE;
|
|
|
|
while (!(GLOBAL_Stream[sno].status & Eof_Stream_f)) {
|
|
|
|
/* skip errors */
|
|
|
|
Int ch = GLOBAL_Stream[sno].stream_getc(sno);
|
|
|
|
Term t;
|
|
|
|
if (ch == EOFCHAR)
|
|
|
|
break;
|
|
|
|
t = MkIntegerTerm(ch);
|
|
|
|
h0[0] = AbsPair(HR);
|
|
|
|
*HR = t;
|
2016-03-03 23:18:04 +00:00
|
|
|
HR += 2;
|
|
|
|
h0 = HR - 1;
|
2016-01-06 12:36:59 +00:00
|
|
|
yhandle_t news, news1, st = Yap_StartSlots();
|
2016-03-03 23:18:04 +00:00
|
|
|
if (HR >= ASP - 1024) {
|
2015-06-18 01:21:20 +01:00
|
|
|
RESET_VARIABLE(h0);
|
2016-01-06 12:36:59 +00:00
|
|
|
news = Yap_InitSlot(AbsPair(HBASE));
|
2016-03-03 23:18:04 +00:00
|
|
|
news1 = Yap_InitSlot((CELL)(h0));
|
|
|
|
if (!Yap_gcl((ASP - HBASE) * sizeof(CELL), 3, ENV, Yap_gcP())) {
|
2016-01-06 12:36:59 +00:00
|
|
|
Yap_Error(RESOURCE_ERROR_STACK, ARG1, "read_stream_to_codes/3");
|
|
|
|
return false;
|
2015-06-18 01:21:20 +01:00
|
|
|
}
|
|
|
|
/* build a legal term again */
|
2016-03-03 23:18:04 +00:00
|
|
|
h0 = (CELL *)(Yap_GetFromSlot(news1));
|
2016-01-06 12:36:59 +00:00
|
|
|
HBASE = RepPair(Yap_GetFromSlot(news));
|
2015-06-18 01:21:20 +01:00
|
|
|
}
|
2016-01-06 12:36:59 +00:00
|
|
|
Yap_CloseSlots(st);
|
2015-06-18 01:21:20 +01:00
|
|
|
}
|
|
|
|
UNLOCK(GLOBAL_Stream[sno].streamlock);
|
|
|
|
if (HR == HBASE)
|
2016-03-03 23:18:04 +00:00
|
|
|
return Yap_unify(ARG2, ARG3);
|
|
|
|
RESET_VARIABLE(HR - 1);
|
|
|
|
Yap_unify(HR[-1], ARG3);
|
|
|
|
return Yap_unify(AbsPair(HBASE), ARG2);
|
2015-06-18 01:21:20 +01:00
|
|
|
}
|
|
|
|
|
2016-03-03 23:18:04 +00:00
|
|
|
static Int read_stream_to_terms(USES_REGS1) {
|
|
|
|
int sno = Yap_CheckStream(ARG1, Input_Stream_f, "read_line_to_codes/2");
|
2015-06-18 01:21:20 +01:00
|
|
|
Term t, hd;
|
2015-10-20 08:01:20 +01:00
|
|
|
yhandle_t tails, news;
|
2015-06-18 01:21:20 +01:00
|
|
|
|
|
|
|
if (sno < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
t = AbsPair(HR);
|
|
|
|
RESET_VARIABLE(HR);
|
2016-03-03 23:18:04 +00:00
|
|
|
Yap_InitSlot((CELL)(HR));
|
|
|
|
tails = Yap_InitSlot((CELL)(HR));
|
|
|
|
news = Yap_InitSlot((CELL)(HR));
|
2015-06-18 01:21:20 +01:00
|
|
|
HR++;
|
2016-03-03 23:18:04 +00:00
|
|
|
|
2015-06-18 01:21:20 +01:00
|
|
|
while (!(GLOBAL_Stream[sno].status & Eof_Stream_f)) {
|
|
|
|
RESET_VARIABLE(HR);
|
2016-03-03 23:18:04 +00:00
|
|
|
RESET_VARIABLE(HR + 1);
|
2015-06-18 01:21:20 +01:00
|
|
|
hd = (CELL)HR;
|
2016-03-03 23:18:04 +00:00
|
|
|
Yap_PutInSlot(news, (CELL)(HR + 1));
|
2015-06-18 01:21:20 +01:00
|
|
|
HR += 2;
|
2016-03-03 23:18:04 +00:00
|
|
|
while ((hd = Yap_read_term(sno, TermNil, 2)) == 0L)
|
2015-06-18 01:21:20 +01:00
|
|
|
;
|
|
|
|
// just ignore failure
|
|
|
|
CELL *pt = VarOfTerm(Yap_GetFromSlot(tails));
|
|
|
|
if (Deref(hd) == TermEOfCode) {
|
|
|
|
*pt = Deref(ARG3);
|
|
|
|
break;
|
|
|
|
} else {
|
2016-03-03 23:18:04 +00:00
|
|
|
CELL *newpt = (CELL *)Yap_GetFromSlot(news);
|
|
|
|
*pt = AbsPair(newpt - 1);
|
|
|
|
Yap_PutInSlot(tails, (CELL)newpt);
|
2015-06-18 01:21:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
UNLOCK(GLOBAL_Stream[sno].streamlock);
|
2016-03-03 23:18:04 +00:00
|
|
|
return Yap_unify(t, ARG2);
|
2015-06-18 01:21:20 +01:00
|
|
|
}
|
|
|
|
|
2016-03-03 23:18:04 +00:00
|
|
|
void Yap_InitReadUtil(void) {
|
|
|
|
CACHE_REGS
|
2015-06-18 01:21:20 +01:00
|
|
|
|
|
|
|
Term cm = CurrentModule;
|
|
|
|
CurrentModule = READUTIL_MODULE;
|
2015-11-13 13:19:42 +00:00
|
|
|
Yap_InitCPred("read_line_to_string", 2, read_line_to_string, SyncPredFlag);
|
2015-09-21 23:05:36 +01:00
|
|
|
Yap_InitCPred("read_line_to_codes", 2, read_line_to_codes, SyncPredFlag);
|
|
|
|
Yap_InitCPred("read_line_to_codes", 3, read_line_to_codes2, SyncPredFlag);
|
|
|
|
Yap_InitCPred("read_stream_to_codes", 3, read_stream_to_codes, SyncPredFlag);
|
|
|
|
Yap_InitCPred("read_stream_to_terms", 3, read_stream_to_terms, SyncPredFlag);
|
2015-06-18 01:21:20 +01:00
|
|
|
CurrentModule = cm;
|
|
|
|
}
|