1 /* 2 Copyright (C) 2004,2005,2006 (Nuno A. Fonseca) <nuno.fonseca@gmail.com> 3 4 This program is free software; you can redistribute it and/or 5 modify it under the terms of the GNU General Public License 6 as published by the Free Software Foundation; either 7 version 2 of the License, or (at your option) any later 8 version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software 17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 19 20 Last rev: $Id: prologterms2c.h,v 1.2 2006-06-04 19:02:07 nunofonseca Exp $ 21 Comments: 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 * The ascii 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 */ 42 char* term2string(char *const ptr,size_t *size, 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 */ 50 YAP_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 */ 55 YAP_Term read_term_from_stream(const int fd); 56 /* 57 * Writes a term to a stream. 58 */ 59 size_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 */ 64 void change_buffer_size(const size_t newsize); 65 66 void write_msg(const char *fun,const char *file, int line,const char *format, ...); 67 /********************************************************************************************* 68 * Macros to manipulate the buffer 69 *********************************************************************************************/ 70 #define BLOCK_SIZE 4*1024 71 72 // deletes the buffer (all fields) but does not release the memory of the buffer.ptr 73 #define DEL_BUFFER {buffer.ptr=NULL;buffer.size=0;buffer.len=0;} 74 // informs the prologterm2c module that the buffer is now used and should not be messed 75 #define USED_BUFFER() DEL_BUFFER 76 // initialize buffer 77 #define RESET_BUFFER {buffer.len=0;change_buffer_size(BLOCK_SIZE);buffer.pos=0;} 78 #define BUFFER_PTR buffer.ptr 79 #define BUFFER_SIZE buffer.size 80 #define BUFFER_LEN buffer.len 81 #define BUFFER_POS buffer.pos 82 // copies two buffers 83 #define COPY_BUFFER_DS(src,dst) {dst.size=src.size;dst.len=src.len;dst.ptr=src.ptr;dst.pos=src.pos;} 84 85 /********************************************************************************************* 86 * Buffer 87 *********************************************************************************************/ 88 struct buffer_ds { 89 size_t size, // size of the buffer 90 len; // size of the string 91 char *ptr; // pointer to the buffer 92 size_t pos; // position (used while reading) 93 }; 94 extern struct buffer_ds buffer; 95 96 #endif 97