[lttng-dev] [Babeltrace PATCH 12/23] Add MinGW implementation of strtok_r

Ikaheimonen, JP jp_ikaheimonen at mentor.com
Wed May 22 04:06:52 EDT 2013


Add an implementation for strtok_r for MinGW, where it is missing.
The Windows equivalent strtok_s seems to be missing in MinGW as well.
---
 include/babeltrace/compat/string.h | 39 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/include/babeltrace/compat/string.h b/include/babeltrace/compat/string.h
index 2584ecb..1db5837 100644
--- a/include/babeltrace/compat/string.h
+++ b/include/babeltrace/compat/string.h
@@ -5,6 +5,45 @@
 
 #ifdef __MINGW32__
 
+static inline char* strtok_r(
+	char *str,
+	const char *delim,
+	char **nextp)
+{
+	char *ret;
+
+	/* if str is NULL, continue from nextp */
+	if (str == NULL) {
+		str = *nextp;
+	}
+	/* skip the leading delimiter characters */
+	str += strspn(str, delim);
+
+	/* return NULL if all are delimiters */
+	if (*str == '\0') {
+		return NULL;
+	}
+	/* return the pointer to the first character that is not delimiter */
+	ret = str;
+
+	/* scan to the next delimiter */
+	str += strcspn(str, delim);
+
+	/* if we found a delimiter instead of a NUL */
+	if (*str) {
+		/* replace the delimiter with the NUL */
+		*str = '\0';
+		/* next time, scan from the character after NUL */
+		*nextp = str + 1;
+	}
+	else {
+		/* end of string: next time, scan at the end (NUL) again */
+		*nextp = str;
+	}
+
+	return ret;
+}
+
 static inline int compat_strerror_r(int errnum, char *buf, size_t buflen)
 {
 	/* non-recursive implementation of strerror_r */
-- 
1.8.1.msysgit.1




More information about the lttng-dev mailing list