From e32a3ea3b7d88c3d4958459c2d5f72d66cadf784 Mon Sep 17 00:00:00 2001 From: rswindell <> Date: Mon, 14 Aug 2006 22:51:12 +0000 Subject: [PATCH] Added a POSIX-compliant strtok_r() implementation for non-*nix platforms. --- src/xpdev/genwrap.c | 41 ++++++++++++++++++++++++++++++++++++++++- src/xpdev/genwrap.h | 2 ++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/xpdev/genwrap.c b/src/xpdev/genwrap.c index 1c8934b602..3b258f22a0 100644 --- a/src/xpdev/genwrap.c +++ b/src/xpdev/genwrap.c @@ -8,7 +8,7 @@ * @format.tab-size 4 (Plain Text/Source Code File Header) * * @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) * * * - * Copyright 2005 Rob Swindell - http://www.synchro.net/copyright.html * + * Copyright 2006 Rob Swindell - http://www.synchro.net/copyright.html * * * * This library is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public License * @@ -231,6 +231,45 @@ char* strrev(char* str) } #endif +#if !defined(__unix__) + +/****************************************************************************/ +/* Implementations of the recursive (thread-safe) version of strtok */ +/* Thanks to Apache Portable Runtime (APR) */ +/****************************************************************************/ +char* DLLCALL strtok_r(char *str, const char *delim, char **last) +{ + char* token; + + if (str==NULL) /* subsequent call */ + str = *last; /* start where we left off */ + + /* skip characters in delimiter (will terminate at '\0') */ + while(*str && strchr(delim, *str)) + ++str; + + if(!*str) /* no more tokens */ + return NULL; + + token = str; + + /* skip valid token characters to terminate token and + * prepare for the next call (will terminate at '\0) + */ + *last = token + 1; + while(**last && !strchr(delim, **last)) + ++*last; + + if (**last) { + **last = '\0'; + ++*last; + } + + return token; +} + +#endif + /****************************************************************************/ /* Initialize (seed) the random number generator */ /****************************************************************************/ diff --git a/src/xpdev/genwrap.h b/src/xpdev/genwrap.h index 5b7cc7c812..76d116c917 100644 --- a/src/xpdev/genwrap.h +++ b/src/xpdev/genwrap.h @@ -290,6 +290,8 @@ DLLEXPORT int DLLCALL get_errno(void); DLLEXPORT struct tm* DLLCALL localtime_r(const time_t* t, struct tm* tm); DLLEXPORT char* DLLCALL ctime_r(const time_t *t, char *buf); DLLEXPORT char* DLLCALL asctime_r(const struct tm *tm, char *buf); + DLLEXPORT char* DLLCALL strtok_r(char *str, const char *delim, char **last); + #endif #if defined(__solaris__) -- GitLab