static Int open_pipe_stream (USES_REGS1) { Term t1, t2; StreamDesc *st; int sno; int filedes[2]; if ( #if _MSC_VER || defined(__MINGW32__) // assume for now only text streams... _pipe(filedes, 1024, O_TEXT) #else pipe(filedes) #endif != 0) { return (PlIOError (SYSTEM_ERROR_INTERNAL,TermNil, "error %s", strerror(errno)) ); } sno = GetFreeStreamD(); if (sno < 0) return (PlIOError (RESOURCE_ERROR_MAX_STREAMS,TermNil, "new stream not available for open_pipe_stream/2")); t1 = Yap_MkStream (sno); st = &GLOBAL_Stream[sno]; st->status = Input_Stream_f | Pipe_Stream_f; st->linepos = 0; st->charcount = 0; st->linecount = 1; st->stream_putc = PipePutc; st->stream_getc = PipeGetc; Yap_DefaultStreamOps( st ); st->u.pipe.fd = filedes[0]; st->file = fdopen( filedes[0], "r"); UNLOCK(st->streamlock); sno = GetFreeStreamD(); if (sno < 0) return (PlIOError (RESOURCE_ERROR_MAX_STREAMS,TermNil, "new stream not available for open_pipe_stream/2")); st = &GLOBAL_Stream[sno]; st->status = Output_Stream_f | Pipe_Stream_f; st->linepos = 0; st->charcount = 0; st->linecount = 1; st->stream_putc = PipePutc; st->stream_getc = PipeGetc; Yap_DefaultStreamOps( st ); st->u.pipe.fd = filedes[1]; st->file = fdopen( filedes[1], "w"); UNLOCK(st->streamlock); t2 = Yap_MkStream (sno); return Yap_unify (ARG1, t1) && Yap_unify (ARG2, t2); }
int Yap_open_buf_write_stream(char *buf, size_t nchars, encoding_t *encp, memBufSource sr) { CACHE_REGS int sno; StreamDesc *st; sno = GetFreeStreamD(); if (sno < 0) return -1; st = GLOBAL_Stream+sno; st->status = Output_Stream_f | InMemory_Stream_f; if (!buf) { if (!nchars) { nchars = Yap_page_size; } buf = malloc( nchars ); st->status |= FreeOnClose_Stream_f; } st->nbuf = buf; if(!st->nbuf) { return -1; } st->nsize = nchars; st->linepos = 0; st->charcount = 0; st->linecount = 1; if (encp) st->encoding = *encp; else st->encoding = LOCAL_encoding; Yap_DefaultStreamOps( st ); #if MAY_WRITE st->file = open_memstream(&st->nbuf, &st->nsize); st->status |= Seekable_Stream_f; #else st->u.mem_string.pos = 0; st->u.mem_string.buf = st->nbuf; st->u.mem_string.max_size = nchars; #endif Yap_MemOps( st ); UNLOCK(st->streamlock); return sno; }