changeset 8532:11f6a87d953a

New module 'search'.
author Bruno Haible <bruno@clisp.org>
date Sun, 25 Mar 2007 02:45:24 +0000
parents 52719799a90e
children 65314e9016cf
files ChangeLog NEWS lib/search_.h lib/tsearch.c lib/tsearch.h m4/search_h.m4 m4/tsearch.m4 modules/search modules/tsearch
diffstat 9 files changed, 217 insertions(+), 89 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2007-03-24  Bruno Haible  <bruno@clisp.org>
+
+	* modules/search: New file.
+	* lib/search_.h: New file, incorporating lib/tsearch.h.
+	* m4/search_h.m4: New file.
+	* lib/tsearch.h: Remove file.
+	* lib/tsearch.c: Include search.h instead of tsearch.h.
+	* m4/tsearch.m4 (gl_FUNC_TSEARCH): Require gl_SEARCH_H_DEFAULTS. Set
+	HAVE_TSEARCH.
+	* modules/tsearch (Files): Remove lib/tsearch.h.
+	(Depends-on): Add search.
+	(configure.ac): Invoke gl_SEARCH_MODULE_INDICATOR.
+	(Include): Change tsearch.h into search.h.
+
 2007-03-24  Bruno Haible  <bruno@clisp.org>
 
 	* modules/fpucw: New file.
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,9 @@
 
 Date        Modules         Changes
 
+2007-03-24  tsearch         The include file is changed from "tsearch.h" to
+                            <search.h>.
+
 2007-03-24  utf8-ucs4       The include file is changed from "utf8-ucs4.h"
                             to "unistr.h".
             utf8-ucs4-unsafe  The include file is changed from
new file mode 100644
--- /dev/null
+++ b/lib/search_.h
@@ -0,0 +1,111 @@
+/* A GNU-like <search.h>.
+
+   Copyright (C) 2007 Free Software Foundation, Inc.
+
+   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 2, 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, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+#ifndef _GL_SEARCH_H
+#define _GL_SEARCH_H
+
+#if @HAVE_SEARCH_H@
+# include @ABSOLUTE_SEARCH_H@
+#endif
+
+
+/* The definition of GL_LINK_WARNING is copied here.  */
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+#if @GNULIB_TSEARCH@
+# if !@HAVE_TSEARCH@
+
+/* See <http://www.opengroup.org/susv3xbd/search.h.html>,
+       <http://www.opengroup.org/susv3xsh/tsearch.html>
+   for details.  */
+
+typedef enum
+{ 
+  preorder,
+  postorder, 
+  endorder,
+  leaf
+}
+VISIT;
+
+/* Searches an element in the tree *VROOTP that compares equal to KEY.
+   If one is found, it is returned.  Otherwise, a new element equal to KEY
+   is inserted in the tree and is returned.  */
+extern void * tsearch (const void *key, void **vrootp,
+		       int (*compar) (const void *, const void *));
+
+/* Searches an element in the tree *VROOTP that compares equal to KEY.
+   If one is found, it is returned.  Otherwise, NULL is returned.  */
+extern void * tfind (const void *key, void *const *vrootp,
+		     int (*compar) (const void *, const void *));
+
+/* Searches an element in the tree *VROOTP that compares equal to KEY.
+   If one is found, it is removed from the tree, and its parent node is
+   returned.  Otherwise, NULL is returned.  */
+extern void * tdelete (const void *key, void **vrootp,
+		       int (*compar) (const void *, const void *));
+
+/* Perform a depth-first, left-to-right traversal of the tree VROOT.
+   The ACTION function is called:
+     - for non-leaf nodes: 3 times, before the left subtree traversal,
+       after the left subtree traversal but before the right subtree traversal,
+       and after the right subtree traversal,
+     - for leaf nodes: once.
+   The arguments passed to ACTION are:
+     1. the node; it can be casted to a 'const void * const *', i.e. into a
+        pointer to the key,
+     2. an indicator which visit of the node this is,
+     3. the level of the node in the tree (0 for the root).  */
+extern void twalk (const void *vroot,
+		   void (*action) (const void *, VISIT, int));
+
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef tsearch
+# define tsearch(k,v,c) \
+    (GL_LINK_WARNING ("tsearch is unportable - " \
+                      "use gnulib module tsearch for portability"), \
+     tsearch (k, v, c))
+# undef tfind
+# define tfind(k,v,c) \
+    (GL_LINK_WARNING ("tfind is unportable - " \
+                      "use gnulib module tsearch for portability"), \
+     tfind (k, v, c))
+# undef tdelete
+# define tdelete(k,v,c) \
+    (GL_LINK_WARNING ("tdelete is unportable - " \
+                      "use gnulib module tsearch for portability"), \
+     tdelete (k, v, c))
+# undef twalk
+# define twalk(v,a) \
+    (GL_LINK_WARNING ("twalk is unportable - " \
+                      "use gnulib module tsearch for portability"), \
+     twalk (v, a))
+#endif
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- a/lib/tsearch.c
+++ b/lib/tsearch.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1995, 1996, 1997, 2000, 2006 Free Software Foundation, Inc.
+/* Copyright (C) 1995-1997, 2000, 2006-2007 Free Software Foundation, Inc.
    Contributed by Bernd Schmidt <crux@Pool.Informatik.RWTH-Aachen.DE>, 1997.
 
    NOTE: The canonical source of this file is maintained with the GNU C
@@ -88,7 +88,11 @@
 #include <config.h>
 
 /* Specification.  */
-#include "tsearch.h"
+#ifdef IN_LIBINTL
+# include "tsearch.h"
+#else
+# include <search.h>
+#endif
 
 #include <stdlib.h>
 
deleted file mode 100644
--- a/lib/tsearch.h
+++ /dev/null
@@ -1,82 +0,0 @@
-/* Binary tree data structure.
-   Copyright (C) 2006 Free Software Foundation, Inc.
-
-   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 2, 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, write to the Free Software Foundation,
-   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
-
-#ifndef _TSEARCH_H
-#define _TSEARCH_H
-
-#if HAVE_TSEARCH
-
-/* Get tseach(), tfind(), tdelete(), twalk() declarations.  */
-#include <search.h>
-
-#else
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* See <http://www.opengroup.org/susv3xbd/search.h.html>,
-       <http://www.opengroup.org/susv3xsh/tsearch.html>
-   for details.  */
-
-typedef enum
-{ 
-  preorder,
-  postorder, 
-  endorder,
-  leaf
-}
-VISIT;
-
-/* Searches an element in the tree *VROOTP that compares equal to KEY.
-   If one is found, it is returned.  Otherwise, a new element equal to KEY
-   is inserted in the tree and is returned.  */
-extern void * tsearch (const void *key, void **vrootp,
-		       int (*compar) (const void *, const void *));
-
-/* Searches an element in the tree *VROOTP that compares equal to KEY.
-   If one is found, it is returned.  Otherwise, NULL is returned.  */
-extern void * tfind (const void *key, void *const *vrootp,
-		     int (*compar) (const void *, const void *));
-
-/* Searches an element in the tree *VROOTP that compares equal to KEY.
-   If one is found, it is removed from the tree, and its parent node is
-   returned.  Otherwise, NULL is returned.  */
-extern void * tdelete (const void *key, void **vrootp,
-		       int (*compar) (const void *, const void *));
-
-/* Perform a depth-first, left-to-right traversal of the tree VROOT.
-   The ACTION function is called:
-     - for non-leaf nodes: 3 times, before the left subtree traversal,
-       after the left subtree traversal but before the right subtree traversal,
-       and after the right subtree traversal,
-     - for leaf nodes: once.
-   The arguments passed to ACTION are:
-     1. the node; it can be casted to a 'const void * const *', i.e. into a
-        pointer to the key,
-     2. an indicator which visit of the node this is,
-     3. the level of the node in the tree (0 for the root).  */
-extern void twalk (const void *vroot,
-		   void (*action) (const void *, VISIT, int));
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
-
-#endif /* _TSEARCH_H */
new file mode 100644
--- /dev/null
+++ b/m4/search_h.m4
@@ -0,0 +1,34 @@
+# search_h.m4 serial 1
+dnl Copyright (C) 2007 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_SEARCH_H],
+[
+  AC_REQUIRE([gl_SEARCH_H_DEFAULTS])
+  AC_CHECK_HEADERS_ONCE([search.h])
+  if test $ac_cv_header_search_h = yes; then
+    HAVE_SEARCH_H=1
+    gl_ABSOLUTE_HEADER([search.h])
+    ABSOLUTE_SEARCH_H=\"$gl_cv_absolute_search_h\"
+  else
+    HAVE_SEARCH_H=0
+  fi
+  AC_SUBST([HAVE_SEARCH_H])
+  AC_SUBST([ABSOLUTE_SEARCH_H])
+])
+
+AC_DEFUN([gl_SEARCH_MODULE_INDICATOR],
+[
+  dnl Use AC_REQUIRE here, so that the default settings are expanded once only.
+  AC_REQUIRE([gl_SEARCH_H_DEFAULTS])
+  GNULIB_[]m4_translit([$1],[abcdefghijklmnopqrstuvwxyz./-],[ABCDEFGHIJKLMNOPQRSTUVWXYZ___])=1
+])
+
+AC_DEFUN([gl_SEARCH_H_DEFAULTS],
+[
+  GNULIB_TSEARCH=0; AC_SUBST([GNULIB_TSEARCH])
+  dnl Assume proper GNU behavior unless another module says otherwise.
+  HAVE_TSEARCH=1; AC_SUBST([HAVE_TSEARCH])
+])
--- a/m4/tsearch.m4
+++ b/m4/tsearch.m4
@@ -1,13 +1,16 @@
-# tsearch.m4 serial 1
-dnl Copyright (C) 2006 Free Software Foundation, Inc.
+# tsearch.m4 serial 2
+dnl Copyright (C) 2006-2007 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
 dnl with or without modifications, as long as this notice is preserved.
 
 AC_DEFUN([gl_FUNC_TSEARCH],
 [
-  AC_REPLACE_FUNCS(tsearch)
+  AC_REQUIRE([gl_SEARCH_H_DEFAULTS])
+  AC_CHECK_FUNCS([tsearch])
   if test $ac_cv_func_tsearch = no; then
+    HAVE_TSEARCH=0
+    AC_LIBOBJ([tsearch])
     gl_PREREQ_TSEARCH
   fi
 ])
new file mode 100644
--- /dev/null
+++ b/modules/search
@@ -0,0 +1,40 @@
+Description:
+A GNU-like <search.h>.
+
+Files:
+lib/search_.h
+m4/search_h.m4
+
+Depends-on:
+absolute-header
+link-warning
+
+configure.ac:
+gl_SEARCH_H
+
+Makefile.am:
+BUILT_SOURCES += search.h
+
+# We need the following in order to create <search.h> when the system
+# doesn't have one that works with the given compiler.
+search.h: search_.h
+	rm -f $@-t $@
+	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
+	  sed -e 's|@''HAVE_SEARCH_H''@|$(HAVE_SEARCH_H)|g' \
+	      -e 's|@''ABSOLUTE_SEARCH_H''@|$(ABSOLUTE_SEARCH_H)|g' \
+	      -e 's|@''GNULIB_TSEARCH''@|$(GNULIB_TSEARCH)|g' \
+	      -e 's|@''HAVE_TSEARCH''@|$(HAVE_TSEARCH)|g' \
+	      -e '/definition of GL_LINK_WARNING/r $(LINK_WARNING_H)' \
+	      < $(srcdir)/search_.h; \
+	} > $@-t
+	mv $@-t $@
+MOSTLYCLEANFILES += search.h search.h-t
+
+Include:
+#include <search.h>
+
+License:
+LGPL
+
+Maintainer:
+all
--- a/modules/tsearch
+++ b/modules/tsearch
@@ -2,19 +2,20 @@
 Binary tree data structure.
 
 Files:
-lib/tsearch.h
 lib/tsearch.c
 m4/tsearch.m4
 
 Depends-on:
+search
 
 configure.ac:
 gl_FUNC_TSEARCH
+gl_SEARCH_MODULE_INDICATOR([tsearch])
 
 Makefile.am:
 
 Include:
-"tsearch.h"
+<search.h>
 
 License:
 LGPL