Example #1
0
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);
}
Example #2
0
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;
}
Example #3
0
 int
 Yap_open_buf_read_stream(const char *nbuf, size_t nchars, encoding_t *encp,  memBufSource src)
{
  CACHE_REGS
  int sno;
  StreamDesc *st;
  FILE *f;
  encoding_t encoding;
  stream_flags_t flags;

  sno = GetFreeStreamD();
  if (sno < 0)
    return (PlIOError (RESOURCE_ERROR_MAX_STREAMS,TermNil, "new stream not available for open_mem_read_stream/1"));
  st = GLOBAL_Stream+sno;
  if (encp)
    encoding = *encp;
  else
    encoding = LOCAL_encoding;
#if MAY_READ
  // like any file stream.
  f = fmemopen( (void *)nbuf, nchars, "r");
  flags = Input_Stream_f | InMemory_Stream_f | Seekable_Stream_f;
#else
  f = NULL;
  flags = Input_Stream_f | InMemory_Stream_f;
#endif
  Yap_initStream(sno, f, NULL, TermNil,
                        encoding,  flags, AtomRead);
  // like any file stream.
#if !MAY_READ
  /* currently these streams are not seekable */
  st->status = Input_Stream_f | InMemory_Stream_f;
  st->u.mem_string.pos = 0;
  st->u.mem_string.buf = (char *)nbuf;
  st->u.mem_string.max_size = nchars;
  st->u.mem_string.error_handler = NULL;
  st->u.mem_string.src = src;
#endif
  Yap_MemOps( st );
  UNLOCK(st->streamlock);
  return sno;
}
Example #4
0
int Yap_GetFreeStreamD(void) { return GetFreeStreamD(); }