Next Previous Contents

128. SLextract_list_element

Synopsis

Extract a substring of a delimited string

Usage

int SLextract_list_element (dlist, nth, delim, buf, buflen)

    char *dlist;
    unsigned int nth;
    char delim;
    char *buf;
    unsigned int buflen;
Description

SLextract_list_element may be used to obtain the nth element of a list of strings, dlist, that are delimited by the character delim. The routine copies the nth element of dlist to the buffer buf whose size is buflen characters. It returns zero upon success, or -1 if dlist does not contain an nth element.

Example

A delimited list of strings may be turned into an array of strings as follows. For conciseness, all malloc error checking has been omitted.

    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;
    }
See Also

SLmalloc, SLmake_string


Next Previous Contents