diff --git a/C/c_interface.c b/C/c_interface.c index 66296488b..be8d276d7 100755 --- a/C/c_interface.c +++ b/C/c_interface.c @@ -2116,32 +2116,33 @@ X_API Term YAP_CopyTerm(Term t) { return tn; } -X_API int YAP_WriteBuffer(Term t, char *buf, size_t sze, int flags) { +X_API char *YAP_WriteBuffer(Term t, char *buf, size_t sze, int flags) { CACHE_REGS size_t length; char *b; BACKUP_MACHINE_REGS(); - if ((b = Yap_TermToString(t, buf, sze, &length, &LOCAL_encoding, flags)) != - buf) { - if (b) - free(b); + if ((b = Yap_TermToString(t, &length, LOCAL_encoding, flags)) != buf) { RECOVER_MACHINE_REGS(); - return FALSE; + return b; } RECOVER_MACHINE_REGS(); - return TRUE; + return buf; } -X_API char *YAP_WriteDynamicBuffer(YAP_Term t, char *buf, size_t sze, - size_t *lengthp, encoding_t *encp, - int flags) { +/// write a a term to n user-provided buffer: make sure not tp +/// overflow the buffer even if the text is much larger. +X_API int YAP_WriteDynamicBuffer(YAP_Term t, char *buf, size_t sze, + size_t *lengthp, encoding_t enc, int flags) { char *b; BACKUP_MACHINE_REGS(); - b = Yap_TermToString(t, buf, sze, lengthp, encp, flags); + b = Yap_TermToString(t, lengthp, enc, flags); + if (*lengthp >= sze) + *lengthp = sze; + strncpy(buf, b, sze); RECOVER_MACHINE_REGS(); - return b; + return true; } X_API char *YAP_CompileClause(Term t) { diff --git a/C/scanner.c b/C/scanner.c index 6b7565464..6fc84ae25 100755 --- a/C/scanner.c +++ b/C/scanner.c @@ -404,11 +404,11 @@ writing, writing a BOM can be requested using the option */ #include "Yap.h" -#include "Yatom.h" #include "YapHeap.h" -#include "yapio.h" +#include "Yatom.h" #include "alloc.h" #include "eval.h" +#include "yapio.h" /* stuff we want to use in standard YAP code */ #include "YapText.h" #if _MSC_VER || defined(__MINGW32__) @@ -458,80 +458,62 @@ char_kind_t Yap_chtype0[NUMBER_OF_CHARS + 1] = { EF, /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ - BS, BS, BS, BS, BS, BS, BS, BS, BS, - BS, BS, BS, BS, BS, BS, BS, + BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ - BS, BS, BS, BS, BS, BS, BS, BS, BS, - BS, BS, BS, BS, BS, BS, BS, + BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, /* sp ! " # $ % & ' ( ) * + , - . / */ - BS, SL, DC, SY, SY, CC, SY, QT, BK, - BK, SY, SY, BK, SY, SY, SY, + BS, SL, DC, SY, SY, CC, SY, QT, BK, BK, SY, SY, BK, SY, SY, SY, /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ - NU, NU, NU, NU, NU, NU, NU, NU, NU, - NU, SY, SL, SY, SY, SY, SY, + NU, NU, NU, NU, NU, NU, NU, NU, NU, NU, SY, SL, SY, SY, SY, SY, /* @ A B C D E F G H I J K L M N O */ - SY, UC, UC, UC, UC, UC, UC, UC, UC, - UC, UC, UC, UC, UC, UC, UC, + SY, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, /* P Q R S T U V W X Y Z [ \ ] ^ _ */ - UC, UC, UC, UC, UC, UC, UC, UC, UC, - UC, UC, BK, SY, BK, SY, UL, + UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, BK, SY, BK, SY, UL, /* ` a b c d e f g h i j k l m n o */ - SY, LC, LC, LC, LC, LC, LC, LC, LC, - LC, LC, LC, LC, LC, LC, LC, + SY, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, /* p q r s t u v w x y z { | } ~ del */ - LC, LC, LC, LC, LC, LC, LC, LC, LC, - LC, LC, BK, BK, BK, SY, BS, + LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, BK, BK, BK, SY, BS, /* 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 */ - BS, BS, BS, BS, BS, BS, BS, BS, BS, - BS, BS, BS, BS, BS, BS, BS, + BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, /* 144 145 ’ 147 148 149 150 151 152 153 154 155 156 157 158 159 */ - BS, BS, BS, BS, BS, BS, BS, BS, BS, - BS, BS, BS, BS, BS, BS, BS, + BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, BS, /* ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬ ­ ® ¯ */ - BS, SY, SY, SY, SY, SY, SY, SY, SY, - SY, LC, SY, SY, SY, SY, SY, + BS, SY, SY, SY, SY, SY, SY, SY, SY, SY, LC, SY, SY, SY, SY, SY, /* ° ± ² ³ ´ µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿ */ - SY, SY, LC, LC, SY, SY, SY, SY, SY, - LC, LC, SY, SY, SY, SY, SY, + SY, SY, LC, LC, SY, SY, SY, SY, SY, LC, LC, SY, SY, SY, SY, SY, /* À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï */ - UC, UC, UC, UC, UC, UC, UC, UC, UC, - UC, UC, UC, UC, UC, UC, UC, + UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, /* Ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ü Ý Þ ß */ #ifdef vms - UC, UC, UC, UC, UC, UC, UC, UC, UC, - UC, UC, UC, UC, UC, UC, LC, + UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, UC, LC, #else - UC, UC, UC, UC, UC, UC, UC, SY, UC, - UC, UC, UC, UC, UC, UC, LC, + UC, UC, UC, UC, UC, UC, UC, SY, UC, UC, UC, UC, UC, UC, UC, LC, #endif /* à á â ã ä å æ ç è é ê ë ì í î ï */ - LC, LC, LC, LC, LC, LC, LC, LC, LC, - LC, LC, LC, LC, LC, LC, LC, + LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, /* ð ñ ò ó ô õ ö ÷ ø ù ú û ü cannot write the last * three because of lcc */ #ifdef vms - LC, LC, LC, LC, LC, LC, LC, LC, LC, - LC, LC, LC, LC, LC, LC, LC + LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC, LC #else - LC, LC, LC, LC, LC, LC, LC, SY, LC, - LC, LC, LC, LC, LC, LC, LC + LC, LC, LC, LC, LC, LC, LC, SY, LC, LC, LC, LC, LC, LC, LC, LC #endif }; @@ -564,7 +546,8 @@ typedef struct scanner_extra_alloc { } ScannerExtraBlock; #define CodeSpaceError(t, p, l) CodeSpaceError__(t, p, l PASS_REGS) -static TokEntry *CodeSpaceError__(TokEntry *t, TokEntry *p, TokEntry *l USES_REGS) { +static TokEntry *CodeSpaceError__(TokEntry *t, TokEntry *p, + TokEntry *l USES_REGS) { LOCAL_Error_TYPE = RESOURCE_ERROR_HEAP; LOCAL_ErrorMessage = "Code Space Overflow"; if (t) { @@ -587,7 +570,8 @@ static TokEntry *TrailSpaceError__(TokEntry *t, TokEntry *l USES_REGS) { } #define AuxSpaceError(p, l, msg) AuxSpaceError__(p, l, msg PASS_REGS) -static TokEntry *AuxSpaceError__(TokEntry *p, TokEntry *l, const char *msg USES_REGS) { +static TokEntry *AuxSpaceError__(TokEntry *p, TokEntry *l, + const char *msg USES_REGS) { /* huge atom or variable, we are in trouble */ LOCAL_ErrorMessage = (char *)msg; LOCAL_Error_TYPE = RESOURCE_ERROR_AUXILIARY_STACK; @@ -1136,7 +1120,8 @@ Term Yap_scan_num(StreamDesc *inp) { } TokEntry *tokptr = (TokEntry *)AllocScannerMemory(sizeof(TokEntry)); tokptr->TokPos = GetCurInpPos(inp); - while((ch = getchr(inp)) == BS); + while ((ch = getchr(inp)) == BS) + ; if (ch == '-') { sign = -1; ch = getchr(inp); @@ -1229,8 +1214,8 @@ Term Yap_scan_num(StreamDesc *inp) { const char *Yap_tokRep(TokEntry *tokptr, encoding_t encoding) { CACHE_REGS Term info = tokptr->TokInfo; - char *b, *buf = LOCAL_FileNameBuf2; - size_t length, sze = YAP_FILENAME_MAX - 1; + char *buf = LOCAL_FileNameBuf2; + size_t length; UInt flags = 0; switch (tokptr->Tok) { @@ -1240,13 +1225,9 @@ const char *Yap_tokRep(TokEntry *tokptr, encoding_t encoding) { Term s = Yap_WCharsToString(wc PASS_REGS); return StringOfTerm(s); } - return RepAtom((Atom)info)->StrOfAE; + return RepAtom((Atom)info)->StrOfAE; case Number_tok: - if ((b = Yap_TermToString(info, buf, sze, &length, &encoding, - flags)) != buf) { - return NULL; - } - return buf; + return Yap_TermToString(info, &length, encoding, flags); case Var_tok: { VarEntry *varinfo = (VarEntry *)info; varinfo->VarAdr = TermNil; @@ -1744,16 +1725,18 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments, } else ch = getchr(inp_stream); break; - case SY: + case SY: if (ch == '.' && (pch = Yap_peek(inp_stream - GLOBAL_Stream)) && (chtype(pch) == BS || chtype(pch) == EF || pch == '%')) { t->Tok = Ord(kind = eot_tok); // consume... - ch = getchr(inp_stream); + ch = getchr(inp_stream); if (chtype(ch) == EF) { mark_eof(inp_stream); t->TokInfo = TermEof; } else { + if (ch == '%') + continue; t->TokInfo = TermNewLine; } return l; @@ -2054,7 +2037,8 @@ TokEntry *Yap_tokenizer(struct stream_desc *inp_stream, bool store_comments, } #if DEBUG if (GLOBAL_Option[2]) - fprintf(stderr, "[Token %d %s]", Ord(kind), Yap_tokRep(t, inp_stream->encoding)); + fprintf(stderr, "[Token %d %s]", Ord(kind), + Yap_tokRep(t, inp_stream->encoding)); #endif if (LOCAL_ErrorMessage) { /* insert an error token to inform the system of what happened */ diff --git a/C/text.c b/C/text.c index 779c1e493..412fe51a7 100644 --- a/C/text.c +++ b/C/text.c @@ -16,34 +16,35 @@ *************************************************************************/ #include "Yap.h" -#include "Yatom.h" #include "YapHeap.h" +#include "YapText.h" +#include "Yatom.h" #include "eval.h" #include "yapio.h" -#include "YapText.h" #include #include #ifndef HAVE_WCSNLEN -inline static size_t -min_size(size_t i, size_t j) { - return ( i < j ? i : j ); -} +inline static size_t min_size(size_t i, size_t j) { return (i < j ? i : j); } #define wcsnlen(S, N) min_size(N, wcslen(S)) #endif -static inline unsigned char *getChar(unsigned char *p, int *c) { *c = *p; return p+1; } +static inline unsigned char *getChar(unsigned char *p, int *c) { + *c = *p; + return p + 1; +} -static inline wchar_t *getWchar(wchar_t *p, int *c) { *c = *p; return p+1; } +static inline wchar_t *getWchar(wchar_t *p, int *c) { + *c = *p; + return p + 1; +} #ifndef NAN -#define NAN (0.0/0.0) +#define NAN (0.0 / 0.0) #endif -static Term -Globalize(Term v USES_REGS) -{ +static Term Globalize(Term v USES_REGS) { if (!IsVarTerm(v = Deref(v))) { return v; } @@ -54,18 +55,17 @@ Globalize(Term v USES_REGS) return v; } -static Int -SkipListCodes(unsigned char **bufp, Term *l, Term **tailp, Int *atoms, bool *wide, seq_tv_t *inp USES_REGS) -{ +static Int SkipListCodes(unsigned char **bufp, Term *l, Term **tailp, + Int *atoms, bool *wide, seq_tv_t *inp USES_REGS) { Int length = 0; Term *s; /* slow */ - Term v; /* temporary */ + Term v; /* temporary */ *wide = false; size_t max = 1; unsigned char *st0 = *bufp, *st; unsigned char *smax = NULL; - do_derefa(v,l,derefa_unk,derefa_nonvar); + do_derefa(v, l, derefa_unk, derefa_nonvar); *tailp = l; s = l; @@ -77,106 +77,110 @@ SkipListCodes(unsigned char **bufp, Term *l, Term **tailp, Int *atoms, bool *wid if (!st0) { if (inp->type & YAP_STRING_MALLOC) { - *bufp = st0 = (unsigned char *)malloc(MAXPATHLEN+1); - smax = st0+(MAXPATHLEN-8); // give 8 bytes for max UTF-8 size + '\0'; + *bufp = st0 = (unsigned char *)malloc(MAXPATHLEN + 1); + smax = st0 + (MAXPATHLEN - 8); // give 8 bytes for max UTF-8 size + '\0'; } else { *bufp = st0 = (unsigned char *)Yap_PreAllocCodeSpace(); - smax = (unsigned char *)AuxTop-8; // give 8 bytes for max UTF-8 size + '\0'; + smax = (unsigned char *)AuxTop - + 8; // give 8 bytes for max UTF-8 size + '\0'; } - } else if (inp->sz > 0) { - smax = st0+(inp->sz-8); // give 8 bytes for max UTF-8 size + '\0'; + } else if (inp->sz > 0) { + smax = st0 + (inp->sz - 8); // give 8 bytes for max UTF-8 size + '\0'; } else { // AUX_ERROR( *l, 2*(length+1), st0, unsigned char); return 0; } *bufp = st = st0; - + if (*l == TermNil) { return 0; } - if ( IsPairTerm(*l) ) - { + if (IsPairTerm(*l)) { Term hd0 = HeadOfTerm(*l); if (IsVarTerm(hd0)) { return -INSTANTIATION_ERROR; } - //are we looking for atoms/codes? + // are we looking for atoms/codes? // whatever the case, we should be consistent throughout, // so we should be consistent with the first arg. if (*atoms == 1) { - if ( !IsIntegerTerm(hd0) ) { - return -INSTANTIATION_ERROR; + if (!IsIntegerTerm(hd0)) { + return -INSTANTIATION_ERROR; } } else if (*atoms == 2) { - if ( !IsAtomTerm(hd0) ) { - return -TYPE_ERROR_ATOM; + if (!IsAtomTerm(hd0)) { + return -TYPE_ERROR_ATOM; } } - - do { + + do { int ch; length++; if (length == max) { *st++ = '\0'; } - { Term hd = Deref(RepPair(*l)[0]); - if (IsVarTerm(hd)) { - return -INSTANTIATION_ERROR; - } else if (IsAtomTerm(hd)) { - (*atoms)++; - if (*atoms < length) - { *tailp = l; return -TYPE_ERROR_NUMBER; } - if (IsWideAtom(AtomOfTerm(hd))) { - int ch; - if ((RepAtom(AtomOfTerm(hd))->WStrOfAE)[1] != '\0') { - length = -REPRESENTATION_ERROR_CHARACTER; - } - ch = RepAtom(AtomOfTerm(hd))->WStrOfAE[0]; - *wide = true; - } else { - AtomEntry *ae = RepAtom(AtomOfTerm(hd)); - if ((ae->StrOfAE)[1] != '\0') { - length = -REPRESENTATION_ERROR_CHARACTER; - } else { - ch = RepAtom(AtomOfTerm(hd))->StrOfAE[0]; - *wide |= ch > 0x80; - } - } - } else if (IsIntegerTerm(hd)) { - ch = IntegerOfTerm(hd); - if (*atoms) length = -TYPE_ERROR_ATOM; - else if (ch < 0) { - *tailp = l; - length = -DOMAIN_ERROR_NOT_LESS_THAN_ZERO; - } else { - *wide |= ch > 0x80; - } - } else { - length = -TYPE_ERROR_INTEGER; - } - if (length < 0) { - *tailp = l; - return length; - } + { + Term hd = Deref(RepPair(*l)[0]); + if (IsVarTerm(hd)) { + return -INSTANTIATION_ERROR; + } else if (IsAtomTerm(hd)) { + (*atoms)++; + if (*atoms < length) { + *tailp = l; + return -TYPE_ERROR_NUMBER; + } + if (IsWideAtom(AtomOfTerm(hd))) { + int ch; + if ((RepAtom(AtomOfTerm(hd))->WStrOfAE)[1] != '\0') { + length = -REPRESENTATION_ERROR_CHARACTER; + } + ch = RepAtom(AtomOfTerm(hd))->WStrOfAE[0]; + *wide = true; + } else { + AtomEntry *ae = RepAtom(AtomOfTerm(hd)); + if ((ae->StrOfAE)[1] != '\0') { + length = -REPRESENTATION_ERROR_CHARACTER; + } else { + ch = RepAtom(AtomOfTerm(hd))->StrOfAE[0]; + *wide |= ch > 0x80; + } + } + } else if (IsIntegerTerm(hd)) { + ch = IntegerOfTerm(hd); + if (*atoms) + length = -TYPE_ERROR_ATOM; + else if (ch < 0) { + *tailp = l; + length = -DOMAIN_ERROR_NOT_LESS_THAN_ZERO; + } else { + *wide |= ch > 0x80; + } + } else { + length = -TYPE_ERROR_INTEGER; + } + if (length < 0) { + *tailp = l; + return length; + } } // now copy char to buffer - size_t chsz = put_utf8( st, ch ); - if (smax <= st+chsz) { + size_t chsz = put_utf8(st, ch); + if (smax <= st + chsz) { *st++ = '\0'; *tailp = l; return length; } else { st += chsz; } - l = RepPair(*l)+1; - do_derefa(v,l,derefa2_unk,derefa2_nonvar); - } while ( *l != *s && IsPairTerm(*l) ); + l = RepPair(*l) + 1; + do_derefa(v, l, derefa2_unk, derefa2_nonvar); + } while (*l != *s && IsPairTerm(*l)); } if (IsVarTerm(*l)) { - return -INSTANTIATION_ERROR; + return -INSTANTIATION_ERROR; } - if ( *l != TermNil) { - return -TYPE_ERROR_LIST; + if (*l != TermNil) { + return -TYPE_ERROR_LIST; } st[0] = '\0'; *tailp = l; @@ -184,9 +188,8 @@ SkipListCodes(unsigned char **bufp, Term *l, Term **tailp, Int *atoms, bool *wid return length; } -static void * -to_buffer(void *buf, Term t, seq_tv_t *inp, bool *widep, Int *atoms, size_t *lenp USES_REGS) -{ +static void *to_buffer(void *buf, Term t, seq_tv_t *inp, bool *widep, + Int *atoms, size_t *lenp USES_REGS) { CELL *r = NULL; Int n; @@ -201,85 +204,83 @@ to_buffer(void *buf, Term t, seq_tv_t *inp, bool *widep, Int *atoms, size_t *len return NULL; } *lenp = n; - return bufc; + return bufc; } -static void * -Yap_ListOfCodesToBuffer(void *buf, Term t, seq_tv_t *inp, bool *widep, size_t *lenp USES_REGS) -{ +static void *Yap_ListOfCodesToBuffer(void *buf, Term t, seq_tv_t *inp, + bool *widep, size_t *lenp USES_REGS) { Int atoms = 1; // we only want lists of atoms - return to_buffer( buf, t, inp, widep, &atoms, lenp PASS_REGS); + return to_buffer(buf, t, inp, widep, &atoms, lenp PASS_REGS); } -static void * -Yap_ListOfAtomsToBuffer(void *buf, Term t, seq_tv_t *inp, bool *widep, size_t *lenp USES_REGS) -{ +static void *Yap_ListOfAtomsToBuffer(void *buf, Term t, seq_tv_t *inp, + bool *widep, size_t *lenp USES_REGS) { Int atoms = 2; // we only want lists of integer codes - return to_buffer( buf, t, inp, widep, &atoms, lenp PASS_REGS); + return to_buffer(buf, t, inp, widep, &atoms, lenp PASS_REGS); } -static void * -Yap_ListToBuffer(void *buf, Term t, seq_tv_t *inp, bool *widep, size_t *lenp USES_REGS) -{ +static void *Yap_ListToBuffer(void *buf, Term t, seq_tv_t *inp, bool *widep, + size_t *lenp USES_REGS) { Int atoms = 0; // we accept both types of lists. - return to_buffer( buf, t, inp, widep, &atoms, lenp PASS_REGS); + return to_buffer(buf, t, inp, widep, &atoms, lenp PASS_REGS); } #if USE_GEN_TYPE_ERROR -static yap_error_number -gen_type_error(int flags) { - if ((flags & (YAP_STRING_STRING|YAP_STRING_ATOM|YAP_STRING_INT|YAP_STRING_FLOAT|YAP_STRING_ATOMS_CODES|YAP_STRING_BIG)) == - (YAP_STRING_STRING|YAP_STRING_ATOM|YAP_STRING_INT|YAP_STRING_FLOAT|YAP_STRING_ATOMS_CODES|YAP_STRING_BIG)) +static yap_error_number gen_type_error(int flags) { + if ((flags & (YAP_STRING_STRING | YAP_STRING_ATOM | YAP_STRING_INT | + YAP_STRING_FLOAT | YAP_STRING_ATOMS_CODES | YAP_STRING_BIG)) == + (YAP_STRING_STRING | YAP_STRING_ATOM | YAP_STRING_INT | YAP_STRING_FLOAT | + YAP_STRING_ATOMS_CODES | YAP_STRING_BIG)) return TYPE_ERROR_TEXT; - if ((flags & (YAP_STRING_STRING|YAP_STRING_ATOM|YAP_STRING_INT|YAP_STRING_FLOAT|YAP_STRING_BIG)) == - (YAP_STRING_STRING|YAP_STRING_ATOM|YAP_STRING_INT|YAP_STRING_FLOAT|YAP_STRING_BIG)) + if ((flags & (YAP_STRING_STRING | YAP_STRING_ATOM | YAP_STRING_INT | + YAP_STRING_FLOAT | YAP_STRING_BIG)) == + (YAP_STRING_STRING | YAP_STRING_ATOM | YAP_STRING_INT | YAP_STRING_FLOAT | + YAP_STRING_BIG)) return TYPE_ERROR_ATOMIC; - if ((flags & (YAP_STRING_INT|YAP_STRING_FLOAT|YAP_STRING_BIG)) == - (YAP_STRING_INT|YAP_STRING_FLOAT|YAP_STRING_BIG)) + if ((flags & (YAP_STRING_INT | YAP_STRING_FLOAT | YAP_STRING_BIG)) == + (YAP_STRING_INT | YAP_STRING_FLOAT | YAP_STRING_BIG)) return TYPE_ERROR_NUMBER; - if (flags & YAP_STRING_ATOM ) + if (flags & YAP_STRING_ATOM) return TYPE_ERROR_ATOM; if (flags & YAP_STRING_STRING) return TYPE_ERROR_STRING; - if (flags & (YAP_STRING_CODES|YAP_STRING_ATOMS)) + if (flags & (YAP_STRING_CODES | YAP_STRING_ATOMS)) return TYPE_ERROR_LIST; return TYPE_ERROR_NUMBER; } #endif - void * -Yap_readText( void *buf, seq_tv_t *inp, encoding_t *enc, int *minimal, size_t *lengp USES_REGS) -{ - char *s, *s0 = buf; +void *Yap_readText(void *buf, seq_tv_t *inp, encoding_t *enc, int *minimal, + size_t *lengp USES_REGS) { + char *s, *s0 = buf; wchar_t *ws; bool wide; /* we know what the term is */ - if ( !(inp->type & (YAP_STRING_CHARS|YAP_STRING_WCHARS))) - { - if ( !(inp->type & YAP_STRING_TERM)) { - if (IsVarTerm(inp->val.t)) { - LOCAL_Error_TYPE = INSTANTIATION_ERROR; - } else if (!IsAtomTerm(inp->val.t) && inp->type == YAP_STRING_ATOM) { - LOCAL_Error_TYPE = TYPE_ERROR_ATOM; - } else if (!IsStringTerm(inp->val.t) && inp->type == YAP_STRING_STRING) { - LOCAL_Error_TYPE = TYPE_ERROR_STRING; - }else if (!IsPairTerm(inp->val.t) && - !IsStringTerm(inp->val.t) && - inp->type == (YAP_STRING_ATOMS_CODES|YAP_STRING_STRING)) { - LOCAL_Error_TYPE = TYPE_ERROR_LIST; - } else if (!IsNumTerm(inp->val.t) && (inp->type & ( YAP_STRING_INT|YAP_STRING_FLOAT| YAP_STRING_BIG)) == inp->type) { - LOCAL_Error_TYPE = TYPE_ERROR_NUMBER; - } - LOCAL_Error_Term = inp->val.t; + if (!(inp->type & (YAP_STRING_CHARS | YAP_STRING_WCHARS))) { + if (!(inp->type & YAP_STRING_TERM)) { + if (IsVarTerm(inp->val.t)) { + LOCAL_Error_TYPE = INSTANTIATION_ERROR; + } else if (!IsAtomTerm(inp->val.t) && inp->type == YAP_STRING_ATOM) { + LOCAL_Error_TYPE = TYPE_ERROR_ATOM; + } else if (!IsStringTerm(inp->val.t) && inp->type == YAP_STRING_STRING) { + LOCAL_Error_TYPE = TYPE_ERROR_STRING; + } else if (!IsPairTerm(inp->val.t) && !IsStringTerm(inp->val.t) && + inp->type == (YAP_STRING_ATOMS_CODES | YAP_STRING_STRING)) { + LOCAL_Error_TYPE = TYPE_ERROR_LIST; + } else if (!IsNumTerm(inp->val.t) && + (inp->type & (YAP_STRING_INT | YAP_STRING_FLOAT | + YAP_STRING_BIG)) == inp->type) { + LOCAL_Error_TYPE = TYPE_ERROR_NUMBER; } + LOCAL_Error_Term = inp->val.t; } + } if (LOCAL_Error_TYPE != YAP_NO_ERROR) return NULL; - - // this is a term, extract the UTF8 representation - if ( IsStringTerm(inp->val.t) && - inp->type & YAP_STRING_STRING) { + + // this is a term, extract the UTF8 representation + if (IsStringTerm(inp->val.t) && inp->type & YAP_STRING_STRING) { const char *s = StringOfTerm(inp->val.t); *enc = ENC_ISO_UTF8; *minimal = FALSE; @@ -287,8 +288,7 @@ Yap_readText( void *buf, seq_tv_t *inp, encoding_t *enc, int *minimal, size_t *l *lengp = strlen(s); return (void *)s; } - if ( IsAtomTerm(inp->val.t) - && inp->type & YAP_STRING_ATOM) { + if (IsAtomTerm(inp->val.t) && inp->type & YAP_STRING_ATOM) { // this is a term, extract to a buffer, and representation is wide *minimal = TRUE; Atom at = AtomOfTerm(inp->val.t); @@ -304,177 +304,184 @@ Yap_readText( void *buf, seq_tv_t *inp, encoding_t *enc, int *minimal, size_t *l return s; } } - if (((inp->type &(YAP_STRING_CODES|YAP_STRING_ATOMS)) == - (YAP_STRING_CODES|YAP_STRING_ATOMS))) { - s = Yap_ListToBuffer( s0, inp->val.t, inp, &wide, lengp PASS_REGS); + if (((inp->type & (YAP_STRING_CODES | YAP_STRING_ATOMS)) == + (YAP_STRING_CODES | YAP_STRING_ATOMS))) { + s = Yap_ListToBuffer(s0, inp->val.t, inp, &wide, lengp PASS_REGS); // this is a term, extract to a sfer, and representation is wide *minimal = true; *enc = ENC_ISO_UTF8; return s; } - if (inp->type == YAP_STRING_CODES) { - s = Yap_ListOfCodesToBuffer( s0, inp->val.t, inp, &wide, lengp PASS_REGS); + if (inp->type == YAP_STRING_CODES) { + s = Yap_ListOfCodesToBuffer(s0, inp->val.t, inp, &wide, lengp PASS_REGS); // this is a term, extract to a sfer, and representation is wide *minimal = true; *enc = ENC_ISO_UTF8; return s; - } - if (inp->type == YAP_STRING_ATOMS) { - s = Yap_ListOfAtomsToBuffer( s0, inp->val.t, inp, &wide, lengp PASS_REGS); + } + if (inp->type == YAP_STRING_ATOMS) { + s = Yap_ListOfAtomsToBuffer(s0, inp->val.t, inp, &wide, lengp PASS_REGS); // this is a term, extract to a buffer, and representation is wide *minimal = true; *enc = ENC_ISO_UTF8; return s; } if (inp->type & YAP_STRING_INT && IsIntegerTerm(inp->val.t)) { - if (s0) s = s0; - else s = Yap_PreAllocCodeSpace(); - AUX_ERROR( inp->val.t, LOCAL_MAX_SIZE, s, char); - if (snprintf(s, LOCAL_MAX_SIZE-1, Int_FORMAT, IntegerOfTerm(inp->val.t)) < 0) { - AUX_ERROR( inp->val.t, 2*LOCAL_MAX_SIZE, s, char); - } - *enc = ENC_ISO_LATIN1; - *lengp = strlen(s); - return s; + if (s0) + s = s0; + else + s = Yap_PreAllocCodeSpace(); + AUX_ERROR(inp->val.t, LOCAL_MAX_SIZE, s, char); + if (snprintf(s, LOCAL_MAX_SIZE - 1, Int_FORMAT, IntegerOfTerm(inp->val.t)) < + 0) { + AUX_ERROR(inp->val.t, 2 * LOCAL_MAX_SIZE, s, char); } + *enc = ENC_ISO_LATIN1; + *lengp = strlen(s); + return s; + } if (inp->type & YAP_STRING_FLOAT && IsFloatTerm(inp->val.t)) { - if (s0) s = s0; - else s = Yap_PreAllocCodeSpace(); - AUX_ERROR( inp->val.t, LOCAL_MAX_SIZE, s, char); - if ( !Yap_FormatFloat( FloatOfTerm(inp->val.t), &s, LOCAL_MAX_SIZE-1 ) ) { - AUX_ERROR( inp->val.t, 2*LOCAL_MAX_SIZE, s, char); - } - *lengp = strlen(s); - *enc = ENC_ISO_LATIN1; - return s; + if (s0) + s = s0; + else + s = Yap_PreAllocCodeSpace(); + AUX_ERROR(inp->val.t, LOCAL_MAX_SIZE, s, char); + if (!Yap_FormatFloat(FloatOfTerm(inp->val.t), &s, LOCAL_MAX_SIZE - 1)) { + AUX_ERROR(inp->val.t, 2 * LOCAL_MAX_SIZE, s, char); } + *lengp = strlen(s); + *enc = ENC_ISO_LATIN1; + return s; + } #if USE_GMP if (inp->type & YAP_STRING_BIG && IsBigIntTerm(inp->val.t)) { - if (s0) s = s0; - else s = Yap_PreAllocCodeSpace(); - if ( !Yap_mpz_to_string( Yap_BigIntOfTerm(inp->val.t), s, LOCAL_MAX_SIZE-1 , 10 ) ) { - AUX_ERROR( inp->val.t, LOCAL_MAX_SIZE, s, char); - } - *enc = ENC_ISO_LATIN1; - *lengp = strlen(s); - return s; + if (s0) + s = s0; + else + s = Yap_PreAllocCodeSpace(); + if (!Yap_mpz_to_string(Yap_BigIntOfTerm(inp->val.t), s, LOCAL_MAX_SIZE - 1, + 10)) { + AUX_ERROR(inp->val.t, LOCAL_MAX_SIZE, s, char); } + *enc = ENC_ISO_LATIN1; + *lengp = strlen(s); + return s; + } #endif - if (inp->type & YAP_STRING_TERM) - { - char *s, *o; - if (s0) s = s0; - else s = Yap_PreAllocCodeSpace(); - size_t sz = LOCAL_MAX_SIZE-1; - encoding_t enc = ENC_ISO_UTF8; - o = Yap_TermToString(inp->val.t, s, sz, lengp, &enc, 0); - return s; - } - if (inp->type & YAP_STRING_CHARS) { - *enc = inp->enc; - if (inp->type & YAP_STRING_NCHARS) - *lengp = inp->sz; - else - *lengp = strlen(inp->val.c); - return (void *)inp->val.c; - } - if (inp->type & YAP_STRING_WCHARS) { - *enc = ENC_WCHAR; - if (inp->type & YAP_STRING_NCHARS) - *lengp = inp->sz; - else - *lengp = wcslen(inp->val.w); - return (void *)inp->val.w; - } - return NULL; + if (inp->type & YAP_STRING_TERM) { + encoding_t enc = ENC_ISO_UTF8; + char *o = Yap_TermToString(inp->val.t, lengp, enc, 0); + return o; + } + if (inp->type & YAP_STRING_CHARS) { + *enc = inp->enc; + if (inp->type & YAP_STRING_NCHARS) + *lengp = inp->sz; + else + *lengp = strlen(inp->val.c); + return (void *)inp->val.c; + } + if (inp->type & YAP_STRING_WCHARS) { + *enc = ENC_WCHAR; + if (inp->type & YAP_STRING_NCHARS) + *lengp = inp->sz; + else + *lengp = wcslen(inp->val.w); + return (void *)inp->val.w; + } + return NULL; } -static Term -write_strings( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng USES_REGS) -{ +static Term write_strings(void *s0, seq_tv_t *out, encoding_t enc, int minimal, + size_t leng USES_REGS) { size_t min = 0, max = leng; - if (out->type & (YAP_STRING_NCHARS|YAP_STRING_TRUNC)) { - if (out->type & YAP_STRING_NCHARS) min = out->sz; - if (out->type & YAP_STRING_TRUNC && out->max < max) max = out->max; + if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) { + if (out->type & YAP_STRING_NCHARS) + min = out->sz; + if (out->type & YAP_STRING_TRUNC && out->max < max) + max = out->max; } switch (enc) { - case ENC_ISO_UTF8: - { unsigned char *s = s0, *lim = s + (max = strlen_utf8(s)); - Term t = init_tstring( PASS_REGS1 ); - unsigned char *cp = s, *buf; + case ENC_ISO_UTF8: { + unsigned char *s = s0, *lim = s + (max = strlen_utf8(s)); + Term t = init_tstring(PASS_REGS1); + unsigned char *cp = s, *buf; - LOCAL_TERM_ERROR( t, 2*(lim-s) ); - buf = buf_from_tstring(HR); - while (*cp && cp < lim) { - utf8proc_int32_t chr; - cp += get_utf8(cp, -1, &chr); - buf += put_utf8(buf, chr); - } - if (max >= min) *buf++ = '\0'; - else while (max < min) { - max++; - buf += put_utf8(buf, '\0'); - } - - close_tstring( buf PASS_REGS ); - out->val.t = t; - } - break; - case ENC_ISO_LATIN1: - { unsigned char *s = s0, *lim = s + (max = strlen_latin_utf8(s0)); - Term t = init_tstring( PASS_REGS1 ); - unsigned char *cp = s; - unsigned char *buf; + LOCAL_TERM_ERROR(t, 2 * (lim - s)); + buf = buf_from_tstring(HR); + while (*cp && cp < lim) { utf8proc_int32_t chr; - - LOCAL_TERM_ERROR( t, 2*(lim-s) ); - buf = buf_from_tstring(HR); - while (cp < lim) { - cp = getChar(cp, &chr); - buf += put_utf8(buf, chr); - } - if (max >= min) *buf++ = '\0'; - else while (max < min) { - max++; - buf += put_utf8(buf, chr); - } - close_tstring( buf PASS_REGS ); - out->val.t = t; + cp += get_utf8(cp, -1, &chr); + buf += put_utf8(buf, chr); } - break; - case ENC_WCHAR: - { wchar_t *s = s0, *lim = s + (max = strlen_ucs2_utf8(s0)); - Term t = init_tstring( PASS_REGS1 ); - wchar_t *wp = s; - unsigned char *buf; + if (max >= min) + *buf++ = '\0'; + else + while (max < min) { + max++; + buf += put_utf8(buf, '\0'); + } - LOCAL_TERM_ERROR( t, 2*(lim-s) ); - buf = buf_from_tstring(HR); - while (wp < lim) { - utf8proc_int32_t chr; - wp = getWchar(wp, &chr); - buf += put_utf8(buf, chr); - } - if (max >= min) *buf++ = '\0'; - else while (max < min) { - max++; - buf += put_utf8(buf, '\0'); - } - close_tstring( buf PASS_REGS ); - out->val.t = t; + close_tstring(buf PASS_REGS); + out->val.t = t; + } break; + case ENC_ISO_LATIN1: { + unsigned char *s = s0, *lim = s + (max = strlen_latin_utf8(s0)); + Term t = init_tstring(PASS_REGS1); + unsigned char *cp = s; + unsigned char *buf; + utf8proc_int32_t chr; + + LOCAL_TERM_ERROR(t, 2 * (lim - s)); + buf = buf_from_tstring(HR); + while (cp < lim) { + cp = getChar(cp, &chr); + buf += put_utf8(buf, chr); } - break; + if (max >= min) + *buf++ = '\0'; + else + while (max < min) { + max++; + buf += put_utf8(buf, chr); + } + close_tstring(buf PASS_REGS); + out->val.t = t; + } break; + case ENC_WCHAR: { + wchar_t *s = s0, *lim = s + (max = strlen_ucs2_utf8(s0)); + Term t = init_tstring(PASS_REGS1); + wchar_t *wp = s; + unsigned char *buf; + + LOCAL_TERM_ERROR(t, 2 * (lim - s)); + buf = buf_from_tstring(HR); + while (wp < lim) { + utf8proc_int32_t chr; + wp = getWchar(wp, &chr); + buf += put_utf8(buf, chr); + } + if (max >= min) + *buf++ = '\0'; + else + while (max < min) { + max++; + buf += put_utf8(buf, '\0'); + } + close_tstring(buf PASS_REGS); + out->val.t = t; + } break; default: - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc), __FUNCTION__); + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", + enc_name(enc), __FUNCTION__); } return out->val.t; } -static Term -write_atoms( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng USES_REGS) -{ +static Term write_atoms(void *s0, seq_tv_t *out, encoding_t enc, int minimal, + size_t leng USES_REGS) { Term t = AbsPair(HR); size_t sz = 0; size_t max = leng; @@ -482,327 +489,354 @@ write_atoms( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng U out->val.t = t; return TermNil; } - if (out->type & (YAP_STRING_NCHARS|YAP_STRING_TRUNC)) { - if (out->type & YAP_STRING_TRUNC && out->max < max) max = out->max; + if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) { + if (out->type & YAP_STRING_TRUNC && out->max < max) + max = out->max; } switch (enc) { - case ENC_ISO_UTF8: - { unsigned char *s = s0, *lim = s + strnlen((char*)s, max); - unsigned char *cp = s; - wchar_t w[2]; - w[1] = '\0'; - LOCAL_TERM_ERROR( t, 2*(lim-s) ); - while (cp < lim && *cp) { - utf8proc_int32_t chr; - CELL *cl; - cp += get_utf8(cp, -1, &chr); - if (chr == '\0') break; - w[0] = chr; - cl = HR; - HR += 2; - cl[0] = MkAtomTerm(Yap_LookupMaybeWideAtom(w)); - cl[1] = AbsPair(HR); - sz++; - if (sz == max) break; - } - break; + case ENC_ISO_UTF8: { + unsigned char *s = s0, *lim = s + strnlen((char *)s, max); + unsigned char *cp = s; + wchar_t w[2]; + w[1] = '\0'; + LOCAL_TERM_ERROR(t, 2 * (lim - s)); + while (cp < lim && *cp) { + utf8proc_int32_t chr; + CELL *cl; + cp += get_utf8(cp, -1, &chr); + if (chr == '\0') + break; + w[0] = chr; + cl = HR; + HR += 2; + cl[0] = MkAtomTerm(Yap_LookupMaybeWideAtom(w)); + cl[1] = AbsPair(HR); + sz++; + if (sz == max) + break; } - case ENC_ISO_LATIN1: - { unsigned char *s = s0, *lim = s + strnlen(s0, max); - unsigned char *cp = s; - char w[2]; - w[1] = '\0'; + break; + } + case ENC_ISO_LATIN1: { + unsigned char *s = s0, *lim = s + strnlen(s0, max); + unsigned char *cp = s; + char w[2]; + w[1] = '\0'; - LOCAL_TERM_ERROR( t, 2*(lim-s) ); - while (cp < lim) { - utf8proc_int32_t chr; - cp = getChar(cp, &chr); - if (chr == '\0') break; - w[0] = chr; - HR[0] = MkAtomTerm(Yap_LookupAtom(w)); - HR[1] = AbsPair(HR+2); - HR += 2; - sz++; - if (sz == max) break; - } - break; + LOCAL_TERM_ERROR(t, 2 * (lim - s)); + while (cp < lim) { + utf8proc_int32_t chr; + cp = getChar(cp, &chr); + if (chr == '\0') + break; + w[0] = chr; + HR[0] = MkAtomTerm(Yap_LookupAtom(w)); + HR[1] = AbsPair(HR + 2); + HR += 2; + sz++; + if (sz == max) + break; } - case ENC_WCHAR: - { wchar_t *s = s0, *lim = s + wcsnlen(s, max); - wchar_t *cp = s; - wchar_t w[2]; - w[1] = '\0'; + break; + } + case ENC_WCHAR: { + wchar_t *s = s0, *lim = s + wcsnlen(s, max); + wchar_t *cp = s; + wchar_t w[2]; + w[1] = '\0'; - LOCAL_TERM_ERROR( t, 2*(lim-s) ); - while (*cp && cp < lim) { - utf8proc_int32_t chr; - cp = getWchar(cp, &chr); - if (chr == '\0') break; - w[0] = chr; - HR[0] = MkAtomTerm(Yap_LookupMaybeWideAtom(w)); - HR[1] = AbsPair(HR+2); - HR += 2; - sz++; - if (sz == max) break; - } - break; + LOCAL_TERM_ERROR(t, 2 * (lim - s)); + while (*cp && cp < lim) { + utf8proc_int32_t chr; + cp = getWchar(cp, &chr); + if (chr == '\0') + break; + w[0] = chr; + HR[0] = MkAtomTerm(Yap_LookupMaybeWideAtom(w)); + HR[1] = AbsPair(HR + 2); + HR += 2; + sz++; + if (sz == max) + break; } + break; + } default: - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc), __FUNCTION__); + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", + enc_name(enc), __FUNCTION__); } if (out->type & YAP_STRING_DIFF) { - if (sz == 0) t = out->dif; - else HR[-1] = Globalize(out->dif PASS_REGS); + if (sz == 0) + t = out->dif; + else + HR[-1] = Globalize(out->dif PASS_REGS); } else { - if (sz == 0) t = TermNil; - else HR[-1] = TermNil; + if (sz == 0) + t = TermNil; + else + HR[-1] = TermNil; } out->val.t = t; return (t); } -static Term -write_codes( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng USES_REGS) -{ +static Term write_codes(void *s0, seq_tv_t *out, encoding_t enc, int minimal, + size_t leng USES_REGS) { Term t = AbsPair(HR); size_t min = 0, max = leng; size_t sz = 0; - if (out->type & (YAP_STRING_NCHARS|YAP_STRING_TRUNC)) { - if (out->type & YAP_STRING_NCHARS) min = out->sz; - if (out->type & YAP_STRING_TRUNC && out->max < max) max = out->max; + if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) { + if (out->type & YAP_STRING_NCHARS) + min = out->sz; + if (out->type & YAP_STRING_TRUNC && out->max < max) + max = out->max; } switch (enc) { - case ENC_ISO_UTF8: - { unsigned char *s = s0, *lim = s + strnlen(s0, max); - unsigned char *cp = s; - LOCAL_TERM_ERROR( t, 2*(lim-s) ); - while (*cp && cp < lim) { - utf8proc_int32_t chr; - cp += get_utf8(cp, -1, &chr); - HR[0] = MkIntTerm(chr); - HR[1] = AbsPair(HR+2); - HR += 2; - sz++; - if (sz == max) break; - } - break; + case ENC_ISO_UTF8: { + unsigned char *s = s0, *lim = s + strnlen(s0, max); + unsigned char *cp = s; + LOCAL_TERM_ERROR(t, 2 * (lim - s)); + while (*cp && cp < lim) { + utf8proc_int32_t chr; + cp += get_utf8(cp, -1, &chr); + HR[0] = MkIntTerm(chr); + HR[1] = AbsPair(HR + 2); + HR += 2; + sz++; + if (sz == max) + break; } - case ENC_ISO_LATIN1: - { unsigned char *s = s0, *lim = s + strnlen(s0, max); - unsigned char *cp = s; + break; + } + case ENC_ISO_LATIN1: { + unsigned char *s = s0, *lim = s + strnlen(s0, max); + unsigned char *cp = s; - LOCAL_TERM_ERROR( t, 2*(lim-s) ); - while (cp < lim) { - utf8proc_int32_t chr; - cp = getChar(cp, &chr); - HR[0] = MkIntTerm(chr); - HR[1] = AbsPair(HR+2); - HR += 2; - sz++; - if (sz == max) break; - } - break; + LOCAL_TERM_ERROR(t, 2 * (lim - s)); + while (cp < lim) { + utf8proc_int32_t chr; + cp = getChar(cp, &chr); + HR[0] = MkIntTerm(chr); + HR[1] = AbsPair(HR + 2); + HR += 2; + sz++; + if (sz == max) + break; } - case ENC_WCHAR: - { wchar_t *s = s0, *lim = s + wcsnlen(s, max); - wchar_t *cp = s; + break; + } + case ENC_WCHAR: { + wchar_t *s = s0, *lim = s + wcsnlen(s, max); + wchar_t *cp = s; - LOCAL_TERM_ERROR( t, 2*(lim-s) ); - while (cp < lim) { - utf8proc_int32_t chr; - cp = getWchar(cp, &chr); - HR[0] = MkIntTerm(chr); - HR[1] = AbsPair(HR+2); - HR += 2; - sz++; - if (sz == max) break; - } - break; + LOCAL_TERM_ERROR(t, 2 * (lim - s)); + while (cp < lim) { + utf8proc_int32_t chr; + cp = getWchar(cp, &chr); + HR[0] = MkIntTerm(chr); + HR[1] = AbsPair(HR + 2); + HR += 2; + sz++; + if (sz == max) + break; } + break; + } default: - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc), __FUNCTION__); + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", + enc_name(enc), __FUNCTION__); } while (sz < min) { HR[0] = MkIntTerm(MkIntTerm(0)); - HR[1] = AbsPair(HR+2); + HR[1] = AbsPair(HR + 2); HR += 2; sz++; } if (out->type & YAP_STRING_DIFF) { - if (sz == 0) t = out->dif; - else HR[-1] = Globalize(out->dif PASS_REGS); + if (sz == 0) + t = out->dif; + else + HR[-1] = Globalize(out->dif PASS_REGS); } else { - if (sz == 0) t = TermNil; - else HR[-1] = TermNil; + if (sz == 0) + t = TermNil; + else + HR[-1] = TermNil; } out->val.t = t; return (t); } - -static Atom -write_atom( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng USES_REGS) -{ +static Atom write_atom(void *s0, seq_tv_t *out, encoding_t enc, int minimal, + size_t leng USES_REGS) { size_t max = leng; - if (out->type & (YAP_STRING_NCHARS|YAP_STRING_TRUNC)) { - if (out->type & YAP_STRING_TRUNC && out->max < max) max = out->max; + if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) { + if (out->type & YAP_STRING_TRUNC && out->max < max) + max = out->max; } switch (enc) { - case ENC_ISO_UTF8: - { unsigned char *s = s0, *lim = s + strnlen(s0, max); - wchar_t *buf = malloc(sizeof(wchar_t)*((lim+2)-s)), *ptr = buf; - Atom at; + case ENC_ISO_UTF8: { + unsigned char *s = s0, *lim = s + strnlen(s0, max); + wchar_t *buf = malloc(sizeof(wchar_t) * ((lim + 2) - s)), *ptr = buf; + Atom at; - while (*s && s < lim) { - utf8proc_int32_t chr; - s += get_utf8(s,-1, &chr); - *ptr++ = chr; - } - *ptr++ = '\0'; - at = Yap_LookupMaybeWideAtomWithLength( buf, max ); - free( buf ); - out->val.a = at; - return at; + while (*s && s < lim) { + utf8proc_int32_t chr; + s += get_utf8(s, -1, &chr); + *ptr++ = chr; } - case ENC_ISO_LATIN1: - { char *s = s0; - Atom at; + *ptr++ = '\0'; + at = Yap_LookupMaybeWideAtomWithLength(buf, max); + free(buf); + out->val.a = at; + return at; + } + case ENC_ISO_LATIN1: { + char *s = s0; + Atom at; - max = strnlen(s, max); - at = Yap_LookupAtomWithLength(s, max); - out->val.a = at; - return at; - } - case ENC_WCHAR: - { wchar_t *s = s0; - Atom at; + max = strnlen(s, max); + at = Yap_LookupAtomWithLength(s, max); + out->val.a = at; + return at; + } + case ENC_WCHAR: { + wchar_t *s = s0; + Atom at; - max = wcsnlen(s, max); - out->val.a = at = Yap_LookupMaybeWideAtomWithLength(s, max); - return at; - } - default: - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc)); + max = wcsnlen(s, max); + out->val.a = at = Yap_LookupMaybeWideAtomWithLength(s, max); + return at; + } + default: + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", + enc_name(enc)); } return NULL; } - -static size_t -write_wbuffer( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng USES_REGS) -{ +static size_t write_wbuffer(void *s0, seq_tv_t *out, encoding_t enc, + int minimal, size_t leng USES_REGS) { size_t min = 0, max = leng, sz_end, sz; out->enc = ENC_WCHAR; - if (out->type & (YAP_STRING_NCHARS|YAP_STRING_TRUNC)) { - if (out->type & YAP_STRING_NCHARS) min = out->sz; - if (out->type & YAP_STRING_TRUNC && out->max < max) max = out->max; + if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) { + if (out->type & YAP_STRING_NCHARS) + min = out->sz; + if (out->type & YAP_STRING_TRUNC && out->max < max) + max = out->max; } - if (out->enc != enc || out->type & (YAP_STRING_WITH_BUFFER|YAP_STRING_MALLOC)) { + if (out->enc != enc || + out->type & (YAP_STRING_WITH_BUFFER | YAP_STRING_MALLOC)) { if (enc != ENC_WCHAR) { - sz = strlen((char *)s0)+1; + sz = strlen((char *)s0) + 1; } else { - sz = wcslen((wchar_t *)s0)+1; + sz = wcslen((wchar_t *)s0) + 1; } - if (sz < min) sz = min; + if (sz < min) + sz = min; sz *= sizeof(wchar_t); if (out->type & (YAP_STRING_MALLOC)) { out->val.w = malloc(sz); } else if (!(out->type & (YAP_STRING_WITH_BUFFER))) { - if (ASP-(sz/sizeof(CELL)+1) > HR+1024) { - out->val.w = (wchar_t *)(ASP-((sz*sizeof(wchar_t *)/sizeof(CELL)+1))); + if (ASP - (sz / sizeof(CELL) + 1) > HR + 1024) { + out->val.w = + (wchar_t *)(ASP - ((sz * sizeof(wchar_t *) / sizeof(CELL) + 1))); } else - return -1; + return -1; } } else { out->val.w = s0; - sz_end = (wcslen( s0 )+1)*sizeof(wchar_t); + sz_end = (wcslen(s0) + 1) * sizeof(wchar_t); } if (out->enc == ENC_WCHAR) { switch (enc) { case ENC_WCHAR: - if (out->type & (YAP_STRING_WITH_BUFFER|YAP_STRING_MALLOC) ) { - wchar_t *s = s0; - size_t n = wcslen( s )+1; - if (n < min) n = min; - memcpy( out->val.c, s0, n*sizeof(wchar_t)); - out->val.w[n] = '\0'; - sz_end = n+1; - } - case ENC_ISO_UTF8: - { - unsigned char *s = s0, *lim = s + (max = strnlen(s0, max)); - unsigned char *cp = s; - wchar_t *buf0, *buf; - - buf = buf0 = out->val.w; - if (!buf) - return -1; - while (*cp && cp < lim) { - utf8proc_int32_t chr; - cp += get_utf8(cp, -1, &chr); - *buf++ = chr; - } - if (max >= min) *buf++ = '\0'; - else while (max < min) { - utf8proc_int32_t chr; - max++; - cp += get_utf8(cp, -1, &chr); - *buf++ = chr; - } - *buf = '\0'; - sz_end = (buf-buf0)+1; - } - break; - case ENC_ISO_LATIN1: - { - char *s = s0; - size_t n = strlen( s ), i; - if (n < min) n = min; - for (i = 0; i < n; i++) - out->val.w[i] = s[i]; - out->val.w[n] = '\0'; - sz_end = n+1; + if (out->type & (YAP_STRING_WITH_BUFFER | YAP_STRING_MALLOC)) { + wchar_t *s = s0; + size_t n = wcslen(s) + 1; + if (n < min) + n = min; + memcpy(out->val.c, s0, n * sizeof(wchar_t)); + out->val.w[n] = '\0'; + sz_end = n + 1; } - break; - default: - sz_end = -1; - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc), __FUNCTION__); + case ENC_ISO_UTF8: { + unsigned char *s = s0, *lim = s + (max = strnlen(s0, max)); + unsigned char *cp = s; + wchar_t *buf0, *buf; + + buf = buf0 = out->val.w; + if (!buf) + return -1; + while (*cp && cp < lim) { + utf8proc_int32_t chr; + cp += get_utf8(cp, -1, &chr); + *buf++ = chr; + } + if (max >= min) + *buf++ = '\0'; + else + while (max < min) { + utf8proc_int32_t chr; + max++; + cp += get_utf8(cp, -1, &chr); + *buf++ = chr; + } + *buf = '\0'; + sz_end = (buf - buf0) + 1; + } break; + case ENC_ISO_LATIN1: { + char *s = s0; + size_t n = strlen(s), i; + if (n < min) + n = min; + for (i = 0; i < n; i++) + out->val.w[i] = s[i]; + out->val.w[n] = '\0'; + sz_end = n + 1; + } break; + default: + sz_end = -1; + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", + enc_name(enc), __FUNCTION__); } } - sz_end *= sizeof( wchar_t ); - if (out->type & (YAP_STRING_MALLOC)) { - out->val.c = realloc(out->val.c,sz_end); - } - out->sz = sz_end; + sz_end *= sizeof(wchar_t); + if (out->type & (YAP_STRING_MALLOC)) { + out->val.c = realloc(out->val.c, sz_end); + } + out->sz = sz_end; return sz_end; - } +} - - size_t -write_buffer( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng USES_REGS) -{ +size_t write_buffer(void *s0, seq_tv_t *out, encoding_t enc, int minimal, + size_t leng USES_REGS) { size_t min = 0, max = leng, sz_end; - if (out->type & (YAP_STRING_NCHARS|YAP_STRING_TRUNC)) { - if (out->type & YAP_STRING_NCHARS) min = out->sz; - if (out->type & YAP_STRING_TRUNC && out->max < max) max = out->max; + if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) { + if (out->type & YAP_STRING_NCHARS) + min = out->sz; + if (out->type & YAP_STRING_TRUNC && out->max < max) + max = out->max; } if (out->enc != enc) { size_t sz; if (enc != ENC_WCHAR) - sz = strlen((char *)s0)+1; + sz = strlen((char *)s0) + 1; else - sz = wcslen((wchar_t *)s0)+1; - if (sz < min) sz = min; - if (!minimal) sz *= 4; + sz = wcslen((wchar_t *)s0) + 1; + if (sz < min) + sz = min; + if (!minimal) + sz *= 4; if (out->type & (YAP_STRING_MALLOC)) { out->val.c = malloc(sz); } else if (!(out->type & (YAP_STRING_WITH_BUFFER))) { - if (ASP-(sz/sizeof(CELL)+1) > HR+1024) { - out->val.c = Yap_PreAllocCodeSpace(); + if (ASP - (sz / sizeof(CELL) + 1) > HR + 1024) { + out->val.c = Yap_PreAllocCodeSpace(); } } } else { @@ -810,397 +844,407 @@ write_buffer( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng } if (out->enc == ENC_ISO_UTF8) { switch (enc) { - case ENC_ISO_UTF8: - if (out->type & (YAP_STRING_WITH_BUFFER|YAP_STRING_MALLOC) ) { - char *s = s0; - size_t n = strlen( s )+1; - out->val.c[n] = '\0'; - sz_end = n+1; - } else { - sz_end = strlen(out->val.c)+1; - } - break; - case ENC_ISO_LATIN1: - { - unsigned char *s = s0, *lim = s + (max = strnlen(s0, max)); - unsigned char *cp = s, *buf0, *buf; - - buf = buf0 = s0; - if (!buf) - return -1; - while (*cp && cp < lim) { - utf8proc_int32_t chr; - chr = *cp++; - buf += put_utf8(buf, chr); - } - if (max >= min) *buf++ = '\0'; - else while (max < min) { - max++; - utf8proc_int32_t chr; - chr = *cp++; - buf += put_utf8(buf, chr); - } - buf[0] = '\0'; - sz_end = (buf+1)-buf0; - } - break; - case ENC_WCHAR: - { - wchar_t *s = s0; - unsigned char *buf = out->val.uc; - size_t n = wcslen( s ), i; - if (n < min) n = min; - for (i = 0; i < n; i++) { - utf8proc_int32_t chr = s[i]; - buf += put_utf8(buf, chr); - } - *buf++ = '\0'; - sz_end = (buf+1)-out->val.uc; - } - break; - default: - sz_end = -1; - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc), __FUNCTION__); - } - }else if (out->enc == ENC_ISO_LATIN1) { - switch (enc) { - case ENC_ISO_LATIN1: - if (out->type & YAP_STRING_WITH_BUFFER) { - char *s = s0; - size_t n = strlen( s ), i; - if (n < min) n = min; - memcpy( out->val.c, s0, n); - for (i = 0; i < n; i++) - out->val.w[i] = s[i]; - out->val.w[n] = '\0'; - sz_end = (n+1)*sizeof(wchar_t); - } else { - sz_end = strlen( out->val.c ) + 1; - } - break; case ENC_ISO_UTF8: - { - unsigned char *s = s0, *lim = s + (max = strnlen(s0, max)); - unsigned char *cp = s; - unsigned char *buf0, *buf; - - buf = buf0 = out->val.uc; - if (!buf) - return -1; - while (*cp && cp < lim) { - utf8proc_int32_t chr; - cp += get_utf8(cp, -1, &chr); - *buf++ = chr; - } - if (max >= min) *buf++ = '\0'; - else while (max < min) { - utf8proc_int32_t chr; - max++; - cp += get_utf8(cp, -1, &chr); - *buf++ = chr; - } - sz_end = buf-out->val.uc; - } - break; - case ENC_WCHAR: - { - wchar_t *s = s0; - size_t n = wcslen( s ), i; - if (n < min) n = min; - for (i = 0; i < n; i++) - out->val.c[i] = s[i]; - out->val.c[n] = '\0'; - sz_end = n+1; + if (out->type & (YAP_STRING_WITH_BUFFER | YAP_STRING_MALLOC)) { + char *s = s0; + size_t n = strlen(s) + 1; + out->val.c[n] = '\0'; + sz_end = n + 1; + } else { + sz_end = strlen(out->val.c) + 1; } break; - default: - sz_end = -1; - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc), __FUNCTION__); + case ENC_ISO_LATIN1: { + unsigned char *s = s0, *lim = s + (max = strnlen(s0, max)); + unsigned char *cp = s, *buf0, *buf; + + buf = buf0 = s0; + if (!buf) + return -1; + while (*cp && cp < lim) { + utf8proc_int32_t chr; + chr = *cp++; + buf += put_utf8(buf, chr); } + if (max >= min) + *buf++ = '\0'; + else + while (max < min) { + max++; + utf8proc_int32_t chr; + chr = *cp++; + buf += put_utf8(buf, chr); + } + buf[0] = '\0'; + sz_end = (buf + 1) - buf0; + } break; + case ENC_WCHAR: { + wchar_t *s = s0; + unsigned char *buf = out->val.uc; + size_t n = wcslen(s), i; + if (n < min) + n = min; + for (i = 0; i < n; i++) { + utf8proc_int32_t chr = s[i]; + buf += put_utf8(buf, chr); + } + *buf++ = '\0'; + sz_end = (buf + 1) - out->val.uc; + } break; + default: + sz_end = -1; + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", + enc_name(enc), __FUNCTION__); + } + } else if (out->enc == ENC_ISO_LATIN1) { + switch (enc) { + case ENC_ISO_LATIN1: + if (out->type & YAP_STRING_WITH_BUFFER) { + char *s = s0; + size_t n = strlen(s), i; + if (n < min) + n = min; + memcpy(out->val.c, s0, n); + for (i = 0; i < n; i++) + out->val.w[i] = s[i]; + out->val.w[n] = '\0'; + sz_end = (n + 1) * sizeof(wchar_t); + } else { + sz_end = strlen(out->val.c) + 1; + } + break; + case ENC_ISO_UTF8: { + unsigned char *s = s0, *lim = s + (max = strnlen(s0, max)); + unsigned char *cp = s; + unsigned char *buf0, *buf; + + buf = buf0 = out->val.uc; + if (!buf) + return -1; + while (*cp && cp < lim) { + utf8proc_int32_t chr; + cp += get_utf8(cp, -1, &chr); + *buf++ = chr; + } + if (max >= min) + *buf++ = '\0'; + else + while (max < min) { + utf8proc_int32_t chr; + max++; + cp += get_utf8(cp, -1, &chr); + *buf++ = chr; + } + sz_end = buf - out->val.uc; + } break; + case ENC_WCHAR: { + wchar_t *s = s0; + size_t n = wcslen(s), i; + if (n < min) + n = min; + for (i = 0; i < n; i++) + out->val.c[i] = s[i]; + out->val.c[n] = '\0'; + sz_end = n + 1; + } break; + default: + sz_end = -1; + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", + enc_name(enc), __FUNCTION__); + } } else { // no other encodings are supported. sz_end = -1; } - if (out->type & (YAP_STRING_MALLOC)) { - out->val.c = realloc(out->val.c,sz_end); - } - out->sz = sz_end; + if (out->type & (YAP_STRING_MALLOC)) { + out->val.c = realloc(out->val.c, sz_end); + } + out->sz = sz_end; return sz_end; } - - -static size_t -write_length( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng USES_REGS) -{ +static size_t write_length(void *s0, seq_tv_t *out, encoding_t enc, int minimal, + size_t leng USES_REGS) { size_t max = -1; - if (out->type & (YAP_STRING_NCHARS|YAP_STRING_TRUNC)) { - if (out->type & YAP_STRING_NCHARS && out->sz != (size_t)-1) return out->sz; - if (out->type & YAP_STRING_TRUNC) max = out->max; + if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) { + if (out->type & YAP_STRING_NCHARS && out->sz != (size_t)-1) + return out->sz; + if (out->type & YAP_STRING_TRUNC) + max = out->max; } switch (enc) { - case ENC_ISO_UTF8: - { - const unsigned char *s = s0; - return strlen_utf8(s); - } - case ENC_ISO_LATIN1: - { - const char *s = s0; - return strnlen(s, max); - } - case ENC_WCHAR: - { - const wchar_t *s = s0; - return wcsnlen(s, max); - } + case ENC_ISO_UTF8: { + const unsigned char *s = s0; + return strlen_utf8(s); + } + case ENC_ISO_LATIN1: { + const char *s = s0; + return strnlen(s, max); + } + case ENC_WCHAR: { + const wchar_t *s = s0; + return wcsnlen(s, max); + } default: - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc), __FUNCTION__); + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", + enc_name(enc), __FUNCTION__); } return (size_t)-1; } -static Term -write_number( void *s0, seq_tv_t *out, encoding_t enc, int minimal, int size USES_REGS) -{ - return - Yap_StringToNumberTerm(s0, &enc); +static Term write_number(void *s0, seq_tv_t *out, encoding_t enc, int minimal, + int size USES_REGS) { + return Yap_StringToNumberTerm(s0, &enc); } -static Term -string_to_term( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng USES_REGS) -{ - Term o = out->val.t = Yap_StringToTerm(s0, strlen(s0)+1, &enc, GLOBAL_MaxPriority, NULL); - return o; +static Term string_to_term(void *s0, seq_tv_t *out, encoding_t enc, int minimal, + size_t leng USES_REGS) { + Term o = out->val.t = + Yap_StringToTerm(s0, strlen(s0) + 1, &enc, GLOBAL_MaxPriority, NULL); + return o; } - -int -write_Text( void *inp, seq_tv_t *out, encoding_t enc, int minimal, size_t leng USES_REGS) -{ +int write_Text(void *inp, seq_tv_t *out, encoding_t enc, int minimal, + size_t leng USES_REGS) { /* we know what the term is */ - switch (out->type & YAP_TYPE_MASK) { + switch (out->type & YAP_TYPE_MASK) { case YAP_STRING_STRING: - out->val.t = - write_strings( inp, out, enc, minimal, leng PASS_REGS); + out->val.t = write_strings(inp, out, enc, minimal, leng PASS_REGS); return out->val.t != 0; case YAP_STRING_ATOMS: - out->val.t = - write_atoms( inp, out, enc, minimal, leng PASS_REGS); + out->val.t = write_atoms(inp, out, enc, minimal, leng PASS_REGS); return out->val.t != 0; case YAP_STRING_CODES: - out->val.t = - write_codes( inp, out, enc, minimal, leng PASS_REGS); + out->val.t = write_codes(inp, out, enc, minimal, leng PASS_REGS); return out->val.t != 0; case YAP_STRING_LENGTH: - out->val.l = - write_length( inp, out, enc, minimal, leng PASS_REGS); + out->val.l = write_length(inp, out, enc, minimal, leng PASS_REGS); return out->val.l != (size_t)(-1); case YAP_STRING_ATOM: - out->val.a = - write_atom( inp, out, enc, minimal, leng PASS_REGS); + out->val.a = write_atom(inp, out, enc, minimal, leng PASS_REGS); return out->val.a != NULL; - case YAP_STRING_INT|YAP_STRING_FLOAT|YAP_STRING_BIG: - out->val.t = - write_number( inp, out, enc, minimal, leng PASS_REGS); + case YAP_STRING_INT | YAP_STRING_FLOAT | YAP_STRING_BIG: + out->val.t = write_number(inp, out, enc, minimal, leng PASS_REGS); return out->val.t != 0; - case YAP_STRING_CHARS: - { - size_t sz = write_buffer( inp, out, enc, minimal, leng PASS_REGS); - return((Int)sz > 0); - } - case YAP_STRING_WCHARS: - { - size_t sz = write_wbuffer( inp, out, enc, minimal, leng PASS_REGS); - return((Int)sz > 0); - } - default: + case YAP_STRING_CHARS: { + size_t sz = write_buffer(inp, out, enc, minimal, leng PASS_REGS); + return ((Int)sz > 0); + } + case YAP_STRING_WCHARS: { + size_t sz = write_wbuffer(inp, out, enc, minimal, leng PASS_REGS); + return ((Int)sz > 0); + } + default: if (!(out->type & YAP_STRING_TERM)) return 0; - if (out->type & (YAP_STRING_INT|YAP_STRING_FLOAT|YAP_STRING_BIG)) - if ((out->val.t = - write_number( inp, out, enc, minimal, leng PASS_REGS)) != 0L) - return out->val.t != 0; + if (out->type & (YAP_STRING_INT | YAP_STRING_FLOAT | YAP_STRING_BIG)) + if ((out->val.t = write_number(inp, out, enc, minimal, leng PASS_REGS)) != + 0L) + return out->val.t != 0; if (out->type & (YAP_STRING_ATOM)) - if (write_atom( inp, out, enc, minimal, leng PASS_REGS) != NIL) { - Atom at = out->val.a; - if (at != NIL) - out->val.t = MkAtomTerm(at); - return at != NIL; + if (write_atom(inp, out, enc, minimal, leng PASS_REGS) != NIL) { + Atom at = out->val.a; + if (at != NIL) + out->val.t = MkAtomTerm(at); + return at != NIL; } - if ((out->val.t = - string_to_term( inp, out, enc, minimal, leng PASS_REGS)) != 0L) - return out->val.t != 0; - } + if ((out->val.t = string_to_term(inp, out, enc, minimal, leng PASS_REGS)) != + 0L) + return out->val.t != 0; + } return FALSE; } - int -Yap_CVT_Text( seq_tv_t *inp, seq_tv_t *out USES_REGS) -{ +int Yap_CVT_Text(seq_tv_t *inp, seq_tv_t *out USES_REGS) { encoding_t enc; int minimal = FALSE; char *buf; size_t leng; - buf = Yap_readText( NULL, inp, &enc, &minimal, &leng PASS_REGS ); + buf = Yap_readText(NULL, inp, &enc, &minimal, &leng PASS_REGS); if (!buf) return 0L; - return write_Text( buf, out, enc, minimal, leng PASS_REGS ); + return write_Text(buf, out, enc, minimal, leng PASS_REGS); } -static void * -compute_end( void *s0, encoding_t enc ) -{ +static void *compute_end(void *s0, encoding_t enc) { switch (enc) { case ENC_ISO_LATIN1: - case ENC_ISO_UTF8: - { - char *s = (char *)s0; - return s+(1+strlen(s)); - } - case ENC_WCHAR: - { - wchar_t *s = (wchar_t *)s0; - return s + (1+wcslen(s)); - } - default: - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc), __FUNCTION__); + case ENC_ISO_UTF8: { + char *s = (char *)s0; + return s + (1 + strlen(s)); + } + case ENC_WCHAR: { + wchar_t *s = (wchar_t *)s0; + return s + (1 + wcslen(s)); + } + default: + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", + enc_name(enc), __FUNCTION__); } return NULL; } -static void * -advance_Text( void *s, int l, encoding_t enc ) -{ +static void *advance_Text(void *s, int l, encoding_t enc) { switch (enc) { case ENC_ISO_LATIN1: - return ((char *)s)+l; - case ENC_ISO_UTF8: - return (char *)skip_utf8(s,l); + return ((char *)s) + l; + case ENC_ISO_UTF8: + return (char *)skip_utf8(s, l); case ENC_WCHAR: - return ((wchar_t *)s)+l; + return ((wchar_t *)s) + l; default: - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc), __FUNCTION__); + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", + enc_name(enc), __FUNCTION__); } return s; } -static int -cmp_Text( void *s1, void *s2, int l, encoding_t enc1, encoding_t enc2 ) -{ +static int cmp_Text(void *s1, void *s2, int l, encoding_t enc1, + encoding_t enc2) { Int i; switch (enc1) { - case ENC_ISO_LATIN1: - { - char *w1 = (char *)s1; - switch (enc2) { - case ENC_ISO_LATIN1: - return strncmp(s1, s2, l); - case ENC_ISO_UTF8: - { - utf8proc_int32_t chr1, chr2; - unsigned char *w2 = s2; - for (i = 0; i < l; i++) { chr1 = *w1++; w2 += get_utf8(w2, -1, &chr2); if (chr1-chr2) return chr1-chr2; } - } - return 0; - case ENC_WCHAR: - { - utf8proc_int32_t chr1, chr2; - wchar_t *w2 = s2; - for (i = 0; i < l; i++) { chr1 = *w1++; chr2 = *w2++; if (chr1-chr2) return chr1-chr2; } - } - return 0; - default: - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc2), __FUNCTION__); - } - } - case ENC_ISO_UTF8: - { - unsigned char *w1 = s1; - switch (enc2) { - case ENC_ISO_LATIN1: - { - utf8proc_int32_t chr1, chr2; - unsigned char *w2 = s2; - for (i = 0; i < l; i++) { chr2 = *w2++; w1 += get_utf8(w1, -1, &chr1); if (chr1-chr2) return chr1-chr2; } - } - return 0; - case ENC_ISO_UTF8: - { - utf8proc_int32_t chr1, chr2; - unsigned char *w2 = s2; - for (i = 0; i < l; i++) { w2 += get_utf8(w2, -1, &chr2); w1 += get_utf8(w1,-1, &chr1); if (chr1-chr2) return chr1-chr2; } - } - return 0; - case ENC_WCHAR: - { - utf8proc_int32_t chr1, chr2; - wchar_t *w2 = s2; - for (i = 0; i < l; i++) { chr2 = *w2++; w1 += get_utf8(w1, -1, &chr1); if (chr1-chr2) return chr1-chr2; } - } - return 0; - default: - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc2), __FUNCTION__); - } - } - case ENC_WCHAR: - { - wchar_t *w1 = (wchar_t *)s1; - switch (enc2) { - case ENC_ISO_LATIN1: - { - utf8proc_int32_t chr1, chr2; - char *w2 = s2; - for (i = 0; i < l; i++) { chr1 = *w1++; chr2 = *w2++; if (chr1-chr2) return chr1-chr2; } - } - return 0; - case ENC_ISO_UTF8: - { - utf8proc_int32_t chr1, chr2; - unsigned char *w2 = s2; - for (i = 0; i < l; i++) { chr1 = *w1++; w2 += get_utf8(w2, -1, &chr2); if (chr1-chr2) return chr1-chr2; } - } - return 0; - case ENC_WCHAR: - return wcsncmp(s1, s2, l); - default: - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc2), __FUNCTION__); + case ENC_ISO_LATIN1: { + char *w1 = (char *)s1; + switch (enc2) { + case ENC_ISO_LATIN1: + return strncmp(s1, s2, l); + case ENC_ISO_UTF8: { + utf8proc_int32_t chr1, chr2; + unsigned char *w2 = s2; + for (i = 0; i < l; i++) { + chr1 = *w1++; + w2 += get_utf8(w2, -1, &chr2); + if (chr1 - chr2) + return chr1 - chr2; } } + return 0; + case ENC_WCHAR: { + utf8proc_int32_t chr1, chr2; + wchar_t *w2 = s2; + for (i = 0; i < l; i++) { + chr1 = *w1++; + chr2 = *w2++; + if (chr1 - chr2) + return chr1 - chr2; + } + } + return 0; default: - Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc1), __FUNCTION__); + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", + enc_name(enc2), __FUNCTION__); + } + } + case ENC_ISO_UTF8: { + unsigned char *w1 = s1; + switch (enc2) { + case ENC_ISO_LATIN1: { + utf8proc_int32_t chr1, chr2; + unsigned char *w2 = s2; + for (i = 0; i < l; i++) { + chr2 = *w2++; + w1 += get_utf8(w1, -1, &chr1); + if (chr1 - chr2) + return chr1 - chr2; + } + } + return 0; + case ENC_ISO_UTF8: { + utf8proc_int32_t chr1, chr2; + unsigned char *w2 = s2; + for (i = 0; i < l; i++) { + w2 += get_utf8(w2, -1, &chr2); + w1 += get_utf8(w1, -1, &chr1); + if (chr1 - chr2) + return chr1 - chr2; + } + } + return 0; + case ENC_WCHAR: { + utf8proc_int32_t chr1, chr2; + wchar_t *w2 = s2; + for (i = 0; i < l; i++) { + chr2 = *w2++; + w1 += get_utf8(w1, -1, &chr1); + if (chr1 - chr2) + return chr1 - chr2; + } + } + return 0; + default: + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", + enc_name(enc2), __FUNCTION__); + } + } + case ENC_WCHAR: { + wchar_t *w1 = (wchar_t *)s1; + switch (enc2) { + case ENC_ISO_LATIN1: { + utf8proc_int32_t chr1, chr2; + char *w2 = s2; + for (i = 0; i < l; i++) { + chr1 = *w1++; + chr2 = *w2++; + if (chr1 - chr2) + return chr1 - chr2; + } + } + return 0; + case ENC_ISO_UTF8: { + utf8proc_int32_t chr1, chr2; + unsigned char *w2 = s2; + for (i = 0; i < l; i++) { + chr1 = *w1++; + w2 += get_utf8(w2, -1, &chr2); + if (chr1 - chr2) + return chr1 - chr2; + } + } + return 0; + case ENC_WCHAR: + return wcsncmp(s1, s2, l); + default: + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", + enc_name(enc2), __FUNCTION__); + } + } + default: + Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", + enc_name(enc1), __FUNCTION__); } return 0; } -static void * -concat( int n, seq_tv_t *out, void *sv[], encoding_t encv[], size_t lengv[] USES_REGS ) -{ +static void *concat(int n, seq_tv_t *out, void *sv[], encoding_t encv[], + size_t lengv[] USES_REGS) { if (out->type == YAP_STRING_STRING) { /* we assume we concatenate strings only, or ASCII stuff like numbers */ - Term t = init_tstring( PASS_REGS1 ); + Term t = init_tstring(PASS_REGS1); unsigned char *buf = buf_from_tstring(HR); int i; for (i = 0; i < n; i++) { if (encv[i] == ENC_WCHAR) { - wchar_t *ptr = sv[i]; - utf8proc_int32_t chr; - while ( (chr = *ptr++) ) buf += put_utf8(buf, chr); + wchar_t *ptr = sv[i]; + utf8proc_int32_t chr; + while ((chr = *ptr++)) + buf += put_utf8(buf, chr); } else if (encv[i] == ENC_ISO_LATIN1) { - char *ptr = sv[i]; - utf8proc_int32_t chr; - while ( (chr = *ptr++) ) buf += put_utf8(buf, chr); + char *ptr = sv[i]; + utf8proc_int32_t chr; + while ((chr = *ptr++)) + buf += put_utf8(buf, chr); } else { - char *ptr = sv[i]; - utf8proc_int32_t chr; - while ( (chr = *ptr++) ) *buf++ = chr; + char *ptr = sv[i]; + utf8proc_int32_t chr; + while ((chr = *ptr++)) + *buf++ = chr; } } - *buf ++ = '\0'; - close_tstring( buf PASS_REGS ); + *buf++ = '\0'; + close_tstring(buf PASS_REGS); out->val.t = t; return HR; } else { @@ -1210,7 +1254,7 @@ concat( int n, seq_tv_t *out, void *sv[], encoding_t encv[], size_t lengv[] USES int i; for (i = 0; i < n; i++) { if (encv[i] != ENC_ISO_LATIN1) { - enc = ENC_WCHAR; + enc = ENC_WCHAR; } sz += write_length(sv[i], out, encv[i], FALSE, lengv[i] PASS_REGS); } @@ -1218,21 +1262,28 @@ concat( int n, seq_tv_t *out, void *sv[], encoding_t encv[], size_t lengv[] USES /* wide atom */ wchar_t *buf = (wchar_t *)HR; Atom at; - LOCAL_ERROR( MkAtomTerm(Yap_LookupWideAtom(buf)), sz+3 ); - for (i = 0; i < n ; i ++) { - if (encv[i] == ENC_WCHAR) { - wchar_t *ptr = sv[i]; - utf8proc_int32_t chr; - while ( (chr = *ptr++) != '\0' ) *buf++ = chr; - } else if (encv[i] == ENC_ISO_LATIN1) { - char *ptr = sv[i]; - utf8proc_int32_t chr; - while ( (chr = *ptr++) != '\0' ) *buf++ = (unsigned char)chr; - } else { - unsigned char *ptr = sv[i]; - utf8proc_int32_t chr; - while ( (ptr += get_utf8( ptr, -1, &chr )) != NULL ) { if (chr == '\0') break; else *buf++ = chr; } - } + LOCAL_ERROR(MkAtomTerm(Yap_LookupWideAtom(buf)), sz + 3); + for (i = 0; i < n; i++) { + if (encv[i] == ENC_WCHAR) { + wchar_t *ptr = sv[i]; + utf8proc_int32_t chr; + while ((chr = *ptr++) != '\0') + *buf++ = chr; + } else if (encv[i] == ENC_ISO_LATIN1) { + char *ptr = sv[i]; + utf8proc_int32_t chr; + while ((chr = *ptr++) != '\0') + *buf++ = (unsigned char)chr; + } else { + unsigned char *ptr = sv[i]; + utf8proc_int32_t chr; + while ((ptr += get_utf8(ptr, -1, &chr)) != NULL) { + if (chr == '\0') + break; + else + *buf++ = chr; + } + } } *buf++ = '\0'; at = out->val.a = Yap_LookupWideAtom((wchar_t *)HR); @@ -1242,11 +1293,12 @@ concat( int n, seq_tv_t *out, void *sv[], encoding_t encv[], size_t lengv[] USES char *buf = (char *)HR; Atom at; - LOCAL_ERROR( MkAtomTerm(Yap_LookupAtom(buf)), sz/sizeof(CELL)+3 ); - for (i = 0; i < n ; i ++) { - char *ptr = sv[i]; - utf8proc_int32_t chr; - while ( (chr = *ptr++) != '\0' ) *buf++ = chr; + LOCAL_ERROR(MkAtomTerm(Yap_LookupAtom(buf)), sz / sizeof(CELL) + 3); + for (i = 0; i < n; i++) { + char *ptr = sv[i]; + utf8proc_int32_t chr; + while ((chr = *ptr++) != '\0') + *buf++ = chr; } *buf++ = '\0'; at = out->val.a = Yap_LookupAtom((const char *)HR); @@ -1256,65 +1308,78 @@ concat( int n, seq_tv_t *out, void *sv[], encoding_t encv[], size_t lengv[] USES return NULL; } -static void * -slice( size_t min, size_t max, void *buf, seq_tv_t *out, encoding_t enc USES_REGS ) -{ +static void *slice(size_t min, size_t max, void *buf, seq_tv_t *out, + encoding_t enc USES_REGS) { if (out->type == YAP_STRING_STRING) { /* we assume we concatenate strings only, or ASCII stuff like numbers */ - Term t = init_tstring( PASS_REGS1 ); + Term t = init_tstring(PASS_REGS1); unsigned char *nbuf = buf_from_tstring(HR); if (enc == ENC_WCHAR) { wchar_t *ptr = (wchar_t *)buf + min; utf8proc_int32_t chr; - while ( min++ < max ) { chr = *ptr++; nbuf += put_utf8(nbuf, chr); } + while (min++ < max) { + chr = *ptr++; + nbuf += put_utf8(nbuf, chr); + } } else if (enc == ENC_ISO_LATIN1) { unsigned char *ptr = (unsigned char *)buf + min; utf8proc_int32_t chr; - while ( min++ < max ) { chr = *ptr++; nbuf += put_utf8(nbuf, chr); } + while (min++ < max) { + chr = *ptr++; + nbuf += put_utf8(nbuf, chr); + } } else { - unsigned char *ptr = skip_utf8 (buf, min ); + unsigned char *ptr = skip_utf8(buf, min); utf8proc_int32_t chr; - if (!ptr) return NULL; - while ( min++ < max ) { ptr += get_utf8(ptr, -1, & chr); nbuf += put_utf8(nbuf, chr); } + if (!ptr) + return NULL; + while (min++ < max) { + ptr += get_utf8(ptr, -1, &chr); + nbuf += put_utf8(nbuf, chr); + } } - *nbuf ++ = '\0'; - close_tstring( nbuf PASS_REGS ); + *nbuf++ = '\0'; + close_tstring(nbuf PASS_REGS); out->val.t = t; return (void *)StringOfTerm(t); } else { Atom at; - /* atom */ + /* atom */ if (enc == ENC_WCHAR) { /* wide atom */ wchar_t *nbuf = (wchar_t *)HR; wchar_t *ptr = (wchar_t *)buf + min; - if (max>min) { - LOCAL_ERROR( MkAtomTerm(Yap_LookupWideAtom(buf)), (max-min)*sizeof(wchar_t) ); - memcpy( nbuf, ptr, (max - min)*sizeof(wchar_t)); + if (max > min) { + LOCAL_ERROR(MkAtomTerm(Yap_LookupWideAtom(buf)), + (max - min) * sizeof(wchar_t)); + memcpy(nbuf, ptr, (max - min) * sizeof(wchar_t)); } - nbuf[max-min] = '\0'; - at = Yap_LookupMaybeWideAtom( nbuf ); + nbuf[max - min] = '\0'; + at = Yap_LookupMaybeWideAtom(nbuf); } else if (enc == ENC_ISO_LATIN1) { /* atom */ char *nbuf = (char *)HR; - if (max>min) { - char *ptr = (char *)buf + min; - LOCAL_ERROR( MkAtomTerm(Yap_LookupAtom(buf)), max-min ); - memcpy( nbuf, ptr, (max - min)); + if (max > min) { + char *ptr = (char *)buf + min; + LOCAL_ERROR(MkAtomTerm(Yap_LookupAtom(buf)), max - min); + memcpy(nbuf, ptr, (max - min)); } - nbuf[max-min] = '\0'; - at = Yap_LookupAtom( nbuf ); + nbuf[max - min] = '\0'; + at = Yap_LookupAtom(nbuf); } else { /* atom */ wchar_t *nbuf = (wchar_t *)HR; - unsigned char *ptr = skip_utf8 ( ( unsigned char *)buf, min ); + unsigned char *ptr = skip_utf8((unsigned char *)buf, min); utf8proc_int32_t chr; - LOCAL_ERROR( MkAtomTerm(Yap_LookupAtom(buf)), max-min ); - while ( min++ < max ) { ptr += get_utf8(ptr, -1, & chr); *nbuf++ = chr; } + LOCAL_ERROR(MkAtomTerm(Yap_LookupAtom(buf)), max - min); + while (min++ < max) { + ptr += get_utf8(ptr, -1, &chr); + *nbuf++ = chr; + } nbuf[0] = '\0'; - at = Yap_LookupMaybeWideAtom( (wchar_t*)HR ); + at = Yap_LookupMaybeWideAtom((wchar_t *)HR); } out->val.a = at; return at->StrOfAE; @@ -1322,35 +1387,33 @@ slice( size_t min, size_t max, void *buf, seq_tv_t *out, encoding_t enc USES_REG return NULL; } - // // Out must be an atom or a string -void * -Yap_Concat_Text( int n, seq_tv_t inp[], seq_tv_t *out USES_REGS) -{ - encoding_t * encv; +void *Yap_Concat_Text(int n, seq_tv_t inp[], seq_tv_t *out USES_REGS) { + encoding_t *encv; void **bufv; int minimal = FALSE; void *buf; size_t leng, *lengv; int i; Term t = ARG1; - bufv = (void **)malloc(n*sizeof(void *)); + bufv = (void **)malloc(n * sizeof(void *)); HEAP_TERM_ERROR(bufv, void *); - encv = (encoding_t *)malloc(n*sizeof(encoding_t)); + encv = (encoding_t *)malloc(n * sizeof(encoding_t)); HEAP_ERROR(encv, encoding_t); buf = NULL; - for (i = 0 ; i < n ; i++) { - void *nbuf = Yap_readText( buf, inp+i, encv+i, &minimal, &leng PASS_REGS ); + for (i = 0; i < n; i++) { + void *nbuf = + Yap_readText(buf, inp + i, encv + i, &minimal, &leng PASS_REGS); if (!nbuf) return 0L; bufv[i] = nbuf; - if ((char *)nbuf >= AuxBase && (char *)nbuf < AuxTop) { - buf = compute_end( nbuf, encv[i] ); + if ((char *)nbuf >= AuxBase && (char *)nbuf < AuxTop) { + buf = compute_end(nbuf, encv[i]); } } - lengv = (size_t *)malloc(n*sizeof(size_t)); + lengv = (size_t *)malloc(n * sizeof(size_t)); HEAP_ERROR(lengv, size_t); buf = concat(n, out, bufv, encv, lengv PASS_REGS); return buf; @@ -1358,27 +1421,25 @@ Yap_Concat_Text( int n, seq_tv_t inp[], seq_tv_t *out USES_REGS) // // out must be an atom or a string -void * -Yap_Splice_Text( int n, size_t cuts[], seq_tv_t *inp, encoding_t encv[], seq_tv_t outv[] USES_REGS) -{ +void *Yap_Splice_Text(int n, size_t cuts[], seq_tv_t *inp, encoding_t encv[], + seq_tv_t outv[] USES_REGS) { encoding_t enc; int minimal = FALSE; void *buf, *store; size_t l, leng; int i, min; - buf = Yap_readText( NULL, inp, &enc, &minimal, &leng PASS_REGS ); + buf = Yap_readText(NULL, inp, &enc, &minimal, &leng PASS_REGS); if (!buf) return NULL; - l = write_length( buf, inp, enc, minimal, leng PASS_REGS); + l = write_length(buf, inp, enc, minimal, leng PASS_REGS); /* where to allocate next is the most complicated part */ - if ((char *)buf >= AuxBase && (char *)buf < AuxTop) { - store = compute_end( buf, enc ); + if ((char *)buf >= AuxBase && (char *)buf < AuxTop) { + store = compute_end(buf, enc); } else { store = NULL; } - if (!cuts) { if (n == 2) { size_t l0, l1; @@ -1388,81 +1449,84 @@ Yap_Splice_Text( int n, size_t cuts[], seq_tv_t *inp, encoding_t encv[], seq_tv void *buf0, *buf1; if (outv[0].val.t) { - buf0 = Yap_readText( store, outv, &enc0, &minimal0, &leng0 PASS_REGS ); - if (!buf0) - return NULL; - l0 = write_length( buf0, outv, enc, minimal0, leng0 PASS_REGS); - if (cmp_Text( buf, buf0, l0, enc, enc0) != 0) - return NULL; + buf0 = Yap_readText(store, outv, &enc0, &minimal0, &leng0 PASS_REGS); + if (!buf0) + return NULL; + l0 = write_length(buf0, outv, enc, minimal0, leng0 PASS_REGS); + if (cmp_Text(buf, buf0, l0, enc, enc0) != 0) + return NULL; - l1 = l-l0; + l1 = l - l0; - buf1 = slice(l0, l, buf, outv+1, enc PASS_REGS); - if (encv) - encv[1] = enc; - return buf1; + buf1 = slice(l0, l, buf, outv + 1, enc PASS_REGS); + if (encv) + encv[1] = enc; + return buf1; } else /* if (outv[1].val.t) */ { - buf1 = Yap_readText( store, outv+1, &enc1, &minimal1, &leng1 PASS_REGS ); - if (!buf1) - return NULL; - l1 = write_length( buf1, outv+1, enc1, minimal1, leng1 PASS_REGS); - if (l < l1) return NULL; - l0 = l-l1; - if (cmp_Text( advance_Text(buf, l0, enc), buf1, l1, enc, enc1) != 0) - return NULL; - buf0 = slice(0, l0, buf, outv, enc PASS_REGS); - if (encv) - encv[0] = enc; - return buf0; + buf1 = + Yap_readText(store, outv + 1, &enc1, &minimal1, &leng1 PASS_REGS); + if (!buf1) + return NULL; + l1 = write_length(buf1, outv + 1, enc1, minimal1, leng1 PASS_REGS); + if (l < l1) + return NULL; + l0 = l - l1; + if (cmp_Text(advance_Text(buf, l0, enc), buf1, l1, enc, enc1) != 0) + return NULL; + buf0 = slice(0, l0, buf, outv, enc PASS_REGS); + if (encv) + encv[0] = enc; + return buf0; } } } for (i = 0; i < n; i++) { - if (i == 0) min = 0; - else min = cuts[i-1]; - slice(min, cuts[i], buf, outv+i, enc PASS_REGS); + if (i == 0) + min = 0; + else + min = cuts[i - 1]; + slice(min, cuts[i], buf, outv + i, enc PASS_REGS); if (!(outv[i].val.a)) return NULL; if (encv) encv[i] = enc; } - return (void *)outv;; + return (void *)outv; + ; } -/** - * Function to convert a generic text term (string, atom, list of codes, list of atoms) into a buff +/** + * Function to convert a generic text term (string, atom, list of codes, list of +atoms) into a buff er. - * + * * @param t the term - * @param buf the buffer, if NULL a buffer is malloced, and the user should reclai it + * @param buf the buffer, if NULL a buffer is malloced, and the user should +reclai it * @param len buffer size * @param enc encoding (UTF-8 is strongly recommended) - * - * @return the buffer, or NULL in case of failure. If so, Yap_Error may be called. + * + * @return the buffer, or NULL in case of failure. If so, Yap_Error may be +called. */ -const char * -Yap_TextTermToText(Term t, char *buf, size_t len, encoding_t enc) -{ - CACHE_REGS - seq_tv_t inp, out; +const char *Yap_TextTermToText(Term t, char *buf, size_t len, encoding_t enc) { + CACHE_REGS + seq_tv_t inp, out; - inp.val.t = t; - if (IsAtomTerm(t)) { - inp.type = YAP_STRING_ATOM; - if (IsWideAtom(AtomOfTerm(t))) - inp.enc = ENC_WCHAR; - else - inp.enc = ENC_ISO_LATIN1; - } - else if (IsStringTerm(t)) { - inp.type = YAP_STRING_STRING; - inp.enc = ENC_ISO_UTF8; -} -else if (IsPairTerm(t)) { - inp.type = (YAP_STRING_CODES | YAP_STRING_ATOMS); - } - else { - Yap_Error(TYPE_ERROR_TEXT, t, NULL); + inp.val.t = t; + if (IsAtomTerm(t)) { + inp.type = YAP_STRING_ATOM; + if (IsWideAtom(AtomOfTerm(t))) + inp.enc = ENC_WCHAR; + else + inp.enc = ENC_ISO_LATIN1; + } else if (IsStringTerm(t)) { + inp.type = YAP_STRING_STRING; + inp.enc = ENC_ISO_UTF8; + } else if (IsPairTerm(t)) { + inp.type = (YAP_STRING_CODES | YAP_STRING_ATOMS); + } else { + Yap_Error(TYPE_ERROR_TEXT, t, NULL); return false; } out.enc = enc; @@ -1477,23 +1541,23 @@ else if (IsPairTerm(t)) { return out.val.c; } - -/** - * Convert from a text buffer (8-bit) to a term that has the same type as _Tguide_ - * +/** + * Convert from a text buffer (8-bit) to a term that has the same type as + * _Tguide_ + * * @param s the buffer * @param tguide the guide - * + * * @return the term */ -Term Yap_MkTextTerm(const char *s, encoding_t enc, Term tguide ) { -CACHE_REGS +Term Yap_MkTextTerm(const char *s, encoding_t enc, Term tguide) { + CACHE_REGS if (IsAtomTerm(tguide)) return MkAtomTerm(Yap_LookupAtom(s)); - if (IsStringTerm(tguide)) + if (IsStringTerm(tguide)) return MkStringTerm(s); - if (IsPairTerm(tguide) && IsAtomTerm(HeadOfTerm(tguide))) { - return Yap_CharsToListOfAtoms( s, enc PASS_REGS ); - } - return Yap_CharsToListOfCodes( s, enc PASS_REGS ); + if (IsPairTerm(tguide) && IsAtomTerm(HeadOfTerm(tguide))) { + return Yap_CharsToListOfAtoms(s, enc PASS_REGS); + } + return Yap_CharsToListOfCodes(s, enc PASS_REGS); } diff --git a/C/write.c b/C/write.c index 4f4b6dda9..2be3529af 100644 --- a/C/write.c +++ b/C/write.c @@ -164,7 +164,6 @@ static void protect_close_number(struct write_globs *wglb, int used_bracket) { 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 */ @@ -389,8 +388,8 @@ int Yap_FormatFloat(Float f, char **s, size_t sz) { int sno; char *so; - sno = Yap_open_buf_write_stream( - *s, sz, &GLOBAL_Stream[LOCAL_c_output_stream].encoding, 0); + sno = Yap_open_buf_write_stream(GLOBAL_Stream[LOCAL_c_output_stream].encoding, + 0); if (sno < 0) return FALSE; wglb.lw = separator; @@ -1272,24 +1271,24 @@ void Yap_plwrite(Term t, StreamDesc *mywrite, int max_depth, int flags, Yap_CloseSlots(sls); } -char *Yap_TermToString(Term t, char *s, size_t sz, size_t *length, - encoding_t *encp, int flags) { +char *Yap_TermToString(Term t, size_t *lengthp, encoding_t enc, int flags) { CACHE_REGS - int sno = Yap_open_buf_write_stream(s, sz, encp, flags); + int sno = Yap_open_buf_write_stream(enc, flags); int old_output_stream = LOCAL_c_output_stream; + const char *sf; if (sno < 0) return NULL; LOCAL_c_output_stream = sno; - if (encp) - GLOBAL_Stream[sno].encoding = *encp; + if (enc) + GLOBAL_Stream[sno].encoding = enc; else GLOBAL_Stream[sno].encoding = LOCAL_encoding; Yap_plwrite(t, GLOBAL_Stream + sno, 0, flags, GLOBAL_MaxPriority); - s = Yap_MemExportStreamPtr(sno); + sf = Yap_MemExportStreamPtr(sno); Yap_CloseStream(sno); LOCAL_c_output_stream = old_output_stream; if (Yap_HasException()) return NULL; - return s; + return (char *)sf; } diff --git a/CXX/yapi.cpp b/CXX/yapi.cpp index edd0657fb..a5c6fe2cf 100644 --- a/CXX/yapi.cpp +++ b/CXX/yapi.cpp @@ -8,8 +8,8 @@ extern "C" { #include "YapInterface.h" #include "blobs.h" -X_API char *Yap_TermToString(Term t, char *s, size_t sz, size_t *length, - encoding_t *encodingp, int flags); +X_API char *Yap_TermToString(Term t, size_t *length, encoding_t encodingp, + int flags); X_API void YAP_UserCPredicate(const char *, YAP_UserCPred, YAP_Arity arity); X_API void YAP_UserCPredicateWithArgs(const char *, YAP_UserCPred, YAP_Arity, @@ -297,14 +297,14 @@ intptr_t YAPTerm::hashTerm(size_t sz, size_t depth, bool variant) { const char *YAPTerm::text() { CACHE_REGS - size_t sze = 4096, length; - char *os = new char[4097]; + size_t length; encoding_t enc = LOCAL_encoding; + char *os; BACKUP_MACHINE_REGS(); - if (!(os = Yap_TermToString(Yap_GetFromSlot(t), os, sze, &length, &enc, 0))) { + if (!(os = Yap_TermToString(Yap_GetFromSlot(t), &length, enc, 0))) { RECOVER_MACHINE_REGS(); - return (char *)NULL; + return nullptr; } RECOVER_MACHINE_REGS(); return os; diff --git a/include/YapInterface.h b/include/YapInterface.h index 5f7b51600..18d6a6c70 100755 --- a/include/YapInterface.h +++ b/include/YapInterface.h @@ -1902,11 +1902,11 @@ extern X_API int YAP_Reset(yap_reset_t reset); extern X_API void YAP_Error(int myerrno, YAP_Term t, const char *buf, ...); -extern X_API int YAP_WriteBuffer(YAP_Term, char *, size_t, int); +extern X_API char *YAP_WriteBuffer(YAP_Term, char *, size_t, int); -extern X_API char *YAP_WriteDynamicBuffer(YAP_Term t, char *buf, size_t sze, - size_t *lengthp, YAP_encoding_t *encp, - int flags); +extern X_API int YAP_WriteDynamicBuffer(YAP_Term t, char *buf, size_t sze, + size_t *lengthp, YAP_encoding_t enc, + int flags); /* void YAP_Term(YAP_Term) */ extern X_API YAP_Term YAP_CopyTerm(YAP_Term); diff --git a/os/mem.c b/os/mem.c index 3667477be..b707aabd6 100644 --- a/os/mem.c +++ b/os/mem.c @@ -110,7 +110,7 @@ static int MemPutc(int sno, int ch) { #endif -int Yap_open_buf_read_stream(const char *nbuf, size_t nchars, encoding_t *encp, +int Yap_open_buf_read_stream(const char *buf, size_t nchars, encoding_t *encp, memBufSource src) { CACHE_REGS int sno; @@ -130,10 +130,10 @@ int Yap_open_buf_read_stream(const char *nbuf, size_t nchars, encoding_t *encp, encoding = LOCAL_encoding; #if MAY_READ // like any file stream. - st->file = f = fmemopen((void *)nbuf, nchars, "r"); + st->file = f = fmemopen((void *)buf, nchars, "r"); flags = Input_Stream_f | InMemory_Stream_f | Seekable_Stream_f; #else - st->file = f = NULL; + st->file = f = NULL; flags = Input_Stream_f | InMemory_Stream_f; #endif Yap_initStream(sno, f, NULL, TermNil, encoding, flags, AtomRead); @@ -142,7 +142,7 @@ int Yap_open_buf_read_stream(const char *nbuf, size_t nchars, encoding_t *encp, /* 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.buf = (char *)buf; st->u.mem_string.max_size = nchars; st->u.mem_string.error_handler = NULL; st->u.mem_string.src = src; @@ -158,7 +158,7 @@ static Int Term t, ti; int sno; Int sl = 0, nchars = 0; - char *nbuf; + char *buf; ti = Deref(ARG1); while (ti != TermNil) { @@ -173,7 +173,7 @@ static Int ti = TailOfTerm(ti); } } - while ((nbuf = (char *)Yap_AllocAtomSpace((sl + 1) * sizeof(char))) == NULL) { + while ((buf = (char *)Yap_AllocAtomSpace((sl + 1) * sizeof(char))) == NULL) { if (!Yap_growheap(FALSE, (sl + 1) * sizeof(char), NULL)) { Yap_Error(RESOURCE_ERROR_HEAP, TermNil, LOCAL_ErrorMessage); return (FALSE); @@ -190,17 +190,18 @@ static Int Yap_Error(TYPE_ERROR_INTEGER, ARG1, "open_mem_read_stream"); return (FALSE); } - nbuf[nchars++] = IntOfTerm(ts); + buf[nchars++] = IntOfTerm(ts); ti = TailOfTerm(ti); } - nbuf[nchars] = '\0'; - sno = Yap_open_buf_read_stream(nbuf, nchars, &LOCAL_encoding, MEM_BUF_CODE); + buf[nchars] = '\0'; + sno = Yap_open_buf_read_stream(buf, nchars, &LOCAL_encoding, MEM_BUF_CODE); t = Yap_MkStream(sno); return (Yap_unify(ARG2, t)); } -int Yap_open_buf_write_stream(char *buf, size_t nchars, encoding_t *encp, - memBufSource sr) { +// open a buffer for writing, currently just ignores buf and nchars. + +int Yap_open_buf_write_stream(encoding_t enc, memBufSource src) { CACHE_REGS int sno; StreamDesc *st; @@ -210,55 +211,37 @@ int Yap_open_buf_write_stream(char *buf, size_t nchars, encoding_t *encp, 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->nsize = nchars; st->linepos = 0; st->charcount = 0; st->linecount = 1; - if (encp) - st->encoding = *encp; - else - st->encoding = LOCAL_encoding; + st->encoding = enc; Yap_DefaultStreamOps(st); #if MAY_WRITE -#if HAVE_FMEMOPEN - st->file = fmemopen((void *)buf, nchars, "w"); -#else - st->file = open_memstream(buf, &st->nsize); +#if HAVE_OPEN_MEMSTREAM + st->file = open_memstream(&st->nbuf, &st->nsize); st->status |= Seekable_Stream_f; -#endif +#else + st->file = fmemopen((void *)buf, nchars, "w"); st->nsize = nchars; st->nbuf = buf; if (!st->nbuf) { return -1; } - st->u.mem_string.pos = 0; - st->u.mem_string.buf = st->nbuf; - st->u.mem_string.max_size = nchars; +#endif +#else + char buf[YAP_FILENAME_MAX + 1]; + st->nbuf = buf; + st->u.mem_string.buf = buf; + st->u.mem_string.max_size = YAP_FILENAME_MAX; #endif UNLOCK(st->streamlock); return sno; } int Yap_OpenBufWriteStream(USES_REGS1) { - char *nbuf; - size_t sz = Yap_page_size; - while ((nbuf = (char *)Yap_AllocAtomSpace(Yap_page_size * sizeof(char))) == - NULL) { - if (!Yap_growheap(FALSE, Yap_page_size * sizeof(char), NULL)) { - Yap_Error(RESOURCE_ERROR_HEAP, TermNil, LOCAL_ErrorMessage); - return -1; - } - } return Yap_open_buf_write_stream( - nbuf, sz, &GLOBAL_Stream[LOCAL_c_output_stream].encoding, 0); + GLOBAL_Stream[LOCAL_c_output_stream].encoding, 0); } static Int @@ -312,7 +295,7 @@ restart: #if MAY_WRITE if (fflush(GLOBAL_Stream[sno].file) == 0) { ptr = GLOBAL_Stream[sno].nbuf; - i = GLOBAL_Stream[sno].nsize; + i = fseek(GLOBAL_Stream[sno].file, SEEK_END, 0); } #else ptr = GLOBAL_Stream[sno].u.mem_string.buf; @@ -356,6 +339,7 @@ void Yap_MemOps(StreamDesc *st) { bool Yap_CloseMemoryStream(int sno) { if (!(GLOBAL_Stream[sno].status & Output_Stream_f)) { #if MAY_WRITE + fflush(GLOBAL_Stream[sno].file); fclose(GLOBAL_Stream[sno].file); if (GLOBAL_Stream[sno].status & FreeOnClose_Stream_f) free(GLOBAL_Stream[sno].nbuf); diff --git a/os/readterm.c b/os/readterm.c index 89d04a95e..9ddbaf888 100644 --- a/os/readterm.c +++ b/os/readterm.c @@ -1325,7 +1325,7 @@ static Int term_to_string(USES_REGS1) { const char *s; if (IsVarTerm(t2)) { size_t length; - s = Yap_TermToString(ARG1, NULL, 0, &length, NULL, + s = Yap_TermToString(ARG1, &length, LOCAL_encoding, Quote_illegal_f | Handle_vars_f); if (!s || !MkStringTerm(s)) { Yap_Error(RESOURCE_ERROR_HEAP, t1, @@ -1357,7 +1357,7 @@ static Int term_to_atom(USES_REGS1) { Atom at; if (IsVarTerm(t2)) { size_t length; - char *s = Yap_TermToString(Deref(ARG1), NULL, 0, &length, NULL, + char *s = Yap_TermToString(Deref(ARG1), &length, LOCAL_encoding, Quote_illegal_f | Handle_vars_f); if (!s || !(at = Yap_LookupAtom(s))) { Yap_Error(RESOURCE_ERROR_HEAP, t2, diff --git a/os/yapio.h b/os/yapio.h index d4004ffe1..61c8fde0a 100644 --- a/os/yapio.h +++ b/os/yapio.h @@ -79,8 +79,7 @@ int Yap_PlFGetchar(void); int Yap_GetCharForSIGINT(void); Int Yap_StreamToFileNo(Term); int Yap_OpenStream(FILE *, char *, Term, int); -char *Yap_TermToString(Term t, char *s, size_t sz, size_t *length, - encoding_t *encoding, int flags); +char *Yap_TermToString(Term t, size_t *length, encoding_t encoding, int flags); char *Yap_HandleToString(yhandle_t l, size_t sz, size_t *length, encoding_t *encoding, int flags); int Yap_GetFreeStreamD(void); @@ -125,13 +124,11 @@ typedef enum mem_buf_source { char *Yap_MemStreamBuf(int sno); extern X_API Term Yap_StringToTerm(const char *s, size_t len, encoding_t *encp, - int prio, Term *bindings_p); + int prio, Term *bindings_p); extern Term Yap_StringToNumberTerm(char *s, encoding_t *encp); int Yap_FormatFloat(Float f, char **s, size_t sz); -int Yap_open_buf_read_stream(const char *nbuf, size_t nchars, encoding_t *encp, - memBufSource src); -int Yap_open_buf_write_stream(char *nbuf, size_t nchars, encoding_t *encp, - memBufSource src); +int Yap_open_buf_read_stream(const char *buf, size_t nchars, encoding_t *encp, memBufSource src); +int Yap_open_buf_write_stream(encoding_t enc, memBufSource src); Term Yap_ReadFromAtom(Atom a, Term opts); FILE *Yap_GetInputStream(Term t, const char *m); FILE *Yap_GetOutputStream(Term t, const char *m);