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

169 lines
3.5 KiB
C
Raw Normal View History

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
* @{
* @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
#include <Yap.h>
#include <Yatom.h>
#include <iopreds.h>
#include <stdio.h>
2010-11-30 21:59:45 +00:00
#include <string.h>
#include "YapText.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"
static PL_blob_t unregistered_blob_atom = {
PL_BLOB_MAGIC, PL_BLOB_NOCOPY | PL_BLOB_TEXT, "unregistered"};
2011-02-11 01:22:07 +00:00
int PL_is_blob(term_t t, PL_blob_t **type) {
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)
PL_unify_blob(term_t t, void *blob, size_t len, PL_blob_t *type) {
CACHE_REGS
2011-02-12 14:14:12 +00:00
AtomEntry *ae;
if (!blob)
return FALSE;
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)
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;
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)
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
}
PL_EXPORT(void *)
PL_blob_data(atom_t a, size_t *len, struct PL_blob_t **type) {
2010-11-30 21:59:45 +00:00
Atom x = SWIAtomToAtom(a);
if (!IsBlob(x)) {
if (len)
*len = strlen_utf8(x->UStrOfAE);
if (type)
*type = &unregistered_blob_atom;
return x->StrOfAE;
2010-11-30 21:59:45 +00:00
}
if (len)
*len = x->rep.blob[0].length;
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
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
}
PL_EXPORT(PL_blob_t *)
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)
PL_unregister_blob_type(PL_blob_t *type) {
fprintf(stderr, "PL_unregister_blob_type not implemented yet\n");
2010-11-30 21:59:45 +00:00
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
/**
* @}
*/