[x265] [PATCH RFC] cmake: detect strtok_r, use workaround when not present

Steve Borho steve at borho.org
Mon Mar 3 20:41:00 CET 2014


# HG changeset patch
# User Steve Borho <steve at borho.org>
# Date 1393875455 21600
#      Mon Mar 03 13:37:35 2014 -0600
# Node ID 6adee81cbd68e5b16c28424f82caea6d4e252371
# Parent  288a83d7e28999798859eba6b2f38c952cac7547
cmake: detect strtok_r, use workaround when not present

diff -r 288a83d7e289 -r 6adee81cbd68 source/CMakeLists.txt
--- a/source/CMakeLists.txt	Sun Mar 02 18:57:46 2014 -0600
+++ b/source/CMakeLists.txt	Mon Mar 03 13:37:35 2014 -0600
@@ -14,6 +14,7 @@
 cmake_minimum_required (VERSION 2.8.8) # OBJECT libraries require 2.8.8
 include(CheckIncludeFiles)
 include(CheckFunctionExists)
+include(CheckSymbolExists)
 include(CheckCXXCompilerFlag)
 
 # X265_BUILD must be incremented each time the public API is changed
diff -r 288a83d7e289 -r 6adee81cbd68 source/common/CMakeLists.txt
--- a/source/common/CMakeLists.txt	Sun Mar 02 18:57:46 2014 -0600
+++ b/source/common/CMakeLists.txt	Mon Mar 03 13:37:35 2014 -0600
@@ -129,6 +129,9 @@
     source_group(Assembly FILES ${ASM_PRIMITIVES})
 endif(ENABLE_ASSEMBLY)
 
+check_symbol_exists(strtok_r "string.h" HAVE_STRTOK_R)
+set_source_files_properties(param.cpp PROPERTIES COMPILE_FLAGS -DHAVE_STRTOK_R=${HAVE_STRTOK_R})
+
 if(GCC AND GCC_HAS_NO_NARROWING)
     set_source_files_properties(cpu.cpp PROPERTIES COMPILE_FLAGS -Wno-narrowing)
 endif()
diff -r 288a83d7e289 -r 6adee81cbd68 source/common/param.cpp
--- a/source/common/param.cpp	Sun Mar 02 18:57:46 2014 -0600
+++ b/source/common/param.cpp	Mon Mar 03 13:37:35 2014 -0600
@@ -38,10 +38,45 @@
 #endif
 
 #if _WIN32
-#define STRTOK strtok_s
 #define strcasecmp _stricmp 
-#else
-#define STRTOK strtok_r
+#endif
+
+#if !HAVE_STROTOK_R
+/* 
+ * adapted from public domain strtok_r() by Charlie Gordon
+ *
+ *   from comp.lang.c  9/14/2007
+ *
+ *      http://groups.google.com/group/comp.lang.c/msg/2ab1ecbb86646684
+ *
+ *     (Declaration that it's public domain):
+ *      http://groups.google.com/group/comp.lang.c/msg/7c7b39328fefab9c
+ */
+
+char* strtok_r(
+    char *str, 
+    const char *delim, 
+    char **nextp)
+{
+    if (!str)
+        str = *nextp;
+
+    str += strspn(str, delim);
+
+    if (!*str)
+        return NULL;
+
+    char *ret = str;
+
+    str += strcspn(str, delim);
+
+    if (*str)
+        *str++ = '\0';
+
+    *nextp = str;
+
+    return ret;
+}
 #endif
 
 using namespace x265;
@@ -735,7 +770,7 @@
         char *tok, *saveptr = NULL, *init;
         bError = 0;
         cpu = 0;
-        for (init = buf; (tok = STRTOK(init, ",", &saveptr)); init = NULL)
+        for (init = buf; (tok = strtok_r(init, ",", &saveptr)); init = NULL)
         {
             int i;
             for (i = 0; x265::cpu_names[i].flags && strcasecmp(tok, x265::cpu_names[i].name); i++);


More information about the x265-devel mailing list