|
|
|
@ -232,7 +232,6 @@ output is directed to the stream used by format/2.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
|
#include "Yatom.h"
|
|
|
|
|
#include "YapHeap.h"
|
|
|
|
@ -261,7 +260,6 @@ output is directed to the stream used by format/2.
|
|
|
|
|
#include "iopreds.h"
|
|
|
|
|
#include "eval.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define FORMAT_MAX_SIZE 1024
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
@ -277,9 +275,7 @@ typedef struct format_status {
|
|
|
|
|
int padders;
|
|
|
|
|
} format_info;
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
fill(int sno, int n, wchar_t nch)
|
|
|
|
|
{
|
|
|
|
|
static int fill(int sno, int n, wchar_t nch) {
|
|
|
|
|
int (*f_putc)(int, wchar_t);
|
|
|
|
|
f_putc = GLOBAL_Stream[sno].stream_wputc;
|
|
|
|
|
while (n--)
|
|
|
|
@ -287,9 +283,7 @@ fill(int sno, int n, wchar_t nch)
|
|
|
|
|
return nch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
f_puts(int sno, char *s, int n)
|
|
|
|
|
{
|
|
|
|
|
static int f_puts(int sno, char *s, int n) {
|
|
|
|
|
int (*f_putc)(int, wchar_t);
|
|
|
|
|
f_putc = GLOBAL_Stream[sno].stream_wputc;
|
|
|
|
|
while (n--)
|
|
|
|
@ -297,13 +291,12 @@ f_puts(int sno, char *s, int n)
|
|
|
|
|
return *s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// uses directly the buffer in the memory stream.
|
|
|
|
|
static bool fill_pads(int sno, int nchars, format_info *fg USES_REGS)
|
|
|
|
|
{
|
|
|
|
|
static bool fill_pads(int sno, int nchars, format_info *fg USES_REGS) {
|
|
|
|
|
int nfillers, fill_space, lfill_space;
|
|
|
|
|
|
|
|
|
|
if (nchars < 0) nchars = 0; /* ignore */
|
|
|
|
|
if (nchars < 0)
|
|
|
|
|
nchars = 0; /* ignore */
|
|
|
|
|
nfillers = fg->padders;
|
|
|
|
|
if (fg->padders == 0) {
|
|
|
|
|
return fill(sno, nchars, ' ');
|
|
|
|
@ -328,9 +321,8 @@ static bool fill_pads(int sno, int nchars, format_info *fg USES_REGS)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
format_print_str (Int sno, Int size, Int has_size, Term args, int (* f_putc)(int, wchar_t))
|
|
|
|
|
{
|
|
|
|
|
static int format_print_str(Int sno, Int size, Int has_size, Term args,
|
|
|
|
|
int (*f_putc)(int, wchar_t)) {
|
|
|
|
|
Term arghd;
|
|
|
|
|
if (IsStringTerm(args)) {
|
|
|
|
|
const unsigned char *pt = UStringOfTerm(args);
|
|
|
|
@ -346,8 +338,7 @@ format_print_str (Int sno, Int size, Int has_size, Term args, int (* f_putc)(int
|
|
|
|
|
return FALSE;
|
|
|
|
|
} else if (args == TermNil) {
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
else if (!IsPairTerm (args)) {
|
|
|
|
|
} else if (!IsPairTerm(args)) {
|
|
|
|
|
Yap_Error(TYPE_ERROR_LIST, args, "format/2");
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
@ -367,15 +358,9 @@ format_print_str (Int sno, Int size, Int has_size, Term args, int (* f_putc)(int
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
|
fst_ok,
|
|
|
|
|
fst_error,
|
|
|
|
|
fst_too_long
|
|
|
|
|
} format_cp_res;
|
|
|
|
|
typedef enum { fst_ok, fst_error, fst_too_long } format_cp_res;
|
|
|
|
|
|
|
|
|
|
static format_cp_res
|
|
|
|
|
copy_format_string(Term inp, char *out, int max)
|
|
|
|
|
{
|
|
|
|
|
static format_cp_res copy_format_string(Term inp, char *out, int max) {
|
|
|
|
|
int i = 0;
|
|
|
|
|
while (inp != TermNil) {
|
|
|
|
|
Term hd;
|
|
|
|
@ -418,9 +403,7 @@ copy_format_string(Term inp, char *out, int max)
|
|
|
|
|
#define FORMAT_COPY_ARGS_ERROR -1
|
|
|
|
|
#define FORMAT_COPY_ARGS_OVERFLOW -2
|
|
|
|
|
|
|
|
|
|
static Int
|
|
|
|
|
format_copy_args(Term args, Term *targs, Int tsz)
|
|
|
|
|
{
|
|
|
|
|
static Int format_copy_args(Term args, Term *targs, Int tsz) {
|
|
|
|
|
Int n = 0;
|
|
|
|
|
while (args != TermNil) {
|
|
|
|
|
if (IsVarTerm(args)) {
|
|
|
|
@ -438,13 +421,11 @@ format_copy_args(Term args, Term *targs, Int tsz)
|
|
|
|
|
n++;
|
|
|
|
|
}
|
|
|
|
|
return n;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
|
|
|
|
|
format_clean_up( int sno, const char *fstr, const Term *targs)
|
|
|
|
|
{
|
|
|
|
|
format_clean_up(int sno, const char *fstr, const Term *targs) {
|
|
|
|
|
if (fstr) {
|
|
|
|
|
Yap_FreeAtomSpace((void *)fstr);
|
|
|
|
|
}
|
|
|
|
@ -452,9 +433,7 @@ format_clean_up( int sno, const char *fstr, const Term *targs)
|
|
|
|
|
Yap_FreeAtomSpace((void *)targs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Int
|
|
|
|
|
fetch_index_from_args(Term t)
|
|
|
|
|
{
|
|
|
|
|
static Int fetch_index_from_args(Term t) {
|
|
|
|
|
Int i;
|
|
|
|
|
|
|
|
|
|
if (IsVarTerm(t))
|
|
|
|
@ -467,10 +446,7 @@ fetch_index_from_args(Term t)
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static wchar_t
|
|
|
|
|
base_dig(Int dig, Int ch)
|
|
|
|
|
{
|
|
|
|
|
static wchar_t base_dig(Int dig, Int ch) {
|
|
|
|
|
if (dig < 10)
|
|
|
|
|
return dig + '0';
|
|
|
|
|
else if (ch == 'r')
|
|
|
|
@ -481,9 +457,8 @@ base_dig(Int dig, Int ch)
|
|
|
|
|
|
|
|
|
|
#define TMP_STRING_SIZE 1024
|
|
|
|
|
|
|
|
|
|
static Int
|
|
|
|
|
doformat(volatile Term otail, volatile Term oargs, int sno USES_REGS)
|
|
|
|
|
{
|
|
|
|
|
static Int doformat(volatile Term otail, volatile Term oargs,
|
|
|
|
|
int sno USES_REGS) {
|
|
|
|
|
char tmp1[TMP_STRING_SIZE], *tmpbase;
|
|
|
|
|
int ch;
|
|
|
|
|
Term mytargs[8], *targs;
|
|
|
|
@ -497,12 +472,10 @@ doformat(volatile Term otail, volatile Term oargs, int sno USES_REGS)
|
|
|
|
|
volatile void *old_handler;
|
|
|
|
|
volatile int old_pos;
|
|
|
|
|
format_info finfo;
|
|
|
|
|
unsigned char *bufp;
|
|
|
|
|
unsigned char *bufp = NULL;
|
|
|
|
|
Term fmod = CurrentModule;
|
|
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LOCAL_FormatInfo = &finfo;
|
|
|
|
|
finfo.padders = 0;
|
|
|
|
|
finfo.format_error = FALSE;
|
|
|
|
|
if (GLOBAL_Stream[sno].status & InMemory_Stream_f) {
|
|
|
|
@ -538,11 +511,11 @@ doformat(volatile Term otail, volatile Term oargs, int sno USES_REGS)
|
|
|
|
|
format_cp_res fr;
|
|
|
|
|
char *fstr0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fstr = fptr = fstr0 = Yap_AllocAtomSpace(sz * sizeof(char));
|
|
|
|
|
if ((fr = copy_format_string(tail, fstr0, sz)) == fst_ok)
|
|
|
|
|
break;
|
|
|
|
|
if (fr == fst_error) return FALSE;
|
|
|
|
|
if (fr == fst_error)
|
|
|
|
|
return FALSE;
|
|
|
|
|
sz += 256;
|
|
|
|
|
Yap_FreeCodeSpace(fstr0);
|
|
|
|
|
} while (TRUE);
|
|
|
|
@ -600,7 +573,7 @@ doformat(volatile Term otail, volatile Term oargs, int sno USES_REGS)
|
|
|
|
|
tnum = 0;
|
|
|
|
|
targs = mytargs;
|
|
|
|
|
}
|
|
|
|
|
finfo.format_error = FALSE;
|
|
|
|
|
finfo.format_error = false;
|
|
|
|
|
|
|
|
|
|
f_putc = GLOBAL_Stream[sno].stream_wputc;
|
|
|
|
|
while ((ch = *fptr++)) {
|
|
|
|
@ -647,10 +620,8 @@ doformat(volatile Term otail, volatile Term oargs, int sno USES_REGS)
|
|
|
|
|
// stream is already locked.
|
|
|
|
|
Yap_plwrite(t, GLOBAL_Stream + sno, 0, Handle_vars_f | To_heap_f, 1200);
|
|
|
|
|
Yap_CloseSlots(sl);
|
|
|
|
|
LOCAL_FormatInfo = &finfo;
|
|
|
|
|
break;
|
|
|
|
|
case 'c':
|
|
|
|
|
{
|
|
|
|
|
case 'c': {
|
|
|
|
|
Int nch, i;
|
|
|
|
|
|
|
|
|
|
if (targ > tnum - 1)
|
|
|
|
@ -673,8 +644,7 @@ doformat(volatile Term otail, volatile Term oargs, int sno USES_REGS)
|
|
|
|
|
case 'E':
|
|
|
|
|
case 'f':
|
|
|
|
|
case 'g':
|
|
|
|
|
case 'G':
|
|
|
|
|
{
|
|
|
|
|
case 'G': {
|
|
|
|
|
Float fl;
|
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
|
@ -704,7 +674,8 @@ doformat(volatile Term otail, volatile Term oargs, int sno USES_REGS)
|
|
|
|
|
#else
|
|
|
|
|
sprintf(ptr, "%d", repeats);
|
|
|
|
|
#endif
|
|
|
|
|
while (*ptr) ptr++;
|
|
|
|
|
while (*ptr)
|
|
|
|
|
ptr++;
|
|
|
|
|
ptr[0] = ch;
|
|
|
|
|
ptr[1] = '\0';
|
|
|
|
|
{
|
|
|
|
@ -752,14 +723,16 @@ doformat(volatile Term otail, volatile Term oargs, int sno USES_REGS)
|
|
|
|
|
sprintf(tmp1, "%ld", (long int)il);
|
|
|
|
|
#endif
|
|
|
|
|
siz = strlen(tmp1);
|
|
|
|
|
if (il < 0) siz--;
|
|
|
|
|
if (il < 0)
|
|
|
|
|
siz--;
|
|
|
|
|
#ifdef HAVE_GMP
|
|
|
|
|
} else if (IsBigIntTerm(t) && RepAppl(t)[1] == BIG_INT) {
|
|
|
|
|
char *res;
|
|
|
|
|
|
|
|
|
|
tmpbase = tmp1;
|
|
|
|
|
|
|
|
|
|
while (!(res = Yap_gmp_to_string(t, tmpbase, TMP_STRING_SIZE, 10))) {
|
|
|
|
|
while (
|
|
|
|
|
!(res = Yap_gmp_to_string(t, tmpbase, TMP_STRING_SIZE, 10))) {
|
|
|
|
|
if (tmpbase == tmp1) {
|
|
|
|
|
tmpbase = NULL;
|
|
|
|
|
} else {
|
|
|
|
@ -784,8 +757,7 @@ doformat(volatile Term otail, volatile Term oargs, int sno USES_REGS)
|
|
|
|
|
int first = TRUE;
|
|
|
|
|
|
|
|
|
|
while (siz > repeats) {
|
|
|
|
|
if ((siz-repeats) % 3 == 0 &&
|
|
|
|
|
!first) {
|
|
|
|
|
if ((siz - repeats) % 3 == 0 && !first) {
|
|
|
|
|
f_putc(sno, (int)',');
|
|
|
|
|
}
|
|
|
|
|
f_putc(sno, (int)(*ptr++));
|
|
|
|
@ -799,8 +771,7 @@ doformat(volatile Term otail, volatile Term oargs, int sno USES_REGS)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (repeats) {
|
|
|
|
|
if (ptr == tmpbase ||
|
|
|
|
|
ptr[-1] == '-') {
|
|
|
|
|
if (ptr == tmpbase || ptr[-1] == '-') {
|
|
|
|
|
f_putc(sno, (int)'0');
|
|
|
|
|
}
|
|
|
|
|
f_putc(sno, (int)'.');
|
|
|
|
@ -817,8 +788,7 @@ doformat(volatile Term otail, volatile Term oargs, int sno USES_REGS)
|
|
|
|
|
free(tmpbase);
|
|
|
|
|
break;
|
|
|
|
|
case 'r':
|
|
|
|
|
case 'R':
|
|
|
|
|
{
|
|
|
|
|
case 'R': {
|
|
|
|
|
Int numb, radix;
|
|
|
|
|
UInt divfactor = 1, size = 1, i;
|
|
|
|
|
wchar_t och;
|
|
|
|
@ -840,7 +810,8 @@ doformat(volatile Term otail, volatile Term oargs, int sno USES_REGS)
|
|
|
|
|
char *pt, *res;
|
|
|
|
|
|
|
|
|
|
tmpbase = tmp1;
|
|
|
|
|
while (!(res = Yap_gmp_to_string(t, tmpbase, TMP_STRING_SIZE, radix))) {
|
|
|
|
|
while (!(res = Yap_gmp_to_string(t, tmpbase, TMP_STRING_SIZE,
|
|
|
|
|
radix))) {
|
|
|
|
|
if (tmpbase == tmp1) {
|
|
|
|
|
tmpbase = NULL;
|
|
|
|
|
} else {
|
|
|
|
@ -899,9 +870,9 @@ doformat(volatile Term otail, volatile Term oargs, int sno USES_REGS)
|
|
|
|
|
goto do_consistency_error;
|
|
|
|
|
t = targs[targ++];
|
|
|
|
|
yhandle_t sl = Yap_StartSlots();
|
|
|
|
|
Yap_plwrite (t, GLOBAL_Stream+sno,0, Quote_illegal_f|Ignore_ops_f|To_heap_f , 1200);
|
|
|
|
|
Yap_plwrite(t, GLOBAL_Stream + sno, 0,
|
|
|
|
|
Quote_illegal_f | Ignore_ops_f | To_heap_f, 1200);
|
|
|
|
|
Yap_CloseSlots(sl);
|
|
|
|
|
LOCAL_FormatInfo = &finfo;
|
|
|
|
|
break;
|
|
|
|
|
case '@':
|
|
|
|
|
t = targs[targ++];
|
|
|
|
@ -920,10 +891,11 @@ doformat(volatile Term otail, volatile Term oargs, int sno USES_REGS)
|
|
|
|
|
sl2 = Yap_InitSlot(ta[1]);
|
|
|
|
|
ts = Yap_MkApplTerm(FunctorGFormatAt, 2, ta);
|
|
|
|
|
res = Yap_execute_goal(ts, 0, CurrentModule, true);
|
|
|
|
|
LOCAL_FormatInfo = &finfo;
|
|
|
|
|
args = Yap_GetFromSlot(sl);
|
|
|
|
|
if (EX) goto ex_handler;
|
|
|
|
|
if (!res) return FALSE;
|
|
|
|
|
if (EX)
|
|
|
|
|
goto ex_handler;
|
|
|
|
|
if (!res)
|
|
|
|
|
return FALSE;
|
|
|
|
|
ts = Yap_GetFromSlot(sl2);
|
|
|
|
|
Yap_CloseSlots(sl0);
|
|
|
|
|
if (!format_print_str(sno, repeats, has_repeats, ts, f_putc)) {
|
|
|
|
@ -937,8 +909,8 @@ doformat(volatile Term otail, volatile Term oargs, int sno USES_REGS)
|
|
|
|
|
t = targs[targ++];
|
|
|
|
|
{
|
|
|
|
|
Int sl = Yap_InitSlot(args);
|
|
|
|
|
Yap_plwrite(t, GLOBAL_Stream+sno, 0, Handle_vars_f|Use_portray_f|To_heap_f, 1200);
|
|
|
|
|
LOCAL_FormatInfo = &finfo;
|
|
|
|
|
Yap_plwrite(t, GLOBAL_Stream + sno, 0,
|
|
|
|
|
Handle_vars_f | Use_portray_f | To_heap_f, 1200);
|
|
|
|
|
args = Yap_GetFromSlot(sl);
|
|
|
|
|
Yap_CloseSlots(sl);
|
|
|
|
|
}
|
|
|
|
@ -966,18 +938,18 @@ doformat(volatile Term otail, volatile Term oargs, int sno USES_REGS)
|
|
|
|
|
goto do_consistency_error;
|
|
|
|
|
t = targs[targ++];
|
|
|
|
|
yhandle_t sl0 = Yap_StartSlots();
|
|
|
|
|
Yap_plwrite (t, GLOBAL_Stream+sno, 0, Handle_vars_f|Quote_illegal_f|To_heap_f, 1200);
|
|
|
|
|
Yap_plwrite(t, GLOBAL_Stream + sno, 0,
|
|
|
|
|
Handle_vars_f | Quote_illegal_f | To_heap_f, 1200);
|
|
|
|
|
Yap_CloseSlots(sl0);
|
|
|
|
|
LOCAL_FormatInfo = &finfo;
|
|
|
|
|
break;
|
|
|
|
|
case 'w':
|
|
|
|
|
if (targ > tnum - 1 || has_repeats)
|
|
|
|
|
goto do_consistency_error;
|
|
|
|
|
t = targs[targ++];
|
|
|
|
|
yhandle_t slf = Yap_StartSlots();
|
|
|
|
|
Yap_plwrite (t, GLOBAL_Stream+sno, 0, Handle_vars_f|To_heap_f, 1200);
|
|
|
|
|
Yap_plwrite(t, GLOBAL_Stream + sno, 0, Handle_vars_f | To_heap_f,
|
|
|
|
|
1200);
|
|
|
|
|
Yap_CloseSlots(slf);
|
|
|
|
|
LOCAL_FormatInfo = &finfo;
|
|
|
|
|
break;
|
|
|
|
|
case '~':
|
|
|
|
|
if (has_repeats)
|
|
|
|
@ -1026,15 +998,15 @@ doformat(volatile Term otail, volatile Term oargs, int sno USES_REGS)
|
|
|
|
|
repeats = (repeats < 0 ? 0 : repeats);
|
|
|
|
|
fill_pads(sno, repeats, &finfo PASS_REGS);
|
|
|
|
|
break;
|
|
|
|
|
case 't':
|
|
|
|
|
{
|
|
|
|
|
case 't': {
|
|
|
|
|
int nsno;
|
|
|
|
|
|
|
|
|
|
finfo.pad_entries[finfo.padders].len = sz;
|
|
|
|
|
finfo.pad_entries[finfo.padders].pad = (char *)bufp;
|
|
|
|
|
bufp = NULL;
|
|
|
|
|
sz = 0;
|
|
|
|
|
nsno = Yap_open_buf_write_stream((char *)bufp, sz, &GLOBAL_Stream[sno].encoding, 0);
|
|
|
|
|
nsno = Yap_open_buf_write_stream((char *)bufp, sz,
|
|
|
|
|
&GLOBAL_Stream[sno].encoding, 0);
|
|
|
|
|
if (osno) {
|
|
|
|
|
GLOBAL_Stream[nsno].linepos = GLOBAL_Stream[sno].linepos;
|
|
|
|
|
GLOBAL_Stream[nsno].linecount = GLOBAL_Stream[sno].linecount;
|
|
|
|
@ -1047,12 +1019,12 @@ doformat(volatile Term otail, volatile Term oargs, int sno USES_REGS)
|
|
|
|
|
GLOBAL_Stream[nsno].charcount = GLOBAL_Stream[sno].charcount;
|
|
|
|
|
}
|
|
|
|
|
sno = nsno;
|
|
|
|
|
finfo.padders++;
|
|
|
|
|
finfo.pad_entries[finfo.padders].start = GLOBAL_Stream[sno].linepos;
|
|
|
|
|
if (!has_repeats)
|
|
|
|
|
finfo.pad_entries[finfo.padders].filler = ' ';
|
|
|
|
|
else
|
|
|
|
|
finfo.pad_entries[finfo.padders].filler = fptr[-2];
|
|
|
|
|
finfo.padders++;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
do_instantiation_error:
|
|
|
|
@ -1086,7 +1058,9 @@ doformat(volatile Term otail, volatile Term oargs, int sno USES_REGS)
|
|
|
|
|
Term ta[2];
|
|
|
|
|
ta[0] = otail;
|
|
|
|
|
ta[1] = oargs;
|
|
|
|
|
Yap_Error(LOCAL_Error_TYPE, Yap_MkApplTerm(Yap_MkFunctor(AtomFormat,2),2,ta), "format/2");
|
|
|
|
|
Yap_Error(LOCAL_Error_TYPE,
|
|
|
|
|
Yap_MkApplTerm(Yap_MkFunctor(AtomFormat, 2), 2, ta),
|
|
|
|
|
"format/2");
|
|
|
|
|
}
|
|
|
|
|
if (GLOBAL_Stream[sno].status & InMemory_Stream_f) {
|
|
|
|
|
GLOBAL_Stream[sno].u.mem_string.error_handler = old_handler;
|
|
|
|
@ -1141,29 +1115,35 @@ X = [104, 101, 108, 108, 111]
|
|
|
|
|
~~~~~
|
|
|
|
|
|
|
|
|
|
+ A Stream handle or alias
|
|
|
|
|
Temporary switch current output to the given stream. Redirection using with_output_to/2 guarantees the original output is restored, also if Goal fails or raises an exception. See also call_cleanup/2.
|
|
|
|
|
Temporary switch current output to the given stream. Redirection using
|
|
|
|
|
with_output_to/2 guarantees the original output is restored, also if Goal fails
|
|
|
|
|
or raises an exception. See also call_cleanup/2.
|
|
|
|
|
+ atom(- _Atom_)
|
|
|
|
|
Create an atom from the emitted characters.
|
|
|
|
|
Applications should generally avoid creating atoms by breaking and
|
|
|
|
|
concatenating other atoms as the creation of large numbers of
|
|
|
|
|
intermediate atoms puts pressure on the atom table and the data-base. This may lead to collisions in the hash tables used to implement atoms, and may result in frequent calls to the garbage collector. In multi-threaded applications, access to the atom table is controlled by locks. This predicate supports creating the therms by expanding
|
|
|
|
|
intermediate atoms puts pressure on the atom table and the data-base. This may
|
|
|
|
|
lead to collisions in the hash tables used to implement atoms, and may result in
|
|
|
|
|
frequent calls to the garbage collector. In multi-threaded applications, access
|
|
|
|
|
to the atom table is controlled by locks. This predicate supports creating the
|
|
|
|
|
therms by expanding
|
|
|
|
|
difference-list.
|
|
|
|
|
|
|
|
|
|
+ string(- _String_)
|
|
|
|
|
Create a string-object, notice that strings are atomic objects.
|
|
|
|
|
+ codes(- _Codes_)
|
|
|
|
|
Create a list of character codes from the emitted characters, similar to atom_codes/2.
|
|
|
|
|
Create a list of character codes from the emitted characters, similar to
|
|
|
|
|
atom_codes/2.
|
|
|
|
|
+ codes(- _Codes_, - _Tail_)
|
|
|
|
|
Create a list of character codes as a difference-list.
|
|
|
|
|
+ chars(- _Chars_)
|
|
|
|
|
Create a list of one-character-atoms codes from the emitted characters, similar to atom_chars/2.
|
|
|
|
|
Create a list of one-character-atoms codes from the emitted characters, similar
|
|
|
|
|
to atom_chars/2.
|
|
|
|
|
+ chars(- _Chars_, - _Tail_)
|
|
|
|
|
Create a list of one-character-atoms as a difference-list.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
static Int
|
|
|
|
|
with_output_to( USES_REGS1 )
|
|
|
|
|
{
|
|
|
|
|
static Int with_output_to(USES_REGS1) {
|
|
|
|
|
int old_out = LOCAL_c_output_stream;
|
|
|
|
|
int output_stream;
|
|
|
|
|
Term tin = Deref(ARG1);
|
|
|
|
@ -1174,12 +1154,9 @@ with_output_to( USES_REGS1 )
|
|
|
|
|
Yap_Error(INSTANTIATION_ERROR, tin, "with_output_to/3");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (IsApplTerm(tin) &&
|
|
|
|
|
(f = FunctorOfTerm(tin)) &&
|
|
|
|
|
(f == FunctorAtom || f == FunctorString ||
|
|
|
|
|
f == FunctorCodes1 || f == FunctorCodes ||
|
|
|
|
|
f == FunctorChars1 || f == FunctorChars) )
|
|
|
|
|
{
|
|
|
|
|
if (IsApplTerm(tin) && (f = FunctorOfTerm(tin)) &&
|
|
|
|
|
(f == FunctorAtom || f == FunctorString || f == FunctorCodes1 ||
|
|
|
|
|
f == FunctorCodes || f == FunctorChars1 || f == FunctorChars)) {
|
|
|
|
|
output_stream = Yap_OpenBufWriteStream(PASS_REGS1);
|
|
|
|
|
} else {
|
|
|
|
|
/* needs to change LOCAL_c_output_stream for write */
|
|
|
|
@ -1220,9 +1197,7 @@ with_output_to( USES_REGS1 )
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Int
|
|
|
|
|
format(Term tout, Term tf, Term tas USES_REGS)
|
|
|
|
|
{
|
|
|
|
|
static Int format(Term tout, Term tf, Term tas USES_REGS) {
|
|
|
|
|
bool mem_stream = false;
|
|
|
|
|
int output_stream;
|
|
|
|
|
Functor f;
|
|
|
|
@ -1232,11 +1207,9 @@ format(Term tout, Term tf, Term tas USES_REGS)
|
|
|
|
|
Yap_Error(INSTANTIATION_ERROR, tout, "format/3");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (IsApplTerm(tout) &&
|
|
|
|
|
(f = FunctorOfTerm(tout)) &&
|
|
|
|
|
(f == FunctorAtom || f == FunctorString1 ||
|
|
|
|
|
f == FunctorCodes1 || f == FunctorCodes ||
|
|
|
|
|
f == FunctorChars1 || f == FunctorChars) ) {
|
|
|
|
|
if (IsApplTerm(tout) && (f = FunctorOfTerm(tout)) &&
|
|
|
|
|
(f == FunctorAtom || f == FunctorString1 || f == FunctorCodes1 ||
|
|
|
|
|
f == FunctorCodes || f == FunctorChars1 || f == FunctorChars)) {
|
|
|
|
|
output_stream = Yap_OpenBufWriteStream(PASS_REGS1);
|
|
|
|
|
mem_stream = true;
|
|
|
|
|
} else {
|
|
|
|
@ -1288,30 +1261,24 @@ format(Term tout, Term tf, Term tas USES_REGS)
|
|
|
|
|
*
|
|
|
|
|
* Print formatted output to the current output stream.
|
|
|
|
|
*/
|
|
|
|
|
static Int
|
|
|
|
|
format2( USES_REGS1 )
|
|
|
|
|
{ /* 'format'(Stream,Control,Args) */
|
|
|
|
|
static Int format2(USES_REGS1) { /* 'format'(Stream,Control,Args) */
|
|
|
|
|
Int res;
|
|
|
|
|
|
|
|
|
|
res = doformat(Deref(ARG1), Deref(ARG2), LOCAL_c_output_stream PASS_REGS);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @pred format(+_Stream_+ _T_, :ListWithArguments)
|
|
|
|
|
*
|
|
|
|
|
* Print formatted output to the stream _Stream_.
|
|
|
|
|
*/
|
|
|
|
|
static Int
|
|
|
|
|
format3( USES_REGS1 )
|
|
|
|
|
{
|
|
|
|
|
static Int format3(USES_REGS1) {
|
|
|
|
|
Int res;
|
|
|
|
|
res = format(Deref(ARG1), Deref(ARG2), Deref(ARG3) PASS_REGS);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Yap_InitFormat(void)
|
|
|
|
|
{
|
|
|
|
|
void Yap_InitFormat(void) {
|
|
|
|
|
Yap_InitCPred("format", 2, format2, SyncPredFlag);
|
|
|
|
|
Yap_InitCPred("format", 3, format3, SyncPredFlag);
|
|
|
|
|
Yap_InitCPred("with_output_to", 2, with_output_to, SyncPredFlag);
|
|
|
|
|