<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE> {The S-lang C Library Reference}: SLextract_list_element</TITLE>
<LINK HREF="cref-127.html" REL=previous>
<LINK HREF="cref.html#toc128" REL=contents>
</HEAD>
<BODY>
Next
<A HREF="cref-127.html">Previous</A>
<A HREF="cref.html#toc128">Contents</A>
<HR>
<H2><A NAME="SLextract_list_element"></A> <A NAME="s128">128. <B>SLextract_list_element</B></A></H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Extract a substring of a delimited string
<DT><B> Usage </B><DD><P><CODE>int SLextract_list_element (dlist, nth, delim, buf, buflen)</CODE>
<BLOCKQUOTE><CODE>
<PRE>
    char *dlist;
    unsigned int nth;
    char delim;
    char *buf;
    unsigned int buflen;
</PRE>
</CODE></BLOCKQUOTE>
<DT><B> Description </B><DD><P><CODE>SLextract_list_element</CODE> may be used to obtain the <CODE>nth</CODE>
element of a list of strings, <CODE>dlist</CODE>, that are delimited by the
character <CODE>delim</CODE>.  The routine copies the <CODE>nth</CODE> element of
<CODE>dlist</CODE> to the buffer <CODE>buf</CODE> whose size is <CODE>buflen</CODE>
characters.  It returns zero upon success, or <CODE>-1</CODE> if <CODE>dlist</CODE>
does not contain an <CODE>nth</CODE> element.
<DT><B> Example </B><DD><P>A delimited list of strings may be turned into an array of strings
as follows.  For conciseness, all malloc error checking has been
omitted.
<BLOCKQUOTE><CODE>
<PRE>
    int list_to_array (char *list, char delim, char ***ap)
    {
       unsigned int nth;
       char **a;
       char buf[1024];

       /* Determine the size of the array */
       nth = 0;
       while (0 == SLextract_list_element (list, nth, delim, buf, sizeof(buf)))
         nth++;
       
       ap = (char **) SLmalloc ((nth + 1) * sizeof (char **));
       nth = 0;
       while (0 == SLextract_list_element (list, nth, delim, buf, sizeof(buf)))
         {
             a[nth] = SLmake_string (buf);
             nth++;
         }
       a[nth] = NULL;
       *ap = a;
       return 0;
    }
</PRE>
</CODE></BLOCKQUOTE>
<DT><B> See Also </B><DD><P><CODE>SLmalloc, SLmake_string</CODE>
</DL>
<P>
<P>
<P>
<P>
<P>
<HR>
Next
<A HREF="cref-127.html">Previous</A>
<A HREF="cref.html#toc128">Contents</A>
</BODY>
</HTML>
