]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/comp_hash.c
ncurses 6.1 - patch 20200111
[ncurses.git] / ncurses / tinfo / comp_hash.c
1 /****************************************************************************
2  * Copyright (c) 1998-2009,2019 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  *     and: Thomas E. Dickey                        1996-on                 *
33  ****************************************************************************/
34
35 /*
36  *      comp_hash.c --- Routines to deal with the hashtable of capability
37  *                      names.
38  *
39  */
40
41 #define USE_TERMLIB 1
42 #include <curses.priv.h>
43
44 #include <tic.h>
45 #include <hashsize.h>
46
47 MODULE_ID("$Id: comp_hash.c,v 1.51 2019/10/12 16:32:13 tom Exp $")
48
49 /*
50  * Finds the entry for the given string in the hash table if present.
51  * Returns a pointer to the entry in the table or 0 if not found.
52  */
53 /* entrypoint used by tack (do not alter) */
54 NCURSES_EXPORT(struct name_table_entry const *)
55 _nc_find_entry(const char *string,
56                const HashValue * hash_table)
57 {
58     bool termcap = (hash_table != _nc_get_hash_table(FALSE));
59     const HashData *data = _nc_get_hash_info(termcap);
60     int hashvalue;
61     struct name_table_entry const *ptr = 0;
62     struct name_table_entry const *real_table;
63
64     hashvalue = data->hash_of(string);
65
66     if (hashvalue >= 0
67         && (unsigned) hashvalue < data->table_size
68         && data->table_data[hashvalue] >= 0) {
69
70         real_table = _nc_get_table(termcap);
71         ptr = real_table + data->table_data[hashvalue];
72         while (!data->compare_names(ptr->nte_name, string)) {
73             if (ptr->nte_link < 0) {
74                 ptr = 0;
75                 break;
76             }
77             ptr = real_table + (ptr->nte_link
78                                 + data->table_data[data->table_size]);
79         }
80     }
81
82     return (ptr);
83 }
84
85 /*
86  * Finds the entry for the given name with the given type in the given table if
87  * present (as distinct from _nc_find_entry, which finds the last entry
88  * regardless of type).
89  *
90  * Returns a pointer to the entry in the table or 0 if not found.
91  */
92 NCURSES_EXPORT(struct name_table_entry const *)
93 _nc_find_type_entry(const char *string,
94                     int type,
95                     bool termcap)
96 {
97     struct name_table_entry const *ptr = NULL;
98     const HashData *data = _nc_get_hash_info(termcap);
99     int hashvalue = data->hash_of(string);
100
101     if (hashvalue >= 0
102         && (unsigned) hashvalue < data->table_size
103         && data->table_data[hashvalue] >= 0) {
104         const struct name_table_entry *const table = _nc_get_table(termcap);
105
106         ptr = table + data->table_data[hashvalue];
107         while (ptr->nte_type != type
108                || !data->compare_names(ptr->nte_name, string)) {
109             if (ptr->nte_link < 0) {
110                 ptr = 0;
111                 break;
112             }
113             ptr = table + (ptr->nte_link + data->table_data[data->table_size]);
114         }
115     }
116
117     return ptr;
118 }
119
120 #if NCURSES_XNAMES
121 NCURSES_EXPORT(struct user_table_entry const *)
122 _nc_find_user_entry(const char *string)
123 {
124     const HashData *data = _nc_get_hash_user();
125     int hashvalue;
126     struct user_table_entry const *ptr = 0;
127     struct user_table_entry const *real_table;
128
129     hashvalue = data->hash_of(string);
130
131     if (hashvalue >= 0
132         && (unsigned) hashvalue < data->table_size
133         && data->table_data[hashvalue] >= 0) {
134
135         real_table = _nc_get_userdefs_table();
136         ptr = real_table + data->table_data[hashvalue];
137         while (!data->compare_names(ptr->ute_name, string)) {
138             if (ptr->ute_link < 0) {
139                 ptr = 0;
140                 break;
141             }
142             ptr = real_table + (ptr->ute_link
143                                 + data->table_data[data->table_size]);
144         }
145     }
146
147     return (ptr);
148 }
149 #endif /* NCURSES_XNAMES */