Browse Source

Import strtok_r from gnulib for Windows portability

Luke Dashjr 13 years ago
parent
commit
fbefafb0fb
4 changed files with 93 additions and 2 deletions
  1. 10 1
      lib/Makefile.am
  2. 76 0
      lib/strtok_r.c
  3. 2 1
      m4/gnulib-cache.m4
  4. 5 0
      m4/gnulib-comp.m4

+ 10 - 1
lib/Makefile.am

@@ -9,7 +9,7 @@
 # the same distribution terms as the rest of that program.
 # the same distribution terms as the rest of that program.
 #
 #
 # Generated by gnulib-tool.
 # Generated by gnulib-tool.
-# Reproduce by: gnulib-tool --import --dir=. --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=. --no-conditional-dependencies --no-libtool --macro-prefix=gl --no-vc-files memmem sigaction signal
+# Reproduce by: gnulib-tool --import --dir=. --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=. --no-conditional-dependencies --no-libtool --macro-prefix=gl --no-vc-files memmem sigaction signal strtok_r
 
 
 AUTOMAKE_OPTIONS = 1.5 gnits
 AUTOMAKE_OPTIONS = 1.5 gnits
 
 
@@ -332,6 +332,15 @@ EXTRA_DIST += string.in.h
 
 
 ## end   gnulib module string
 ## end   gnulib module string
 
 
+## begin gnulib module strtok_r
+
+
+EXTRA_DIST += strtok_r.c
+
+EXTRA_libgnu_a_SOURCES += strtok_r.c
+
+## end   gnulib module strtok_r
+
 ## begin gnulib module warn-on-use
 ## begin gnulib module warn-on-use
 
 
 BUILT_SOURCES += warn-on-use.h
 BUILT_SOURCES += warn-on-use.h

+ 76 - 0
lib/strtok_r.c

@@ -0,0 +1,76 @@
+/* Reentrant string tokenizer.  Generic version.
+   Copyright (C) 1991, 1996-1999, 2001, 2004, 2007, 2009-2011 Free Software
+   Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <string.h>
+
+#ifdef _LIBC
+# undef strtok_r
+# undef __strtok_r
+#else
+# define __strtok_r strtok_r
+# define __rawmemchr strchr
+#endif
+
+/* Parse S into tokens separated by characters in DELIM.
+   If S is NULL, the saved pointer in SAVE_PTR is used as
+   the next starting point.  For example:
+        char s[] = "-abc-=-def";
+        char *sp;
+        x = strtok_r(s, "-", &sp);      // x = "abc", sp = "=-def"
+        x = strtok_r(NULL, "-=", &sp);  // x = "def", sp = NULL
+        x = strtok_r(NULL, "=", &sp);   // x = NULL
+                // s = "abc\0-def\0"
+*/
+char *
+__strtok_r (char *s, const char *delim, char **save_ptr)
+{
+  char *token;
+
+  if (s == NULL)
+    s = *save_ptr;
+
+  /* Scan leading delimiters.  */
+  s += strspn (s, delim);
+  if (*s == '\0')
+    {
+      *save_ptr = s;
+      return NULL;
+    }
+
+  /* Find the end of the token.  */
+  token = s;
+  s = strpbrk (token, delim);
+  if (s == NULL)
+    /* This token finishes the string.  */
+    *save_ptr = __rawmemchr (token, '\0');
+  else
+    {
+      /* Terminate the token and make *SAVE_PTR point past it.  */
+      *s = '\0';
+      *save_ptr = s + 1;
+    }
+  return token;
+}
+#ifdef weak_alias
+libc_hidden_def (__strtok_r)
+weak_alias (__strtok_r, strtok_r)
+#endif

+ 2 - 1
m4/gnulib-cache.m4

@@ -15,7 +15,7 @@
 
 
 
 
 # Specification in the form of a command-line invocation:
 # Specification in the form of a command-line invocation:
-#   gnulib-tool --import --dir=. --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=. --no-conditional-dependencies --no-libtool --macro-prefix=gl --no-vc-files memmem sigaction signal
+#   gnulib-tool --import --dir=. --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=. --no-conditional-dependencies --no-libtool --macro-prefix=gl --no-vc-files memmem sigaction signal strtok_r
 
 
 # Specification in the form of a few gnulib-tool.m4 macro invocations:
 # Specification in the form of a few gnulib-tool.m4 macro invocations:
 gl_LOCAL_DIR([])
 gl_LOCAL_DIR([])
@@ -23,6 +23,7 @@ gl_MODULES([
   memmem
   memmem
   sigaction
   sigaction
   signal
   signal
+  strtok_r
 ])
 ])
 gl_AVOID([])
 gl_AVOID([])
 gl_SOURCE_BASE([lib])
 gl_SOURCE_BASE([lib])

+ 5 - 0
m4/gnulib-comp.m4

@@ -41,6 +41,7 @@ AC_DEFUN([gl_EARLY],
   # Code from module stddef:
   # Code from module stddef:
   # Code from module stdint:
   # Code from module stdint:
   # Code from module string:
   # Code from module string:
+  # Code from module strtok_r:
   # Code from module warn-on-use:
   # Code from module warn-on-use:
 ])
 ])
 
 
@@ -92,6 +93,8 @@ gl_SIGNAL_MODULE_INDICATOR([sigprocmask])
 gl_STDDEF_H
 gl_STDDEF_H
 gl_STDINT_H
 gl_STDINT_H
 gl_HEADER_STRING_H
 gl_HEADER_STRING_H
+gl_FUNC_STRTOK_R
+gl_STRING_MODULE_INDICATOR([strtok_r])
   # End of code from modules
   # End of code from modules
   m4_ifval(gl_LIBSOURCES_LIST, [
   m4_ifval(gl_LIBSOURCES_LIST, [
     m4_syscmd([test ! -d ]m4_defn([gl_LIBSOURCES_DIR])[ ||
     m4_syscmd([test ! -d ]m4_defn([gl_LIBSOURCES_DIR])[ ||
@@ -247,6 +250,7 @@ AC_DEFUN([gl_FILE_LIST], [
   lib/stdint.in.h
   lib/stdint.in.h
   lib/str-two-way.h
   lib/str-two-way.h
   lib/string.in.h
   lib/string.in.h
+  lib/strtok_r.c
   m4/00gnulib.m4
   m4/00gnulib.m4
   m4/extensions.m4
   m4/extensions.m4
   m4/gnulib-common.m4
   m4/gnulib-common.m4
@@ -263,6 +267,7 @@ AC_DEFUN([gl_FILE_LIST], [
   m4/stddef_h.m4
   m4/stddef_h.m4
   m4/stdint.m4
   m4/stdint.m4
   m4/string_h.m4
   m4/string_h.m4
+  m4/strtok_r.m4
   m4/warn-on-use.m4
   m4/warn-on-use.m4
   m4/wchar_t.m4
   m4/wchar_t.m4
 ])
 ])