xref: /openbsd-src/lib/libcurses/base/tries.c (revision 92dd1ec0a89df25171bc5d61a3d95ea1a68cef0b)
1 /*	$OpenBSD: tries.c,v 1.1 1999/01/18 19:10:07 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998 Free Software Foundation, Inc.                        *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Thomas E. Dickey <dickey@clark.net> 1997                        *
33  ****************************************************************************/
34 
35 /*
36 **	tries.c
37 **
38 **	Functions to manage the tree of partial-completions for keycodes.
39 **
40 */
41 
42 #include <curses.priv.h>
43 
44 MODULE_ID("$From: tries.c,v 1.8 1998/11/07 22:54:48 tom Exp $")
45 
46 /*
47  * Expand a keycode into the string that it corresponds to, returning null if
48  * no match was found, otherwise allocating a string of the result.
49  */
50 char *_nc_expand_try(struct tries *tree, unsigned short code, size_t len)
51 {
52 	struct tries *ptr = tree;
53 	char *result = 0;
54 
55 	if (code != 0) {
56 		while (ptr != 0) {
57 			if ((result = _nc_expand_try(ptr->child, code, len + 1)) != 0) {
58 				break;
59 			}
60 			if (ptr->value == code) {
61 				result = typeCalloc(char, len+2);
62 				break;
63 			}
64 			ptr = ptr->sibling;
65 		}
66 	}
67 	if (result != 0) {
68 		if ((result[len] = ptr->ch) == 0)
69 			*((unsigned char *)(result+len)) = 128;
70 #ifdef TRACE
71 		if (len == 0)
72 			_tracef("expand_key %s %s", _trace_key(code), _nc_visbuf(result));
73 #endif
74 	}
75 	return result;
76 }
77 
78 /*
79  * Remove a code from the specified tree, freeing the unused nodes.  Returns
80  * true if the code was found/removed.
81  */
82 int _nc_remove_key(struct tries **tree, unsigned short code)
83 {
84 	if (code == 0)
85 		return FALSE;
86 
87 	while (*tree != 0) {
88 		if (_nc_remove_key(&(*tree)->child, code)) {
89 			return TRUE;
90 		}
91 		if ((*tree)->value == code) {
92 			if((*tree)->child) {
93 				/* don't cut the whole sub-tree */
94 				(*tree)->value = 0;
95 			} else {
96 				struct tries *to_free = *tree;
97 				*tree = (*tree)->sibling;
98 				free(to_free);
99 			}
100 			return TRUE;
101 		}
102 		tree = &(*tree)->sibling;
103 	}
104 	return FALSE;
105 }
106