Hi people.

I needed a function to split a string into a list. My first func used Ecore_List * as return value, and dourse said me that's not really good. He said me to return a char ** so i did it. And i put it under ecore_str.

The patch have two functions. The one to allocate the array of ptr. And the second to free it.

Their's work as the glib's funcs ones.

I hope you will like it.
? ecore_str.patch
cvs diff: Diffing .
Index: Ecore_Str.h
===================================================================
RCS file: /var/cvs/e/e17/libs/ecore/src/lib/ecore/Ecore_Str.h,v
retrieving revision 1.4
diff -u -r1.4 Ecore_Str.h
--- Ecore_Str.h	17 Feb 2007 06:25:53 -0000	1.4
+++ Ecore_Str.h	12 Mar 2007 12:56:59 -0000
@@ -46,7 +46,8 @@
 EAPI int ecore_str_has_prefix(const char *str, const char *prefix);
 
 EAPI int ecore_str_has_suffix(const char *str, const char *suffix);
-
+EAPI char **ecore_str_split(const char *string, const char *delimiter, int max_tokens);
+EAPI int ecore_str_freev(char **str_array);
 
 #ifdef __cplusplus
 }
Index: Makefile.am
===================================================================
RCS file: /var/cvs/e/e17/libs/ecore/src/lib/ecore/Makefile.am,v
retrieving revision 1.11
diff -u -r1.11 Makefile.am
--- Makefile.am	16 Feb 2007 23:49:55 -0000	1.11
+++ Makefile.am	12 Mar 2007 12:56:59 -0000
@@ -35,3 +35,4 @@
 
 libecore_la_LIBADD = -lm @dlopen_libs@ @winsock_libs@
 libecore_la_LDFLAGS = -version-info 1:0:0
+libecore_la_CFLAGS = -Wall -g
Index: ecore_str.c
===================================================================
RCS file: /var/cvs/e/e17/libs/ecore/src/lib/ecore/ecore_str.c,v
retrieving revision 1.5
diff -u -r1.5 ecore_str.c
--- ecore_str.c	17 Feb 2007 06:25:53 -0000	1.5
+++ ecore_str.c	12 Mar 2007 12:57:00 -0000
@@ -20,7 +20,8 @@
 #include <sys/types.h>
 #include <string.h>
 
-# include "ecore_private.h"
+#include "ecore_private.h"
+#include "Ecore_Data.h"
 
 /*
  * Copy src to string dst of size siz.  At most siz-1 characters
@@ -129,3 +130,95 @@
 
    return (strncmp(str + str_len - suffix_len, suffix, suffix_len) == 0);
 }
+
+
+/**
+ * Splits a string into a maximum of max_tokens pieces, using the given
+ * delimiter. If max_tokens is reached, the final string in the returned
+ * string array contains the remainder of string.
+ *
+ * @param string      A string to split.
+ * @param delimiter   A string which specifies the places at which to split the string.
+ *                    The delimiter is not included in any of the resulting strings,
+ *                    unless max_tokens is reached.
+ * @param max_tokens  The maximum number of strings to split string into. If this
+ *                    is less than 1, the string is split completely.
+ * @return            A newly-allocated NULL-terminated array of strings.
+ *                    Use ecore_str_freev() to free it.
+ */
+char**
+ecore_str_split(const char *string, const char *delimiter, int max_tokens)
+{
+   Ecore_List *list = NULL;
+   char **str_array = NULL;
+   char *s;
+   int n = 0;
+   const char *remainder;
+
+   CHECK_PARAM_POINTER_RETURN("string", string, NULL);
+   CHECK_PARAM_POINTER_RETURN("delimiter", delimiter, NULL);
+
+   if ( max_tokens < 1 ) max_tokens = 1000000;
+
+   list = ecore_list_new();
+   if ( !list || !ecore_list_init(list) ) return NULL;
+
+   remainder = string;
+   s = strstr(remainder, delimiter);
+   if (s)
+   {
+	int delimiter_len = strlen(delimiter);   
+	while (--max_tokens && s)
+	{
+	  int len;     
+	  char *new_string;
+
+	  len = s - remainder;
+	  new_string = malloc(sizeof(char)*(len + 1));
+	  strncpy(new_string, remainder, len);
+	  new_string[len] = 0;
+	  ecore_list_prepend(list, new_string);
+	  n++;
+	  remainder = s + delimiter_len;
+	  s = strstr(remainder, delimiter);
+	}
+   }
+   if ( *string )
+   {
+	n++;
+	ecore_list_prepend(list, strdup(remainder));
+   }
+
+   str_array = malloc(sizeof(char *)*(n));
+   str_array[n] = NULL;
+
+   ecore_list_goto_first(list);
+   while (( s = ecore_list_next(list) ))
+   {
+        n--;
+	str_array[n] = s;
+   }
+   ecore_list_destroy(list);
+
+   return str_array;
+}
+
+
+/**
+ * Free an array of strings and the array itself
+ *
+ * @param str_array An NULL-terminated array of strings to free.
+ */
+int
+ecore_str_freev(char **str_array)
+{
+   CHECK_PARAM_POINTER_RETURN("str_array", str_array, 0);
+   int i = 0;
+   for(i=0; str_array[i] != NULL; i++)
+   {
+	FREE(str_array[i]);
+   }
+   FREE(str_array);
+
+   return 1;
+}
-------------------------------------------------------------------------
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