]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/tinfo/comp_hash.c
ncurses 5.7 - patch 20090718
[ncurses.git] / ncurses / tinfo / comp_hash.c
index abbdeedb6433be3073254d97eb69d32396c3e5e4..e120e3a7413fc0c30d13b087d8b7e676809a02ab 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2005,2007 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2008,2009 Free Software Foundation, Inc.              *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -50,7 +50,7 @@
 #define DEBUG(level, params)   /*nothing */
 #endif
 
-MODULE_ID("$Id: comp_hash.c,v 1.32 2007/08/12 00:40:27 tom Exp $")
+MODULE_ID("$Id: comp_hash.c,v 1.45 2009/07/18 20:30:21 tom Exp $")
 
 static int hash_function(const char *);
 
@@ -73,7 +73,7 @@ static void
 _nc_make_hash_table(struct name_table_entry *table,
                    short *hash_table)
 {
-    int i;
+    short i;
     int hashvalue;
     int collisions = 0;
 
@@ -91,8 +91,7 @@ _nc_make_hash_table(struct name_table_entry *table,
        hash_table[hashvalue] = i;
     }
 
-    DEBUG(4, ("Hash table complete: %d collisions out of %d entries",
-             collisions, CAPTABSIZE));
+    printf("/* %d collisions out of %d entries */\n", collisions, CAPTABSIZE);
 }
 #endif
 
@@ -121,16 +120,24 @@ hash_function(const char *string)
     return (int) (sum % HASHTABSIZE);
 }
 
+#ifndef MAIN_PROGRAM
+
+#define SameName(a,b,termcap) (termcap ? !strncmp(a,b,2) : !strcmp(a,b))
+#if 0
+static bool
+same_name(const char *a, const char *b, bool termcap)
+{
+    fprintf(stderr, "compare(%s,%s)\n", a, b);
+    return SameName(a, b, termcap);
+}
+#else
+#define same_name(a,b,termcap) SameName(a,b,termcap)
+#endif
+
 /*
- *     struct name_table_entry *
- *     find_entry(string)
- *
- *     Finds the entry for the given string in the hash table if present.
- *     Returns a pointer to the entry in the table or 0 if not found.
- *
+ * Finds the entry for the given string in the hash table if present.
+ * Returns a pointer to the entry in the table or 0 if not found.
  */
-
-#ifndef MAIN_PROGRAM
 NCURSES_EXPORT(struct name_table_entry const *)
 _nc_find_entry(const char *string,
               const short *hash_table)
@@ -142,11 +149,15 @@ _nc_find_entry(const char *string,
     hashvalue = hash_function(string);
 
     if (hash_table[hashvalue] >= 0) {
-       real_table = _nc_get_table(hash_table != _nc_get_hash_table(FALSE));
+       bool termcap = (hash_table != _nc_get_hash_table(FALSE));
+
+       real_table = _nc_get_table(termcap);
        ptr = real_table + hash_table[hashvalue];
-       while (strcmp(ptr->nte_name, string) != 0) {
-           if (ptr->nte_link < 0)
-               return 0;
+       while (!same_name(ptr->nte_name, string, termcap)) {
+           if (ptr->nte_link < 0) {
+               ptr = 0;
+               break;
+           }
            ptr = real_table + (ptr->nte_link + hash_table[HASHTABSIZE]);
        }
     }
@@ -155,29 +166,36 @@ _nc_find_entry(const char *string,
 }
 
 /*
- *     struct name_table_entry *
- *     find_type_entry(string, type, table)
+ * Finds the entry for the given name with the given type in the given table if
+ * present (as distinct from _nc_find_entry, which finds the last entry
+ * regardless of type).
  *
- *     Finds the first entry for the given name with the given type in the
- *     given table if present (as distinct from find_entry, which finds the
- *     the last entry regardless of type).  You can use this if you detect
- *     a name clash.  It's slower, though.  Returns a pointer to the entry
- *     in the table or 0 if not found.
+ * Returns a pointer to the entry in the table or 0 if not found.
  */
-
 NCURSES_EXPORT(struct name_table_entry const *)
 _nc_find_type_entry(const char *string,
                    int type,
-                   const struct name_table_entry *table)
+                   bool termcap)
 {
-    struct name_table_entry const *ptr;
+    struct name_table_entry const *ptr = NULL;
+    const short *hash_table = _nc_get_hash_table(termcap);
+    int hashvalue = hash_function(string);
+
+    if (hash_table[hashvalue] >= 0) {
+       const struct name_table_entry *const table = _nc_get_table(termcap);
 
-    for (ptr = table; ptr < table + CAPTABSIZE; ptr++) {
-       if (ptr->nte_type == type && strcmp(string, ptr->nte_name) == 0)
-           return (ptr);
+       ptr = table + hash_table[hashvalue];
+       while (ptr->nte_type != type
+              || !same_name(ptr->nte_name, string, termcap)) {
+           if (ptr->nte_link < 0) {
+               ptr = 0;
+               break;
+           }
+           ptr = table + (ptr->nte_link + hash_table[HASHTABSIZE]);
+       }
     }
 
-    return ((struct name_table_entry *) NULL);
+    return ptr;
 }
 #endif
 
@@ -215,8 +233,9 @@ parse_columns(char *buffer)
                if ((s - buffer) > 1
                    && (*buffer == '"')
                    && (s[-1] == '"')) {        /* strip the quotes */
-                   buffer++;
+                   assert(s > buffer + 1);
                    s[-1] = '\0';
+                   buffer++;
                }
                list[col] = buffer;
                col++;
@@ -258,7 +277,9 @@ main(int argc, char **argv)
        || (column = atoi(argv[1])) <= 0
        || (column >= MAX_COLUMNS)
        || *(root_name = argv[2]) == 0
-       || (bigstring = atoi(argv[3])) < 0) {
+       || (bigstring = atoi(argv[3])) < 0
+       || name_table == 0
+       || hash_table == 0) {
        fprintf(stderr, "usage: make_hash column root_name bigstring\n");
        exit(EXIT_FAILURE);
     }
@@ -299,17 +320,17 @@ main(int argc, char **argv)
        int len = 0;
        int nxt;
 
-       printf("static const char %s_names_text[] = \"\\\n", root_name);
+       printf("static const char %s_names_text[] = \\\n", root_name);
        for (n = 0; n < CAPTABSIZE; n++) {
-           nxt = strlen(name_table[n].nte_name) + 2;
+           nxt = (int) strlen(name_table[n].nte_name) + 5;
            if (nxt + len > 72) {
                printf("\\\n");
                len = 0;
            }
-           printf("%s\\0", name_table[n].nte_name);
+           printf("\"%s\\0\" ", name_table[n].nte_name);
            len += nxt;
        }
-       printf("\";\n\n");
+       printf(";\n\n");
 
        len = 0;
        printf("static name_table_data const %s_names_data[] =\n",
@@ -322,7 +343,7 @@ main(int argc, char **argv)
                   name_table[n].nte_index,
                   name_table[n].nte_link,
                   n < CAPTABSIZE - 1 ? ',' : ' ');
-           len += strlen(name_table[n].nte_name) + 1;
+           len += (int) strlen(name_table[n].nte_name) + 1;
        }
        printf("};\n\n");
        printf("static struct name_table_entry *_nc_%s_table = 0;\n\n", root_name);
@@ -361,6 +382,7 @@ main(int argc, char **argv)
     printf("#error\t--> numbers of booleans, numbers and/or strings <--\n");
     printf("#endif\n\n");
 
+    free(hash_table);
     return EXIT_SUCCESS;
 }
 #endif