[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [41337] trunk/blender/source/blender/ blenlib: BLI_string: Adding the BLI_strtok_r function, which mimics stdlib strtok_r (unavailable on some systems).

Bastien Montagne montagne29 at wanadoo.fr
Fri Oct 28 15:07:11 CEST 2011


Revision: 41337
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=41337
Author:   mont29
Date:     2011-10-28 13:07:11 +0000 (Fri, 28 Oct 2011)
Log Message:
-----------
BLI_string: Adding the BLI_strtok_r function, which mimics stdlib strtok_r (unavailable on some systems).

It allows to iterate over a string, returning an new element each time, using a char as separator. See BLI_String.h's comments for more info and an example.

Needed by the UI template list patch following!

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/BLI_string.h
    trunk/blender/source/blender/blenlib/intern/string.c

Modified: trunk/blender/source/blender/blenlib/BLI_string.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_string.h	2011-10-28 12:57:37 UTC (rev 41336)
+++ trunk/blender/source/blender/blenlib/BLI_string.h	2011-10-28 13:07:11 UTC (rev 41337)
@@ -133,6 +133,28 @@
 int BLI_natstrcmp(const char *s1, const char *s2);
 size_t BLI_strnlen(const char *str, size_t maxlen);
 
+	/**
+	 * Split str on the first occurence of delimiter, returns the first
+	 * part as a mallocN'd string, and stores the second part into
+	 * ctx (also mallocN'd).
+	 * If str is NULL, split on ctx instead.
+	 * This allows to iterate over this "generator" function:
+	 *
+	 * char *ctx = NULL;
+	 * char *res = NULL;
+	 * for(res = BLI_strtok_r("a;dummy;csv;line", ";", &ctx); res; res = BLI_strtok_r(NULL, ";", &ctx)) {
+	 *     printf(res);
+	 *     MEM_freeN(res);
+	 * }
+	 * 
+	 * @param str The string to be split.
+	 * @param delimiter The char used to split str apart.
+	 * @param ctx The "context" string. It’s a pointer inside the org passed @str,
+	 *            so it has no specific mem management.
+	 * @retval Returns the mallocN'd first element from split str (or ctx).
+	 */
+char *BLI_strtok_r(char *str, const char *delimiter, char **ctx);
+
 void BLI_timestr(double _time, char *str); /* time var is global */
 
 void BLI_ascii_strtolower(char *str, int len);

Modified: trunk/blender/source/blender/blenlib/intern/string.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/string.c	2011-10-28 12:57:37 UTC (rev 41336)
+++ trunk/blender/source/blender/blenlib/intern/string.c	2011-10-28 13:07:11 UTC (rev 41337)
@@ -375,6 +375,35 @@
 	return 0;
 }
 
+/* As unfortunately strtok_r is not available everywhere... */
+char *BLI_strtok_r(char *str, const char *delimiter, char **ctx)
+{
+	char *cut = NULL, *ret = NULL;
+	char *split = str ? str : *ctx;
+
+	if(!split) {
+		return ret;
+	}
+
+	cut = strchr(split, *delimiter);
+	if(cut) {
+		size_t len_ret = cut - split;
+		size_t len_ctx = strlen(split) - len_ret - 1;
+		ret = BLI_strdupn(split, len_ret);
+		if(len_ctx > 0) {
+			*ctx = split+len_ret+1;
+		}
+		else {
+			*ctx = NULL;
+		}
+	}
+	else {
+		ret = BLI_strdup(split);
+		*ctx = NULL;
+	}
+	return ret;
+}
+
 void BLI_timestr(double _time, char *str)
 {
 	/* format 00:00:00.00 (hr:min:sec) string has to be 12 long */




More information about the Bf-blender-cvs mailing list