This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/C/text.c

1217 lines
31 KiB
C
Raw Normal View History

/*************************************************************************
2017-09-23 02:17:55 +01:00
* *
* YAP Prolog *
* *
* Yap Prolog was developed at NCCUP - Universidade do Porto *
* *
* Copyright L.Damas, V. Santos Costa and Universidade do Porto 1985-- *
* *
**************************************************************************
* *
* File: strings.c *
* comments: General-conversion of character sequences. *
* *
* Last rev: $Date: 2008-07-24 16:02:00 $,$Author: vsc $ *
* *
*************************************************************************/
#include "Yap.h"
2017-02-20 14:21:46 +00:00
#include "YapEval.h"
#include "YapHeap.h"
#include "YapText.h"
#include "Yatom.h"
#include "yapio.h"
#include <YapText.h>
#include <string.h>
2014-06-19 15:05:46 +01:00
#include <wchar.h>
2014-03-15 23:47:29 +00:00
#ifndef HAVE_WCSNLEN
inline static size_t min_size(size_t i, size_t j) { return (i < j ? i : j); }
2014-03-20 12:19:38 +00:00
#define wcsnlen(S, N) min_size(N, wcslen(S))
2014-03-15 23:47:29 +00:00
#endif
2017-12-10 01:22:45 +00:00
#if !defined(HAVE_STPCPY) && !defined(__APPLE__)
2018-05-16 02:58:37 +01:00
inline static void *__stpcpy(void *i, const void *j) {
return strcpy(i, j) + strlen(j);
}
2017-11-21 15:44:43 +00:00
#define stpcpy __stpcpy
#endif
#ifndef NAN
#define NAN (0.0 / 0.0)
#endif
#define MAX_PATHNAME 2048
struct mblock {
2017-09-23 02:17:55 +01:00
struct mblock *prev, *next;
int lvl;
size_t sz;
};
2016-10-20 04:38:17 +01:00
typedef struct TextBuffer_manager {
2017-09-23 02:17:55 +01:00
void *buf, *ptr;
size_t sz;
struct mblock *first[16];
struct mblock *last[16];
int lvl;
2016-10-20 04:38:17 +01:00
} text_buffer_t;
2016-08-16 05:42:49 +01:00
2017-09-26 15:15:15 +01:00
int AllocLevel(void) { return LOCAL_TextBuffer->lvl; }
int push_text_stack__(USES_REGS1) {
int i = LOCAL_TextBuffer->lvl;
i++;
LOCAL_TextBuffer->lvl = i;
return i;
}
2017-09-26 15:15:15 +01:00
int pop_text_stack__(int i) {
2017-09-23 02:17:55 +01:00
int lvl = LOCAL_TextBuffer->lvl;
while (lvl >= i) {
struct mblock *p = LOCAL_TextBuffer->first[lvl];
while (p) {
struct mblock *np = p->next;
free(p);
p = np;
}
LOCAL_TextBuffer->first[lvl] = NULL;
LOCAL_TextBuffer->last[lvl] = NULL;
lvl--;
}
2017-09-26 15:15:15 +01:00
LOCAL_TextBuffer->lvl = lvl;
2017-09-23 02:17:55 +01:00
return lvl;
}
2017-11-27 13:36:19 +00:00
void *pop_output_text_stack__(int i, const void *export) {
2017-10-11 02:24:15 +01:00
int lvl = LOCAL_TextBuffer->lvl;
while (lvl >= i) {
struct mblock *p = LOCAL_TextBuffer->first[lvl];
while (p) {
struct mblock *np = p->next;
2017-10-18 17:03:23 +01:00
if (p + 1 == export) {
2018-05-16 02:58:37 +01:00
size_t sz = p->sz - sizeof(struct mblock);
2018-06-27 11:38:42 +01:00
memmove(p, p + 1, sz);
2017-10-18 17:03:23 +01:00
export = p;
2017-10-11 02:24:15 +01:00
} else {
2017-10-18 17:03:23 +01:00
free(p);
2017-10-11 02:24:15 +01:00
}
p = np;
}
LOCAL_TextBuffer->first[lvl] = NULL;
LOCAL_TextBuffer->last[lvl] = NULL;
lvl--;
}
LOCAL_TextBuffer->lvl = lvl;
2017-12-05 15:14:57 +00:00
return (void *)export;
2017-10-11 02:24:15 +01:00
}
2017-09-23 02:17:55 +01:00
// void pop_text_stack(int i) { LOCAL_TextBuffer->lvl = i; }
void insert_block(struct mblock *o) {
int lvl = o->lvl;
o->prev = LOCAL_TextBuffer->last[lvl];
if (o->prev) {
o->prev->next = o;
}
if (LOCAL_TextBuffer->first[lvl]) {
LOCAL_TextBuffer->last[lvl] = o;
} else {
LOCAL_TextBuffer->first[lvl] = LOCAL_TextBuffer->last[lvl] = o;
}
o->next = NULL;
2016-12-10 07:04:37 +00:00
}
2017-09-23 02:17:55 +01:00
void release_block(struct mblock *o) {
int lvl = o->lvl;
if (LOCAL_TextBuffer->first[lvl] == o) {
if (LOCAL_TextBuffer->last[lvl] == o) {
LOCAL_TextBuffer->first[lvl] = LOCAL_TextBuffer->last[lvl] = NULL;
}
LOCAL_TextBuffer->first[lvl] = o->next;
} else if (LOCAL_TextBuffer->last[lvl] == o) {
LOCAL_TextBuffer->last[lvl] = o->prev;
}
2018-02-22 08:18:45 +00:00
if (o->prev)
o->prev->next = o->next;
if (o->next)
o->next->prev = o->prev;
2017-09-23 02:17:55 +01:00
}
void *Malloc(size_t sz USES_REGS) {
2017-09-23 02:17:55 +01:00
int lvl = LOCAL_TextBuffer->lvl;
if (sz == 0)
sz = 1024;
sz = ALIGN_BY_TYPE(sz + sizeof(struct mblock), CELL);
struct mblock *o = malloc(sz);
if (!o)
return NULL;
o->sz = sz;
o->lvl = lvl;
o->prev = o->next = 0;
insert_block(o);
return o + 1;
}
2017-09-26 15:15:15 +01:00
void *MallocAtLevel(size_t sz, int atL USES_REGS) {
int lvl = LOCAL_TextBuffer->lvl;
if (atL > 0 && atL <= lvl) {
lvl = atL;
} else if (atL < 0 && lvl - atL >= 0) {
lvl += atL;
} else {
return NULL;
2017-09-23 02:17:55 +01:00
}
2017-09-26 15:15:15 +01:00
if (sz == 0)
sz = 1024;
sz = ALIGN_BY_TYPE(sz + sizeof(struct mblock), CELL);
struct mblock *o = malloc(sz);
if (!o)
return NULL;
2017-09-23 02:17:55 +01:00
o->sz = sz;
2017-09-26 15:15:15 +01:00
o->lvl = lvl;
o->prev = o->next = 0;
insert_block(o);
2017-09-23 02:17:55 +01:00
return o + 1;
}
2017-09-21 09:47:21 +01:00
2017-09-26 15:15:15 +01:00
void *Realloc(void *pt, size_t sz USES_REGS) {
sz += sizeof(struct mblock);
struct mblock *old = pt, *o;
old--;
release_block(old);
o = realloc(old, sz);
o->sz = sz;
insert_block(o);
return o + 1;
}
2016-10-20 04:38:17 +01:00
2018-02-21 17:41:00 +00:00
/**
2018-05-16 02:58:37 +01:00
* Export a local memory object as a RO object to the outside world, that is,
* recovering as much storage as one can.
2018-02-21 17:41:00 +00:00
* @param pt pointer to object
* @return new object
*/
const void *MallocExportAsRO(const void *pt USES_REGS) {
2018-05-16 02:58:37 +01:00
struct mblock *old = (void *)pt, *o = old - 1;
if (old == NULL)
return NULL;
2018-02-22 08:18:45 +00:00
size_t sz = o->sz;
release_block(o);
2018-05-16 02:58:37 +01:00
memcpy((void *)o, pt, sz);
2018-02-22 08:18:45 +00:00
return realloc((void *)o, sz);
2018-02-21 17:41:00 +00:00
}
void Free(void *pt USES_REGS) {
2017-09-23 02:17:55 +01:00
struct mblock *o = pt;
o--;
release_block(o);
free(o);
}
2016-10-20 04:38:17 +01:00
void *Yap_InitTextAllocator(void) {
2017-09-23 02:17:55 +01:00
struct TextBuffer_manager *new = calloc(sizeof(struct TextBuffer_manager), 1);
return new;
}
2016-10-20 04:38:17 +01:00
static size_t MaxTmp(USES_REGS1) {
2016-10-20 04:38:17 +01:00
2018-06-22 23:55:50 +01:00
return 1025;
}
static Term Globalize(Term v USES_REGS) {
2017-09-23 02:17:55 +01:00
if (!IsVarTerm(v = Deref(v))) {
return v;
2017-09-23 02:17:55 +01:00
}
if (VarOfTerm(v) > HR && VarOfTerm(v) < LCL0) {
Bind_Local(VarOfTerm(v), MkVarTerm());
v = Deref(v);
}
return v;
}
2017-09-26 15:15:15 +01:00
static void *codes2buf(Term t0, void *b0, bool *get_codes USES_REGS) {
unsigned char *st0, *st, ar[16];
Term t = t0;
size_t length = 0;
2017-09-23 02:17:55 +01:00
2017-09-26 15:15:15 +01:00
if (t == TermNil) {
2018-02-07 21:39:28 +00:00
st0 = Malloc(4);
2017-09-26 15:15:15 +01:00
st0[0] = 0;
return st0;
2017-09-23 02:17:55 +01:00
}
2017-09-26 15:15:15 +01:00
if (!IsPairTerm(t))
return NULL;
bool codes = IsIntegerTerm(HeadOfTerm(t));
if (get_codes)
*get_codes = codes;
if (codes) {
while (IsPairTerm(t)) {
Term hd = HeadOfTerm(t);
2017-10-18 17:03:23 +01:00
if (IsVarTerm(hd)) {
2018-04-20 14:27:26 +01:00
Yap_ThrowError(INSTANTIATION_ERROR, hd, "scanning list of codes");
2017-10-18 17:03:23 +01:00
return NULL;
2017-09-26 15:15:15 +01:00
}
2017-10-18 17:03:23 +01:00
if (!IsIntegerTerm(hd)) {
2018-04-20 14:27:26 +01:00
Yap_ThrowError(TYPE_ERROR_CHARACTER_CODE, hd, "scanning list of codes");
2017-10-18 17:03:23 +01:00
return NULL;
2017-09-26 15:15:15 +01:00
}
2017-10-18 17:03:23 +01:00
Int code = IntegerOfTerm(hd);
if (code < 0) {
2018-05-16 02:58:37 +01:00
Yap_ThrowError(TYPE_ERROR_CHARACTER_CODE, hd, "scanning list of codes");
2017-10-18 17:03:23 +01:00
return NULL;
}
length += put_utf8(ar, code);
t = TailOfTerm(t);
2018-04-20 14:27:26 +01:00
if (IsVarTerm(t)) {
Yap_ThrowError(INSTANTIATION_ERROR, t, "scanning list of codes");
return NULL;
}
if (!IsPairTerm(t) && t != TermNil) {
Yap_ThrowError(TYPE_ERROR_LIST, t, "scanning list of codes");
return NULL;
}
2017-09-23 02:17:55 +01:00
}
2017-10-18 17:03:23 +01:00
} else {
while (IsPairTerm(t)) {
Term hd = HeadOfTerm(t);
2018-04-20 14:27:26 +01:00
if (IsVarTerm(hd)) {
Yap_ThrowError(INSTANTIATION_ERROR, hd, "scanning list of codes");
return NULL;
}
2017-10-18 17:03:23 +01:00
if (!IsAtomTerm(hd)) {
2018-06-22 23:55:50 +01:00
Yap_ThrowError(TYPE_ERROR_CHARACTER, hd, "scanning list of texts");
2017-10-18 17:03:23 +01:00
return NULL;
}
const char *code = RepAtom(AtomOfTerm(hd))->StrOfAE;
if (code < 0) {
2018-04-20 14:27:26 +01:00
Yap_ThrowError(TYPE_ERROR_CHARACTER, hd, "scanning list of atoms");
2017-10-18 17:03:23 +01:00
return NULL;
2017-09-26 15:15:15 +01:00
}
2017-10-18 17:03:23 +01:00
length += strlen(code);
t = TailOfTerm(t);
2018-04-20 14:27:26 +01:00
if (IsVarTerm(t)) {
Yap_ThrowError(INSTANTIATION_ERROR, t, "scanning list of codes");
return NULL;
}
if (!IsPairTerm(t) && t != TermNil) {
Yap_ThrowError(TYPE_ERROR_LIST, t, "scanning list of codes");
return NULL;
}
2017-09-23 02:17:55 +01:00
}
2017-10-18 17:03:23 +01:00
}
2017-09-21 09:47:21 +01:00
2017-10-18 17:03:23 +01:00
if (!IsVarTerm(t)) {
if (t != TermNil) {
2018-06-18 10:25:17 +01:00
Yap_ThrowError(TYPE_ERROR_LIST, t, "scanning list of codes");
2017-10-18 17:03:23 +01:00
return NULL;
}
}
2017-09-26 15:15:15 +01:00
2017-10-18 17:03:23 +01:00
st0 = st = Malloc(length + 1);
t = t0;
if (codes) {
while (IsPairTerm(t)) {
Term hd = HeadOfTerm(t);
Int code = IntegerOfTerm(hd);
st = st + put_utf8(st, code);
t = TailOfTerm(t);
}
} else {
while (IsPairTerm(t)) {
Term hd = HeadOfTerm(t);
const char *code = RepAtom(AtomOfTerm(hd))->StrOfAE;
st = (unsigned char *)stpcpy((char *)st, code);
t = TailOfTerm(t);
2017-09-23 02:17:55 +01:00
}
2017-10-18 17:03:23 +01:00
}
st[0] = '\0';
2017-09-23 02:17:55 +01:00
2017-10-18 17:03:23 +01:00
return st0;
2017-10-18 01:02:52 +01:00
}
2017-09-23 02:17:55 +01:00
2017-10-18 01:02:52 +01:00
static unsigned char *latin2utf8(seq_tv_t *inp) {
unsigned char *b0 = inp->val.uc;
size_t sz = strlen(inp->val.c);
sz *= 2;
int ch;
unsigned char *buf = Malloc(sz + 1), *pt = buf;
if (!buf)
return NULL;
while ((ch = *b0++)) {
int off = put_utf8(pt, ch);
if (off < 0) {
continue;
2017-09-23 02:17:55 +01:00
}
2017-10-18 17:03:23 +01:00
pt += off;
2017-09-23 02:17:55 +01:00
}
2017-10-18 17:03:23 +01:00
*pt++ = '\0';
return buf;
}
2017-09-23 02:17:55 +01:00
2017-10-18 17:03:23 +01:00
static unsigned char *wchar2utf8(seq_tv_t *inp) {
size_t sz = wcslen(inp->val.w) * 4;
wchar_t *b0 = inp->val.w;
unsigned char *buf = Malloc(sz + 1), *pt = buf;
int ch;
if (!buf)
return NULL;
while ((ch = *b0++))
pt += put_utf8(pt, ch);
*pt++ = '\0';
return buf;
}
2017-10-18 17:03:23 +01:00
static void *slice(size_t min, size_t max, const unsigned char *buf USES_REGS);
2017-09-23 02:17:55 +01:00
2017-10-18 17:03:23 +01:00
static unsigned char *Yap_ListOfCodesToBuffer(unsigned char *buf, Term t,
seq_tv_t *inp USES_REGS) {
bool codes;
unsigned char *nbuf = codes2buf(t, buf, &codes PASS_REGS);
if (!codes)
return NULL;
return nbuf;
}
2017-10-18 17:03:23 +01:00
static unsigned char *Yap_ListOfAtomsToBuffer(unsigned char *buf, Term t,
seq_tv_t *inp USES_REGS) {
bool codes;
unsigned char *nbuf = codes2buf(t, buf, &codes PASS_REGS);
if (codes)
return NULL;
return nbuf;
}
2015-10-18 11:47:01 +01:00
2017-10-18 01:02:52 +01:00
static unsigned char *Yap_ListToBuffer(unsigned char *buf, Term t,
2017-10-18 17:03:23 +01:00
seq_tv_t *inp USES_REGS) {
unsigned char *nbuf = codes2buf(t, buf, NULL PASS_REGS);
return nbuf;
}
2015-09-21 23:05:36 +01:00
#if USE_GEN_TYPE_ERROR
2017-10-18 17:03:23 +01:00
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))
return TYPE_ERROR_ATOMIC;
if ((flags & (YAP_STRING_INT | YAP_STRING_FLOAT | YAP_STRING_BIG)) ==
(YAP_STRING_INT | YAP_STRING_FLOAT | YAP_STRING_BIG))
2013-12-08 19:12:24 +00:00
return TYPE_ERROR_NUMBER;
2017-10-18 17:03:23 +01:00
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))
return TYPE_ERROR_LIST;
return TYPE_ERROR_NUMBER;
}
2015-09-21 23:05:36 +01:00
#endif
2013-12-06 15:08:35 +00:00
2017-10-18 17:03:23 +01:00
// static int cnt;
2016-08-30 14:27:32 +01:00
2017-10-18 17:03:23 +01:00
unsigned char *Yap_readText(seq_tv_t *inp USES_REGS) {
2018-05-16 00:00:12 +01:00
int lvl = push_text_stack();
2017-10-18 17:03:23 +01:00
/* 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 (!IsPairOrNilTerm(inp->val.t) && !IsStringTerm(inp->val.t) &&
inp->type == (YAP_STRING_ATOMS_CODES | YAP_STRING_STRING)) {
LOCAL_Error_TYPE = TYPE_ERROR_LIST;
} else if (!IsPairOrNilTerm(inp->val.t) && !IsStringTerm(inp->val.t) &&
!IsAtomTerm(inp->val.t) && !(inp->type & YAP_STRING_DATUM)) {
LOCAL_Error_TYPE = TYPE_ERROR_TEXT;
2017-09-26 15:15:15 +01:00
}
}
2018-05-16 02:58:37 +01:00
if (LOCAL_Error_TYPE != YAP_NO_ERROR) {
2018-06-28 12:48:29 +01:00
if (inp->val.uc != NULL) {
LOCAL_ActiveError->errorRawTerm = MkUStringTerm(inp->val.uc);
}
2018-06-25 13:44:09 +01:00
Yap_ThrowError(LOCAL_Error_TYPE, LOCAL_ActiveError->errorRawTerm, "Converting to text from term ");
2017-10-18 17:03:23 +01:00
return NULL;
2018-05-16 02:58:37 +01:00
}
2018-06-28 12:48:29 +01:00
}
2017-10-18 17:03:23 +01:00
if (IsAtomTerm(inp->val.t) && inp->type & YAP_STRING_ATOM) {
// this is a term, extract to a buffer, and representation is wide
// Yap_DebugPlWriteln(inp->val.t);
Atom at = AtomOfTerm(inp->val.t);
if (RepAtom(at)->UStrOfAE[0] == 0) {
unsigned char *o = Malloc(4);
memset(o, 0, 4);
2018-05-16 00:00:12 +01:00
return pop_output_text_stack(lvl, o);
2017-09-26 15:15:15 +01:00
}
2018-05-16 02:58:37 +01:00
if (inp->type & YAP_STRING_WITH_BUFFER) {
pop_text_stack(lvl);
2017-10-18 17:03:23 +01:00
return at->UStrOfAE;
2018-05-16 02:58:37 +01:00
}
2017-10-18 17:03:23 +01:00
size_t sz = strlen(at->StrOfAE);
2018-05-16 00:00:12 +01:00
void *o = Malloc(sz + 1);
2017-10-18 17:03:23 +01:00
strcpy(o, at->StrOfAE);
2018-05-16 00:00:12 +01:00
return pop_output_text_stack(lvl, o);
2017-10-18 17:03:23 +01:00
}
if (IsStringTerm(inp->val.t) && inp->type & YAP_STRING_STRING) {
// this is a term, extract to a buffer, and representation is wide
// Yap_DebugPlWriteln(inp->val.t);
const char *s = StringOfTerm(inp->val.t);
if (s[0] == 0) {
2018-05-16 00:00:12 +01:00
char *o = Malloc(4);
2017-10-18 17:03:23 +01:00
memset(o, 0, 4);
2018-05-16 00:00:12 +01:00
return pop_output_text_stack(lvl, o);
2017-09-26 15:15:15 +01:00
}
2017-10-18 17:03:23 +01:00
if (inp->type & YAP_STRING_WITH_BUFFER)
return (unsigned char *)UStringOfTerm(inp->val.t);
inp->type |= YAP_STRING_IN_TMP;
size_t sz = strlen(s);
2018-05-16 02:58:37 +01:00
char *o = Malloc(sz + 1);
2017-10-18 17:03:23 +01:00
strcpy(o, s);
2018-05-16 00:00:12 +01:00
return pop_output_text_stack(lvl, o);
2017-10-18 17:03:23 +01:00
}
if (((inp->type & (YAP_STRING_CODES | YAP_STRING_ATOMS)) ==
(YAP_STRING_CODES | YAP_STRING_ATOMS)) &&
IsPairOrNilTerm(inp->val.t)) {
// Yap_DebugPlWriteln(inp->val.t);
2018-06-14 11:27:43 +01:00
return pop_output_text_stack(
lvl, Yap_ListToBuffer(NULL, inp->val.t, inp PASS_REGS));
2017-10-18 17:03:23 +01:00
// this is a term, extract to a sfer, and representation is wide
}
if (inp->type & YAP_STRING_CODES && IsPairOrNilTerm(inp->val.t)) {
// Yap_DebugPlWriteln(inp->val.t);
2018-06-14 11:27:43 +01:00
return pop_output_text_stack(
lvl, Yap_ListOfCodesToBuffer(NULL, inp->val.t, inp PASS_REGS));
2017-10-18 17:03:23 +01:00
// this is a term, extract to a sfer, and representation is wide
}
if (inp->type & YAP_STRING_ATOMS && IsPairOrNilTerm(inp->val.t)) {
// Yap_DebugPlWriteln(inp->val.t);
2018-06-14 11:27:43 +01:00
return pop_output_text_stack(
lvl, Yap_ListOfAtomsToBuffer(NULL, inp->val.t, inp PASS_REGS));
2017-10-18 17:03:23 +01:00
// this is a term, extract to a buffer, and representation is wide
}
if (inp->type & YAP_STRING_INT && IsIntegerTerm(inp->val.t)) {
// ASCII, so both LATIN1 and UTF-8
// Yap_DebugPlWriteln(inp->val.t);
char *s;
2018-05-16 00:00:12 +01:00
s = Malloc(2 * MaxTmp(PASS_REGS1));
2017-10-18 17:03:23 +01:00
if (snprintf(s, MaxTmp(PASS_REGS1) - 1, Int_FORMAT,
IntegerOfTerm(inp->val.t)) < 0) {
AUX_ERROR(inp->val.t, 2 * MaxTmp(PASS_REGS1), s, char);
2017-09-26 15:15:15 +01:00
}
2018-05-16 00:00:12 +01:00
return pop_output_text_stack(lvl, s);
2017-10-18 17:03:23 +01:00
}
if (inp->type & YAP_STRING_FLOAT && IsFloatTerm(inp->val.t)) {
char *s;
// Yap_DebugPlWriteln(inp->val.t);
if (!Yap_FormatFloat(FloatOfTerm(inp->val.t), &s, 1024)) {
2018-05-16 02:58:37 +01:00
pop_text_stack(lvl);
2017-10-18 17:03:23 +01:00
return NULL;
2017-09-23 02:17:55 +01:00
}
2018-05-16 00:00:12 +01:00
return pop_output_text_stack(lvl, s);
2017-10-18 17:03:23 +01:00
}
2017-09-19 14:23:20 +01:00
#if USE_GMP
2017-10-18 17:03:23 +01:00
if (inp->type & YAP_STRING_BIG && IsBigIntTerm(inp->val.t)) {
// Yap_DebugPlWriteln(inp->val.t);
char *s;
2018-05-16 00:00:12 +01:00
s = Malloc(MaxTmp());
2017-10-18 17:03:23 +01:00
if (!Yap_mpz_to_string(Yap_BigIntOfTerm(inp->val.t), s, MaxTmp() - 1, 10)) {
AUX_ERROR(inp->val.t, MaxTmp(PASS_REGS1), s, char);
2017-09-23 02:17:55 +01:00
}
2018-05-16 00:00:12 +01:00
return inp->val.uc = pop_output_text_stack(lvl, s);
2017-10-18 17:03:23 +01:00
}
2017-09-19 14:23:20 +01:00
#endif
2017-10-18 17:03:23 +01:00
if (inp->type & YAP_STRING_TERM) {
// Yap_DebugPlWriteln(inp->val.t);
2018-05-16 02:58:37 +01:00
char *s = (char *)Yap_TermToBuffer(inp->val.t, ENC_ISO_UTF8, 0);
2018-05-16 00:00:12 +01:00
return inp->val.uc = pop_output_text_stack(lvl, s);
2017-10-18 17:03:23 +01:00
}
if (inp->type & YAP_STRING_CHARS) {
2018-05-16 00:00:12 +01:00
pop_text_stack(lvl);
2017-10-18 17:03:23 +01:00
if (inp->enc == ENC_ISO_LATIN1) {
return latin2utf8(inp);
} else if (inp->enc == ENC_ISO_ASCII) {
return inp->val.uc;
} else { // if (inp->enc == ENC_ISO_UTF8) {
return inp->val.uc;
2017-09-26 15:15:15 +01:00
}
2017-09-23 02:17:55 +01:00
}
2018-05-16 00:00:12 +01:00
pop_text_stack(lvl);
2017-10-18 17:03:23 +01:00
if (inp->type & YAP_STRING_WCHARS) {
// printf("%S\n",inp->val.w);
return wchar2utf8(inp);
}
return NULL;
}
2017-09-23 02:17:55 +01:00
2017-10-18 17:03:23 +01:00
static Term write_strings(unsigned char *s0, seq_tv_t *out USES_REGS) {
size_t min = 0, max = strlen((char *)s0);
2016-08-30 14:27:32 +01:00
2017-10-18 17:03:23 +01:00
if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) {
if (out->type & YAP_STRING_NCHARS)
min = out->max;
if (out->type & YAP_STRING_TRUNC && out->max < max) {
max = out->max;
s0[max] = '\0';
2017-09-23 02:17:55 +01:00
}
2017-10-18 17:03:23 +01:00
}
2016-08-30 14:27:32 +01:00
2017-10-18 17:03:23 +01:00
char *s = (char *)s0;
Term t = init_tstring(PASS_REGS1);
LOCAL_TERM_ERROR(t, 2 * max);
unsigned char *buf = buf_from_tstring(HR);
2018-05-16 02:58:37 +01:00
if (max == 0)
2018-02-07 21:39:28 +00:00
buf[0] = '\0';
else
strcpy((char *)buf, s);
2017-10-18 17:03:23 +01:00
if (max + 1 < min) {
LOCAL_TERM_ERROR(t, 2 * min);
memset(buf + min, '\0', max);
buf += min;
} else {
buf += max + 1;
}
close_tstring(buf PASS_REGS);
out->val.t = t;
2017-09-23 02:17:55 +01:00
2017-10-18 17:03:23 +01:00
return out->val.t;
}
static Term write_atoms(void *s0, seq_tv_t *out USES_REGS) {
Term t = AbsPair(HR);
char *s1 = (char *)s0;
size_t sz = 0;
size_t max = strlen(s1);
if (s1[0] == '\0') {
out->val.t = TermNil;
return TermNil;
}
if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) {
if (out->type & YAP_STRING_TRUNC && out->max < max)
max = out->max;
2017-09-23 02:17:55 +01:00
}
2017-09-19 14:23:20 +01:00
2017-10-18 17:03:23 +01:00
unsigned char *s = s0, *lim = s + strnlen((char *)s, max);
unsigned char *cp = s;
unsigned char w[10];
int wp = 0;
LOCAL_TERM_ERROR(t, 2 * (lim - s));
while (cp < lim && *cp) {
utf8proc_int32_t chr;
CELL *cl;
s += get_utf8(s, -1, &chr);
if (chr == '\0') {
w[0] = '\0';
break;
2017-09-19 14:23:20 +01:00
}
2017-10-18 17:03:23 +01:00
wp = put_utf8(w, chr);
w[wp] = '\0';
cl = HR;
HR += 2;
cl[0] = MkAtomTerm(Yap_ULookupAtom(w));
cl[1] = AbsPair(HR);
sz++;
if (sz == max)
break;
}
if (out->type & YAP_STRING_DIFF) {
if (sz == 0)
t = out->dif;
else
HR[-1] = Globalize(out->dif PASS_REGS);
} else {
if (sz == 0)
t = TermNil;
else
HR[-1] = TermNil;
}
out->val.t = t;
return (t);
}
2017-09-26 15:15:15 +01:00
2017-10-18 17:03:23 +01:00
static Term write_codes(void *s0, seq_tv_t *out USES_REGS) {
Term t;
size_t sz = strlen(s0);
if (sz == 0) {
2017-09-26 15:15:15 +01:00
if (out->type & YAP_STRING_DIFF) {
2017-10-18 17:03:23 +01:00
out->val.t = Globalize(out->dif PASS_REGS);
2017-09-26 15:15:15 +01:00
} else {
2017-10-18 17:03:23 +01:00
out->val.t = TermNil;
2017-09-26 15:15:15 +01:00
}
2017-10-18 17:03:23 +01:00
return out->val.t;
2017-09-26 15:15:15 +01:00
}
2017-10-18 17:03:23 +01:00
unsigned char *s = s0, *lim = s + sz;
unsigned char *cp = s;
2017-09-26 15:15:15 +01:00
2017-10-18 17:03:23 +01:00
t = AbsPair(HR);
LOCAL_TERM_ERROR(t, 2 * (lim - s));
t = AbsPair(HR);
while (*cp) {
utf8proc_int32_t chr;
CELL *cl;
cp += get_utf8(cp, -1, &chr);
if (chr == '\0')
break;
cl = HR;
HR += 2;
cl[0] = MkIntegerTerm(chr);
cl[1] = AbsPair(HR);
}
if (sz == 0) {
HR[-1] = Globalize(out->dif PASS_REGS);
} else {
HR[-1] = TermNil;
}
out->val.t = t;
return (t);
}
2017-10-18 17:03:23 +01:00
static Atom write_atom(void *s0, seq_tv_t *out USES_REGS) {
unsigned char *s = s0;
int32_t ch;
size_t leng = strlen(s0);
if (leng == 0) {
return Yap_LookupAtom("");
}
if (strlen_utf8(s0) <= leng) {
return Yap_LookupAtom(s0);
} else {
size_t n = get_utf8(s, -1, &ch);
unsigned char *buf = Malloc(n + 1);
memcpy(buf, s0, n + 1);
return Yap_ULookupAtom(buf);
2017-09-26 15:15:15 +01:00
}
2017-10-18 17:03:23 +01:00
}
2017-09-26 15:15:15 +01:00
2017-10-18 17:03:23 +01:00
void *write_buffer(unsigned char *s0, seq_tv_t *out USES_REGS) {
2018-05-24 12:00:10 +01:00
int l = push_text_stack();
2017-10-18 17:03:23 +01:00
size_t leng = strlen((char *)s0);
size_t min = 0, max = leng;
if (out->enc == ENC_ISO_UTF8) {
2018-05-16 02:58:37 +01:00
if (out->val.uc == NULL) { // this should always be the case
2018-05-24 12:00:10 +01:00
out->val.uc = Malloc(leng + 1);
2017-10-18 17:03:23 +01:00
strcpy(out->val.c, (char *)s0);
} else if (out->val.uc != s0) {
2018-05-24 12:00:10 +01:00
out->val.c = Malloc(leng + 1);
2017-10-18 17:03:23 +01:00
strcpy(out->val.c, (char *)s0);
2017-09-26 15:15:15 +01:00
}
2017-10-18 17:03:23 +01:00
} else if (out->enc == ENC_ISO_LATIN1) {
2017-09-26 15:15:15 +01:00
2017-10-18 17:03:23 +01:00
unsigned char *s = s0;
unsigned char *cp = s;
unsigned char *buf = out->val.uc;
2018-05-24 12:00:10 +01:00
if (!buf) {
pop_text_stack(l);
2017-10-18 17:03:23 +01:00
return NULL;
2018-05-24 12:00:10 +01:00
}
2017-10-18 17:03:23 +01:00
while (*cp) {
utf8proc_int32_t chr;
int off = get_utf8(cp, -1, &chr);
2018-05-24 12:00:10 +01:00
if (off <= 0 || chr > 255) {
2018-06-14 11:27:43 +01:00
pop_text_stack(l);
2017-10-18 17:03:23 +01:00
return NULL;
2018-05-24 12:00:10 +01:00
}
2017-10-18 17:03:23 +01:00
if (off == max)
break;
cp += off;
*buf++ = chr;
}
if (max >= min)
*buf++ = '\0';
else
while (max < min) {
utf8proc_int32_t chr;
max++;
cp += get_utf8(cp, -1, &chr);
*buf++ = chr;
2017-09-26 15:15:15 +01:00
}
2017-10-18 17:03:23 +01:00
} else if (out->enc == ENC_WCHAR) {
unsigned char *s = s0, *lim = s + (max = strnlen((char *)s0, max));
unsigned char *cp = s;
wchar_t *buf0, *buf;
buf = buf0 = out->val.w;
2018-05-24 12:00:10 +01:00
if (!buf) {
pop_text_stack(l);
2017-09-26 15:15:15 +01:00
return NULL;
2018-05-24 12:00:10 +01:00
}
2017-10-18 17:03:23 +01:00
while (*cp && cp < lim) {
utf8proc_int32_t chr;
cp += get_utf8(cp, -1, &chr);
*buf++ = chr;
2017-09-26 15:15:15 +01:00
}
2017-10-18 17:03:23 +01:00
if (max >= min)
*buf++ = '\0';
else
while (max < min) {
utf8proc_int32_t chr;
max++;
cp += get_utf8(cp, -1, &chr);
*buf++ = chr;
}
*buf = '\0';
} else {
// no other encodings are supported.
2018-05-24 12:00:10 +01:00
pop_text_stack(l);
2017-10-18 17:03:23 +01:00
return NULL;
2017-09-23 02:17:55 +01:00
}
2018-06-18 10:25:17 +01:00
out->val.c = pop_output_text_stack(l, out->val.c);
2017-10-18 17:03:23 +01:00
return out->val.c;
}
2017-09-23 02:17:55 +01:00
static size_t write_length(const unsigned char *s0, seq_tv_t *out USES_REGS) {
return strlen_utf8(s0);
2017-09-21 09:47:21 +01:00
}
2016-05-30 11:22:47 +01:00
2017-09-23 02:17:55 +01:00
static Term write_number(unsigned char *s, seq_tv_t *out,
2017-10-18 17:03:23 +01:00
bool error_on USES_REGS) {
2017-09-23 02:17:55 +01:00
Term t;
2018-05-16 00:00:12 +01:00
LOCAL_delay = !error_on;
2018-05-16 02:58:37 +01:00
t = Yap_StringToNumberTerm((char *)s, &out->enc, error_on);
LOCAL_delay = false;
return t;
2017-09-21 09:47:21 +01:00
}
2017-09-23 02:17:55 +01:00
static Term string_to_term(void *s, seq_tv_t *out USES_REGS) {
Term o;
2018-06-14 11:27:43 +01:00
yap_error_descriptor_t *new_error = malloc(sizeof(yap_error_descriptor_t));
bool mdnew = Yap_pushErrorContext(true, new_error);
2018-05-16 02:58:37 +01:00
o = out->val.t = Yap_BufferToTerm(s, TermNil);
Yap_popErrorContext(mdnew, true);
2017-09-21 09:47:21 +01:00
2018-05-16 02:58:37 +01:00
return o;
2017-09-21 09:47:21 +01:00
}
2016-08-30 14:27:32 +01:00
2017-09-23 02:17:55 +01:00
bool write_Text(unsigned char *inp, seq_tv_t *out USES_REGS) {
/* we know what the term is */
if (out->type == 0) {
return true;
}
2018-05-16 00:00:12 +01:00
if (LOCAL_Error_TYPE) {
return false;
}
2017-09-23 02:17:55 +01:00
if (out->type & (YAP_STRING_INT | YAP_STRING_FLOAT | YAP_STRING_BIG)) {
if ((out->val.t = write_number(
2017-10-18 17:03:23 +01:00
inp, out, !(out->type & YAP_STRING_ATOM) PASS_REGS)) != 0L) {
2017-09-23 02:17:55 +01:00
// Yap_DebugPlWriteln(out->val.t);
2017-10-18 17:03:23 +01:00
return true;
2017-09-23 02:17:55 +01:00
}
2017-10-18 17:03:23 +01:00
if (!(out->type & YAP_STRING_ATOM))
return false;
}
if (out->type & (YAP_STRING_ATOM)) {
if ((out->val.a = write_atom(inp, out PASS_REGS)) != NIL) {
Atom at = out->val.a;
if (at && (out->type & YAP_STRING_OUTPUT_TERM))
out->val.t = MkAtomTerm(at);
2017-09-26 15:15:15 +01:00
// Yap_DebugPlWriteln(out->val.t);
2017-10-18 17:03:23 +01:00
return at != NIL;
2017-09-23 02:17:55 +01:00
}
}
2017-10-18 17:03:23 +01:00
if (out->type & YAP_STRING_DATUM) {
if ((out->val.t = string_to_term(inp, out PASS_REGS)) != 0L)
return out->val.t != 0;
}
switch (out->type & YAP_TYPE_MASK) {
case YAP_STRING_CHARS: {
void *room = write_buffer(inp, out PASS_REGS);
// printf("%s\n", out->val.c);
return room != NULL;
}
case YAP_STRING_WCHARS: {
void *room = write_buffer(inp, out PASS_REGS);
// printf("%S\n", out->val.w);
return room != NULL;
}
case YAP_STRING_STRING:
out->val.t = write_strings(inp, out PASS_REGS);
// Yap_DebugPlWriteln(out->val.t);
return out->val.t != 0;
case YAP_STRING_ATOMS:
out->val.t = write_atoms(inp, out PASS_REGS);
// Yap_DebugPlWriteln(out->val.t);
return out->val.t != 0;
case YAP_STRING_CODES:
out->val.t = write_codes(inp, out PASS_REGS);
// Yap_DebugPlWriteln(out->val.t);
return out->val.t != 0;
case YAP_STRING_LENGTH:
out->val.l = write_length(inp, out PASS_REGS);
// printf("s\n",out->val.l);
return out->val.l != (size_t)(-1);
case YAP_STRING_ATOM:
out->val.a = write_atom(inp, out PASS_REGS);
// Yap_DebugPlWriteln(out->val.t);
return out->val.a != NULL;
case YAP_STRING_INT | YAP_STRING_FLOAT | YAP_STRING_BIG:
out->val.t = write_number(inp, out, true PASS_REGS);
// Yap_DebugPlWriteln(out->val.t);
return out->val.t != 0;
default: { return true; }
}
return false;
}
2017-09-23 02:17:55 +01:00
2017-10-18 17:03:23 +01:00
static size_t upcase(void *s0, seq_tv_t *out USES_REGS) {
2016-05-30 11:22:47 +01:00
2017-10-18 17:03:23 +01:00
unsigned char *s = s0;
while (*s) {
// assumes the two code have always the same size;
utf8proc_int32_t chr;
get_utf8(s, -1, &chr);
chr = utf8proc_toupper(chr);
s += put_utf8(s, chr);
2017-09-23 02:17:55 +01:00
}
2017-10-18 17:03:23 +01:00
return true;
}
2017-10-18 17:03:23 +01:00
static size_t downcase(void *s0, seq_tv_t *out USES_REGS) {
2016-08-30 14:27:32 +01:00
2017-10-18 17:03:23 +01:00
unsigned char *s = s0;
while (*s) {
// assumes the two code have always the same size;
utf8proc_int32_t chr;
get_utf8(s, -1, &chr);
chr = utf8proc_tolower(chr);
s += put_utf8(s, chr);
2017-09-23 02:17:55 +01:00
}
2017-10-18 17:03:23 +01:00
return true;
}
2017-02-20 14:40:24 +00:00
2017-10-18 17:03:23 +01:00
bool Yap_CVT_Text(seq_tv_t *inp, seq_tv_t *out USES_REGS) {
unsigned char *buf;
bool rc;
2018-05-16 02:58:37 +01:00
2017-10-18 17:03:23 +01:00
/*
//printf(stderr, "[ %d ", n++) ;
if (inp->type & (YAP_STRING_TERM|YAP_STRING_ATOM|YAP_STRING_ATOMS_CODES
|YAP_STRING_STRING))
//Yap_DebugPlWriteln(inp->val.t);
else if (inp->type & YAP_STRING_WCHARS) fprintf(stderr,"S %S\n", inp->val
.w);
else fprintf(stderr,"s %s\n", inp->val.c);
*/
// cnt++;
int l = push_text_stack();
buf = Yap_readText(inp PASS_REGS);
2018-02-07 21:39:28 +00:00
if (!buf) {
2017-10-18 17:03:23 +01:00
pop_text_stack(l);
return 0L;
}
2018-02-07 21:39:28 +00:00
if (buf[0]) {
2018-05-16 02:58:37 +01:00
size_t leng = strlen_utf8(buf);
if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) {
if (out->max < leng) {
const unsigned char *ptr = skip_utf8(buf, out->max);
size_t diff = (ptr - buf);
char *nbuf = Malloc(diff + 1);
memcpy(nbuf, buf, diff);
nbuf[diff] = '\0';
leng = diff;
}
// else if (out->type & YAP_STRING_NCHARS &&
// const unsigned char *ptr = skip_utf8(buf)
2017-09-19 14:23:20 +01:00
}
2017-09-26 15:15:15 +01:00
2018-05-16 02:58:37 +01:00
if (out->type & (YAP_STRING_UPCASE | YAP_STRING_DOWNCASE)) {
if (out->type & YAP_STRING_UPCASE) {
if (!upcase(buf, out)) {
pop_text_stack(l);
return false;
}
2017-09-26 15:15:15 +01:00
}
2018-05-16 02:58:37 +01:00
if (out->type & YAP_STRING_DOWNCASE) {
if (!downcase(buf, out)) {
pop_text_stack(l);
return false;
}
2017-09-23 02:17:55 +01:00
}
}
2017-09-26 15:15:15 +01:00
}
2017-10-18 17:03:23 +01:00
rc = write_Text(buf, out PASS_REGS);
/* fprintf(stderr, " -> ");
if (!rc) fprintf(stderr, "NULL");
else if (out->type &
(YAP_STRING_TERM|YAP_STRING_ATOMS_CODES
|YAP_STRING_STRING)) //Yap_DebugPlWrite(out->val.t);
else if (out->type &
YAP_STRING_ATOM) //Yap_DebugPlWriteln(MkAtomTerm(out->val.a));
else if (out->type & YAP_STRING_WCHARS) fprintf(stderr, "%S",
out->val.w);
else
fprintf(stderr, "%s", out->val.c);
fprintf(stderr, "\n]\n"); */
pop_text_stack(l);
return rc;
}
2017-09-23 02:17:55 +01:00
2017-10-18 17:03:23 +01:00
static unsigned char *concat(int n, void *sv[] USES_REGS) {
void *buf;
unsigned char *buf0;
size_t room = 0;
int i;
for (i = 0; i < n; i++) {
char *s = sv[i];
if (s[0])
room += strlen(s);
}
buf = Malloc(room + 1);
buf0 = buf;
for (i = 0; i < n; i++) {
char *s = sv[i];
if (!s[0])
continue;
2016-12-04 18:52:42 +00:00
#if _WIN32 || defined(__ANDROID__)
2017-10-18 17:03:23 +01:00
strcpy(buf, s);
buf = (char *)buf + strlen(buf);
2016-11-08 07:37:36 +00:00
#else
2017-10-18 17:03:23 +01:00
buf = stpcpy(buf, s);
2016-11-08 07:37:36 +00:00
#endif
2017-09-23 02:17:55 +01:00
}
2017-10-18 17:03:23 +01:00
return buf0;
}
2013-12-04 23:01:30 +00:00
2017-10-18 17:03:23 +01:00
static void *slice(size_t min, size_t max, const unsigned char *buf USES_REGS) {
unsigned char *nbuf = BaseMalloc((max - min) * 4 + 1);
const unsigned char *ptr = skip_utf8(buf, min);
unsigned char *nptr = nbuf;
utf8proc_int32_t chr;
2017-09-23 02:17:55 +01:00
2017-10-18 17:03:23 +01:00
while (min++ < max) {
ptr += get_utf8(ptr, -1, &chr);
nptr += put_utf8(nptr, chr);
2017-09-23 02:17:55 +01:00
}
2017-10-18 17:03:23 +01:00
nptr[0] = '\0';
return nbuf;
}
2016-08-30 14:27:32 +01:00
2017-10-18 17:03:23 +01:00
//
// Out must be an atom or a string
bool Yap_Concat_Text(int tot, seq_tv_t inp[], seq_tv_t *out USES_REGS) {
void **bufv;
unsigned char *buf;
int i, j;
2018-06-14 11:27:43 +01:00
// int lvl = push_text_stack();
2017-10-18 17:03:23 +01:00
bufv = Malloc(tot * sizeof(unsigned char *));
if (!bufv) {
2018-06-14 11:27:43 +01:00
// pop_text_stack(lvl);
2017-10-18 17:03:23 +01:00
return NULL;
}
for (i = 0, j = 0; i < tot; i++) {
inp[j].type |= YAP_STRING_WITH_BUFFER;
unsigned char *nbuf = Yap_readText(inp + i PASS_REGS);
2017-09-23 02:17:55 +01:00
2017-10-18 17:03:23 +01:00
if (!nbuf) {
2018-06-14 11:27:43 +01:00
// pop_text_stack(lvl);
2017-09-23 02:17:55 +01:00
return NULL;
}
2017-10-18 17:03:23 +01:00
// if (!nbuf[0])
// continue;
bufv[j++] = nbuf;
}
if (j == 0) {
buf = Malloc(8);
memset(buf, 0, 4);
} else if (j == 1) {
buf = bufv[0];
} else {
buf = concat(tot, bufv PASS_REGS);
2017-09-23 02:17:55 +01:00
}
2017-10-18 17:03:23 +01:00
bool rc = write_Text(buf, out PASS_REGS);
2018-06-14 11:27:43 +01:00
// pop_text_stack( lvl );
2017-10-18 17:03:23 +01:00
return rc;
}
2017-09-23 02:17:55 +01:00
2017-10-18 01:02:52 +01:00
//
bool Yap_Splice_Text(int n, size_t cuts[], seq_tv_t *inp,
2017-10-18 17:03:23 +01:00
seq_tv_t outv[] USES_REGS) {
2018-05-16 00:00:12 +01:00
int lvl = push_text_stack();
2017-10-18 01:02:52 +01:00
const unsigned char *buf;
size_t b_l, u_l;
2017-09-21 09:47:21 +01:00
2017-10-18 01:02:52 +01:00
inp->type |= YAP_STRING_IN_TMP;
buf = Yap_readText(inp PASS_REGS);
if (!buf) {
2018-05-16 00:00:12 +01:00
pop_text_stack(lvl);
2017-10-18 01:02:52 +01:00
return false;
}
b_l = strlen((char *)buf);
if (b_l == 0) {
2018-05-16 00:00:12 +01:00
pop_text_stack(lvl);
2017-10-18 01:02:52 +01:00
return false;
}
u_l = strlen_utf8(buf);
if (!cuts) {
if (n == 2) {
size_t b_l0, b_l1, u_l0, u_l1;
2017-10-18 17:03:23 +01:00
unsigned char *buf0, *buf1;
if (outv[0].val.t) {
buf0 = Yap_readText(outv PASS_REGS);
if (!buf0) {
return false;
}
b_l0 = strlen((const char *)buf0);
2017-11-21 15:44:43 +00:00
if (memcmp(buf, buf0, b_l0) != 0) {
2018-05-16 00:00:12 +01:00
pop_text_stack(lvl);
2017-10-18 17:03:23 +01:00
return false;
}
u_l0 = strlen_utf8(buf0);
u_l1 = u_l - u_l0;
b_l1 = b_l - b_l0;
buf1 = slice(u_l0, u_l, buf PASS_REGS);
b_l1 = strlen((const char *)buf1);
bool rc = write_Text(buf1, outv + 1 PASS_REGS);
2018-05-16 00:00:12 +01:00
pop_text_stack(lvl);
2017-10-18 17:03:23 +01:00
if (!rc) {
return false;
}
return rc;
} else /* if (outv[1].val.t) */ {
buf1 = Yap_readText(outv + 1 PASS_REGS);
if (!buf1) {
2018-05-16 00:00:12 +01:00
pop_text_stack(lvl);
2017-10-18 17:03:23 +01:00
return false;
}
b_l1 = strlen((char *)buf1);
u_l1 = strlen_utf8(buf1);
b_l0 = b_l - b_l1;
u_l0 = u_l - u_l1;
2017-11-21 15:44:43 +00:00
if (memcmp(skip_utf8((const unsigned char *)buf, b_l0), buf1, b_l1) !=
2017-10-18 17:03:23 +01:00
0) {
2018-05-16 00:00:12 +01:00
pop_text_stack(lvl);
2017-10-18 17:03:23 +01:00
return false;
}
buf0 = slice(0, u_l0, buf PASS_REGS);
2018-05-16 02:58:37 +01:00
buf0 = pop_output_text_stack(lvl, buf0);
2017-10-18 17:03:23 +01:00
bool rc = write_Text(buf0, outv PASS_REGS);
return rc;
2017-09-26 15:15:15 +01:00
}
}
2017-10-18 17:03:23 +01:00
}
int i, next;
for (i = 0; i < n; i++) {
if (i == 0)
next = 0;
else
next = cuts[i - 1];
if (i > 0 && cuts[i] == 0)
break;
void *bufi = slice(next, cuts[i], buf PASS_REGS);
2018-06-14 11:27:43 +01:00
bufi = pop_output_text_stack(lvl, bufi);
2017-10-18 17:03:23 +01:00
if (!write_Text(bufi, outv + i PASS_REGS)) {
return false;
2017-09-26 15:15:15 +01:00
}
2017-09-23 02:17:55 +01:00
}
2018-05-16 00:00:12 +01:00
pop_text_stack(lvl);
2017-09-21 09:47:21 +01:00
2017-10-18 17:03:23 +01:00
return true;
}
2017-09-26 15:15:15 +01:00
2017-10-18 17:03:23 +01:00
/**
* Convert from a predicate structure to an UTF-8 string of the form
*
* module:name/arity.
*
* The result is in very volatile memory.
*
* @param s the buffer
*
* @return the temporary string
*/
const char *Yap_PredIndicatorToUTF8String(PredEntry *ap) {
CACHE_REGS
Atom at;
arity_t arity = 0;
Functor f;
char *s, *smax, *s0;
s = s0 = malloc(1024);
smax = s + 1024;
Term tmod = ap->ModuleOfPred;
if (tmod) {
char *sn = Yap_AtomToUTF8Text(AtomOfTerm(tmod));
stpcpy(s, sn);
if (smax - s > 1) {
strcat(s, ":");
2017-09-23 02:17:55 +01:00
} else {
2017-10-18 17:03:23 +01:00
return NULL;
2017-09-23 02:17:55 +01:00
}
2017-10-18 17:03:23 +01:00
s++;
} else {
if (smax - s > strlen("prolog:")) {
s = strcpy(s, "prolog:");
2017-09-23 02:17:55 +01:00
} else {
2017-09-26 15:15:15 +01:00
return NULL;
}
}
2017-10-18 17:03:23 +01:00
// follows the actual functor
if (ap->ModuleOfPred == IDB_MODULE) {
if (ap->PredFlags & NumberDBPredFlag) {
Int key = ap->src.IndxId;
snprintf(s, smax - s, "%" PRIdPTR, key);
return LOCAL_FileNameBuf;
} else if (ap->PredFlags & AtomDBPredFlag) {
at = (Atom)(ap->FunctorOfPred);
if (!stpcpy(s, Yap_AtomToUTF8Text(at)))
return NULL;
} else {
f = ap->FunctorOfPred;
at = NameOfFunctor(f);
arity = ArityOfFunctor(f);
}
} else {
arity = ap->ArityOfPE;
if (arity) {
at = NameOfFunctor(ap->FunctorOfPred);
} else {
at = (Atom)(ap->FunctorOfPred);
}
}
if (!stpcpy(s, Yap_AtomToUTF8Text(at))) {
return NULL;
}
s += strlen(s);
snprintf(s, smax - s, "/%" PRIdPTR, arity);
return s0;
}
2017-09-26 15:15:15 +01:00
2017-10-18 17:03:23 +01:00
/**
* 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, int guide USES_REGS) {
if (guide == YAP_STRING_ATOM) {
return MkAtomTerm(Yap_LookupAtom(s));
} else if (guide == YAP_STRING_STRING) {
return MkStringTerm(s);
2017-10-02 08:58:51 +01:00
} else if (guide == YAP_STRING_ATOMS) {
return Yap_CharsToListOfAtoms(s, ENC_ISO_UTF8 PASS_REGS);
2017-10-18 17:03:23 +01:00
} else {
return Yap_CharsToListOfCodes(s, ENC_ISO_UTF8 PASS_REGS);
2017-09-23 02:17:55 +01:00
}
2017-10-18 17:03:23 +01:00
}