diff --git a/common/osdep.h b/common/osdep.h index 915ec05..5b704a9 100644 --- a/common/osdep.h +++ b/common/osdep.h @@ -60,9 +60,7 @@ #endif #ifdef _WIN32 #define rename(src,dst) (unlink(dst), rename(src,dst)) // POSIX says that rename() removes the destination, but win32 doesn't. -#ifndef strtok_r -#define strtok_r(str,delim,save) strtok(str,delim) -#endif +#define NEED_CUSTOM_STRTOK_R #endif #ifdef _MSC_VER @@ -188,4 +186,31 @@ static int ALWAYS_INLINE x264_clz( uint32_t x ) } #endif +#ifdef NEED_CUSTOM_STRTOK_R +#include // strtok() and strlen() + +static ALWAYS_INLINE char *custom_strtok_r(char *str, const char *delim, char **save) +{ + char *res, *last; + + if( !save ) + return strtok(str, delim); + if( !str && !(str = *save) ) + return NULL; + last = str + strlen(str); + if( (*save = res = strtok(str, delim)) ) + { + *save += strlen(res); + if( *save < last ) + (*save)++; + else + *save = NULL; + } + return res; +} + +#undef strtok_r //pthread.h can already define own variant (non reentrant version) +#define strtok_r(str,delim,save) custom_strtok_r(str,delim,save) +#endif + #endif /* X264_OSDEP_H */