YAP 7.1.0
prologterms2c.h
1/*
2Copyright (C) 2004,2005,2006 (Nuno A. Fonseca) <nuno.fonseca@gmail.com>
3
4This program is free software; you can redistribute it and/or
5modify it under the terms of the GNU General Public License
6as published by the Free Software Foundation; either
7version 2 of the License, or (at your option) any later
8version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19
20Last rev: $Id: prologterms2c.h,v 1.2 2006-06-04 19:02:07 nunofonseca Exp $
21Comments: This file provides a set of functions to convert a prolog term to a C string and back.
22*/
23#ifndef PROLOGTERMS2C
24#define PROLOGTERMS2C 1
25
26#ifndef _yap_c_interface_h
27#include <YapInterface.h>
28//#include <yap_structs.h>
29#endif
30
31#ifndef size_t
32#include <unistd.h>
33#endif
34#include <stdarg.h>
35/*
36 * Converts a term t into a string.
37 * An internal representation of t is
38 * copied to ptr if it occupies less than size. Otherwise the
39 * necessary memory is aloccated (dyn_ptr) and the ascii
40 * representation of the term is copied to there.
41 */
42char* term2string(const YAP_Term t);
43/*
44 * Converts a string with a ascci representation of a term into a term.
45 * The ascii representation of t is
46 * copied to ptr if it occupies less than *size. Otherwise the
47 * necessary memory is aloccated and the ascii
48 * representation of the term is copied to there.
49 */
50YAP_Term string2term(char *const ptr,const size_t *size);
51/*
52 * Read a prolog term from a stream
53 * (the prolog term must have been writen by the write_term_to_stream)
54 */
55YAP_Term read_term_from_stream(const int fd);
56/*
57 * Writes a term to a stream.
58 */
59size_t write_term_to_stream(const int fd,const YAP_Term term);
60/*
61 * Changes the size of the buffer to contain at least newsize bytes.
62 * Useful to reduce the number of reallocs,mallocs, and frees
63 */
64void change_buffer_size(const size_t newsize);
65
66void write_msg(const char *fun,const char *file, int line,const char *format, ...);
67/*********************************************************************************************
68 * Macros to manipulate the buffer
69 *********************************************************************************************/
70
71#define BLOCK_SIZE 4096
72
73#if THREADS
74#define buffer (buffers[YAP_ThreadSelf()])
75#else
76#define buffer (buffers[0])
77#endif
78
79// deletes the buffer (all fields) but does not release the memory of the buffer.ptr
80#define DEL_BUFFER() {}
81// informs the prologterm2c module that the buffer is now used and should not be messed
82#define USED_BUFFER() DEL_BUFFER()
83// initialize buffer
84#define RESET_BUFFER() \
85 {buffer.ptr[0]= '\0'; buffer.pos=0;}
86#define BUFFER_PTR buffer.ptr
87#define BUFFER_SIZE buffer.size
88#define BUFFER_LEN buffer.len
89#define BUFFER_POS buffer.pos
90// copies two buffers
91#define COPY_BUFFER_DS(src,dst) {dst.size=src.size;dst.len=src.len;dst.ptr=src.ptr;dst.pos=src.pos;}
92
93/***
94 * Buffer
95 *********************************************************************************************/
96struct buffer_ds {
97 size_t size, // size of the buffer
98 len; // size of the string
99 char ptr[BLOCK_SIZE]; // pointer to the buffer
100 size_t pos; // position (used while reading)
101};
102extern struct buffer_ds buffers[1024];
103
104
105#endif