YAP 7.1.0
prologterms2c.c
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.c,v 1.4 2006-09-28 11:42:51 vsc Exp $
21Comments: 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#if HAVE_MPI_H
38
39#ifdef COMPRESS
40#include "minilzo.h"
41#endif
42
43#ifndef Quote_illegal_f
44#define Quote_illegal_f 1
45#define Ignore_ops_f 2
46#define Handle_vars_f 4
47#define Use_portray_f 8
48#define To_heap_f 16
49#endif
50
51
52#ifdef COMPRESS
53
54#endif
55
56#if THREADS
57struct buffer_ds buffer;
58#else
59struct buffer_ds buffers[1024];
60#endif
61
62/*********************************************************************************************/
63// prototypes
64void write_msg(const char *fun,const char *file, int line,const char *format, ...);
65
66size_t write_term_to_stream(const int fd,const YAP_Term term);
67YAP_Term read_term_from_stream(const int fd);
68/*********************************************************************************************/
69/*
70 * Writes a debug message containing the processid, function name, filename, line, and a user message
71 */
72void
73write_msg(const char *fun,const char *file, int line,
74 const char *format, ...) {
75 va_list ap;
76
77 va_start(ap, format);
78 /* Print the message to stderr */
79 fprintf(stderr,
80 "[%d:%s in %s at line %d] ", getpid(),fun, file, line);
81 vfprintf(stderr, format, ap);
82 va_end(ap);
83}
84/*********************************************************************************************
85 * Memory handling functions
86 *********************************************************************************************/
87/*
88 * Adds 'space' to the size of the currently allocated buffer
89 */
90static void
91expand_buffer(const size_t space ) {
92 //BUFFER_PTR = realloc( BUFFER_PTR, BUFFER_SIZE + space );
93 if( BUFFER_PTR == NULL ) {
94 YAP_Error(0,0,"Prolog2Term: Out of memory.\n");
95#ifdef MPI
96 MPI_Finalize();
97#endif
98 YAP_Exit( 1 );
99 }
100 BUFFER_SIZE+=space;
101}
102/*
103 * Changes the size of the buffer to contain at least newsize bytes
104 */
105void change_buffer_size(const size_t newsize) {
106 return;
107 if ( BUFFER_PTR == NULL )
108 {
109 if (false //(BUFFER_PTR = malloc( BLOCK_SIZE < newsize ? newsize : BLOCK_SIZE)) == NULL
110 ) {
111 YAP_Error(0,0,"Prolog2Term: Out of memory.\n");
112#ifdef MPI
113 MPI_Finalize();
114#endif
115 YAP_Exit( 1 );
116 }
117 }
118 else if ((BUFFER_SIZE>=BLOCK_SIZE &&
119 BUFFER_SIZE>=newsize) )
120 {
121 return;
122 }
123 else //if ((BUFFER_PTR = realloc( BUFFER_PTR, newsize)) == NULL)
124 {
125 YAP_Error(0,0,"Prolog2Term: Out of memory.\n");
126#ifdef MPI
127 MPI_Finalize();
128#endif
129 YAP_Exit( 1 );
130 }
131 BUFFER_SIZE=newsize;
132}
133/*********************************************************************************************
134 * I/O functions
135 *********************************************************************************************/
136/*
137 * Function used by YAP to write a char to a string
138 */
139static void
140p2c_putt(const YAP_Term t) {
141 // if( buffer.size==buffer.len+1 )
142
143 while ((BUFFER_LEN=YAP_ExportTerm(t, BUFFER_PTR, BUFFER_SIZE)) <= 0) {
144#ifdef DEBUG
145 write_msg(__FUNCTION__,__FILE__,__LINE__,"p2c_putc:buffer expanded: size=%u pos=%u len=%u\n",BUFFER_SIZE,BUFFER_POS,BUFFER_LEN);
146#endif
147 expand_buffer( BLOCK_SIZE );
148 }
149}
150/*
151 * Function used by YAP to read a char from a string
152 */
153/*
154 * Writes a term to a stream.
155 */
156size_t
157write_term_to_stream(const int fd,const YAP_Term term) {
158
159 RESET_BUFFER();
160 printf("BUFFER_PTR=%p\n", BUFFER_PTR);
161 p2c_putt(term);
162 if (write(fd,(void*)BUFFER_PTR,BUFFER_LEN) < 0) { // write term
163 YAP_Error(0,0,"Prolog2Term: IO error in write.\n");
164 return -1;
165 }
166 return BUFFER_LEN;
167}
168/*
169 * Read a prolog term from a stream
170 * (the prolog term must have been writen by the write_term_to_stream)
171 */
172YAP_Term
173read_term_from_stream(const int fd) {
174 size_t size;
175
176 RESET_BUFFER();
177 if (!read(fd,(void*)&size,sizeof(size_t))) { // read the size of the term
178 YAP_Error(0,0,"Prolog2Term: IO error in read.\n");
179 }
180#ifdef DEBUG
181 write_msg(__FUNCTION__,__FILE__,__LINE__,"read_term_from_stream>>>>size:%d\n",size);
182#endif
183 if ( size> BUFFER_SIZE)
184 expand_buffer(size-BUFFER_SIZE);
185 if (!read(fd,BUFFER_PTR,size)) {
186 YAP_Error(0,0,"Prolog2Term: IO error in read.\n");
187 }; // read term from stream
188 return YAP_ImportTerm( BUFFER_PTR);
189}
190/*********************************************************************************************
191 * Conversion: Prolog Term->char[] and char[]->Prolog Term
192 *********************************************************************************************/
193/*
194 * Converts a term t into a string.
195 * The ascii representation of t is
196 * copied to ptr if it occupies less than size.
197 */
198char*
199term2string( const YAP_Term t) {
200 char *b=YAP_WriteDynamicBuffer(t,0,YAP_WRITE_QUOTED|YAP_WRITE_IGNORE_OPS);
201 //fprintf(stderr,"<< %s \n",b);
202
203 return b;
204}
205/*
206 * Converts a string with a ascci representation of a term into a Prolog term.
207 */
208YAP_Term
209string2term(char *const ptr,const size_t *size) {
210 YAP_Term t;
211 t = YAP_ReadBuffer( ptr, NULL );
212 // fprintf(stderr,"%s >>\n",ptr);
213 if ( t==FALSE ) {
214 write_msg(__FUNCTION__,__FILE__,__LINE__,"FAILED string2term>>>>size:%lx %d\n",t,*size);
215 exit(1);
216 }
217 return t;
218}
219#endif /* HAVE_MPI_H */
X_API char * YAP_WriteDynamicBuffer(YAP_Term t, encoding_t enc, int flags)
write a a term to n user-provided buffer: make sure not tp overflow the buffer even if the text is mu...
Definition: c_interface.c:2305