On Monday, 12 March 2007, at 21:06:46 (+0100),
St?phane Bauland wrote:

> > +char**
> > +ecore_str_split(const char *string, const char *delimiter, int max_tokens)
> > +{
> > +   char **str_array = NULL;
> > +   char *s;
> > +   size_t n = 0;
> > +   int max = max_tokens;
> > +   const char *remainder;
> > +   size_t delimiter_len;   
> > +
> > +   CHECK_PARAM_POINTER_RETURN("string", string, NULL);
> > +   CHECK_PARAM_POINTER_RETURN("delimiter", delimiter, NULL);
> > +  
> > +  /* on the first run we just count the number of the strings we'll finally
> > +   * have */ 
> > +   remainder = string;
> > +   s = strstr(remainder, delimiter);
> > +   if (s)
> > +   {
> > +   delimiter_len = strlen(delimiter);   
> > +   while (--max_tokens && s)
> > +   {
> > +     remainder = s + delimiter_len;
> > +     s = strstr(remainder, delimiter);
> > +     n++;
> > +   }
> > +   }
> > +   if (*string != '\0') n++;
> > +   
> > +   str_array = malloc(sizeof(char *)*(n + 1));
> > +   str_array[n] = NULL;
> > +
> > +   /* reset to the initial values */
> > +   n = 0;
> > +   max_tokens = max;
> > +   remainder = string;
> > +   s = strstr(remainder, delimiter);
> > +   if (s)
> > +   {
> > +   while (--max_tokens && s)
> > +   {
> > +     size_t len;     
> > +     char *new_string;
> > +
> > +     len = s - remainder;
> > +     new_string = malloc(sizeof(char)*(len + 1));
> > +     memcpy(new_string, remainder, len);
> > +     new_string[len] = 0;
> > +     str_array[n++] = new_string;
> > +
> > +     remainder = s + delimiter_len;
> > +     s = strstr(remainder, delimiter);
> > +   }
> > +   }
> > +   if (*string != '\0') str_array[n] = strdup(remainder);
> > +
> > +   return str_array;
> > +}

Long and messy.  Find better version attached.  And as a bonus, you
only have to free the array pointer and its first element.

Quickie test program supplied also.

Michael

-- 
Michael Jennings (a.k.a. KainX)  http://www.kainx.org/  <[EMAIL PROTECTED]>
n + 1, Inc., http://www.nplus1.net/       Author, Eterm (www.eterm.org)
-----------------------------------------------------------------------
 "With every kiss our love is like brand new, and every star up in the
  sky was made for me and you.  Still we both know that the road is
  long.  But we know that we will be together because our love is
  strong."                          -- Firehouse, "Love of a Lifetime"
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

char **
str_split(const char *str, const char *delim, int max_tokens)
{
    char *s, *sep, **str_array;
    size_t len, dlen;
    int i;

    max_tokens = ((max_tokens <= 0) ? (INT_MAX) : (max_tokens - 1));
    len = strlen(str);
    dlen = strlen(delim);
    s = strdup(str);
    str_array = malloc(sizeof(char *) * len);
    for (i = 0; (i < max_tokens) && (sep = strstr(s, delim)); i++) {
        str_array[i] = s;
        s = sep + dlen;
        *sep = 0;
    }
    str_array[i++] = s;
    str_array = realloc(str_array, sizeof(char *) * (i + 1));
    str_array[i] = NULL;
    return str_array;
}

int
main(int argc, char *argv[])
{
    char **foo;

    foo = str_split("a:bcd::efgh:ijklmnop::qrstuvwxyz", ":", -1);
    printf("%s, %s, %s, %s, %s, %s, %s, %s\n", foo[0], foo[1], foo[2], foo[3], 
foo[4], foo[5], foo[6], foo[7]);
    foo = str_split("a:bcd::efgh:ijklmnop::qrstuvwxyz", ":", 3);
    printf("%s, %s, %s, %s\n", foo[0], foo[1], foo[2], foo[3]);
    foo = str_split("::::", ":", -1);
    printf("%s, %s, %s, %s, %s, %s\n", foo[0], foo[1], foo[2], foo[3], foo[4], 
foo[5]);
}
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
enlightenment-devel mailing list
enlightenment-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/enlightenment-devel

Reply via email to