diff --git a/common/osdep.h b/common/osdep.h index 4f49d30..5a1d930 100644 --- a/common/osdep.h +++ b/common/osdep.h @@ -52,9 +52,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 #define DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n))) @@ -294,4 +292,31 @@ static inline uint8_t x264_is_regular_file( FILE *filehandle ) return S_ISREG( file_stat.st_mode ); } +#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 */