kbproto unentanglement: action types
[platform/upstream/libxkbcommon.git] / src / atom.c
1 /***********************************************************
2  * Copyright 1987, 1998  The Open Group
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation.
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
16  * OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
17  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  *
20  * Except as contained in this notice, the name of The Open Group shall not be
21  * used in advertising or otherwise to promote the sale, use or other dealings
22  * in this Software without prior written authorization from The Open Group.
23  *
24  *
25  * Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
26  *
27  *                      All Rights Reserved
28  *
29  * Permission to use, copy, modify, and distribute this software and its
30  * documentation for any purpose and without fee is hereby granted,
31  * provided that the above copyright notice appear in all copies and that
32  * both that copyright notice and this permission notice appear in
33  * supporting documentation, and that the name of Digital not be
34  * used in advertising or publicity pertaining to distribution of the
35  * software without specific, written prior permission.
36  *
37  * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39  * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
43  * SOFTWARE.
44  *
45  ******************************************************************/
46
47 /************************************************************
48  * Copyright 1994 by Silicon Graphics Computer Systems, Inc.
49  *
50  * Permission to use, copy, modify, and distribute this
51  * software and its documentation for any purpose and without
52  * fee is hereby granted, provided that the above copyright
53  * notice appear in all copies and that both that copyright
54  * notice and this permission notice appear in supporting
55  * documentation, and that the name of Silicon Graphics not be
56  * used in advertising or publicity pertaining to distribution
57  * of the software without specific prior written permission.
58  * Silicon Graphics makes no representation about the suitability
59  * of this software for any purpose. It is provided "as is"
60  * without any express or implied warranty.
61  *
62  * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
63  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
64  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
65  * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
66  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
67  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
68  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
69  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
70  *
71  ********************************************************/
72
73 #include "atom.h"
74
75 struct atom_node {
76     struct atom_node *left, *right;
77     xkb_atom_t atom;
78     unsigned int fingerprint;
79     char *string;
80 };
81
82 struct atom_table {
83     struct atom_node *root;
84     darray(struct atom_node *) table;
85 };
86
87 struct atom_table *
88 atom_table_new(void)
89 {
90     struct atom_table *table;
91
92     table = calloc(1, sizeof(*table));
93     if (!table)
94         return NULL;
95
96     darray_init(table->table);
97     darray_growalloc(table->table, 100);
98     darray_append(table->table, NULL);
99
100     return table;
101 }
102
103 static void
104 free_atom(struct atom_node *patom)
105 {
106     if (!patom)
107         return;
108
109     free_atom(patom->left);
110     free_atom(patom->right);
111     free(patom->string);
112     free(patom);
113 }
114
115 void
116 atom_table_free(struct atom_table *table)
117 {
118     if (!table)
119         return;
120
121     free_atom(table->root);
122     darray_free(table->table);
123     free(table);
124 }
125
126 const char *
127 atom_text(struct atom_table *table, xkb_atom_t atom)
128 {
129     if (atom >= darray_size(table->table) ||
130         darray_item(table->table, atom) == NULL)
131         return NULL;
132
133     return darray_item(table->table, atom)->string;
134 }
135
136 char *
137 atom_strdup(struct atom_table *table, xkb_atom_t atom)
138 {
139     return strdup_safe(atom_text(table, atom));
140 }
141
142 static bool
143 find_node_pointer(struct atom_table *table, const char *string,
144                   struct atom_node ***np_out, unsigned int *fingerprint_out)
145 {
146     struct atom_node **np;
147     unsigned i;
148     int comp;
149     unsigned int fp = 0;
150     size_t len;
151     bool found = false;
152
153     len = strlen(string);
154     np = &table->root;
155     for (i = 0; i < (len + 1) / 2; i++) {
156         fp = fp * 27 + string[i];
157         fp = fp * 27 + string[len - 1 - i];
158     }
159
160     while (*np) {
161         if (fp < (*np)->fingerprint) {
162             np = &((*np)->left);
163         }
164         else if (fp > (*np)->fingerprint) {
165             np = &((*np)->right);
166         }
167         else {
168             /* now start testing the strings */
169             comp = strncmp(string, (*np)->string, len);
170             if (comp < 0 || (comp == 0 && len < strlen((*np)->string))) {
171                 np = &((*np)->left);
172             }
173             else if (comp > 0) {
174                 np = &((*np)->right);
175             }
176             else {
177                 found = true;
178                 break;
179             }
180         }
181     }
182
183     *fingerprint_out = fp;
184     *np_out = np;
185     return found;
186 }
187
188 xkb_atom_t
189 atom_lookup(struct atom_table *table, const char *string)
190 {
191     struct atom_node **np;
192     unsigned int fp;
193
194     if (!string)
195         return XKB_ATOM_NONE;
196
197     if (!find_node_pointer(table, string, &np, &fp))
198         return XKB_ATOM_NONE;
199
200     return (*np)->atom;
201 }
202
203 /*
204  * If steal is true, we do not strdup @string; therefore it must be
205  * dynamically allocated, not be free'd by the caller and not be used
206  * afterwards. Use to avoid some redundant allocations.
207  */
208 xkb_atom_t
209 atom_intern(struct atom_table *table, const char *string,
210             bool steal)
211 {
212     struct atom_node **np;
213     struct atom_node *nd;
214     unsigned int fp;
215
216     if (!string)
217         return XKB_ATOM_NONE;
218
219     if (find_node_pointer(table, string, &np, &fp)) {
220         if (steal)
221             free(UNCONSTIFY(string));
222         return (*np)->atom;
223     }
224
225     nd = malloc(sizeof(*nd));
226     if (!nd)
227         return XKB_ATOM_NONE;
228
229     if (steal) {
230         nd->string = UNCONSTIFY(string);
231     }
232     else {
233         nd->string = strdup(string);
234         if (!nd->string) {
235             free(nd);
236             return XKB_ATOM_NONE;
237         }
238     }
239
240     *np = nd;
241     nd->left = nd->right = NULL;
242     nd->fingerprint = fp;
243     nd->atom = darray_size(table->table);
244     darray_append(table->table, nd);
245
246     return nd->atom;
247 }