| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  | /*************************************************************************
 | 
					
						
							|  |  |  | *									 * | 
					
						
							|  |  |  | *	 YAP Prolog 							 * | 
					
						
							|  |  |  | *	Yap Prolog was developed at NCCUP - Universidade do Porto	 * | 
					
						
							|  |  |  | *									 * | 
					
						
							|  |  |  | * Copyright V.Santos Costa and Universidade do Porto 1985--		 * | 
					
						
							|  |  |  | *									 * | 
					
						
							|  |  |  | ************************************************************************** | 
					
						
							|  |  |  | *									 * | 
					
						
							|  |  |  | * File:		blobs.c							 * | 
					
						
							|  |  |  | * comments:	support blobs in YAP definition 			 * | 
					
						
							|  |  |  | *									 * | 
					
						
							|  |  |  | * Last rev:	$Date: $,$Author: vsc $					 * | 
					
						
							|  |  |  | *									 * | 
					
						
							|  |  |  | *									 * | 
					
						
							|  |  |  | *************************************************************************/ | 
					
						
							| 
									
										
										
										
											2014-05-14 10:01:11 +01:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |  * @{ | 
					
						
							| 
									
										
										
										
											2015-12-15 09:28:43 +00:00
										 |  |  | *   @file swi/fli/blobs.c | 
					
						
							| 
									
										
										
										
											2014-05-14 10:01:11 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  *   @addtogroup swi-c-interface | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | #include	<stdio.h>
 | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  | #include	<Yap.h>
 | 
					
						
							|  |  |  | #include	<Yatom.h>
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | #include	<iopreds.h>
 | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-17 12:59:52 +00:00
										 |  |  | /* for freeBSD9.1 */ | 
					
						
							|  |  |  | #define _WITH_DPRINTF
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | //#include	<SWI-Stream.h>
 | 
					
						
							|  |  |  | //#include	<pl-shared.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "swi.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-11 01:22:07 +00:00
										 |  |  | static PL_blob_t unregistered_blob_atom = | 
					
						
							|  |  |  | { PL_BLOB_MAGIC, | 
					
						
							|  |  |  |   PL_BLOB_NOCOPY|PL_BLOB_TEXT, | 
					
						
							|  |  |  |   "unregistered" | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  | PL_is_blob(term_t t, PL_blob_t **type) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   Term yt = Yap_GetFromSlot(t); | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  |   Atom a; | 
					
						
							| 
									
										
										
										
											2015-10-22 00:33:38 +01:00
										 |  |  |   YAP_BlobPropEntry *b; | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (IsVarTerm(yt)) | 
					
						
							|  |  |  |     return FALSE; | 
					
						
							|  |  |  |   if (!IsAtomTerm(yt)) | 
					
						
							|  |  |  |     return FALSE; | 
					
						
							|  |  |  |   a = AtomOfTerm(yt); | 
					
						
							|  |  |  |   if (!IsBlob(a)) | 
					
						
							|  |  |  |     return FALSE; | 
					
						
							|  |  |  |   b = RepBlobProp(a->PropsOfAE); | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   *type = (struct PL_blob_t *)b->blob_type; | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  |   return TRUE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-09 23:27:22 +00:00
										 |  |  | /* void check_chain(void); */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | PL_EXPORT(int) | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  | PL_unify_blob(term_t t, void *blob, size_t len, PL_blob_t *type) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 16:02:55 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							| 
									
										
										
										
											2011-02-12 14:14:12 +00:00
										 |  |  |   AtomEntry *ae; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!blob) | 
					
						
							|  |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2014-10-14 15:53:24 +01:00
										 |  |  |   ae = Yap_lookupBlob(blob, len, type, NULL); | 
					
						
							| 
									
										
										
										
											2011-02-12 14:14:12 +00:00
										 |  |  |   if (!ae) { | 
					
						
							|  |  |  |     return FALSE; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-03-11 19:49:32 +00:00
										 |  |  |   if (type->acquire) { | 
					
						
							|  |  |  |     type->acquire(AtomToSWIAtom(AbsAtom(ae))); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   return Yap_unify(Yap_GetFromSlot(t), MkAtomTerm(AbsAtom(ae))); | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | PL_EXPORT(int) | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  | PL_put_blob(term_t t, void *blob, size_t len, PL_blob_t *type) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-22 15:16:54 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							|  |  |  |   AtomEntry *ae; | 
					
						
							|  |  |  |   int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!blob) | 
					
						
							|  |  |  |     return FALSE; | 
					
						
							| 
									
										
										
										
											2014-10-14 15:53:24 +01:00
										 |  |  |   ae = Yap_lookupBlob(blob, len, type, & ret); | 
					
						
							| 
									
										
										
										
											2013-11-22 15:16:54 +00:00
										 |  |  |   if (!ae) { | 
					
						
							|  |  |  |     return FALSE; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (type->acquire) { | 
					
						
							|  |  |  |     type->acquire(AtomToSWIAtom(AbsAtom(ae))); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-01-03 02:06:09 +00:00
										 |  |  |   Yap_PutInSlot(t, MkAtomTerm(AbsAtom(ae))); | 
					
						
							| 
									
										
										
										
											2013-11-22 15:16:54 +00:00
										 |  |  |   return ret; | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | PL_EXPORT(int) | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  | PL_get_blob(term_t t, void **blob, size_t *len, PL_blob_t **type) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-22 15:16:54 +00:00
										 |  |  |   CACHE_REGS | 
					
						
							|  |  |  |   Atom a; | 
					
						
							|  |  |  |   Term tt; | 
					
						
							|  |  |  |   AtomEntry *ae; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  |   tt = Yap_GetFromSlot(t); | 
					
						
							| 
									
										
										
										
											2013-11-22 15:16:54 +00:00
										 |  |  |   if (IsVarTerm(tt)) | 
					
						
							|  |  |  |     return FALSE; | 
					
						
							|  |  |  |   if (!IsAtomTerm(tt)) | 
					
						
							|  |  |  |     return FALSE; | 
					
						
							|  |  |  |   a = AtomOfTerm(tt); | 
					
						
							|  |  |  |   if (!IsBlob(a)) | 
					
						
							|  |  |  |     return FALSE; | 
					
						
							|  |  |  |   ae = RepAtom(a); | 
					
						
							|  |  |  |   if (type) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     *type = (struct PL_blob_t *)RepBlobProp(ae->PropsOfAE)->blob_type; | 
					
						
							| 
									
										
										
										
											2013-11-22 15:16:54 +00:00
										 |  |  |   if (len) | 
					
						
							|  |  |  |     *len = ae->rep.blob[0].length; | 
					
						
							|  |  |  |   if (blob) | 
					
						
							|  |  |  |     *blob = ae->rep.blob[0].data; | 
					
						
							|  |  |  |   return TRUE; | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | PL_EXPORT(void*) | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  | PL_blob_data(atom_t a, size_t *len, struct PL_blob_t **type) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   Atom x = SWIAtomToAtom(a); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!IsBlob(x)) { | 
					
						
							|  |  |  |     if (IsWideAtom(x)) { | 
					
						
							|  |  |  |       if ( len ) | 
					
						
							|  |  |  | 	*len = wcslen(x->WStrOfAE); | 
					
						
							|  |  |  |       if ( type ) | 
					
						
							| 
									
										
										
										
											2011-02-11 01:22:07 +00:00
										 |  |  | 	*type = &unregistered_blob_atom; | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  |       return x->WStrOfAE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if ( len ) | 
					
						
							|  |  |  |       *len = strlen(x->StrOfAE); | 
					
						
							|  |  |  |       if ( type ) | 
					
						
							| 
									
										
										
										
											2011-02-11 01:22:07 +00:00
										 |  |  | 	*type = &unregistered_blob_atom; | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  |       return x->StrOfAE; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if ( len ) | 
					
						
							| 
									
										
										
										
											2010-12-02 11:49:58 +00:00
										 |  |  |     *len = x->rep.blob[0].length; | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  |   if ( type ) | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |     *type = (struct PL_blob_t *)RepBlobProp(x->PropsOfAE)->blob_type; | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-02 11:49:58 +00:00
										 |  |  |   return x->rep.blob[0].data; | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PL_EXPORT(void) | 
					
						
							|  |  |  | PL_register_blob_type(PL_blob_t *type) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-19 01:29:16 +01:00
										 |  |  |   type->next = (PL_blob_t *)BlobTypes; | 
					
						
							|  |  |  |   BlobTypes = (struct YAP_blob_t *)type; | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | PL_EXPORT(PL_blob_t*) | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  | PL_find_blob_type(const char* name) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-02-10 00:01:19 +00:00
										 |  |  |   Atom at = Yap_LookupAtom((char *)name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return YAP_find_blob_type((YAP_Atom)at); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 13:28:17 +01:00
										 |  |  | PL_EXPORT(int) | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  | PL_unregister_blob_type(PL_blob_t *type) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   fprintf(stderr,"PL_unregister_blob_type not implemented yet\n"); | 
					
						
							|  |  |  |   return FALSE; | 
					
						
							| 
									
										
										
										
											2015-01-05 12:21:58 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-11-30 21:59:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-14 10:01:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * @} | 
					
						
							|  |  |  |  */ |