Enlightenment CVS committal
Author : pfritz
Project : e17
Module : libs/ecore
Dir : e17/libs/ecore/src/lib/ecore
Modified Files:
Ecore_Str.h ecore_str.c
Log Message:
add ecore_str_split(), thanks to rookmoot
===================================================================
RCS file: /cvs/e/e17/libs/ecore/src/lib/ecore/Ecore_Str.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -3 -r1.4 -r1.5
--- Ecore_Str.h 17 Feb 2007 06:25:53 -0000 1.4
+++ Ecore_Str.h 13 Mar 2007 01:17:33 -0000 1.5
@@ -46,7 +46,9 @@
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 void ecore_str_vector_free(char **str_array);
#ifdef __cplusplus
}
===================================================================
RCS file: /cvs/e/e17/libs/ecore/src/lib/ecore/ecore_str.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -3 -r1.5 -r1.6
--- ecore_str.c 17 Feb 2007 06:25:53 -0000 1.5
+++ ecore_str.c 13 Mar 2007 01:17:33 -0000 1.6
@@ -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_vector_free() to free it.
+ */
+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;
+}
+
+/**
+ * Free an array of strings and the array itself
+ *
+ * @param str_array An NULL-terminated array of strings to free.
+ */
+void
+ecore_str_vector_free(char **str_array)
+{
+ CHECK_PARAM_POINTER("str_array", str_array);
+ int i;
+
+ for(i=0; str_array[i] != NULL; i++)
+ {
+ FREE(str_array[i]);
+ }
+ FREE(str_array);
+}
+
-------------------------------------------------------------------------
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-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/enlightenment-cvs