Sophie

Sophie

distrib > Mandriva > 8.1 > i586 > by-pkgid > 700475c8ae73fb4d57b6df4485c29e1c > files > 96

slang-doc-1.4.4-2mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
 <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9">
 <TITLE> {The S-lang C Library Reference}: SLclass_set_string_function</TITLE>
 <LINK HREF="cref-43.html" REL=next>
 <LINK HREF="cref-41.html" REL=previous>
 <LINK HREF="cref.html#toc42" REL=contents>
</HEAD>
<BODY>
<A HREF="cref-43.html">Next</A>
<A HREF="cref-41.html">Previous</A>
<A HREF="cref.html#toc42">Contents</A>
<HR>
<H2><A NAME="SLclass_set_string_function"></A> <A NAME="s42">42. <B>SLclass_set_string_function</B></A></H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Set a data type's string representation callback
<DT><B> Usage </B><DD><P><CODE>int SLclass_set_string_function (cl, sfun)</CODE>
<BLOCKQUOTE><CODE>
<PRE>
   SLang_Class_Type *cl
   char *(*sfun) (unsigned char, VOID_STAR);
</PRE>
</CODE></BLOCKQUOTE>
<DT><B> Description </B><DD><P>The <CODE>SLclass_set_string_function</CODE> routine is used to define a
callback function, <CODE>sfun</CODE>, that that will be used when a string
representation of an object of the data type represented by <CODE>cl</CODE>
is needed.  <CODE>cl</CODE> must have already been obtained via a call to
<CODE>SLclass_allocate_class</CODE>.  When called, <CODE>sfun</CODE> will be
passed two arguments: a unsigned char which represents the data
type, and the address of the object for which a string represetation
is required.  The callback function must return a <EM>malloced</EM>
string.
<P>Upon success, <CODE>SLclass_set_string_function</CODE> returns zero, or
upon error it returns <CODE>-1</CODE>.
<DT><B> Example </B><DD><P>A callback function that handles both <CODE>SLANG_STRING_TYPE</CODE> and
<CODE>SLANG_INT_TYPE</CODE> variables looks like:
<BLOCKQUOTE><CODE>
<PRE>
     char *string_and_int_callback (unsigned char type, VOID_STAR addr)
     {
        char buf[64];

        switch (type)
          {
             case SLANG_STRING_TYPE:
               return SLmake_string (*(char **)addr);
             
             case SLANG_INTEGER_TYPE:
               sprintf (buf, "%d", *(int *)addr);
               return SLmake_string (buf);
          }
        return NULL;
     }
</PRE>
</CODE></BLOCKQUOTE>
<DT><B> Notes </B><DD><P>The default string callback simply returns the name of the data type.
<DT><B> See Also </B><DD><P><CODE>SLclass_allocate_class, SLclass_register_class</CODE>
</DL>
<P>
<P>
<P>
<HR>
<A HREF="cref-43.html">Next</A>
<A HREF="cref-41.html">Previous</A>
<A HREF="cref.html#toc42">Contents</A>
</BODY>
</HTML>