18static char SccsId[] =
"%W% %G%";
25#include "YapEncoding.h"
36static Int rl_to_codes(Term TEnd,
int do_as_binary,
int end USES_REGS) {
37 int sno = Yap_CheckStream(ARG1, Input_Stream_f,
"read_line_to_codes/2");
47 status = GLOBAL_Stream[sno].status;
48 binary_stream = GLOBAL_Stream[sno].status & Binary_Stream_f;
49 if (status & Eof_Stream_f) {
50 UNLOCK(GLOBAL_Stream[sno].streamlock);
51 return Yap_unify_constant(ARG2, MkAtomTerm(AtomEof));
56 if (do_as_binary && !binary_stream) {
57 GLOBAL_Stream[sno].status |= Binary_Stream_f;
59 if (st->status & Binary_Stream_f) {
60 sz = fread(buf, 1, buf_sz, GLOBAL_Stream[sno].file);
62 unsigned char *pt = buf;
73 pt += get_utf8(pt, 4, &ch);
74 if (pt + 4 == buf + buf_sz)
80 if (do_as_binary && !binary_stream)
81 GLOBAL_Stream[sno].status &= ~Binary_Stream_f;
82 if (sz == -1 || sz == 0) {
83 if (GLOBAL_Stream[sno].status & Eof_Stream_f) {
84 UNLOCK(GLOBAL_Stream[sno].streamlock);
85 return Yap_unify_constant(ARG2, MkAtomTerm(AtomEof));
87 UNLOCK(GLOBAL_Stream[sno].streamlock);
89 if (GLOBAL_Stream[sno].status & Eof_Stream_f || buf[sz - 1] == 10) {
91 if (!(do_as_binary || GLOBAL_Stream[sno].status & Eof_Stream_f)) {
92 UNLOCK(GLOBAL_Stream[sno].streamlock);
94 if ((Int)sz - 2 >= 0 && buf[sz - 2] == 13)
99 UNLOCK(GLOBAL_Stream[sno].streamlock);
102 ARG2, Yap_UTF8ToDiffListOfCodes(buf, TEnd PASS_REGS));
113static Int read_line_to_codes(USES_REGS1) {
114 return rl_to_codes(TermNil, FALSE, TermNil PASS_REGS);
125static Int read_line_to_codes2(USES_REGS1) {
126 return rl_to_codes(TermNil, TRUE, TermNil PASS_REGS);
138static Int read_line_to_string(USES_REGS1) {
139 int sno = Yap_CheckStream(ARG1, Input_Stream_f,
"read_line_to_codes/2");
141 UInt max_inp, buf_sz;
149 status = GLOBAL_Stream[sno].status;
150 if (status & Eof_Stream_f) {
151 UNLOCK(GLOBAL_Stream[sno].streamlock);
152 return Yap_unify_constant(ARG2, MkAtomTerm(AtomEof));
154 max_inp = (ASP - HR) / 2 - 1024;
155 buf = (
unsigned char *)TR;
156 buf_sz = (
unsigned char *)LOCAL_TrailTop - buf;
158 if (buf_sz > max_inp) {
161 if (st->status & Binary_Stream_f) {
162 char *b = (
char *)TR;
163 sz = fread(b, 1, buf_sz, GLOBAL_Stream[sno].file);
165 unsigned char *pt = buf;
174 pt += get_utf8(pt, 4, &ch);
175 if (pt + 4 == buf + buf_sz)
178 }
while (ch !=
'\n');
181 if (sz == -1 || sz == 0) {
182 if (GLOBAL_Stream[sno].status & Eof_Stream_f) {
183 UNLOCK(GLOBAL_Stream[sno].streamlock);
184 return Yap_unify_constant(ARG2, MkAtomTerm(AtomEof));
186 UNLOCK(GLOBAL_Stream[sno].streamlock);
189 if (GLOBAL_Stream[sno].status & Eof_Stream_f || buf[sz - 1] == 10) {
192 if (!(GLOBAL_Stream[sno].status & Eof_Stream_f)) {
193 UNLOCK(GLOBAL_Stream[sno].streamlock);
195 if ((Int)sz - 2 >= 0 && buf[sz - 2] == 13)
201 UNLOCK(GLOBAL_Stream[sno].streamlock);
204 if (GLOBAL_Stream[sno].
encoding == ENC_ISO_UTF8) {
205 return Yap_unify(ARG2, Yap_UTF8ToString((
const char *)TR PASS_REGS));
206 }
else if (GLOBAL_Stream[sno].
encoding == ENC_WCHAR) {
207 return Yap_unify(ARG2, Yap_WCharsToString((
const wchar_t *)TR PASS_REGS));
210 ARG2, Yap_CharsToString((
const char *)TR, ENC_ISO_LATIN1 PASS_REGS));
213 max_inp -= (buf_sz - 1);
215 UNLOCK(GLOBAL_Stream[sno].streamlock);
216 Yap_Error(RESOURCE_ERROR_STACK, ARG1, NULL);
229static Int read_stream_to_codes(USES_REGS1) {
230 int sno = Yap_CheckStream(ARG1, Input_Stream_f,
231 "reaMkAtomTerm (AtomEofd_line_to_codes/2");
237 while (!(GLOBAL_Stream[sno].status & Eof_Stream_f)) {
239 Int ch = GLOBAL_Stream[sno].stream_getc(sno);
243 t = MkIntegerTerm(ch);
248 yhandle_t news, news1, st = Yap_StartSlots();
249 if (HR >= ASP - 1024) {
251 news = Yap_InitSlot(AbsPair(HBASE));
252 news1 = Yap_InitSlot((CELL)(h0));
253 if (!Yap_dogc(PASS_REGS1)) {
254 Yap_Error(RESOURCE_ERROR_STACK, ARG1,
"read_stream_to_codes/3");
258 h0 = (CELL *)(Yap_GetFromSlot(news1));
259 HBASE = RepPair(Yap_GetFromSlot(news));
263 UNLOCK(GLOBAL_Stream[sno].streamlock);
265 return Yap_unify(ARG2, ARG3);
266 RESET_VARIABLE(HR - 1);
267 Yap_unify(HR[-1], ARG3);
268 return Yap_unify(AbsPair(HBASE), ARG2);
280static Int read_stream_to_terms(USES_REGS1) {
281 int sno = Yap_CheckStream(ARG1, Input_Stream_f,
"read_line_to_codes/2");
283 yhandle_t hdl, hd3, od;
288 hd3 = Yap_InitSlot((ARG3));
290 Term opts = MkPairTerm(Yap_MkApplTerm(FunctorSyntaxErrors,1,&td),TermNil);
291 od = Yap_InitSlot(opts);
292 hdl = Yap_InitSlot(ARG2);
293 while (!(GLOBAL_Stream[sno].status & Eof_Stream_f)) {
296 Yap_DebugPlWriteln(r);
298 t = Deref(Yap_GetFromHandle(hdl));
299 if (Deref(r) == TermEOfCode) {
304 Yap_unify(t, Yap_MkNewPairTerm());
306 }
else if (!IsPairTerm(t))
308 Term h = HeadOfTerm(t);
311 Yap_DebugPlWriteln(t);
312 Yap_PutInHandle(hdl,TailOfTerm(t));
315 UNLOCK(GLOBAL_Stream[sno].streamlock);
316 return Yap_unify( Yap_GetFromHandle(hdl), Yap_GetFromHandle(hd3));
319void Yap_InitReadUtil(
void) {
322 Term cm = CurrentModule;
323 CurrentModule = READUTIL_MODULE;
324 Yap_InitCPred(
"read_line_to_string", 2, read_line_to_string, SyncPredFlag);
325 Yap_InitCPred(
"read_line_to_codes", 2, read_line_to_codes, SyncPredFlag);
326 Yap_InitCPred(
"read_line_to_codes", 3, read_line_to_codes2, SyncPredFlag);
327 Yap_InitCPred(
"read_stream_to_codes", 3, read_stream_to_codes, SyncPredFlag);
328 Yap_InitCPred(
"read_stream_to_terms", 3, read_stream_to_terms, SyncPredFlag);
void * Malloc(size_t sz USES_REGS)
allocate a temporary text block
@ encoding
support for coding systens, YAP relies on UTF-8 internally
Term Yap_read_term(int sno, Term opts, bool clause)
generic routine to read terms from a stream
int(* stream_wgetc_for_read)(int)
direct handle to stream in that space