xref: /openbsd-src/lib/libcurses/base/tries.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: tries.c,v 1.4 2001/01/22 18:01:48 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998,2000 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.14 2000/12/10 02:43:28 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 NCURSES_EXPORT(char *)
51 _nc_expand_try
52 (struct tries *tree, unsigned short code, int *count, size_t len)
53 {
54     struct tries *ptr = tree;
55     char *result = 0;
56 
57     if (code != 0) {
58 	while (ptr != 0) {
59 	    if ((result = _nc_expand_try(ptr->child, code, count, len + 1))
60 		!= 0) {
61 		break;
62 	    }
63 	    if (ptr->value == code) {
64 		*count -= 1;
65 		if (*count == -1) {
66 		    result = typeCalloc(char, len + 2);
67 		    break;
68 		}
69 	    }
70 	    ptr = ptr->sibling;
71 	}
72     }
73     if (result != 0) {
74 	if ((result[len] = ptr->ch) == 0)
75 	    *((unsigned char *) (result + len)) = 128;
76 #ifdef TRACE
77 	if (len == 0)
78 	    _tracef("expand_key %s %s", _trace_key(code), _nc_visbuf(result));
79 #endif
80     }
81     return result;
82 }
83 
84 /*
85  * Remove a code from the specified tree, freeing the unused nodes.  Returns
86  * true if the code was found/removed.
87  */
88 NCURSES_EXPORT(int)
89 _nc_remove_key
90 (struct tries **tree, unsigned short code)
91 {
92     T((T_CALLED("_nc_remove_key(%p,%d)"), tree, code));
93 
94     if (code == 0)
95 	returnCode(FALSE);
96 
97     while (*tree != 0) {
98 	if (_nc_remove_key(&(*tree)->child, code)) {
99 	    returnCode(TRUE);
100 	}
101 	if ((*tree)->value == code) {
102 	    if ((*tree)->child) {
103 		/* don't cut the whole sub-tree */
104 		(*tree)->value = 0;
105 	    } else {
106 		struct tries *to_free = *tree;
107 		*tree = (*tree)->sibling;
108 		free(to_free);
109 	    }
110 	    returnCode(TRUE);
111 	}
112 	tree = &(*tree)->sibling;
113     }
114     returnCode(FALSE);
115 }
116 
117 /*
118  * Remove a string from the specified tree, freeing the unused nodes.  Returns
119  * true if the string was found/removed.
120  */
121 NCURSES_EXPORT(int)
122 _nc_remove_string(struct tries **tree, char *string)
123 {
124     T((T_CALLED("_nc_remove_string(%p,%s)"), tree, _nc_visbuf(string)));
125 
126     if (string == 0 || *string == 0)
127 	returnCode(FALSE);
128 
129     while (*tree != 0) {
130 	if ((unsigned char) (*tree)->ch == (unsigned char) *string) {
131 	    if (string[1] != 0)
132 		returnCode(_nc_remove_string(&(*tree)->child, string + 1));
133 	    if ((*tree)->child) {
134 		/* don't cut the whole sub-tree */
135 		(*tree)->value = 0;
136 	    } else {
137 		struct tries *to_free = *tree;
138 		*tree = (*tree)->sibling;
139 		free(to_free);
140 	    }
141 	    returnCode(TRUE);
142 	}
143 	tree = &(*tree)->sibling;
144     }
145     returnCode(FALSE);
146 }
147