| iMatix home page
| << | < | > | >>
SFL Logo SFL
Version 2.11

 

tok_split

#include "sfltok.h"
char **
tok_split (
    const char *string)

Synopsis

Accepts a string and breaks it into words, delimited by white space (spaces, tabs, newlines). Returns an array of strings which ends in a NULL string. If the string is empty or NULL, returns an array containing a single NULL value. The array is allocated dynamically by this function, and you must call tok free() to release it when you have finished. Returns NULL if there was insufficient memory to complete the split operation.

Source Code - (sfltok.c)

{
    char
        *buffer,
        *bufptr,
        **token_list,                   /*  Returned token list              */
        last_char = '\0';               /*  Last character parsed            */
    int
        word_count = 0,                 /*  Number of words in string        */
        word_nbr;

    /*  Allocate space for work string, up to the size of the input string   */
    if (string == NULL)
        string = "";

    buffer = mem_alloc (strlen (string) + 1);
    if (buffer == NULL)
        return (NULL);

    /*  Now copy string and eliminate whitespace                             */
    bufptr = buffer;                    /*  Move to start of target buffer   */
    while (*string)                     /*  Copy entire string               */
        if (isspace (*string))          /*  Collapse whitespace to           */
          {                             /*    a single null byte             */
            while (isspace (*string))
                string++;
            if (bufptr > buffer)
              {
                word_count++;           /*  We have a new word               */
                last_char = *bufptr++ = '\0';
              }
          }
        else
            last_char = *bufptr++ = *string++;

    /*  Count last word if it was not terminated in a white space            */
    if (last_char > 0)
        word_count++;
    *bufptr = '\0';                     /*  End with final NULL              */

    /*  The token list starts with a pointer to the buffer, then has one     */
    /*  pointer to each string in the buffer.  It ends with a null pointer.  */
    /*  We return the address of the first string pointer, i.e. the caller   */
    /*  does not see the pointer to the buffer.  We can thus get away with   */
    /*  just two allocs; one for the buffer and one for the token list.      */
    token_list = mem_alloc (sizeof (char *) * (word_count + 2));
    if (token_list == NULL)
        return (NULL);

    token_list [0] = buffer;            /*  Store buffer address             */
    token_list++;                       /*    and bump starting address      */

    bufptr = buffer;
    for (word_nbr = 0; word_nbr < word_count; word_nbr++)
      {
        token_list [word_nbr] = bufptr;
        bufptr += strlen (bufptr) + 1;
      }
    token_list [word_count] = NULL;     /*  Store final null pointer         */
    return (token_list);
}

| << | < | > | >> iMatix Copyright © 1996-2000 iMatix Corporation