Example #1
0
Term Yap_StringToNumberTerm(char *s, encoding_t *encp) {
  CACHE_REGS
  int sno;
  Term t;

  sno = Yap_open_buf_read_stream(s, strlen(s), encp, MEM_BUF_USER);
  if (sno < 0)
    return FALSE;
  if (encp)
    GLOBAL_Stream[sno].encoding = *encp;
  else
    GLOBAL_Stream[sno].encoding = LOCAL_encoding;
#ifdef __ANDROID__
  while (*s && isblank(*s) && Yap_wide_chtype(*s) == BS)
    s++;
#else
  while (*s && iswblank(*s++))
    ;
#endif
  t = Yap_scan_num(GLOBAL_Stream + sno);
  if (LOCAL_Error_TYPE == SYNTAX_ERROR)
    LOCAL_Error_TYPE = YAP_NO_ERROR;
  Yap_CloseStream(sno);
  UNLOCK(GLOBAL_Stream[sno].streamlock);
  return t;
}
Example #2
0
/**
 * Specify an alias to the stream. The alias <tt>Name</tt> must be an atom. The
 * alias can be used instead of the stream descriptor for every operation
 * concerning the stream.
 *
 * @param + _tname_ Name of Alias
 * @param + _tstream_ stream identifier
 *
 * @return
 */
static Int add_alias_to_stream (USES_REGS1)
{
  Term tname = Deref(ARG1);
  Term tstream = Deref(ARG2);
  Atom at;
  Int sno;

  if (IsVarTerm(tname)) {
    Yap_Error(INSTANTIATION_ERROR, tname, "$add_alias_to_stream");
    return (FALSE);
  } else if (!IsAtomTerm (tname)) {
    Yap_Error(TYPE_ERROR_ATOM, tname, "$add_alias_to_stream");
    return (FALSE);
  }
  if (IsVarTerm(tstream)) {
    Yap_Error(INSTANTIATION_ERROR, tstream, "$add_alias_to_stream");
    return (FALSE);
  } else if (!IsApplTerm (tstream) || FunctorOfTerm (tstream) != FunctorStream ||
	     !IsIntTerm(ArgOfTerm(1,tstream))) {
    Yap_Error(DOMAIN_ERROR_STREAM_OR_ALIAS, tstream, "$add_alias_to_stream");
    return (FALSE);
  }
  at = AtomOfTerm(tname);
  sno = (int)IntOfTerm(ArgOfTerm(1,tstream));
  if (Yap_AddAlias(at, sno))
    return(TRUE);
  /* we could not create the alias, time to close the stream */
  Yap_CloseStream(sno);
  Yap_Error(PERMISSION_ERROR_NEW_ALIAS_FOR_STREAM, tname, "open/3");
  return (FALSE);
}
Example #3
0
static Term readFromBuffer(const char *s, Term opts) {
  Term rval;
  int sno;
  encoding_t enc = ENC_ISO_UTF8;
  sno = Yap_open_buf_read_stream(
      (char *)s, strlen_utf8((unsigned char *)s), &enc, MEM_BUF_USER,
      Yap_LookupAtom(Yap_StrPrefix((char *)s, 16)), TermNone);

  rval = Yap_read_term(sno, opts, 3);
  Yap_CloseStream(sno);
  return rval;
}
Example #4
0
int Yap_FormatFloat(Float f, char **s, size_t sz) {
  CACHE_REGS
  struct write_globs wglb;
  int sno;
  char *so;

  sno = Yap_open_buf_write_stream(
      *s, sz, &GLOBAL_Stream[LOCAL_c_output_stream].encoding, 0);
  if (sno < 0)
    return FALSE;
  wglb.stream = GLOBAL_Stream + sno;
  wrputf(f, &wglb);
  so = Yap_MemExportStreamPtr(sno);
  Yap_CloseStream(sno);
  *s = so;
  return TRUE;
}
Example #5
0
int Yap_FormatFloat(Float f, char **s, size_t sz) {
  CACHE_REGS
    struct write_globs wglb;
  int sno;
  char *so;

  sno = Yap_open_buf_write_stream(GLOBAL_Stream[LOCAL_c_output_stream].encoding,
                                  0);
  if (sno < 0)
    return false;
  wglb.lw = separator;
  wglb.stream = GLOBAL_Stream + sno;
  wrputf(f, &wglb);
  wrputc('\0', wglb.stream);
  so = Yap_MemExportStreamPtr(sno);
  *s = Malloc( strlen(so) )+1;
  strcpy(*s, so );
  Yap_CloseStream(sno);
  return true;
}
Example #6
0
Int
Yap_CloseSocket(int fd, socket_info status, socket_domain domain)
{
#if _MSC_VER || defined(__MINGW32__)
  /* prevent further writing
     to the socket */
  if (status == server_session_socket ||
      status == client_socket) {
    char bfr;

    if (shutdown(fd, 1) != 0) {
      Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
	    "socket_close/1 (close)");
      return(FALSE);
    }
    /* read all pending characters
       from the socket */
    while( recv( fd, &bfr, 1, 0 ) > 0 );
    /* prevent further reading
       from the socket */
    if (shutdown(fd, 0) < 0)  {
      Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
	    "socket_close/1 (close)");
      return(FALSE);
    }

    /* close the socket */
    if (closesocket(fd) != 0) {
#if HAVE_STRERROR
      Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
	    "socket_close/1 (close: %s)", strerror(socket_errno));
#else
      Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
	    "socket_close/1 (close)");
#endif
    }
#else
    if (status == server_session_socket ||
      status == client_socket) {
    if (shutdown(fd,2) < 0) {
#if HAVE_STRERROR
      Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
	    "socket_close/1 (shutdown: %s)", strerror(socket_errno));
#else
      Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
	    "socket_close/1 (shutdown)");
#endif
      return(FALSE);
    }
  }
  if (close(fd) != 0) {
#if HAVE_STRERROR
    Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
	    "socket_close/1 (close: %s)", strerror(socket_errno));
#else
    Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
	    "socket_close/1 (close)");
#endif
#endif
    return(FALSE);
  }
  return(TRUE);
}

static Int
p_socket_close(USES_REGS1)
{
  Term t1 = Deref(ARG1);
  int sno;

  if ((sno = Yap_CheckSocketStream(t1, "socket_close/1")) < 0) {
    return (FALSE);
  }
  Yap_CloseStream(sno);
  return(TRUE);
}