Next: Passing Data into an
Up: Calling XSB from C
Previous: C Functions for Calling
  Contents
  Index
The Variable-length String Data Type
XSB uses variable-length strings to communicate with
certain C subroutines when the size of the output that needs to be passed
from the Prolog side to the C side is not known. Variable-length strings
adjust themselves depending on the size of the data they must hold and are
ideal for this situation. For instance, as we have seem the two subroutines
xsb_query_string_string(query,buff,sep) and
xsb_next_string(buff,sep) use the variable string data type,
VarString, for their second argument. To use this data type, make sure
that
#include "cinterf.h"
appears at the top of the program file. Variables of the VarString
type are declared using a macro that must appear in the declaration section
of the program:
vstrDEFINE(buf);
There is one important consideration concerning VarString with the
automatic storage class: they must be
destroyed on exit (see vstrDESTROY, below) from the procedure
that defines them, or else there will be a memory leak.
It is not necessary to destroy static VarString's.
The public attributes of the type are int length and char *string.
Thus, buf.string represents the actual contents of the buffer and
buf.length is the length of that data. Although the length and the
contents of a VarString string is readily accessible, the user must
not modify these items directly. Instead, he should use the macros
provided for that purpose:
- vstrSET(VarString *vstr, char *str):
Assign the value of the regular null-terminated C string to the
VarString vstr. The size of vstr is adjusted
automatically.
- vstrSETV(VarString *vstr1, VarString *vstr2):
Like vstrSET, but the second argument is a variable-length
string, not a regular C string.
- vstrAPPEND(VarString *vstr, char *str):
Append the null-terminated string str to the VarString
vstr. The size of vstr is adjusted.
- vstrPREPEND(VarString *vstr, char *str):
Like vstrAPPEND, except that str is prepended.
- vstrAPPENDV(VarString *vstr1, VarString *vstr2):
Like vstrAPPEND, except that the second string is also a
VarString.
- vstrPREPENDV(VarString *vstr1, VarString *vstr2):
Like vstrAPPENDV, except that the second string is prepended.
- vstrCOMPARE(VarString *vstr1, VarString *vstr2):
Compares two VarString. If the first one is lexicographically
larger, then the result is positive; if the first string is smaller,
than the result is negative; if the two strings have the same content
( i.e., vstr1->string equals vstr2->string then the
result is zero.
- vstrSTRCMP(VarString *vstr, char *str):
Like vstrCOMPARE but the second argument is a regular,
null-terminated string.
- vstrAPPENDBLK(VarString *vstr, char *blk, int size):
This is like vstrAPPEND, but the second argument is not assumed
to be null-terminated. Instead, size characters pointed to by
blk are appended to vstr. The size of vstr is
adjusted, but the content is not null terminated.
- vstrPREPENDBLK(VarString *vstr, char *blk, int size):
Like vstrPREPEND, but blk is not assumed to point to a
null-terminated string. Instead, size characters from the region
pointed to by blk are prepended to vstr.
- vstrNULL_TERMINATE(VarString *vstr):
Null-terminates the VarString string vstr. This is used in
conjunction with vstrAPPENDBLK, because the latter does not
null-terminate variable-length strings.
- vstrENSURE_SIZE(VarString *vstr, int minsize):
Ensure that the string has room for at least minsize bytes.
This is a low-level routine, which is used to interface to procedures
that do not use VarString internally. If the string is larger
than minsize, the size might actually shrink to the nearest
increment that is larger minsize.
- vstrSHRINK(VarString *vstr, int increment):
Shrink the size of vstr to the minimum necessary to hold the
data. increment becomes the new increment by which vstr is
adjusted. Since VarString is automatically shrunk by
vstrSET, it is rarely necessary to shrink a VarString explicitly.
However, one might want to change the adustment increment using this
macro (the default increment is 128).
- vstrDESTROY(VarString *vstr):
Destroys a VarString. Explicit destruction is necessary for
VarString's with the automatic storage class. Otherwise, memory
leak is possible.
Next: Passing Data into an
Up: Calling XSB from C
Previous: C Functions for Calling
  Contents
  Index
Baoqiu Cui
2000-04-23