| 
									
										
										
										
											2006-06-04 19:02:07 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  | Copyright (C) 2004,2005,2006 (Nuno A. Fonseca) <nuno.fonseca@gmail.com> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This program is free software; you can redistribute it and/or  | 
					
						
							|  |  |  | modify it under the terms of the GNU General Public License  | 
					
						
							|  |  |  | as published by the Free Software Foundation; either  | 
					
						
							|  |  |  | version 2 of the License, or (at your option) any later  | 
					
						
							|  |  |  | version. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | GNU General Public License for more details. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | along with this program; if not, write to the Free Software | 
					
						
							|  |  |  | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-28 11:42:51 +00:00
										 |  |  | Last rev: $Id: prologterms2c.c,v 1.4 2006-09-28 11:42:51 vsc Exp $ | 
					
						
							| 
									
										
										
										
											2006-06-04 19:02:07 +00:00
										 |  |  | Comments: This file provides a set of functions to convert a prolog term to a C string and back. | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2006-09-28 11:42:51 +00:00
										 |  |  | #include "config.h"
 | 
					
						
							| 
									
										
										
										
											2006-06-02 04:16:31 +00:00
										 |  |  | #include "prologterms2c.h"
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2006-09-28 11:42:51 +00:00
										 |  |  | #if HAVE_STRING_H
 | 
					
						
							| 
									
										
										
										
											2006-06-02 04:16:31 +00:00
										 |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2006-09-28 11:42:51 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #if HAVE_STDARG_H
 | 
					
						
							| 
									
										
										
										
											2006-06-02 04:16:31 +00:00
										 |  |  | #include <stdarg.h>
 | 
					
						
							| 
									
										
										
										
											2006-09-28 11:42:51 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #if HAVE_MALLOC_H
 | 
					
						
							| 
									
										
										
										
											2006-06-02 04:16:31 +00:00
										 |  |  | #include <malloc.h>
 | 
					
						
							| 
									
										
										
										
											2006-09-28 11:42:51 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2006-06-02 04:16:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 18:12:23 -05:00
										 |  |  | #if HAVE_MPI_H
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-02 04:16:31 +00:00
										 |  |  | #ifdef COMPRESS
 | 
					
						
							|  |  |  | #include "minilzo.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef Quote_illegal_f
 | 
					
						
							|  |  |  | #define Quote_illegal_f         1
 | 
					
						
							|  |  |  | #define Ignore_ops_f            2
 | 
					
						
							|  |  |  | #define Handle_vars_f           4
 | 
					
						
							|  |  |  | #define Use_portray_f           8
 | 
					
						
							|  |  |  | #define To_heap_f              16
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef COMPRESS
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-24 19:59:42 +00:00
										 |  |  | struct buffer_ds buffers[1024];  | 
					
						
							| 
									
										
										
										
											2006-06-02 04:16:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*********************************************************************************************/ | 
					
						
							|  |  |  | // prototypes
 | 
					
						
							|  |  |  | void write_msg(const char *fun,const char *file, int line,const char *format, ...); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | size_t write_term_to_stream(const int fd,const YAP_Term term); | 
					
						
							|  |  |  | YAP_Term read_term_from_stream(const int fd); | 
					
						
							|  |  |  | /*********************************************************************************************/ | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Writes a debug message containing the processid, function name, filename, line, and a user message | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void  | 
					
						
							|  |  |  | write_msg(const char *fun,const char *file, int line, | 
					
						
							|  |  |  |             const char *format, ...) { | 
					
						
							|  |  |  |   va_list ap; | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  |   va_start(ap, format); | 
					
						
							|  |  |  |   /* Print the message to stderr */ | 
					
						
							|  |  |  |   fprintf(stderr, | 
					
						
							|  |  |  |           "[%d:%s in %s at line %d] ", getpid(),fun, file, line); | 
					
						
							|  |  |  |   vfprintf(stderr, format, ap); | 
					
						
							|  |  |  |   va_end(ap); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /*********************************************************************************************
 | 
					
						
							|  |  |  |  * Memory handling functions | 
					
						
							|  |  |  |  *********************************************************************************************/ | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Adds 'space' to the size of the currently allocated buffer | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | expand_buffer(const size_t space ) { | 
					
						
							| 
									
										
										
										
											2012-02-02 23:25:31 +00:00
										 |  |  |   BUFFER_PTR = realloc( BUFFER_PTR, BUFFER_SIZE + space ); | 
					
						
							| 
									
										
										
										
											2006-06-02 04:16:31 +00:00
										 |  |  |   if( BUFFER_PTR == NULL ) { | 
					
						
							|  |  |  |     YAP_Error(0,0,"Prolog2Term: Out of memory.\n"); | 
					
						
							|  |  |  | #ifdef MPI
 | 
					
						
							|  |  |  |     MPI_Finalize(); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     YAP_Exit( 1 ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   BUFFER_SIZE+=space; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Changes the size of the buffer to contain at least newsize bytes  | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-02-11 01:50:59 +00:00
										 |  |  | void change_buffer_size(const size_t newsize) { | 
					
						
							|  |  |  |   if ( BUFFER_PTR == NULL ) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if ((BUFFER_PTR = malloc(  BLOCK_SIZE < newsize ? newsize : BLOCK_SIZE)) == NULL) { | 
					
						
							|  |  |  |         YAP_Error(0,0,"Prolog2Term: Out of memory.\n"); | 
					
						
							|  |  |  | #ifdef MPI
 | 
					
						
							|  |  |  |         MPI_Finalize(); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |         YAP_Exit( 1 ); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else if ((BUFFER_SIZE>=BLOCK_SIZE && | 
					
						
							|  |  |  |        BUFFER_SIZE>=newsize) ) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else if ((BUFFER_PTR = realloc( BUFFER_PTR, newsize)) == NULL) { | 
					
						
							| 
									
										
										
										
											2006-06-02 04:16:31 +00:00
										 |  |  |     YAP_Error(0,0,"Prolog2Term: Out of memory.\n"); | 
					
						
							|  |  |  | #ifdef MPI
 | 
					
						
							|  |  |  |     MPI_Finalize(); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     YAP_Exit( 1 ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   BUFFER_SIZE=newsize; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /*********************************************************************************************
 | 
					
						
							|  |  |  |  * I/O functions | 
					
						
							|  |  |  |  *********************************************************************************************/ | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Function used by YAP to write a char to a string | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2012-02-01 19:26:58 +00:00
										 |  |  | p2c_putt(const YAP_Term t) { | 
					
						
							| 
									
										
										
										
											2006-06-02 04:16:31 +00:00
										 |  |  |   //  if( buffer.size==buffer.len+1 ) 
 | 
					
						
							| 
									
										
										
										
											2012-02-01 19:26:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   while ((BUFFER_LEN=YAP_ExportTerm(t, BUFFER_PTR, BUFFER_SIZE)) <= 0) { | 
					
						
							| 
									
										
										
										
											2006-06-02 04:16:31 +00:00
										 |  |  | #ifdef DEBUG
 | 
					
						
							| 
									
										
										
										
											2012-02-01 19:26:58 +00:00
										 |  |  |      write_msg(__FUNCTION__,__FILE__,__LINE__,"p2c_putc:buffer expanded: size=%u pos=%u len=%u\n",BUFFER_SIZE,BUFFER_POS,BUFFER_LEN);   | 
					
						
							| 
									
										
										
										
											2006-06-02 04:16:31 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  |      expand_buffer( BLOCK_SIZE );     | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Function used by YAP to read a char from a string | 
					
						
							|  |  |  |  */                                                                                                | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Writes a term to a stream. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | size_t  | 
					
						
							|  |  |  | write_term_to_stream(const int fd,const YAP_Term term) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 01:50:59 +00:00
										 |  |  |   RESET_BUFFER(); | 
					
						
							| 
									
										
										
										
											2014-01-24 19:59:42 +00:00
										 |  |  |   printf("BUFFER_PTR=%p\n", BUFFER_PTR); | 
					
						
							| 
									
										
										
										
											2012-02-01 19:26:58 +00:00
										 |  |  |   p2c_putt(term); | 
					
						
							| 
									
										
										
										
											2011-10-21 22:06:13 +01:00
										 |  |  |   if (write(fd,(void*)BUFFER_PTR,BUFFER_LEN) < 0) {     // write term
 | 
					
						
							|  |  |  |     YAP_Error(0,0,"Prolog2Term: IO error in write.\n"); | 
					
						
							|  |  |  |     return -1; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2006-06-02 04:16:31 +00:00
										 |  |  |   return BUFFER_LEN; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Read a prolog term from a stream | 
					
						
							|  |  |  |  * (the prolog term must have been writen by the write_term_to_stream) | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | YAP_Term  | 
					
						
							|  |  |  | read_term_from_stream(const int fd) { | 
					
						
							|  |  |  |   size_t size;  | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 01:50:59 +00:00
										 |  |  |   RESET_BUFFER();     | 
					
						
							| 
									
										
										
										
											2012-02-01 19:26:58 +00:00
										 |  |  |   if (!read(fd,(void*)&size,sizeof(size_t))) { // read the size of the term
 | 
					
						
							|  |  |  |     YAP_Error(0,0,"Prolog2Term: IO error in read.\n"); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2006-06-02 04:16:31 +00:00
										 |  |  | #ifdef DEBUG
 | 
					
						
							|  |  |  |   write_msg(__FUNCTION__,__FILE__,__LINE__,"read_term_from_stream>>>>size:%d\n",size);   | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |   if ( size> BUFFER_SIZE) | 
					
						
							|  |  |  |     expand_buffer(size-BUFFER_SIZE); | 
					
						
							| 
									
										
										
										
											2011-10-21 22:06:13 +01:00
										 |  |  |   if (!read(fd,BUFFER_PTR,size)) { | 
					
						
							|  |  |  |     YAP_Error(0,0,"Prolog2Term: IO error in read.\n"); | 
					
						
							|  |  |  |   };            // read term from stream
 | 
					
						
							| 
									
										
										
										
											2012-02-01 19:26:58 +00:00
										 |  |  |   return YAP_ImportTerm( BUFFER_PTR); | 
					
						
							| 
									
										
										
										
											2006-06-02 04:16:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | /*********************************************************************************************
 | 
					
						
							|  |  |  |  * Conversion: Prolog Term->char[] and char[]->Prolog Term | 
					
						
							|  |  |  |  *********************************************************************************************/ | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Converts a term t into a string.   | 
					
						
							|  |  |  |  * The ascii representation of t is | 
					
						
							|  |  |  |  * copied to ptr if it occupies less than size.  | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | char*  | 
					
						
							| 
									
										
										
										
											2012-02-02 09:58:05 +00:00
										 |  |  | term2string(char *const ptr, size_t *size, const YAP_Term t) { | 
					
						
							| 
									
										
										
										
											2006-06-02 04:16:31 +00:00
										 |  |  |   char *ret; | 
					
						
							| 
									
										
										
										
											2015-02-11 01:50:59 +00:00
										 |  |  |   RESET_BUFFER(); | 
					
						
							| 
									
										
										
										
											2006-06-02 04:16:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-02 09:58:05 +00:00
										 |  |  |   do { | 
					
						
							| 
									
										
										
										
											2012-02-02 23:25:31 +00:00
										 |  |  |     if (*size == 0) { | 
					
						
							| 
									
										
										
										
											2012-02-03 16:31:49 +00:00
										 |  |  |       *size = BUFFER_LEN = YAP_ExportTerm( t, BUFFER_PTR, BUFFER_SIZE );// canonical
 | 
					
						
							| 
									
										
										
										
											2012-02-02 09:58:05 +00:00
										 |  |  |       ret=BUFFER_PTR; | 
					
						
							| 
									
										
										
										
											2012-02-03 16:31:49 +00:00
										 |  |  |       if (BUFFER_LEN == 0) { | 
					
						
							|  |  |  | 	expand_buffer(BLOCK_SIZE); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2012-02-02 09:58:05 +00:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2012-02-03 16:31:49 +00:00
										 |  |  |       *size = YAP_ExportTerm( t, ptr, BUFFER_SIZE );// canonical
 | 
					
						
							| 
									
										
										
										
											2012-02-02 09:58:05 +00:00
										 |  |  |       ret=ptr; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-02-03 16:31:49 +00:00
										 |  |  |   } while (*size <= 0); | 
					
						
							| 
									
										
										
										
											2006-06-02 04:16:31 +00:00
										 |  |  |   return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Converts a string with a ascci representation of a term into a Prolog term. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | YAP_Term  | 
					
						
							|  |  |  | string2term(char *const ptr,const size_t *size) { | 
					
						
							|  |  |  |   YAP_Term t; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-03 16:31:49 +00:00
										 |  |  |   t = YAP_ImportTerm( ptr ); | 
					
						
							| 
									
										
										
										
											2006-06-02 04:16:31 +00:00
										 |  |  |   if ( t==FALSE ) { | 
					
						
							| 
									
										
										
										
											2012-02-03 16:31:49 +00:00
										 |  |  |     write_msg(__FUNCTION__,__FILE__,__LINE__,"FAILED string2term>>>>size:%lx %d\n",t,*size); | 
					
						
							| 
									
										
										
										
											2006-06-02 04:16:31 +00:00
										 |  |  |     exit(1); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return t; | 
					
						
							|  |  |  | }                                                                                    | 
					
						
							| 
									
										
										
										
											2013-04-26 18:23:17 -05:00
										 |  |  | #endif /* HAVE_MPI_H */
 |