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.c,v 1.4 2006-09-28 11:42:51 vsc Exp $
21 Comments: This file provides a set of functions to convert a prolog term to a C string and back.
22 */
23 #include "config.h"
24 #include "prologterms2c.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #if HAVE_STRING_H
28 #include <string.h>
29 #endif
30 #if HAVE_STDARG_H
31 #include <stdarg.h>
32 #endif
33 #if HAVE_MALLOC_H
34 #include <malloc.h>
35 #endif
36
37 #ifdef COMPRESS
38 #include "minilzo.h"
39 #endif
40
41 #ifndef Quote_illegal_f
42 #define Quote_illegal_f 1
43 #define Ignore_ops_f 2
44 #define Handle_vars_f 4
45 #define Use_portray_f 8
46 #define To_heap_f 16
47 #endif
48
49
50 #ifdef COMPRESS
51
52 #endif
53
54 struct buffer_ds buffer;
55 extern char *Yap_ErrorMessage;
56
57 /*********************************************************************************************/
58 // prototypes
59 void write_msg(const char *fun,const char *file, int line,const char *format, ...);
60
61 size_t write_term_to_stream(const int fd,const YAP_Term term);
62 YAP_Term read_term_from_stream(const int fd);
63 /*********************************************************************************************/
64 /*
65 * Writes a debug message containing the processid, function name, filename, line, and a user message
66 */
67 void
write_msg(const char * fun,const char * file,int line,const char * format,...)68 write_msg(const char *fun,const char *file, int line,
69 const char *format, ...) {
70 va_list ap;
71
72 va_start(ap, format);
73 /* Print the message to stderr */
74 fprintf(stderr,
75 "[%d:%s in %s at line %d] ", getpid(),fun, file, line);
76 vfprintf(stderr, format, ap);
77 va_end(ap);
78 }
79 /*********************************************************************************************
80 * Memory handling functions
81 *********************************************************************************************/
82 /*
83 * Adds 'space' to the size of the currently allocated buffer
84 */
85 static void
expand_buffer(const size_t space)86 expand_buffer(const size_t space ) {
87 char *oldblock;
88
89 // BUFFER_PTR = realloc( BUFFER_PTR, BUFFER_SIZE + space );
90 oldblock= BUFFER_PTR;
91 BUFFER_PTR = (char*)malloc( BUFFER_SIZE + space );
92 if( BUFFER_PTR == NULL ) {
93 YAP_Error(0,0,"Prolog2Term: Out of memory.\n");
94 #ifdef MPI
95 MPI_Finalize();
96 #endif
97 YAP_Exit( 1 );
98 }
99 memcpy(BUFFER_PTR,oldblock,BUFFER_SIZE);
100
101 if(oldblock!=NULL)
102 free(oldblock);
103
104 BUFFER_SIZE+=space;
105 }
106 /*
107 * Changes the size of the buffer to contain at least newsize bytes
108 */
109 void
change_buffer_size(const size_t newsize)110 change_buffer_size(const size_t newsize) {
111
112 if ( BUFFER_SIZE>=BLOCK_SIZE && BUFFER_SIZE>newsize)
113 return;
114 if(BUFFER_PTR!=NULL)
115 free(BUFFER_PTR);
116 BUFFER_PTR = (char*)malloc(newsize);
117 if( BUFFER_PTR == NULL ) {
118 YAP_Error(0,0,"Prolog2Term: Out of memory.\n");
119 #ifdef MPI
120 MPI_Finalize();
121 #endif
122 YAP_Exit( 1 );
123 }
124 BUFFER_SIZE=newsize;
125 }
126 /*********************************************************************************************
127 * I/O functions
128 *********************************************************************************************/
129 /*
130 * Function used by YAP to write a char to a string
131 */
132 static void
p2c_putc(const int c)133 p2c_putc(const int c) {
134 // if( buffer.size==buffer.len+1 )
135 if( BUFFER_SIZE==BUFFER_LEN ) {
136 #ifdef DEBUG
137 write_msg(__FUNCTION__,__FILE__,__LINE__,"p2c_putc:buffer expanded: size=%u pos=%u len=%u\n",BUFFER_SIZE,BUFFER_POS,BUFFER_LEN);
138 #endif
139 expand_buffer( BLOCK_SIZE );
140 }
141 BUFFER_PTR[BUFFER_LEN++] = c;
142 }
143 /*
144 * Function used by YAP to read a char from a string
145 */
146 static int
p2c_getc(void)147 p2c_getc(void) {
148 if( BUFFER_POS < BUFFER_LEN )
149 return BUFFER_PTR[BUFFER_POS++];
150 return -1;
151 }
152 /*
153 * Writes a term to a stream.
154 */
155 size_t
write_term_to_stream(const int fd,const YAP_Term term)156 write_term_to_stream(const int fd,const YAP_Term term) {
157
158 RESET_BUFFER;
159
160 YAP_Write( term, p2c_putc,3); // 3=canonical
161 write(fd,(void*)&BUFFER_LEN,sizeof(size_t));// write size of term
162 write(fd,(void*)BUFFER_PTR,BUFFER_LEN); // write term
163 return BUFFER_LEN;
164 }
165 /*
166 * Read a prolog term from a stream
167 * (the prolog term must have been writen by the write_term_to_stream)
168 */
169 YAP_Term
read_term_from_stream(const int fd)170 read_term_from_stream(const int fd) {
171 size_t size;
172
173 RESET_BUFFER;
174 read(fd,(void*)&size,sizeof(size_t)); // read the size of the term
175 #ifdef DEBUG
176 write_msg(__FUNCTION__,__FILE__,__LINE__,"read_term_from_stream>>>>size:%d\n",size);
177 #endif
178 if ( size> BUFFER_SIZE)
179 expand_buffer(size-BUFFER_SIZE);
180 read(fd,BUFFER_PTR,size); // read term from stream
181 return YAP_Read( p2c_getc );
182 }
183 /*********************************************************************************************
184 * Conversion: Prolog Term->char[] and char[]->Prolog Term
185 *********************************************************************************************/
186 /*
187 * Converts a term t into a string.
188 * The ascii representation of t is
189 * copied to ptr if it occupies less than size.
190 */
191 char*
term2string(char * const ptr,size_t * size,const YAP_Term t)192 term2string(char *const ptr,size_t *size, const YAP_Term t) {
193 char *ret;
194
195 RESET_BUFFER;
196
197 YAP_Write( t, p2c_putc, 3 );// canonical
198 p2c_putc('\0'); //add terminator
199
200 if (BUFFER_LEN<=*size) { // user allocated buffer size is ok
201 memcpy(ptr,BUFFER_PTR,BUFFER_LEN); // copy data to user block
202 ret=ptr;
203 *size=BUFFER_LEN;
204 } else { // user buffer is too small
205 ret=BUFFER_PTR;
206 *size=BUFFER_LEN;
207 //DEL_BUFFER;
208 }
209 return ret;
210 }
211 /*
212 * Converts a string with a ascci representation of a term into a Prolog term.
213 */
214 YAP_Term
string2term(char * const ptr,const size_t * size)215 string2term(char *const ptr,const size_t *size) {
216 YAP_Term t;
217 struct buffer_ds b;
218 b.size=b.len=b.pos=0;
219
220 if (BUFFER_PTR!=ptr) { //
221 #ifdef DEBUG
222 write_msg(__FUNCTION__,__FILE__,__LINE__,"copy buffer string2term\n");
223 #endif
224 COPY_BUFFER_DS(buffer,b); // keep a copy of buffer_ds
225 BUFFER_PTR=ptr; // make the buffer use the buffer provided
226 BUFFER_LEN=*size;
227 BUFFER_SIZE=BUFFER_LEN;
228 } else { // b aux. struct. not needed
229 b.ptr=NULL;
230 }
231 BUFFER_POS=0;
232 Yap_ErrorMessage=NULL;
233 t = YAP_Read(p2c_getc);
234 if ( t==FALSE ) {
235 write_msg(__FUNCTION__,__FILE__,__LINE__,"FAILED string2term>>>>size:%d %d %s\n",BUFFER_SIZE,strlen(BUFFER_PTR),Yap_ErrorMessage);
236 exit(1);
237 }
238
239 if (b.ptr!=NULL)
240 COPY_BUFFER_DS(b,buffer);
241
242 #ifdef DEBUG
243 write_msg(__FUNCTION__,__FILE__,__LINE__,"ending: buffer(ptr=%p;size=%d;pos=%d;len=%d)\n",BUFFER_PTR,BUFFER_SIZE,BUFFER_POS,BUFFER_LEN);
244 #endif
245
246 return t;
247 }
248