18static char SccsId[] =
"%W% %G%";
33 char *Yap_StrPrefix(
const char *buf,
size_t n) {
35 strncpy(b, buf, n - 1);
36 if (strlen(buf) > n - 1)
44bool fill_pads(
int sno,
int sno0,
int total,
format_info *fg USES_REGS)
47 int nfillers, fill_space, lfill_space, nchars;
48 int (*f_putc)(int, int);
52 f_putc = GLOBAL_Stream[sno0].stream_putc;
53 if (fflush(GLOBAL_Stream[sno].file) == 0) {
54 buf = GLOBAL_Stream[sno].nbuf;
55 phys_end = ftell(GLOBAL_Stream[sno].file);
59 fg->gap[0].phys = phys_end;
60 fg->gap[0].filler =
' ';
64 nchars =total-( GLOBAL_Stream[sno].charcount - GLOBAL_Stream[sno].linestart) ;
68 fill_space = nchars / nfillers;
69 lfill_space = nchars % nfillers;
71 int i = fg->phys_start;
72 gap_t *padi = fg->gap;
73 while (i < phys_end) {
74 if (i == padi->phys) {
76 for (j = 0; j < fill_space; j++)
77 f_putc(sno0, padi->filler);
80 if (padi - fg->gap == fg->gapi) {
81 for (j = 0; j < fill_space; j++)
82 f_putc(sno0, (padi - 1)->filler);
85 f_putc(sno0, buf[i++]);
88 if (i == padi->phys) {
90 for (j = 0; j < fill_space + lfill_space; j++)
91 f_putc(sno0, padi->filler);
94 rewind(GLOBAL_Stream[sno].file);
96 fg->lstart = GLOBAL_Stream[sno].charcount-GLOBAL_Stream[sno].linestart ;
97GLOBAL_Stream[sno].linecount = 1;
98 GLOBAL_Stream[sno].charcount=0;
99 GLOBAL_Stream[sno].linestart = 0;
100 GLOBAL_Stream[sno].buf.on =
false;
106bool Yap_set_stream_to_buf(
StreamDesc *st,
const char *buf,
107 size_t nchars USES_REGS) {
111 st->file = f = fmemopen((
char *)buf, nchars,
"r");
112 st->status = Input_Stream_f | Seekable_Stream_f | InMemory_Stream_f;
116 Yap_DefaultStreamOps(st);
118 st->linestart = st->charcount = 0;
123int Yap_open_buf_read_stream(
void *spt,
const char *buf,
size_t nchars,
124 encoding_t *encp, memBufSource src,
Atom fname,
130 stream_flags_t flags;
136 sno = GetFreeStreamD();
138 return (PlIOError(RESOURCE_ERROR_MAX_STREAMS, TermNil,
139 "new stream not available for open_mem_read_stream/1"));
140 st = GLOBAL_Stream + sno;
142 sno = st-GLOBAL_Stream;
154 strncpy(nbuf+2,buf,30);
156 nbuf[28]=nbuf[29]=nbuf[30]=
',';
157 Atom name = Yap_LookupAtom(nbuf);
163 uname = MkAtomTerm(fname);
165 f = st->file = fmemopen((
void*)buf, nchars,
"r");
167 flags = Input_Stream_f | InMemory_Stream_f | Seekable_Stream_f;
168 Yap_initStream(sno, f, fname,
"r", uname,
encoding, flags, NULL);
170 Yap_DefaultStreamOps(st);
171 UNLOCK(st->streamlock);
176open_mem_read_stream(USES_REGS1)
183 int l = push_text_stack();
184 buf = Yap_TextTermToText(ti);
189 buf = pop_output_text_stack(l, buf);
190 sno = Yap_open_buf_read_stream(NULL,buf, strlen(buf) + 1, &LOCAL_encoding,
191 MEM_BUF_MALLOC, Yap_LookupAtom(Yap_StrPrefix((
char *)buf,16)), TermNone);
192 t = Yap_MkStream(sno);
193 return Yap_unify(ARG2, t);
198int Yap_open_buf_write_stream(encoding_t enc, memBufSource src) {
203 sno = GetFreeStreamD();
207 st = GLOBAL_Stream + sno;
208 st->status = Output_Stream_f | InMemory_Stream_f;
216 st->status |= (Seekable_Stream_f|CloseOnException_Stream_f);
217 #if HAVE_OPEN_MEMSTREAM
218 st->file = open_memstream(&st->nbuf, &st->nsize);
220 if (st->nbuf == NULL)
221 st->nbuf = malloc(32*K);
222 st->file = fmemopen((
void *)st->nbuf, st->nsize,
"w+");
224 Yap_DefaultStreamOps(st);
225 UNLOCK(st->streamlock);
229int Yap_OpenBufWriteStream(USES_REGS1) {
231 return Yap_open_buf_write_stream(
232 GLOBAL_Stream[LOCAL_c_output_stream].
encoding, 0);
236open_mem_write_stream(USES_REGS1)
241 sno = Yap_OpenBufWriteStream(PASS_REGS1);
243 return (PlIOError(SYSTEM_ERROR_INTERNAL, TermNil,
244 "new stream not available for open_mem_read_stream/1"));
245 t = Yap_MkStream(sno);
246 GLOBAL_Stream[sno].status |= InMemory_Stream_f;
247 return (Yap_unify(ARG1, t));
258char *Yap_MemExportStreamPtr(
int sno) {
259FILE *f = GLOBAL_Stream[sno].file;
264 size_t sz = GLOBAL_Stream[sno].nsize+2;
265 GLOBAL_Stream[sno].nsize=
'\0';
267#if HAVE_OPEN_MEMSTREAM
269 strcpy(buf, GLOBAL_Stream[sno].nbuf );
274 len = fseek(GLOBAL_Stream[sno].file, 0, SEEK_END);
276 fread(buf, len, 1, GLOBAL_Stream[sno].file);
283static Int peek_mem_write_stream(
286 Yap_CheckStream(ARG1, (Output_Stream_f | InMemory_Stream_f),
"close/2");
294 char *p = ptr = Yap_MemExportStreamPtr(sno);
297 while ((ch = *p++)) {
298 HR[0] = MkIntTerm(ch);
299 HR[1] = AbsPair(HR+2);
301 if (HR + 1024 >= ASP) {
302 UNLOCK(GLOBAL_Stream[sno].streamlock);
305 UNLOCK(GLOBAL_Stream[sno].streamlock);
306 Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage);
309 LOCK(GLOBAL_Stream[sno].streamlock);
314 UNLOCK(GLOBAL_Stream[sno].streamlock);
316 fclose(GLOBAL_Stream[sno].file);
317 free(GLOBAL_Stream[sno].nbuf);
318 #if HAVE_OPEN_MEMSTREAM
319 GLOBAL_Stream[sno].file = open_memstream(&GLOBAL_Stream[sno].nbuf, &GLOBAL_Stream[sno].nsize);
321 if (GLOBAL_Stream[sno].nbuf == NULL)
322 GLOBAL_Stream[sno].nbuf = malloc(32*K);
323 GLOBAL_Stream[sno].file = fmemopen((
void *)GLOBAL_Stream[sno].nbuf, GLOBAL_Stream[sno]. nsize,
"w+");
326 return (Yap_unify(ARG3, AbsPair(HI)));
330 st->stream_putc = FilePutc;
335int format_synch(
int sno,
int sno0,
format_info *fg) {
339 s = Yap_MemExportStreamPtr(sno);
340 if (GLOBAL_Stream[sno0].
vfs) {
343 while ((ch = *s++)) {
348 int (*f)() = GLOBAL_Stream[sno0].stream_putc;
349 while ((ch = *s++)) {
353 fclose(GLOBAL_Stream[sno].file);
354 free(GLOBAL_Stream[sno].nbuf);
355 #if HAVE_OPEN_MEMSTREAM
356 GLOBAL_Stream[sno].file = open_memstream(&GLOBAL_Stream[sno].nbuf, &GLOBAL_Stream[sno].nsize);
358 if (GLOBAL_Stream[sno].nbuf == NULL)
359 GLOBAL_Stream[sno].nbuf = malloc(32*K);
360 GLOBAL_Stream[sno].file = fmemopen((
void *)GLOBAL_Stream[sno].nbuf, GLOBAL_Stream[sno]. nsize,
"w+");
371bool Yap_CloseMemoryStream(
int sno) {
373 if ((GLOBAL_Stream[sno].status & Output_Stream_f) &&
374 GLOBAL_Stream[sno].file) {
375 fflush(GLOBAL_Stream[sno].file);
376 fclose(GLOBAL_Stream[sno].file);
377 if (GLOBAL_Stream[sno].status & FreeOnClose_Stream_f)
378 free(GLOBAL_Stream[sno].nbuf);
380 if (GLOBAL_Stream[sno].file)
381 fclose(GLOBAL_Stream[sno].file);
382 if (GLOBAL_Stream[sno].status & FreeOnClose_Stream_f)
383 free(GLOBAL_Stream[sno].nbuf);
385 Yap_ReleaseStream(sno);
389void Yap_InitMems(
void) {
391 Yap_InitCPred(
"open_mem_read_stream", 2, open_mem_read_stream, SyncPredFlag);
392 Yap_InitCPred(
"open_mem_write_stream", 1, open_mem_write_stream,
394 Yap_InitCPred(
"peek_mem_write_stream", 3, peek_mem_write_stream,
void * Malloc(size_t sz USES_REGS)
allocate a temporary text block
@ encoding
support for coding systens, YAP relies on UTF-8 internally
int(* stream_getc)(int)
function the stream uses for writing a character
encoding_t encoding
check if the next wide character is available
struct vfs * vfs
function the stream uses for reading a character
int(* put_char)(int sno, int ch)
unget an octet from the stream