/* writes a bignum */ static void writebig(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, struct rewind_term *rwt) { CELL *pt = RepAppl(t) + 1; CELL big_tag = pt[0]; if (big_tag == ARRAY_INT || big_tag == ARRAY_FLOAT) { wrputc('{', wglb->stream); wrputs("...", wglb->stream); wrputc('}', wglb->stream); lastw = separator; return; #ifdef USE_GMP } else if (big_tag == BIG_INT) { MP_INT *big = Yap_BigIntOfTerm(t); write_mpint(big, wglb); return; } else if (big_tag == BIG_RATIONAL) { Term trat = Yap_RatTermToApplTerm(t); writeTerm(trat, p, depth, rinfixarg, wglb, rwt); return; #endif } else if (big_tag >= USER_BLOB_START && big_tag < USER_BLOB_END) { Opaque_CallOnWrite f; CELL blob_info; blob_info = big_tag - USER_BLOB_START; if (GLOBAL_OpaqueHandlers && (f = GLOBAL_OpaqueHandlers[blob_info].write_handler)) { (f)(wglb->stream->file, big_tag, ExternalBlobFromTerm(t), 0); return; } } wrputs("0", wglb->stream); }
/* protect bracket from merging with previoous character. avoid stuff like not (2,3) -> not(2,3) or */ static void wropen_bracket(struct write_globs *wglb, int protect) { StreamDesc *stream = wglb->stream; if (lastw != separator && protect) wrputc(' ', stream); wrputc('(', stream); lastw = separator; }
static int protect_open_number(struct write_globs *wglb, int lm, int minus_required) { wrf stream = wglb->stream; if (lastw == symbol && lm && !minus_required) { wropen_bracket(wglb, TRUE); return TRUE; } else if (lastw == alphanum || (lastw == symbol && minus_required)) { wrputc(' ', stream); } return FALSE; }
static void wrputn(Int n, struct write_globs *wglb) /* writes an integer */ { wrf stream = wglb->stream; char s[256], *s1 = s; /* that should be enough for most integers */ int has_minus = (n < 0); int ob; ob = protect_open_number(wglb, last_minus, has_minus); #if HAVE_SNPRINTF snprintf(s, 256, Int_FORMAT, n); #else sprintf(s, Int_FORMAT, n); #endif while (*s1) wrputc(*s1++, stream); protect_close_number(wglb, ob); }
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; }
static void wrputf(Float f, struct write_globs *wglb) /* writes a float */ { #if THREADS char s[256]; #endif wrf stream = wglb->stream; int sgn; int ob; #if HAVE_ISNAN || defined(__WIN32) if (isnan(f)) { wrputs("(nan)", stream); lastw = separator; return; } #endif sgn = (f < 0.0); #if HAVE_ISINF || defined(_WIN32) if (isinf(f)) { if (sgn) { wrputs("(-inf)", stream); } else { wrputs("(+inf)", stream); } lastw = separator; return; } #endif ob = protect_open_number(wglb, last_minus, sgn); #if THREADS /* old style writing */ int found_dot = FALSE; char *pt = s; int ch; /* always use C locale for writing numbers */ #if O_LOCALE const unsigned char *decimalpoint = (unsigned char *)localeconv()->decimal_point; size_t l1 = strlen((const char *)decimalpoint + 1); #else const unsigned char decimalpoint[2] = "."; size_t l1 = 0; #endif if (lastw == symbol || lastw == alphanum) { wrputc(' ', stream); } lastw = alphanum; // sprintf(s, "%.15g", f); sprintf(s, floatFormat(), f); while (*pt == ' ') pt++; if (*pt == '-') { wrputc('-', stream); pt++; } while ((ch = *pt) != '\0') { // skip locale if (ch == decimalpoint[0] && !strncmp(pt + 1, (char *)decimalpoint + 1, l1)) { found_dot = TRUE; pt += l1; ch = '.'; } if (ch == 'e' || ch == 'E') { if (!found_dot) { found_dot = TRUE; wrputs(".0", stream); } found_dot = TRUE; } wrputc(ch, stream); pt++; } if (!found_dot) { wrputs(".0", stream); } #else char buf[256]; if (lastw == symbol || lastw == alphanum) { wrputc(' ', stream); } /* use SWI's format_float */ sprintf(buf, (char *)floatFormat(), f); wrputs(buf, stream); #endif protect_close_number(wglb, ob); }
inline static void wrputs(char *s, StreamDesc *stream) { int c; while ((c = *s++)) wrputc(c, stream); }
static void wrclose_bracket(struct write_globs *wglb, int protect) { wrf stream = wglb->stream; wrputc(')', stream); lastw = separator; }
static void wrputws(wchar_t *s, wrf stream) /* writes a string */ { while (*s) wrputc(*s++, stream); }