Yap_WriteBuffer
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1067 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
parent
874758e936
commit
e7fbb54532
@ -10,8 +10,11 @@
|
||||
* File: c_interface.c *
|
||||
* comments: c_interface primitives definition *
|
||||
* *
|
||||
* Last rev: $Date: 2004-05-14 17:11:30 $,$Author: vsc $ *
|
||||
* Last rev: $Date: 2004-05-14 17:56:45 $,$Author: vsc $ *
|
||||
* $Log: not supported by cvs2svn $
|
||||
* Revision 1.45 2004/05/14 17:11:30 vsc
|
||||
* support BigNums in interface
|
||||
*
|
||||
* Revision 1.44 2004/05/14 16:33:44 vsc
|
||||
* add Yap_ReadBuffer
|
||||
* *
|
||||
@ -104,6 +107,7 @@ X_API void STD_PROTO(YAP_InitConsult,(int, char *));
|
||||
X_API void STD_PROTO(YAP_EndConsult,(void));
|
||||
X_API Term STD_PROTO(YAP_Read, (int (*)(void)));
|
||||
X_API void STD_PROTO(YAP_Write, (Term, void (*)(int), int));
|
||||
X_API Term STD_PROTO(YAP_WriteBuffer, (Term, char *, unsigned int, int));
|
||||
X_API char *STD_PROTO(YAP_CompileClause, (Term));
|
||||
X_API void STD_PROTO(YAP_PutValue, (Atom,Term));
|
||||
X_API Term STD_PROTO(YAP_GetValue, (Atom));
|
||||
@ -893,6 +897,15 @@ YAP_Write(Term t, void (*myputc)(int), int flags)
|
||||
RECOVER_MACHINE_REGS();
|
||||
}
|
||||
|
||||
X_API Term
|
||||
YAP_WriteBuffer(Term t, char *buf, unsigned int sze, int flags)
|
||||
{
|
||||
BACKUP_MACHINE_REGS();
|
||||
t = Yap_TermToString(t, buf, sze, flags);
|
||||
RECOVER_MACHINE_REGS();
|
||||
return t;
|
||||
}
|
||||
|
||||
X_API char *
|
||||
YAP_CompileClause(Term t)
|
||||
{
|
||||
|
57
C/iopreds.c
57
C/iopreds.c
@ -2058,26 +2058,17 @@ p_open_mem_read_stream (void) /* $open_mem_read_stream(+List,-Stream) */
|
||||
return (Yap_unify (ARG2, t));
|
||||
}
|
||||
|
||||
static Int
|
||||
p_open_mem_write_stream (void) /* $open_mem_write_stream(-Stream) */
|
||||
static int
|
||||
open_buf_write_stream(char *nbuf, UInt sz)
|
||||
{
|
||||
Term t;
|
||||
StreamDesc *st;
|
||||
int sno;
|
||||
char *nbuf;
|
||||
extern int Yap_page_size;
|
||||
StreamDesc *st;
|
||||
|
||||
while ((nbuf = (char *)Yap_AllocAtomSpace(Yap_page_size*sizeof(char))) == NULL) {
|
||||
if (!Yap_growheap(FALSE, Yap_page_size*sizeof(char), NULL)) {
|
||||
Yap_Error(SYSTEM_ERROR, TermNil, Yap_ErrorMessage);
|
||||
return(FALSE);
|
||||
}
|
||||
}
|
||||
for (sno = 0; sno < MaxStreams; ++sno)
|
||||
if (Stream[sno].status & Free_Stream_f)
|
||||
break;
|
||||
if (sno == MaxStreams)
|
||||
return (PlIOError (SYSTEM_ERROR,TermNil, "new stream not available for open_mem_read_stream/1"));
|
||||
return -1;
|
||||
st = &Stream[sno];
|
||||
/* currently these streams are not seekable */
|
||||
st->status = Output_Stream_f | InMemory_Stream_f;
|
||||
@ -2092,7 +2083,27 @@ p_open_mem_write_stream (void) /* $open_mem_write_stream(-Stream) */
|
||||
st->stream_getc_for_read = MemGetc;
|
||||
st->u.mem_string.pos = 0;
|
||||
st->u.mem_string.buf = nbuf;
|
||||
st->u.mem_string.max_size = Yap_page_size;
|
||||
st->u.mem_string.max_size = sz;
|
||||
return sno;
|
||||
}
|
||||
|
||||
static Int
|
||||
p_open_mem_write_stream (void) /* $open_mem_write_stream(-Stream) */
|
||||
{
|
||||
Term t;
|
||||
int sno;
|
||||
char *nbuf;
|
||||
extern int Yap_page_size;
|
||||
|
||||
while ((nbuf = (char *)Yap_AllocAtomSpace(Yap_page_size*sizeof(char))) == NULL) {
|
||||
if (!Yap_growheap(FALSE, Yap_page_size*sizeof(char), NULL)) {
|
||||
Yap_Error(SYSTEM_ERROR, TermNil, Yap_ErrorMessage);
|
||||
return(FALSE);
|
||||
}
|
||||
}
|
||||
sno = open_buf_write_stream(nbuf, Yap_page_size);
|
||||
if (sno == -1)
|
||||
return (PlIOError (SYSTEM_ERROR,TermNil, "new stream not available for open_mem_read_stream/1"));
|
||||
t = MkStream (sno);
|
||||
return (Yap_unify (ARG1, t));
|
||||
}
|
||||
@ -4672,6 +4683,24 @@ Yap_StringToTerm(char *s,Term *tp)
|
||||
return t;
|
||||
}
|
||||
|
||||
Term
|
||||
Yap_TermToString(Term t, char *s, unsigned int sz, int flags)
|
||||
{
|
||||
int sno = open_buf_write_stream(s, sz);
|
||||
int old_output_stream = Yap_c_output_stream;
|
||||
|
||||
if (sno < 0)
|
||||
return FALSE;
|
||||
*--ASP = MkIntTerm(0);
|
||||
Yap_c_output_stream = sno;
|
||||
Yap_plwrite (t, Stream[sno].stream_putc, flags);
|
||||
s[Stream[sno].u.mem_string.pos] = '\0';
|
||||
Stream[sno].status = Free_Stream_f;
|
||||
Yap_c_output_stream = old_output_stream;
|
||||
++ASP;
|
||||
return EX;
|
||||
}
|
||||
|
||||
void
|
||||
Yap_InitBackIO (void)
|
||||
{
|
||||
|
@ -275,6 +275,7 @@ int STD_PROTO(Yap_GetCharForSIGINT,(void));
|
||||
int STD_PROTO(Yap_StreamToFileNo,(Term));
|
||||
Term STD_PROTO(Yap_OpenStream,(FILE *,char *,Term,int));
|
||||
Term STD_PROTO(Yap_StringToTerm,(char *,Term *));
|
||||
Term STD_PROTO(Yap_TermToString,(Term,char *,unsigned int,int));
|
||||
|
||||
extern int
|
||||
Yap_c_input_stream,
|
||||
|
73
docs/yap.tex
73
docs/yap.tex
@ -14006,43 +14006,43 @@ interface may change and improve in the future.
|
||||
The following C-functions are available from Yap:
|
||||
|
||||
@itemize @bullet
|
||||
@item YapCompileClause(@code{Term} @var{Clause})
|
||||
@findex YapCompileClause/1
|
||||
@item YAP_CompileClause(@code{Term} @var{Clause})
|
||||
@findex YAP_CompileClause/1
|
||||
Compile the Prolog term @var{Clause} and assert it as the last clause
|
||||
for the corresponding procedure.
|
||||
|
||||
@item @code{int} YapContinueGoal(@code{void})
|
||||
@findex YapContinueGoal/0
|
||||
@item @code{int} YAP_ContinueGoal(@code{void})
|
||||
@findex YAP_ContinueGoal/0
|
||||
Continue execution from the point where it stopped.
|
||||
|
||||
@item @code{void} YapError(@code{char *} @var{error_description})
|
||||
@findex YapError/1
|
||||
@item @code{void} YAP_Error(@code{char *} @var{error_description})
|
||||
@findex YAP_Error/1
|
||||
Generate an YAP System Error with description given by the string
|
||||
@var{error_description}.
|
||||
|
||||
@item @code{void} YapExit(@code{int} @var{exit_code})
|
||||
@findex YapExit/1
|
||||
@item @code{void} YAP_Exit(@code{int} @var{exit_code})
|
||||
@findex YAP_Exit/1
|
||||
Exit YAP immediately. The argument @var{exit_code} gives the error code
|
||||
and is supposed to be 0 after successful execution in Unix and Unix-like
|
||||
systems.
|
||||
|
||||
@item @code{Term} YapGetValue(@code{Atom} @var{at})
|
||||
@findex YapGetValue/1
|
||||
@item @code{Term} YAP_GetValue(@code{Atom} @var{at})
|
||||
@findex YAP_GetValue/1
|
||||
Return the term @var{value} associated with the atom @var{at}. If no
|
||||
such term exists the function will return the empty list.
|
||||
|
||||
@item YapFastInit(@code{char *} @var{SavedState})
|
||||
@findex YapFastInit/1
|
||||
@item YAP_FastInit(@code{char *} @var{SavedState})
|
||||
@findex YAP_FastInit/1
|
||||
Initialize a copy of YAP from @var{SavedState}. The copy is
|
||||
monolithic and currently must be loaded at the same address where it was
|
||||
saved. @code{YapFastInit} is a simpler version of @code{YapInit}.
|
||||
saved. @code{YAP_FastInit} is a simpler version of @code{YAP_Init}.
|
||||
|
||||
@item YapInit(@code{char *} @var{SavedState}, @code{int}
|
||||
@item YAP_Init(@code{char *} @var{SavedState}, @code{int}
|
||||
@var{HeapSize}, @code{int} @var{StackSize}, @code{int}
|
||||
@var{TrailSize}, @code{int} @var{NumberofWorkers}, @code{int}
|
||||
@var{SchedulerLoop}, @code{int} @var{DelayedReleaseLoad}, @code{int}
|
||||
@var{argc}, @code{char **} @var{argv})
|
||||
@findex YapInit/9
|
||||
@findex YAP_Init/9
|
||||
Initialize YAP. In the future the arguments as a single @code{C}
|
||||
structure.
|
||||
|
||||
@ -14066,41 +14066,50 @@ The argument count @var{argc} and string of arguments @var{argv}
|
||||
arguments are to be passed to user programs as the arguments used to
|
||||
call YAP.
|
||||
|
||||
@item @code{void} YapPutValue(@code{Atom} @var{at}, @code{Term} @var{value})
|
||||
@findex YapPutValue/2
|
||||
@item @code{void} YAP_PutValue(@code{Atom} @var{at}, @code{Term} @var{value})
|
||||
@findex YAP_PutValue/2
|
||||
Associate the term @var{value} with the atom @var{at}. The term
|
||||
@var{value} must be a constant. This functionality is used by YAP as a
|
||||
simple way for controlling and communicating with the Prolog run-time.
|
||||
|
||||
@item @code{Term} YapRead(@code{int (*)(void)} @var{GetC})
|
||||
@findex YapRead/1
|
||||
@item @code{Term} YAP_Read(@code{int (*)(void)} @var{GetC})
|
||||
@findex YAP_Read/1
|
||||
Parse a Term using the function @var{GetC} to input characters.
|
||||
|
||||
@item @code{int} YapRunGoal(@code{Term} @var{Goal})
|
||||
@findex YapRunGoal/1
|
||||
@item @code{int} YAP_RunGoal(@code{Term} @var{Goal})
|
||||
@findex YAP_RunGoal/1
|
||||
Execute query @var{Goal} and return 1 if the query succeeds, and
|
||||
0 otherwise.
|
||||
|
||||
@item @code{int} YapRestartGoal(@code{void})
|
||||
@findex YapRestartGoal/0
|
||||
@item @code{int} YAP_RestartGoal(@code{void})
|
||||
@findex YAP_RestartGoal/0
|
||||
Look for the next solution to the current query by forcing YAP to backtrack.
|
||||
|
||||
@item @code{int} YapReset(@code{void})
|
||||
@findex YapReset/0
|
||||
@item @code{int} YAP_Reset(@code{void})
|
||||
@findex YAP_Reset/0
|
||||
Reset execution environment (similar to the @code{abort/0}
|
||||
builtin). This is useful when you want to start a new query before
|
||||
asking all solutions to the previous query.
|
||||
|
||||
@item @code{void} YapWrite(@code{Term} @var{t}, @code{void (*)(int)}
|
||||
@item @code{void} YAP_Write(@code{Term} @var{t}, @code{void (*)(int)}
|
||||
@var{PutC}, @code{int} @var{flags})
|
||||
@findex YapRead/1
|
||||
@findex YAP_Write/3
|
||||
Write a Term @var{t} using the function @var{PutC} to output
|
||||
characters. The term is written according to a mask of the following
|
||||
flags in the @code{flag} argument: @code{YAP_WRITE_QUOTED},
|
||||
@code{YAP_WRITE_HANDLE_VARS}, and @code{YAP_WRITE_IGNORE_OPS}.
|
||||
|
||||
@item @code{void} YapInitConsult(@code{int} @var{mode}, @code{char *} @var{filename})
|
||||
@findex YapInitConsult/2
|
||||
@item @code{void} YAP_WriteBuffer(@code{Term} @var{t}, @code{char *}
|
||||
@var{buff}, @code{unsigned int}
|
||||
@var{size}, @code{int} @var{flags})
|
||||
@findex YAP_WriteBuffer/4
|
||||
Write a Term @var{t} to buffer @var{buff} with size @var{size}. The
|
||||
term is written according to a mask of the following flags in the
|
||||
@code{flag} argument: @code{YAP_WRITE_QUOTED},
|
||||
@code{YAP_WRITE_HANDLE_VARS}, and @code{YAP_WRITE_IGNORE_OPS}.
|
||||
|
||||
@item @code{void} YAP_InitConsult(@code{int} @var{mode}, @code{char *} @var{filename})
|
||||
@findex YAP_InitConsult/2
|
||||
Enter consult mode on file @var{filename}. This mode maintains a few
|
||||
data-structures internally, for instance to know whether a predicate
|
||||
before or not. It is still possible to execute goals in consult mode.
|
||||
@ -14111,11 +14120,11 @@ bootstrapping Prolog, as otherwise one may call the Prolog predicate
|
||||
@code{compile/1} or @code{consult/1} to do compilation.
|
||||
|
||||
Note that it is up to the user to open the file @var{filename}. The
|
||||
@code{YapInitConsult} function only uses the file name for internal
|
||||
@code{YAP_InitConsult} function only uses the file name for internal
|
||||
bookkeeping.
|
||||
|
||||
@item @code{void} YapEndConsult(@code{void})
|
||||
@findex YapEndConsult/0
|
||||
@item @code{void} YAP_EndConsult(@code{void})
|
||||
@findex YAP_EndConsult/0
|
||||
Finish consult mode.
|
||||
|
||||
@end itemize
|
||||
|
@ -229,6 +229,9 @@ extern X_API YAP_Term PROTO(YAP_Read,(int (*)(void)));
|
||||
/* void YAP_Write(YAP_Term,void (*)(int),int) */
|
||||
extern X_API void PROTO(YAP_Write,(YAP_Term,void (*)(int),int));
|
||||
|
||||
/* void YAP_WriteBuffer(YAP_Term,char *,unsgined int,int) */
|
||||
extern X_API void PROTO(YAP_WriteBuffer,(YAP_Term,char *,unsigned int,int));
|
||||
|
||||
/* char *YAP_CompileClause(YAP_Term) */
|
||||
extern X_API char *PROTO(YAP_CompileClause,(YAP_Term));
|
||||
|
||||
|
@ -57,6 +57,7 @@ YAP_InitConsult
|
||||
YAP_EndConsult
|
||||
YAP_Read
|
||||
YAP_Write
|
||||
YAP_WriteBuffer
|
||||
YAP_CompileClause
|
||||
YAP_PutValue
|
||||
YAP_GetValue
|
||||
|
Reference in New Issue
Block a user