From 3870285fabf51f862c4122c5e0f0f115dd5e33d7 Mon Sep 17 00:00:00 2001
From: Robert Izzard <r.izzard@surrey.ac.uk>
Date: Wed, 11 Dec 2019 17:23:03 +0000
Subject: [PATCH] add check for C library's hash functions : now if they are
 not available we don't try to use them

---
 meson.build                                  | 22 ++++++-
 src/nucsyn/nucsyn_element_to_atomic_number.c | 60 +++++++++++---------
 2 files changed, 51 insertions(+), 31 deletions(-)

diff --git a/meson.build b/meson.build
index 086c31188..fcd71a471 100644
--- a/meson.build
+++ b/meson.build
@@ -538,7 +538,7 @@ _i = ' '.join(my_incdirs)
 # features which are converted into preprocessor flags (-D)
 #
 
-############
+#############
 # ieee754.h #
 #
 if compiler.has_header('ieee754.h',
@@ -569,6 +569,24 @@ else
     message('drand48 disabled')
 endif
 
+##################
+# hash functions #
+#
+if compiler.has_header('search.h',
+                       args: cflags,
+                       include_directories: include_directories(incdirs)\
+                      ) and \
+   compiler.sizeof('struct hsearch_data',
+                   prefix : '''
+#define __USE_GNU
+#include <search.h>
+''') > 0
+    cflags += '-D__HAVE_HSEARCH_DATA__'
+    message('hsearch_data is available')
+else
+    message('hsearch_data is not available')
+endif
+
 ############
 # malloc.h #
 #
@@ -578,8 +596,6 @@ if compiler.has_header('malloc.h',
     cflags += '-D__HAVE_MALLOC_H__'
 endif
 
-
-
 ############ 
 # setitimer 
 #
diff --git a/src/nucsyn/nucsyn_element_to_atomic_number.c b/src/nucsyn/nucsyn_element_to_atomic_number.c
index a9e6aac8a..dd091b0a0 100644
--- a/src/nucsyn/nucsyn_element_to_atomic_number.c
+++ b/src/nucsyn/nucsyn_element_to_atomic_number.c
@@ -8,7 +8,9 @@
  */
 #ifdef NUCSYN
 
+#ifdef __HAVE_HSEARCH_DATA__
 #define USEHASH
+#endif // __HAVE_HSEARCH_DATA__
 
 /* hash debugging */
 //#define HDEBUG
@@ -60,7 +62,7 @@ Atomic_number nucsyn_element_to_atomic_number(struct store_t * RESTRICT const st
 #ifdef HDEBUG
                 printf("Set element info Z=%d from %d\n",
                        store->element_info[Z].Z,Z);
-#endif
+#endif//HDEBUG
 
                 /* set the hash item */
                 ENTRY item;
@@ -82,7 +84,7 @@ Atomic_number nucsyn_element_to_atomic_number(struct store_t * RESTRICT const st
                 printf("found %p with Z=%d\n",found_item,
                        ((struct element_info_t*)(found_item->data))->Z
                     );
-#endif
+#endif//HDEBUG
             }
 
         }
@@ -103,43 +105,45 @@ Atomic_number nucsyn_element_to_atomic_number(struct store_t * RESTRICT const st
         {
             Atomic_number Z = ((struct element_info_t*)(found_item->data))->Z;
 #ifdef HDEBUG
-            printf("element %s gave Z=%d\n",element,Z;
+            printf("element %s gave Z=%d\n",element,Z);
 #endif
-                   return Z;
-                   }
+            return Z;
         }
-#else
+    }
 
+#else
+    /*
+     * Non-hashed version
+     */
 
-        if(element!=NULL)
+    if(element!=NULL)
+    {
+        /*
+         * Simple array search.
+         */
+        Atomic_number Z = -1,i;
+        for(i=0;i<NUMBER_OF_ELEMENTS;i++)
         {
-            /*
-             * Simple array search.
-             */
-            Atomic_number Z = -1,i;
-            for(i=0;i<NUMBER_OF_ELEMENTS;i++)
+            if(strncmp(element_strings[i],element,3)==0)
             {
-                if(strncmp(element_strings[i],element,3)==0)
-                {
-                    Z = i;
-                    break;
-                }
-            }
-
-            if(Z==-1)
-            {
-                Exit_binary_c_no_stardata(BINARY_C_OUT_OF_RANGE,
-                                          "Element %s failed to match any element in nature... please check!\n",
-                                          element);
-            }
-            else
-            {
-                return Z;
+                Z = i;
+                break;
             }
         }
 
+        if(Z==-1)
+        {
+            Exit_binary_c_no_stardata(BINARY_C_OUT_OF_RANGE,
+                                      "Element %s failed to match any element in nature... please check!\n",
+                                      element);
+        }
+        else
+        {
+            return Z;
+        }
     }
 #endif //USEHASH
+    
     return 0;
 }
 
-- 
GitLab