| 
									
										
										
										
											2013-12-02 14:50:02 +00: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"
 | 
					
						
							|  |  |  | #include "Yatom.h"
 | 
					
						
							|  |  |  | #include "YapHeap.h"
 | 
					
						
							|  |  |  | #include "eval.h"
 | 
					
						
							|  |  |  | #include "yapio.h"
 | 
					
						
							| 
									
										
										
										
											2013-12-06 23:24:01 +00:00
										 |  |  | #include "YapText.h"
 | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2014-06-19 15:05:46 +01:00
										 |  |  | #include <wchar.h>
 | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-15 23:47:29 +00:00
										 |  |  | #ifndef HAVE_WCSNLEN
 | 
					
						
							| 
									
										
										
										
											2014-03-20 12:19:38 +00:00
										 |  |  | inline static size_t | 
					
						
							|  |  |  | min_size(size_t i, size_t j) { | 
					
						
							|  |  |  |   return ( i < j ?  i :  j ); | 
					
						
							| 
									
										
										
										
											2014-03-18 15:40:50 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-08 02:23:45 +01:00
										 |  |  | static inline unsigned char *getChar(unsigned char *p, int *c) { *c = *p; return p+1; } | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-08 02:23:45 +01:00
										 |  |  | static inline wchar_t *getWchar(wchar_t *p, int *c) { *c = *p; return p+1; } | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifndef NAN
 | 
					
						
							|  |  |  | #define NAN      (0.0/0.0)
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static Term | 
					
						
							|  |  |  | Globalize(Term v USES_REGS) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (!IsVarTerm(v = Deref(v))) { | 
					
						
							|  |  |  |     return v; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   if (VarOfTerm(v) > HR && VarOfTerm(v) < LCL0) { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |     Bind_Local(VarOfTerm(v), MkVarTerm()); | 
					
						
							|  |  |  |     v = Deref(v); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return v; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static Int | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  | SkipListCodes(unsigned char **bufp, Term *l, Term **tailp, Int *atoms, bool *wide, seq_tv_t *inp USES_REGS) | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   Int length = 0; | 
					
						
							|  |  |  |   Term *s; /* slow */ | 
					
						
							|  |  |  |   Term v; /* temporary */ | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |   *wide = false; | 
					
						
							|  |  |  |   size_t max = 1; | 
					
						
							|  |  |  |   unsigned char *st0 = *bufp, *st; | 
					
						
							|  |  |  |   unsigned char *smax = NULL; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   do_derefa(v,l,derefa_unk,derefa_nonvar); | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |   *tailp = l; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   s = l; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |   if (inp->type & YAP_STRING_TRUNC) { | 
					
						
							|  |  |  |     max = inp->max; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     max = 0; // basically, this will never be reached;
 | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-12-08 22:56:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |   if (!st0) { | 
					
						
							| 
									
										
										
										
											2016-02-13 03:11:25 +00:00
										 |  |  |     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';
 | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |       *bufp = st0 = (unsigned char *)Yap_PreAllocCodeSpace(); | 
					
						
							|  |  |  |       smax = (unsigned char *)AuxTop-8; // give 8 bytes for max UTF-8 size + '\0';
 | 
					
						
							| 
									
										
										
										
											2016-02-13 03:11:25 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |   }  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; | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2013-12-08 22:56:20 +00:00
										 |  |  |   if (*l == TermNil) { | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   if ( IsPairTerm(*l) ) | 
					
						
							| 
									
										
										
										
											2015-10-22 00:52:49 +01:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |     Term hd0 = HeadOfTerm(*l); | 
					
						
							|  |  |  |     if (IsVarTerm(hd0)) { | 
					
						
							|  |  |  |       return -INSTANTIATION_ERROR; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     //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; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |     } else if (*atoms == 2) { | 
					
						
							|  |  |  |       if ( !IsAtomTerm(hd0) ) { | 
					
						
							|  |  |  | 	return -TYPE_ERROR_ATOM; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  |     do {  | 
					
						
							|  |  |  |       int ch; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       length++; | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |       if (length == max) { | 
					
						
							| 
									
										
										
										
											2016-02-13 03:11:25 +00:00
										 |  |  |         *st++ = '\0'; | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       { Term hd = Deref(RepPair(*l)[0]); | 
					
						
							|  |  |  | 	if (IsVarTerm(hd)) { | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  | 	  return  -INSTANTIATION_ERROR; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 	} else if (IsAtomTerm(hd)) { | 
					
						
							|  |  |  | 	  (*atoms)++; | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  | 	  if (*atoms < length) | 
					
						
							|  |  |  | 	     { *tailp = l; return -TYPE_ERROR_NUMBER; } | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 	  if (IsWideAtom(AtomOfTerm(hd))) { | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  | 	    int ch; | 
					
						
							|  |  |  | 	    if ((RepAtom(AtomOfTerm(hd))->WStrOfAE)[1] != '\0') { | 
					
						
							|  |  |  | 	      length = -REPRESENTATION_ERROR_CHARACTER; | 
					
						
							|  |  |  | 	    } | 
					
						
							|  |  |  | 	    ch = RepAtom(AtomOfTerm(hd))->WStrOfAE[0]; | 
					
						
							|  |  |  | 	    *wide = true; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 	  } else { | 
					
						
							| 
									
										
										
										
											2013-12-08 19:12:24 +00:00
										 |  |  | 	    AtomEntry *ae = RepAtom(AtomOfTerm(hd)); | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  | 	    if ((ae->StrOfAE)[1] != '\0') { | 
					
						
							|  |  |  | 	      length = -REPRESENTATION_ERROR_CHARACTER; | 
					
						
							|  |  |  | 	    } else { | 
					
						
							|  |  |  | 	      ch = RepAtom(AtomOfTerm(hd))->StrOfAE[0]; | 
					
						
							|  |  |  | 	      *wide |= ch > 0x80; | 
					
						
							|  |  |  | 	    } | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 	  } | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	} else if (IsIntegerTerm(hd)) { | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  | 	   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; | 
					
						
							|  |  |  | 	  } | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 	  length = -TYPE_ERROR_INTEGER; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (length < 0) { | 
					
						
							|  |  |  | 	  *tailp = l; | 
					
						
							|  |  |  | 	  return length; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |       // now copy char to buffer
 | 
					
						
							|  |  |  |       size_t chsz = put_utf8( st, ch ); | 
					
						
							|  |  |  |       if (smax <= st+chsz) { | 
					
						
							| 
									
										
										
										
											2016-02-13 03:11:25 +00:00
										 |  |  |         *st++ = '\0'; | 
					
						
							|  |  |  |         *tailp = l; | 
					
						
							|  |  |  |         return length; | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2016-02-13 03:11:25 +00:00
										 |  |  |         st += chsz; | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |       l = RepPair(*l)+1; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       do_derefa(v,l,derefa2_unk,derefa2_nonvar); | 
					
						
							|  |  |  |     } while ( *l != *s && IsPairTerm(*l) ); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |   if (IsVarTerm(*l)) { | 
					
						
							|  |  |  | 	  return  -INSTANTIATION_ERROR; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (   *l != TermNil) { | 
					
						
							|  |  |  | 	  return  -TYPE_ERROR_LIST; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   st[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   *tailp = l; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return length; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void * | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  | to_buffer(void *buf, Term t, seq_tv_t *inp, bool *widep, Int *atoms, size_t *lenp USES_REGS) | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   CELL *r = NULL; | 
					
						
							|  |  |  |   Int n; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |   if (!buf) { | 
					
						
							|  |  |  |     inp->sz = *lenp; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   unsigned char *bufc = buf; | 
					
						
							|  |  |  |   n = SkipListCodes(&bufc, &t, &r, atoms, widep, inp PASS_REGS); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   if (n < 0) { | 
					
						
							|  |  |  |     LOCAL_Error_TYPE = -n; | 
					
						
							|  |  |  |     LOCAL_Error_Term = *r; | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |   *lenp = n; | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |   return bufc;   | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void * | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | Yap_ListOfCodesToBuffer(void *buf, Term t, seq_tv_t *inp, bool *widep, size_t *lenp USES_REGS) | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |   Int atoms = 1; // we only want lists of atoms
 | 
					
						
							|  |  |  |   return to_buffer( buf, t, inp, widep, &atoms, lenp PASS_REGS); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  | 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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | #if USE_GEN_TYPE_ERROR
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | static yap_error_number | 
					
						
							| 
									
										
										
										
											2013-12-06 15:08:35 +00:00
										 |  |  | gen_type_error(int flags) { | 
					
						
							| 
									
										
										
										
											2013-12-10 18:55:58 +00:00
										 |  |  |   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)) | 
					
						
							| 
									
										
										
										
											2013-12-08 19:12:24 +00:00
										 |  |  |     return TYPE_ERROR_TEXT; | 
					
						
							| 
									
										
										
										
											2013-12-10 18:55:58 +00:00
										 |  |  |   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)) | 
					
						
							| 
									
										
										
										
											2013-12-08 19:12:24 +00:00
										 |  |  |     return TYPE_ERROR_ATOMIC; | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   if ((flags & (YAP_STRING_INT|YAP_STRING_FLOAT|YAP_STRING_BIG)) == | 
					
						
							| 
									
										
										
										
											2013-12-10 18:55:58 +00:00
										 |  |  |       (YAP_STRING_INT|YAP_STRING_FLOAT|YAP_STRING_BIG)) | 
					
						
							| 
									
										
										
										
											2013-12-08 19:12:24 +00:00
										 |  |  |     return TYPE_ERROR_NUMBER; | 
					
						
							| 
									
										
										
										
											2013-12-10 18:55:58 +00:00
										 |  |  |   if (flags & YAP_STRING_ATOM ) | 
					
						
							| 
									
										
										
										
											2013-12-06 15:08:35 +00:00
										 |  |  |     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 17:05:36 -05:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2013-12-06 15:08:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |  void * | 
					
						
							|  |  |  | Yap_readText( void *buf, seq_tv_t *inp, encoding_t *enc, int *minimal, size_t *lengp USES_REGS) | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |   char  *s, *s0 = buf; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   wchar_t *ws; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |   bool wide; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* we know what the term is */ | 
					
						
							| 
									
										
										
										
											2015-10-08 02:23:45 +01:00
										 |  |  |   if ( !(inp->type & (YAP_STRING_CHARS|YAP_STRING_WCHARS))) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |       if ( !(inp->type & YAP_STRING_TERM)) { | 
					
						
							| 
									
										
										
										
											2016-02-14 04:18:15 +00:00
										 |  |  |         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; | 
					
						
							| 
									
										
										
										
											2015-10-13 01:59:50 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |   if (LOCAL_Error_TYPE != YAP_NO_ERROR) | 
					
						
							|  |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											2016-02-14 04:18:15 +00:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |     // this is a term, extract the UTF8 representation
 | 
					
						
							| 
									
										
										
										
											2015-10-13 01:59:50 +01:00
										 |  |  |   if ( IsStringTerm(inp->val.t) && | 
					
						
							|  |  |  |        inp->type & YAP_STRING_STRING) { | 
					
						
							|  |  |  |     const char *s = StringOfTerm(inp->val.t); | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |     *enc = ENC_ISO_UTF8; | 
					
						
							|  |  |  |     *minimal = FALSE; | 
					
						
							|  |  |  |     if (lengp) | 
					
						
							|  |  |  |       *lengp = strlen(s); | 
					
						
							|  |  |  |     return (void *)s; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-10-13 01:59:50 +01:00
										 |  |  |    if ( IsAtomTerm(inp->val.t) | 
					
						
							|  |  |  | 	&& inp->type & YAP_STRING_ATOM) { | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |     // this is a term, extract to a buffer, and representation is wide
 | 
					
						
							|  |  |  |     *minimal = TRUE; | 
					
						
							|  |  |  |     Atom at = AtomOfTerm(inp->val.t); | 
					
						
							|  |  |  |     if (IsWideAtom(at)) { | 
					
						
							|  |  |  |       ws = at->WStrOfAE; | 
					
						
							|  |  |  |       *lengp = wcslen(ws); | 
					
						
							|  |  |  |       *enc = ENC_WCHAR; | 
					
						
							|  |  |  |       return ws; | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |       s = (char *)at->StrOfAE; | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |       *lengp = strlen(s); | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |       *enc = ENC_ISO_LATIN1; | 
					
						
							|  |  |  |       return s; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |    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); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |     // this is a term, extract to a sfer, and representation is wide
 | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |     *minimal = true; | 
					
						
							|  |  |  |     *enc = ENC_ISO_UTF8; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |     return s; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |        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); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |     // this is a term, extract to a buffer, and representation is wide
 | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |     *minimal = true; | 
					
						
							|  |  |  |     *enc = ENC_ISO_UTF8; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |     return s; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |   } | 
					
						
							|  |  |  |   if (inp->type & YAP_STRING_INT && IsIntegerTerm(inp->val.t)) { | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |     if (s0) s = s0; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |       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); | 
					
						
							| 
									
										
										
										
											2015-02-03 02:36:51 +00:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |       *enc = ENC_ISO_LATIN1; | 
					
						
							|  |  |  |       *lengp = strlen(s); | 
					
						
							|  |  |  |       return s; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |   if (inp->type & YAP_STRING_FLOAT && IsFloatTerm(inp->val.t)) { | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |       if (s0) s = s0; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |       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); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |       *lengp = strlen(s); | 
					
						
							|  |  |  |       *enc = ENC_ISO_LATIN1; | 
					
						
							|  |  |  |       return s; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-12-16 15:35:34 +00:00
										 |  |  | #if USE_GMP
 | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |   if (inp->type & YAP_STRING_BIG && IsBigIntTerm(inp->val.t)) { | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |       if (s0) s = s0; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |       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; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-12-16 15:35:34 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |    if (inp->type & YAP_STRING_TERM) | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |       char *s, *o; | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |       if (s0) s = s0; | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |       else s = Yap_PreAllocCodeSpace(); | 
					
						
							|  |  |  |       size_t sz = LOCAL_MAX_SIZE-1; | 
					
						
							| 
									
										
										
										
											2015-10-08 02:23:45 +01:00
										 |  |  |       encoding_t enc = ENC_ISO_UTF8; | 
					
						
							|  |  |  |       o = Yap_TermToString(inp->val.t, s, sz, lengp, &enc, 0); | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |       return s; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |    if (inp->type & YAP_STRING_CHARS) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |       *enc = inp->enc; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |       if (inp->type & YAP_STRING_NCHARS) | 
					
						
							|  |  |  | 	*lengp = inp->sz; | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  | 	*lengp = strlen(inp->val.c); | 
					
						
							|  |  |  |       return (void *)inp->val.c; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |     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; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static Term | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  | write_strings( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng USES_REGS) | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |   size_t min = 0, max = leng; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   if (out->type & (YAP_STRING_NCHARS|YAP_STRING_TRUNC)) { | 
					
						
							|  |  |  |     if (out->type & YAP_STRING_NCHARS) min = out->sz; | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |     if (out->type & YAP_STRING_TRUNC && out->max < max) max = out->max; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   switch (enc) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   case ENC_ISO_UTF8: | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |     { unsigned char *s = s0, *lim = s + (max = strlen_utf8(s)); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       Term t = init_tstring( PASS_REGS1  ); | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |       unsigned char *cp = s, *buf; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |       LOCAL_TERM_ERROR( t, 2*(lim-s) ); | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |       buf = buf_from_tstring(HR); | 
					
						
							| 
									
										
										
										
											2013-12-05 21:26:46 +00:00
										 |  |  |       while (*cp && cp < lim) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2016-02-18 12:10:58 +00:00
										 |  |  | 	cp +=  get_utf8(cp, -1, &chr); | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	buf += put_utf8(buf, chr); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |       if (max >= min) *buf++ = '\0'; | 
					
						
							|  |  |  |       else while (max < min) { | 
					
						
							|  |  |  | 	max++; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	buf += put_utf8(buf, '\0'); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       close_tstring( buf  PASS_REGS ); | 
					
						
							|  |  |  |       out->val.t = t; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     break; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   case ENC_ISO_LATIN1: | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |     { unsigned char *s = s0, *lim = s + (max = strlen_latin_utf8(s0)); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       Term t = init_tstring( PASS_REGS1  ); | 
					
						
							| 
									
										
										
										
											2013-12-05 11:20:57 +00:00
										 |  |  |       unsigned char *cp = s; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |       unsigned char *buf; | 
					
						
							|  |  |  |       utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |       LOCAL_TERM_ERROR( t, 2*(lim-s) ); | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |       buf = buf_from_tstring(HR); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       while (cp < lim) { | 
					
						
							| 
									
										
										
										
											2015-10-08 02:23:45 +01:00
										 |  |  | 	cp = getChar(cp, &chr); | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	buf += put_utf8(buf, chr); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |       if (max >= min) *buf++ = '\0'; | 
					
						
							|  |  |  |       else while (max < min) { | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	  max++; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	  buf += put_utf8(buf, chr); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       close_tstring( buf  PASS_REGS ); | 
					
						
							|  |  |  |       out->val.t = t; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     break; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   case ENC_WCHAR: | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |     { wchar_t *s = s0, *lim = s + (max = strlen_ucs2_utf8(s0)); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       Term t = init_tstring( PASS_REGS1  ); | 
					
						
							|  |  |  |       wchar_t *wp = s; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |       unsigned char *buf; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |       LOCAL_TERM_ERROR( t, 2*(lim-s) ); | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |       buf = buf_from_tstring(HR); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       while (wp < lim) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2015-10-08 02:23:45 +01:00
										 |  |  | 	wp = getWchar(wp, &chr); | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	buf += put_utf8(buf, chr); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |       if (max >= min) *buf++ = '\0'; | 
					
						
							|  |  |  |       else while (max < min) { | 
					
						
							|  |  |  | 	max++; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	buf += put_utf8(buf,  '\0'); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |       close_tstring( buf  PASS_REGS ); | 
					
						
							|  |  |  |       out->val.t = t; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |     break; | 
					
						
							|  |  |  |   default: | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |     Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc), __FUNCTION__); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-12-05 11:20:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   return out->val.t; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static Term | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  | write_atoms( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng USES_REGS) | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   Term t = AbsPair(HR); | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |   size_t sz = 0; | 
					
						
							| 
									
										
										
										
											2015-01-05 16:06:32 +00:00
										 |  |  |   size_t max = leng; | 
					
						
							| 
									
										
										
										
											2014-12-02 01:46:03 +00:00
										 |  |  |   if (leng == 0) { | 
					
						
							|  |  |  |     out->val.t = t; | 
					
						
							|  |  |  |     return TermNil; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   if (out->type & (YAP_STRING_NCHARS|YAP_STRING_TRUNC)) { | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |     if (out->type & YAP_STRING_TRUNC && out->max < max) max = out->max; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   switch (enc) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   case ENC_ISO_UTF8: | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |     { unsigned char *s = s0, *lim = s + strnlen((char*)s, max); | 
					
						
							|  |  |  |       unsigned char *cp = s; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       wchar_t w[2]; | 
					
						
							|  |  |  |       w[1] = '\0'; | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |      LOCAL_TERM_ERROR( t, 2*(lim-s) ); | 
					
						
							| 
									
										
										
										
											2014-12-02 01:46:03 +00:00
										 |  |  |       while (cp < lim && *cp) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2014-12-02 01:46:03 +00:00
										 |  |  | 	CELL *cl; | 
					
						
							| 
									
										
										
										
											2016-02-18 12:10:58 +00:00
										 |  |  | 	cp +=  get_utf8(cp, -1, &chr); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | 	if (chr == '\0') break; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 	w[0] = chr; | 
					
						
							| 
									
										
										
										
											2014-12-02 01:46:03 +00:00
										 |  |  | 	cl = HR; | 
					
						
							|  |  |  |         HR += 2; | 
					
						
							|  |  |  | 	cl[0] = MkAtomTerm(Yap_LookupMaybeWideAtom(w)); | 
					
						
							|  |  |  | 	cl[1] = AbsPair(HR); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 	sz++; | 
					
						
							|  |  |  | 	if (sz == max) break; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   case ENC_ISO_LATIN1: | 
					
						
							| 
									
										
										
										
											2013-12-05 11:20:57 +00:00
										 |  |  |     { unsigned char *s = s0, *lim = s + strnlen(s0, max); | 
					
						
							|  |  |  |       unsigned char *cp = s; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       char w[2]; | 
					
						
							|  |  |  |       w[1] = '\0'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |       LOCAL_TERM_ERROR( t, 2*(lim-s) ); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       while (cp < lim) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2015-10-08 02:23:45 +01:00
										 |  |  | 	cp = getChar(cp, &chr); | 
					
						
							| 
									
										
										
										
											2014-12-02 01:46:03 +00:00
										 |  |  | 	if (chr == '\0') break; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 	w[0] = chr; | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  | 	HR[0] = MkAtomTerm(Yap_LookupAtom(w)); | 
					
						
							|  |  |  | 	HR[1] = AbsPair(HR+2); | 
					
						
							|  |  |  | 	HR += 2; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 	sz++; | 
					
						
							|  |  |  | 	if (sz == max) break; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   case ENC_WCHAR: | 
					
						
							| 
									
										
										
										
											2014-12-02 01:46:03 +00:00
										 |  |  |     { wchar_t *s = s0, *lim = s + wcsnlen(s, max); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       wchar_t *cp = s; | 
					
						
							|  |  |  |       wchar_t w[2]; | 
					
						
							|  |  |  |       w[1] = '\0'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |       LOCAL_TERM_ERROR( t, 2*(lim-s) ); | 
					
						
							| 
									
										
										
										
											2013-12-05 21:26:46 +00:00
										 |  |  |       while (*cp && cp < lim) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2015-10-08 02:23:45 +01:00
										 |  |  | 	cp = getWchar(cp, &chr); | 
					
						
							| 
									
										
										
										
											2014-12-02 01:46:03 +00:00
										 |  |  |         if (chr == '\0') break; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 	w[0] = chr; | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  | 	HR[0] = MkAtomTerm(Yap_LookupMaybeWideAtom(w)); | 
					
						
							|  |  |  | 	HR[1] = AbsPair(HR+2); | 
					
						
							|  |  |  | 	HR += 2; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 	sz++; | 
					
						
							|  |  |  | 	if (sz == max) break; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |       break; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   default: | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |     Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc), __FUNCTION__); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   if (out->type & YAP_STRING_DIFF) { | 
					
						
							|  |  |  |     if (sz == 0) t = out->dif; | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |     else HR[-1] = Globalize(out->dif PASS_REGS); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   } else { | 
					
						
							|  |  |  |     if (sz == 0) t = TermNil; | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |     else HR[-1] = TermNil; | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   out->val.t = t; | 
					
						
							|  |  |  |   return (t); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static Term | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  | write_codes( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng USES_REGS) | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   Term t = AbsPair(HR); | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |   size_t min = 0, max = leng; | 
					
						
							|  |  |  |   size_t sz = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   if (out->type & (YAP_STRING_NCHARS|YAP_STRING_TRUNC)) { | 
					
						
							|  |  |  |     if (out->type & YAP_STRING_NCHARS) min = out->sz; | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |     if (out->type & YAP_STRING_TRUNC && out->max < max) max = out->max; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   switch (enc) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   case ENC_ISO_UTF8: | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |     { unsigned char *s = s0, *lim = s + strnlen(s0, max); | 
					
						
							|  |  |  |       unsigned char *cp = s; | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |       LOCAL_TERM_ERROR( t, 2*(lim-s) ); | 
					
						
							| 
									
										
										
										
											2013-12-05 21:26:46 +00:00
										 |  |  |       while (*cp && cp < lim) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2016-02-18 12:10:58 +00:00
										 |  |  | 	cp +=  get_utf8(cp, -1, &chr); | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  | 	HR[0] = MkIntTerm(chr); | 
					
						
							|  |  |  | 	HR[1] = AbsPair(HR+2); | 
					
						
							|  |  |  | 	HR += 2; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 	sz++; | 
					
						
							|  |  |  | 	if (sz == max) break; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   case ENC_ISO_LATIN1: | 
					
						
							| 
									
										
										
										
											2013-12-05 11:20:57 +00:00
										 |  |  |     { unsigned char *s = s0, *lim = s + strnlen(s0, max); | 
					
						
							|  |  |  |       unsigned char *cp = s; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |       LOCAL_TERM_ERROR( t, 2*(lim-s) ); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       while (cp < lim) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2015-10-08 02:23:45 +01:00
										 |  |  | 	cp = getChar(cp, &chr); | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  | 	HR[0] = MkIntTerm(chr); | 
					
						
							|  |  |  | 	HR[1] = AbsPair(HR+2); | 
					
						
							|  |  |  | 	HR += 2; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 	sz++; | 
					
						
							|  |  |  | 	if (sz == max) break; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |  case ENC_WCHAR: | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |     { wchar_t *s = s0, *lim = s + wcsnlen(s, max); | 
					
						
							|  |  |  |       wchar_t *cp = s; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |       LOCAL_TERM_ERROR( t, 2*(lim-s) ); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       while (cp < lim) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2015-10-08 02:23:45 +01:00
										 |  |  | 	cp = getWchar(cp, &chr); | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  | 	HR[0] = MkIntTerm(chr); | 
					
						
							|  |  |  | 	HR[1] = AbsPair(HR+2); | 
					
						
							|  |  |  | 	HR += 2; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 	sz++; | 
					
						
							|  |  |  | 	if (sz == max) break; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |       break; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   default: | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |     Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc), __FUNCTION__); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   while (sz < min) { | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |     HR[0] = MkIntTerm(MkIntTerm(0)); | 
					
						
							|  |  |  |     HR[1] = AbsPair(HR+2); | 
					
						
							|  |  |  |     HR += 2; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |     sz++; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (out->type & YAP_STRING_DIFF) { | 
					
						
							|  |  |  |     if (sz == 0) t = out->dif; | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |     else HR[-1] = Globalize(out->dif PASS_REGS); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   } else { | 
					
						
							|  |  |  |     if (sz == 0) t = TermNil; | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |     else HR[-1] = TermNil; | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   out->val.t = t; | 
					
						
							|  |  |  |   return (t); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static Atom | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  | write_atom( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng USES_REGS) | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |   size_t max = leng; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   if (out->type & (YAP_STRING_NCHARS|YAP_STRING_TRUNC)) { | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |      if (out->type & YAP_STRING_TRUNC && out->max < max) max = out->max; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   switch (enc) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   case ENC_ISO_UTF8: | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |     { unsigned char *s = s0, *lim = s + strnlen(s0, max); | 
					
						
							| 
									
										
										
										
											2016-02-11 06:00:56 -08:00
										 |  |  |       wchar_t *buf = malloc(sizeof(wchar_t)*((lim+2)-s)), *ptr = buf; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       Atom at; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 21:26:46 +00:00
										 |  |  |       while (*s && s < lim) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2016-02-18 12:10:58 +00:00
										 |  |  | 	s +=  get_utf8(s,-1, &chr); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 	*ptr++ = chr; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2013-12-05 21:26:46 +00:00
										 |  |  |       *ptr++ = '\0'; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |       at = Yap_LookupMaybeWideAtomWithLength( buf, max ); | 
					
						
							|  |  |  |       free( buf ); | 
					
						
							|  |  |  |       out->val.a = at; | 
					
						
							|  |  |  |       return at; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   case ENC_ISO_LATIN1: | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |     { char *s = s0; | 
					
						
							|  |  |  |       Atom at; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       max = strnlen(s, max); | 
					
						
							|  |  |  |       at = Yap_LookupAtomWithLength(s, max); | 
					
						
							|  |  |  |       out->val.a = at; | 
					
						
							|  |  |  |       return at; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |  case ENC_WCHAR: | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |     { wchar_t *s = s0; | 
					
						
							|  |  |  |       Atom at; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       max = wcsnlen(s, max); | 
					
						
							|  |  |  |       out->val.a = at = Yap_LookupMaybeWideAtomWithLength(s, max); | 
					
						
							|  |  |  |       return at; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |    default: | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |      Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc)); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |   return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | 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->enc != enc || out->type & (YAP_STRING_WITH_BUFFER|YAP_STRING_MALLOC)) { | 
					
						
							|  |  |  |     if (enc != ENC_WCHAR) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |       sz = strlen((char *)s0)+1; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |       sz = wcslen((wchar_t *)s0)+1; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |     } | 
					
						
							|  |  |  |     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))); | 
					
						
							|  |  |  |       } else | 
					
						
							|  |  |  | 	return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     out->val.w = s0; | 
					
						
							|  |  |  |     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; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	 size_t n = wcslen( s )+1; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | 	 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: | 
					
						
							|  |  |  |       { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	unsigned char *s = s0, *lim = s + (max = strnlen(s0, max)); | 
					
						
							|  |  |  | 	unsigned char *cp = s; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | 	wchar_t *buf0, *buf; | 
					
						
							|  |  |  |        | 
					
						
							|  |  |  | 	buf = buf0 = out->val.w; | 
					
						
							|  |  |  | 	if (!buf) | 
					
						
							|  |  |  | 	  return -1; | 
					
						
							|  |  |  | 	while (*cp && cp < lim) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	  utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2016-02-18 12:10:58 +00:00
										 |  |  | 	  cp +=  get_utf8(cp, -1, &chr); | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | 	  *buf++ = chr; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (max >= min) *buf++ = '\0'; | 
					
						
							|  |  |  | 	else while (max < min) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	  utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | 	  max++; | 
					
						
							| 
									
										
										
										
											2016-02-18 12:10:58 +00:00
										 |  |  | 	  cp +=  get_utf8(cp, -1, &chr); | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | 	  *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: | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |      sz_end = -1; | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |      Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc), __FUNCTION__); | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   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; | 
					
						
							|  |  |  |  } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |  size_t | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | 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; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-11-05 16:12:51 +00:00
										 |  |  |   if (out->enc != enc) { | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |     size_t sz; | 
					
						
							|  |  |  |     if (enc != ENC_WCHAR) | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |       sz = strlen((char *)s0)+1; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |       sz = wcslen((wchar_t *)s0)+1; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |     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) { | 
					
						
							| 
									
										
										
										
											2015-10-13 01:59:50 +01:00
										 |  |  | 	out->val.c = Yap_PreAllocCodeSpace(); | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     out->val.c = s0; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   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; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	 size_t n = strlen( s )+1; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | 	 memcpy( out->val.c, s0, n*sizeof(wchar_t)); | 
					
						
							|  |  |  |    	 out->val.c[n] = '\0'; | 
					
						
							|  |  |  | 	 sz_end = n+1; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 	  sz_end = strlen(out->val.c)+1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	break; | 
					
						
							|  |  |  |    case ENC_ISO_LATIN1: | 
					
						
							|  |  |  |       { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	unsigned char *s = s0, *lim = s + (max = strnlen(s0, max)); | 
					
						
							| 
									
										
										
										
											2015-10-13 01:59:50 +01:00
										 |  |  | 	unsigned char *cp = s, *buf0, *buf;         | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  | 	buf = buf0 = s0; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | 	if (!buf) | 
					
						
							|  |  |  | 	  return -1; | 
					
						
							|  |  |  | 	while (*cp && cp < lim) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	  utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | 	  chr = *cp++; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	  buf += put_utf8(buf, chr); | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if (max >= min) *buf++ = '\0'; | 
					
						
							|  |  |  | 	else while (max < min) { | 
					
						
							|  |  |  | 	    max++; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	    utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | 	    chr = *cp++; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	    buf += put_utf8(buf, chr); | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | 	  } | 
					
						
							|  |  |  |         buf[0] = '\0'; | 
					
						
							|  |  |  | 	sz_end = (buf+1)-buf0; | 
					
						
							|  |  |  |       }  | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     case ENC_WCHAR: | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  | 	wchar_t *s = s0; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	unsigned char *buf =  out->val.uc; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | 	size_t n = wcslen( s ), i; | 
					
						
							|  |  |  | 	if (n < min) n = min; | 
					
						
							|  |  |  | 	for (i = 0; i < n; i++) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	  utf8proc_int32_t chr = s[i]; | 
					
						
							|  |  |  | 	  buf += put_utf8(buf, chr); | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	*buf++ = '\0'; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	sz_end = (buf+1)-out->val.uc; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |       } | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |    default: | 
					
						
							|  |  |  |         sz_end = -1; | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |     Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc), __FUNCTION__); | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |    } | 
					
						
							|  |  |  |   }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: | 
					
						
							|  |  |  |       { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	unsigned char *s = s0, *lim = s + (max = strnlen(s0, max)); | 
					
						
							|  |  |  | 	unsigned char *cp = s; | 
					
						
							|  |  |  | 	unsigned char *buf0, *buf; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |        | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	buf = buf0 = out->val.uc; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | 	if (!buf) | 
					
						
							|  |  |  | 	  return -1; | 
					
						
							|  |  |  | 	while (*cp && cp < lim) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	  utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2016-02-18 12:10:58 +00:00
										 |  |  | 	  cp +=  get_utf8(cp, -1, &chr); | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | 	  *buf++ = chr; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (max >= min) *buf++ = '\0'; | 
					
						
							|  |  |  | 	else while (max < min) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	  utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | 	  max++; | 
					
						
							| 
									
										
										
										
											2016-02-18 12:10:58 +00:00
										 |  |  | 	  cp +=  get_utf8(cp, -1, &chr); | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  | 	  *buf++ = chr; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |  	sz_end = buf-out->val.uc; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |      } | 
					
						
							|  |  |  |       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; | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |        Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc), __FUNCTION__); | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |   } else { | 
					
						
							|  |  |  |     // no other encodings are supported.
 | 
					
						
							|  |  |  |     sz_end = -1; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |    if (out->type & (YAP_STRING_MALLOC)) { | 
					
						
							|  |  |  |      out->val.c = realloc(out->val.c,sz_end); | 
					
						
							|  |  |  |    } | 
					
						
							|  |  |  |    out->sz = sz_end; | 
					
						
							|  |  |  |   return sz_end; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | static ssize_t | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  | write_length( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng USES_REGS) | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   size_t max = -1; | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |   if (out->type & (YAP_STRING_NCHARS|YAP_STRING_TRUNC)) { | 
					
						
							| 
									
										
										
										
											2013-12-08 19:12:24 +00:00
										 |  |  |     if (out->type & YAP_STRING_NCHARS && out->sz != (size_t)-1) return out->sz; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     if (out->type & YAP_STRING_TRUNC) max = out->max; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   switch (enc) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   case ENC_ISO_UTF8: | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |       const unsigned char *s = s0; | 
					
						
							|  |  |  |       return strlen_utf8(s); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   case ENC_ISO_LATIN1: | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       const char *s = s0; | 
					
						
							|  |  |  |       return strnlen(s, max); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   case ENC_WCHAR: | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       const wchar_t *s = s0; | 
					
						
							|  |  |  |       return wcsnlen(s, max); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   default: | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |     Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc), __FUNCTION__); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   return (size_t)-1; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static Term | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  | write_number( void *s0, seq_tv_t *out, encoding_t enc, int minimal, int size USES_REGS) | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |   return | 
					
						
							|  |  |  |     Yap_StringToNumberTerm(s0, &enc); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static Term | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  | string_to_term( void *s0, seq_tv_t *out, encoding_t enc, int minimal, size_t leng USES_REGS) | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-12-15 08:38:56 +00:00
										 |  |  |  Term o = out->val.t = Yap_StringToTerm(s0, strlen(s0)+1, &enc, GLOBAL_MaxPriority, NULL); | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |  return o; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-06 15:08:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  | write_Text( void *inp, seq_tv_t *out, encoding_t enc, int minimal, size_t leng USES_REGS) | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   /* we know what the term is */ | 
					
						
							|  |  |  |   switch (out->type &  YAP_TYPE_MASK) { | 
					
						
							|  |  |  |   case YAP_STRING_STRING: | 
					
						
							|  |  |  |     out->val.t = | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |       write_strings( inp, out, enc, minimal, leng PASS_REGS); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |     return out->val.t != 0; | 
					
						
							|  |  |  |   case YAP_STRING_ATOMS: | 
					
						
							|  |  |  |     out->val.t = | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |       write_atoms( inp, out, enc, minimal, leng PASS_REGS); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |     return out->val.t != 0; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   case YAP_STRING_CODES: | 
					
						
							|  |  |  |     out->val.t = | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |       write_codes( inp, out, enc, minimal, leng PASS_REGS); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |     return out->val.t != 0; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |   case YAP_STRING_LENGTH: | 
					
						
							|  |  |  |     out->val.l = | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |       write_length( inp, out, enc, minimal, leng PASS_REGS); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     return out->val.l != (ssize_t)(-1); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   case YAP_STRING_ATOM: | 
					
						
							|  |  |  |     out->val.a = | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |       write_atom( inp, out, enc, minimal, leng PASS_REGS); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |     return out->val.a != NULL; | 
					
						
							| 
									
										
										
										
											2015-10-18 11:47:01 +01:00
										 |  |  |   case YAP_STRING_INT|YAP_STRING_FLOAT|YAP_STRING_BIG: | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |     out->val.t = | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |       write_number( inp, out, enc, minimal, leng PASS_REGS); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |     return out->val.t != 0; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   case YAP_STRING_CHARS: | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |     { | 
					
						
							|  |  |  |       size_t sz = write_buffer( inp, out, enc, minimal, leng PASS_REGS); | 
					
						
							|  |  |  |       return((Int)sz > 0); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   case YAP_STRING_WCHARS: | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |     { | 
					
						
							|  |  |  |       size_t sz = write_wbuffer( inp, out, enc, minimal, leng PASS_REGS); | 
					
						
							|  |  |  |       return((Int)sz > 0); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |    default: | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |     if (!(out->type & YAP_STRING_TERM)) | 
					
						
							|  |  |  |       return 0; | 
					
						
							|  |  |  |     if (out->type & (YAP_STRING_INT|YAP_STRING_FLOAT|YAP_STRING_BIG)) | 
					
						
							|  |  |  |       if ((out->val.t = | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  | 	   write_number( inp, out, enc, minimal, leng PASS_REGS)) != 0L) | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 	return out->val.t != 0; | 
					
						
							|  |  |  |     if (out->type & (YAP_STRING_ATOM)) | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |       if (write_atom( inp, out, enc, minimal, leng PASS_REGS) != NIL) { | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 	Atom at = out->val.a; | 
					
						
							|  |  |  | 	if (at != NIL) | 
					
						
							|  |  |  | 	  out->val.t = MkAtomTerm(at); | 
					
						
							|  |  |  | 	return at != NIL; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if ((out->val.t = | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  | 	   string_to_term( inp, out, enc, minimal, leng PASS_REGS)) != 0L) | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 	return out->val.t != 0; | 
					
						
							| 
									
										
										
										
											2015-08-07 16:57:53 -05:00
										 |  |  |     } | 
					
						
							|  |  |  |   return FALSE; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |  int | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | Yap_CVT_Text( seq_tv_t *inp, seq_tv_t *out USES_REGS) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   encoding_t enc; | 
					
						
							|  |  |  |   int minimal = FALSE; | 
					
						
							|  |  |  |   char *buf; | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |   size_t leng; | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   buf = Yap_readText( NULL, inp, &enc, &minimal, &leng PASS_REGS ); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  |   if (!buf) | 
					
						
							|  |  |  |     return 0L; | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |   return write_Text( buf, out, enc, minimal, leng PASS_REGS ); | 
					
						
							| 
									
										
										
										
											2013-12-02 14:50:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | static void * | 
					
						
							|  |  |  | compute_end( void *s0, encoding_t enc ) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   switch (enc) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   case ENC_ISO_LATIN1: | 
					
						
							|  |  |  |   case ENC_ISO_UTF8: | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       char *s = (char *)s0; | 
					
						
							|  |  |  |       return s+(1+strlen(s)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   case ENC_WCHAR: | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       wchar_t *s = (wchar_t *)s0; | 
					
						
							|  |  |  |       return s + (1+wcslen(s)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |  default: | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |     Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc), __FUNCTION__); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void * | 
					
						
							|  |  |  | advance_Text( void *s, int l, encoding_t enc ) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   switch (enc) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   case ENC_ISO_LATIN1: | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     return ((char *)s)+l; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   case ENC_ISO_UTF8:    | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |     return (char *)skip_utf8(s,l); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   case ENC_WCHAR: | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     return ((wchar_t *)s)+l; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   default: | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |     Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc), __FUNCTION__); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   return s; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2013-12-08 19:12:24 +00:00
										 |  |  | cmp_Text( void *s1, void *s2, int l, encoding_t enc1, encoding_t enc2 ) | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-12-08 19:12:24 +00:00
										 |  |  |   Int i; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |   switch (enc1) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   case ENC_ISO_LATIN1: | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       char *w1 = (char *)s1; | 
					
						
							|  |  |  |       switch (enc2) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |       case ENC_ISO_LATIN1: | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	return strncmp(s1, s2, l); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |       case ENC_ISO_UTF8: | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	  utf8proc_int32_t chr1, chr2; | 
					
						
							|  |  |  | 	  unsigned char *w2 = s2; | 
					
						
							| 
									
										
										
										
											2016-02-18 12:10:58 +00:00
										 |  |  | 	  for (i = 0; i < l; i++) { chr1 = *w1++; w2 +=  get_utf8(w2, -1, &chr2); if (chr1-chr2) return chr1-chr2; } | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |     case ENC_WCHAR: | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	  utf8proc_int32_t chr1, chr2; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	  wchar_t *w2 = s2; | 
					
						
							|  |  |  | 	  for (i = 0; i < l; i++) { chr1 = *w1++; chr2 = *w2++; if (chr1-chr2) return chr1-chr2; } | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |       default: | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  | 	Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc2), __FUNCTION__); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |      } | 
					
						
							|  |  |  |   case ENC_ISO_UTF8: | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |       unsigned char *w1 = s1; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       switch (enc2) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |       case ENC_ISO_LATIN1: | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	  utf8proc_int32_t chr1, chr2; | 
					
						
							|  |  |  | 	  unsigned char *w2 = s2; | 
					
						
							| 
									
										
										
										
											2016-02-18 12:10:58 +00:00
										 |  |  | 	  for (i = 0; i < l; i++) { chr2 = *w2++; w1 += get_utf8(w1, -1, &chr1); if (chr1-chr2) return chr1-chr2; } | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |       case ENC_ISO_UTF8: | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	  utf8proc_int32_t chr1, chr2; | 
					
						
							|  |  |  | 	  unsigned char *w2 = s2; | 
					
						
							| 
									
										
										
										
											2016-02-18 12:10:58 +00:00
										 |  |  | 	  for (i = 0; i < l; i++) { w2 +=  get_utf8(w2, -1, &chr2); w1 +=  get_utf8(w1,-1, &chr1); if (chr1-chr2) return chr1-chr2; } | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |     case ENC_WCHAR: | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	  utf8proc_int32_t chr1, chr2; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	  wchar_t *w2 = s2; | 
					
						
							| 
									
										
										
										
											2016-02-18 12:10:58 +00:00
										 |  |  | 	  for (i = 0; i < l; i++) { chr2 = *w2++; w1 +=  get_utf8(w1, -1, &chr1); if (chr1-chr2) return chr1-chr2; } | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |       default: | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  | 	Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc2), __FUNCTION__); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |      } | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   case ENC_WCHAR: | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       wchar_t *w1 = (wchar_t *)s1; | 
					
						
							|  |  |  |       switch (enc2) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |       case ENC_ISO_LATIN1: | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	  utf8proc_int32_t chr1, chr2; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	  char *w2 = s2; | 
					
						
							|  |  |  | 	  for (i = 0; i < l; i++) { chr1 = *w1++; chr2 = *w2++; if (chr1-chr2) return chr1-chr2; } | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |       case ENC_ISO_UTF8: | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	  utf8proc_int32_t chr1, chr2; | 
					
						
							|  |  |  | 	  unsigned char *w2 = s2; | 
					
						
							| 
									
										
										
										
											2016-02-18 12:10:58 +00:00
										 |  |  | 	  for (i = 0; i < l; i++) { chr1 = *w1++; w2 +=  get_utf8(w2, -1, &chr2); if (chr1-chr2) return chr1-chr2; } | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |     case ENC_WCHAR: | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	return wcsncmp(s1, s2, l); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |       default: | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  | 	Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc2), __FUNCTION__); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |       Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "Unsupported Encoding ~s in %s", enc_name(enc1), __FUNCTION__); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void * | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  | concat( int n, seq_tv_t *out, void *sv[], encoding_t encv[], size_t lengv[] USES_REGS ) | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   if (out->type == YAP_STRING_STRING) { | 
					
						
							|  |  |  |     /* we assume we concatenate strings only, or ASCII stuff like numbers */ | 
					
						
							|  |  |  |     Term t = init_tstring( PASS_REGS1  ); | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |     unsigned char *buf = buf_from_tstring(HR); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     int i; | 
					
						
							|  |  |  |     for (i = 0; i < n; i++) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |       if (encv[i] == ENC_WCHAR) { | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	wchar_t *ptr = sv[i]; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	utf8proc_int32_t chr; | 
					
						
							|  |  |  | 	while ( (chr = *ptr++) ) buf += put_utf8(buf,  chr); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |       } else if (encv[i] == ENC_ISO_LATIN1) { | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	char *ptr = sv[i]; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	utf8proc_int32_t chr; | 
					
						
							|  |  |  | 	while ( (chr = *ptr++) ) buf += put_utf8(buf,  chr); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       } else { | 
					
						
							|  |  |  | 	char *ptr = sv[i]; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	while ( (chr = *ptr++) ) *buf++ = chr; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     *buf ++ = '\0'; | 
					
						
							|  |  |  |     close_tstring( buf  PASS_REGS ); | 
					
						
							|  |  |  |     out->val.t = t; | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |     return HR; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |     encoding_t enc = ENC_ISO_LATIN1; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     size_t sz = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  |     for (i = 0; i < n; i++) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |       if (encv[i] != ENC_ISO_LATIN1) { | 
					
						
							|  |  |  | 	enc = ENC_WCHAR; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |       sz += write_length(sv[i], out, encv[i], FALSE, lengv[i] PASS_REGS); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |     if (enc == ENC_WCHAR) { | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       /* wide atom */ | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |       wchar_t *buf = (wchar_t *)HR; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       Atom at; | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |       LOCAL_ERROR( MkAtomTerm(Yap_LookupWideAtom(buf)), sz+3 ); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       for (i = 0; i < n ; i ++) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  | 	if (encv[i] == ENC_WCHAR) { | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	  wchar_t *ptr = sv[i]; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	  utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	  while ( (chr = *ptr++) != '\0' ) *buf++ = chr; | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  | 	} else if (encv[i] == ENC_ISO_LATIN1) { | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	  char *ptr = sv[i]; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	  utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2013-12-08 19:12:24 +00:00
										 |  |  | 	  while ( (chr = *ptr++) != '\0' ) *buf++ = (unsigned char)chr; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	  unsigned char *ptr = sv[i]; | 
					
						
							|  |  |  | 	  utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2016-02-18 12:10:58 +00:00
										 |  |  | 	  while ( (ptr +=  get_utf8( ptr, -1, &chr )) != NULL ) { if (chr == '\0') break; else *buf++ = chr; } | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       *buf++ = '\0'; | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |       at = out->val.a = Yap_LookupWideAtom((wchar_t *)HR); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       return at; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       /* atom */ | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |       char *buf = (char *)HR; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       Atom at; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |       LOCAL_ERROR( MkAtomTerm(Yap_LookupAtom(buf)), sz/sizeof(CELL)+3 ); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       for (i = 0; i < n ; i ++) { | 
					
						
							|  |  |  | 	char *ptr = sv[i]; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  | 	utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	while ( (chr = *ptr++) != '\0' ) *buf++ = chr; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       *buf++ = '\0'; | 
					
						
							| 
									
										
										
										
											2014-05-25 20:47:35 +01:00
										 |  |  |       at = out->val.a = Yap_LookupAtom((const char *)HR); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       return at; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void * | 
					
						
							| 
									
										
										
										
											2013-12-08 19:12:24 +00:00
										 |  |  | slice( size_t min, size_t max, void *buf, seq_tv_t *out, encoding_t enc USES_REGS ) | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   if (out->type == YAP_STRING_STRING) { | 
					
						
							|  |  |  |     /* we assume we concatenate strings only, or ASCII stuff like numbers */ | 
					
						
							|  |  |  |     Term t = init_tstring( PASS_REGS1  ); | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |     unsigned char *nbuf = buf_from_tstring(HR); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |     if (enc == ENC_WCHAR) { | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       wchar_t *ptr = (wchar_t *)buf + min; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |       utf8proc_int32_t chr; | 
					
						
							|  |  |  |       while ( min++ < max ) { chr = *ptr++; nbuf += put_utf8(nbuf, chr); } | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |     } else if (enc == ENC_ISO_LATIN1) { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |       unsigned char *ptr = (unsigned char *)buf + min; | 
					
						
							|  |  |  |       utf8proc_int32_t chr; | 
					
						
							|  |  |  |       while ( min++ < max ) { chr = *ptr++; nbuf += put_utf8(nbuf, chr); } | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |        unsigned char *ptr = skip_utf8 (buf, min ); | 
					
						
							|  |  |  |       utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2013-12-08 19:12:24 +00:00
										 |  |  |       if (!ptr) return NULL; | 
					
						
							| 
									
										
										
										
											2016-02-18 12:10:58 +00:00
										 |  |  |       while ( min++ < max ) { ptr +=  get_utf8(ptr, -1, & chr); nbuf += put_utf8(nbuf, chr); } | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     *nbuf ++ = '\0'; | 
					
						
							| 
									
										
										
										
											2013-12-08 19:12:24 +00:00
										 |  |  |     close_tstring( nbuf  PASS_REGS ); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     out->val.t = t; | 
					
						
							| 
									
										
										
										
											2013-12-08 19:12:24 +00:00
										 |  |  |     return (void *)StringOfTerm(t); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |   } else { | 
					
						
							|  |  |  |     Atom at; | 
					
						
							|  |  |  |      /* atom */ | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |     if (enc == ENC_WCHAR) { | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       /* wide atom */ | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |       wchar_t *nbuf = (wchar_t *)HR; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       wchar_t *ptr = (wchar_t *)buf + min; | 
					
						
							| 
									
										
										
										
											2013-12-08 19:12:24 +00:00
										 |  |  |       if (max>min) { | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  | 	LOCAL_ERROR( MkAtomTerm(Yap_LookupWideAtom(buf)), (max-min)*sizeof(wchar_t) ); | 
					
						
							| 
									
										
										
										
											2013-12-08 19:12:24 +00:00
										 |  |  | 	memcpy( nbuf, ptr, (max - min)*sizeof(wchar_t)); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       nbuf[max-min] = '\0'; | 
					
						
							|  |  |  |       at = Yap_LookupMaybeWideAtom( nbuf ); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |     } else if (enc == ENC_ISO_LATIN1) { | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       /*  atom */ | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |       char *nbuf = (char *)HR; | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-08 19:12:24 +00:00
										 |  |  |       if (max>min) { | 
					
						
							|  |  |  | 	char *ptr = (char *)buf + min; | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  | 	LOCAL_ERROR( MkAtomTerm(Yap_LookupAtom(buf)), max-min ); | 
					
						
							| 
									
										
										
										
											2013-12-08 19:12:24 +00:00
										 |  |  | 	memcpy( nbuf, ptr, (max - min)); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       nbuf[max-min] = '\0'; | 
					
						
							|  |  |  |       at = Yap_LookupAtom( nbuf ); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       /*  atom */ | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |       wchar_t *nbuf = (wchar_t *)HR; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:05:36 -05:00
										 |  |  |        unsigned char *ptr = skip_utf8 ( ( unsigned char *)buf, min ); | 
					
						
							|  |  |  |       utf8proc_int32_t chr; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 10:57:26 +01:00
										 |  |  |       LOCAL_ERROR( MkAtomTerm(Yap_LookupAtom(buf)), max-min ); | 
					
						
							| 
									
										
										
										
											2016-02-18 12:10:58 +00:00
										 |  |  |       while ( min++ < max ) { ptr +=  get_utf8(ptr, -1, & chr); *nbuf++ = chr; } | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       nbuf[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2014-01-19 21:15:05 +00:00
										 |  |  |       at = Yap_LookupMaybeWideAtom( (wchar_t*)HR ); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     out->val.a = at; | 
					
						
							| 
									
										
										
										
											2013-12-06 15:08:35 +00:00
										 |  |  |     return at->StrOfAE; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   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; | 
					
						
							| 
									
										
										
										
											2014-06-14 10:27:39 +01:00
										 |  |  |   void **bufv; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |   int minimal = FALSE; | 
					
						
							|  |  |  |   void *buf; | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |   size_t leng, *lengv; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |   int i; | 
					
						
							|  |  |  |   Term t = ARG1; | 
					
						
							|  |  |  |   bufv = (void **)malloc(n*sizeof(void *)); | 
					
						
							|  |  |  |   HEAP_TERM_ERROR(bufv, void *); | 
					
						
							|  |  |  |   encv = (encoding_t *)malloc(n*sizeof(encoding_t)); | 
					
						
							|  |  |  |   HEAP_ERROR(encv, encoding_t); | 
					
						
							|  |  |  |   buf = NULL; | 
					
						
							|  |  |  |   for (i = 0 ; i < n ; i++) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |     void *nbuf = Yap_readText( buf, inp+i, encv+i, &minimal, &leng PASS_REGS ); | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     if (!nbuf) | 
					
						
							|  |  |  |       return 0L; | 
					
						
							|  |  |  |     bufv[i] = nbuf; | 
					
						
							|  |  |  |     if ((char *)nbuf >= AuxBase &&  (char *)nbuf < AuxTop) { | 
					
						
							|  |  |  |       buf = compute_end( nbuf, encv[i] ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-06-14 10:27:39 +01:00
										 |  |  |   lengv = (size_t *)malloc(n*sizeof(size_t)); | 
					
						
							|  |  |  |   HEAP_ERROR(lengv, size_t); | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |   buf = concat(n, out, bufv, encv, lengv PASS_REGS); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |   return buf; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | //
 | 
					
						
							|  |  |  | // out must be an atom or a string
 | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | void * | 
					
						
							| 
									
										
										
										
											2013-12-06 15:08:35 +00:00
										 |  |  | Yap_Splice_Text( int n,  size_t cuts[], seq_tv_t *inp, encoding_t encv[], seq_tv_t outv[] USES_REGS) | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   encoding_t enc; | 
					
						
							|  |  |  |   int minimal = FALSE; | 
					
						
							| 
									
										
										
										
											2013-12-06 15:08:35 +00:00
										 |  |  |   void *buf, *store; | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |   size_t l, leng; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |   int i, min; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  |   buf = Yap_readText( NULL, inp, &enc, &minimal, &leng PASS_REGS ); | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |   if (!buf) | 
					
						
							|  |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |   l = write_length( buf, inp, enc, minimal, leng PASS_REGS); | 
					
						
							| 
									
										
										
										
											2013-12-06 15:08:35 +00:00
										 |  |  |   /* where to allocate next is the most complicated part */ | 
					
						
							|  |  |  |   if ((char *)buf >= AuxBase &&  (char *)buf < AuxTop) { | 
					
						
							|  |  |  |     store = compute_end( buf, enc ); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     store = NULL; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (!cuts) { | 
					
						
							|  |  |  |     if (n == 2) { | 
					
						
							|  |  |  |       size_t l0, l1; | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  |       size_t leng0, leng1; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       encoding_t enc0, enc1; | 
					
						
							|  |  |  |       int minimal0, minimal1; | 
					
						
							|  |  |  |       void *buf0, *buf1; | 
					
						
							| 
									
										
										
										
											2013-12-06 15:08:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       if (outv[0].val.t) { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  | 	buf0 = Yap_readText( store, outv, &enc0, &minimal0, &leng0 PASS_REGS ); | 
					
						
							| 
									
										
										
										
											2013-12-06 15:08:35 +00:00
										 |  |  | 	if (!buf0) | 
					
						
							|  |  |  | 	  return NULL; | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  | 	l0 = write_length( buf0, outv, enc, minimal0, leng0 PASS_REGS); | 
					
						
							| 
									
										
										
										
											2013-12-06 15:08:35 +00:00
										 |  |  | 	if (cmp_Text( buf, buf0, l0, enc, enc0) != 0) | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	  return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	l1 = l-l0; | 
					
						
							| 
									
										
										
										
											2013-12-08 19:12:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-06 15:08:35 +00:00
										 |  |  | 	buf1 = slice(l0, l, buf, outv+1, enc PASS_REGS); | 
					
						
							|  |  |  | 	if (encv) | 
					
						
							|  |  |  | 	  encv[1] = enc; | 
					
						
							|  |  |  | 	return buf1; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       } else /* if (outv[1].val.t) */ { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:30:13 +01:00
										 |  |  | 	buf1 = Yap_readText( store, outv+1, &enc1, &minimal1, &leng1 PASS_REGS ); | 
					
						
							| 
									
										
										
										
											2013-12-06 15:08:35 +00:00
										 |  |  | 	if (!buf1) | 
					
						
							|  |  |  | 	  return NULL; | 
					
						
							| 
									
										
										
										
											2013-12-15 08:37:10 +00:00
										 |  |  | 	l1 = write_length( buf1, outv+1, enc1, minimal1, leng1 PASS_REGS); | 
					
						
							| 
									
										
										
										
											2013-12-08 19:12:24 +00:00
										 |  |  | 	if (l < l1) return NULL; | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | 	l0 = l-l1; | 
					
						
							| 
									
										
										
										
											2013-12-06 15:08:35 +00:00
										 |  |  | 	if (cmp_Text( advance_Text(buf, l0, enc), buf1, l1, enc, enc1) != 0) | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  | 	  return NULL; | 
					
						
							| 
									
										
										
										
											2013-12-06 15:08:35 +00:00
										 |  |  | 	buf0 = slice(0, l0, buf, outv, enc PASS_REGS); | 
					
						
							|  |  |  | 	if (encv) | 
					
						
							|  |  |  | 	  encv[0] = enc; | 
					
						
							|  |  |  | 	return buf0; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-12-06 15:08:35 +00:00
										 |  |  |   for (i = 0; i < n; i++) { | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |     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; | 
					
						
							| 
									
										
										
										
											2013-12-06 15:08:35 +00:00
										 |  |  |     if (encv) | 
					
						
							|  |  |  |       encv[i] = enc; | 
					
						
							| 
									
										
										
										
											2013-12-04 23:01:30 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   return (void *)outv;; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-02-13 03:11:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** 
 | 
					
						
							|  |  |  |  * 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 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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const char * | 
					
						
							|  |  |  | Yap_TextTermToText(Term t, char *buf, size_t len) | 
					
						
							|  |  |  | { CACHE_REGS | 
					
						
							|  |  |  |   seq_tv_t inp, out; | 
					
						
							|  |  |  |   encoding_t enc = LOCAL_encoding; | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   inp.val.t = t; | 
					
						
							|  |  |  |   if (IsAtomTerm(t)) | 
					
						
							|  |  |  |     inp.type = YAP_STRING_ATOM; | 
					
						
							|  |  |  |   else if (IsStringTerm(t)) | 
					
						
							|  |  |  |     inp.type = YAP_STRING_STRING; | 
					
						
							|  |  |  |   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; | 
					
						
							|  |  |  |   out.type = YAP_STRING_CHARS; | 
					
						
							|  |  |  |   if (!buf) { | 
					
						
							|  |  |  |     inp.type |= YAP_STRING_MALLOC; | 
					
						
							|  |  |  |     out.type |= YAP_STRING_MALLOC; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   out.val.c = buf; | 
					
						
							|  |  |  |   if (!Yap_CVT_Text(&inp, &out PASS_REGS)) | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  |   return out.val.c; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  | /** 
 | 
					
						
							|  |  |  |  * 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, | 
					
						
							|  |  |  |                                             Term tguide ) { | 
					
						
							|  |  |  | CACHE_REGS | 
					
						
							|  |  |  |   if (IsAtomTerm(tguide)) | 
					
						
							|  |  |  |     return MkAtomTerm(Yap_LookupAtom(s)); | 
					
						
							|  |  |  |  if (IsStringTerm(tguide)) | 
					
						
							|  |  |  |     return MkStringTerm(s); | 
					
						
							|  |  |  |  if (IsPairTerm(tguide) && IsAtomTerm(HeadOfTerm(tguide))) { | 
					
						
							|  |  |  |    return Yap_CharsToListOfAtoms( s, LOCAL_encoding  PASS_REGS ); | 
					
						
							|  |  |  |  } | 
					
						
							|  |  |  |  return Yap_CharsToListOfCodes( s, LOCAL_encoding  PASS_REGS ); | 
					
						
							|  |  |  | } |