Add benchmarking test to rulescomp
[profile/ivi/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 #define INITIAL_TABLE_SIZE 100
76
77 struct atom_node {
78     struct atom_node *left, *right;
79     uint32_t a;
80     unsigned int fingerprint;
81     char *string;
82 };
83
84 struct atom_table {
85     xkb_atom_t last_atom;
86     struct atom_node *atom_root;
87     size_t table_length;
88     struct atom_node **node_table;
89 };
90
91 struct atom_table *
92 atom_table_new(void)
93 {
94     struct atom_table *table;
95
96     table = calloc(1, sizeof(*table));
97     if (!table)
98         return NULL;
99
100     table->last_atom = XKB_ATOM_NONE;
101
102     return table;
103 }
104
105 static void
106 free_atom(struct atom_node *patom)
107 {
108     if (!patom)
109         return;
110
111     free_atom(patom->left);
112     free_atom(patom->right);
113     free(patom->string);
114     free(patom);
115 }
116
117 void
118 atom_table_free(struct atom_table *table)
119 {
120     if (!table)
121         return;
122
123     free_atom(table->atom_root);
124     free(table->node_table);
125     free(table);
126 }
127
128 const char *
129 atom_text(struct atom_table *table, xkb_atom_t atom)
130 {
131     if (atom == XKB_ATOM_NONE || atom > table->last_atom ||
132         !table->node_table[atom])
133         return NULL;
134
135     return table->node_table[atom]->string;
136 }
137
138 char *
139 atom_strdup(struct atom_table *table, xkb_atom_t atom)
140 {
141     const char *ret = atom_text(table, atom);
142     return ret ? strdup(ret) : NULL;
143 }
144
145 xkb_atom_t
146 atom_intern(struct atom_table *table, const char *string)
147 {
148     struct atom_node **np;
149     struct atom_node *nd;
150     unsigned i;
151     int comp;
152     unsigned int fp = 0;
153     size_t len;
154
155     if (!string)
156         return XKB_ATOM_NONE;
157
158     len = strlen(string);
159     np = &table->atom_root;
160     for (i = 0; i < (len + 1) / 2; i++) {
161         fp = fp * 27 + string[i];
162         fp = fp * 27 + string[len - 1 - i];
163     }
164
165     while (*np) {
166         if (fp < (*np)->fingerprint)
167             np = &((*np)->left);
168         else if (fp > (*np)->fingerprint)
169             np = &((*np)->right);
170         else {
171             /* now start testing the strings */
172             comp = strncmp(string, (*np)->string, len);
173             if ((comp < 0) || ((comp == 0) && (len < strlen((*np)->string))))
174                 np = &((*np)->left);
175             else if (comp > 0)
176                 np = &((*np)->right);
177             else
178                 return(*np)->a;
179             }
180     }
181
182     nd = malloc(sizeof(*nd));
183     if (!nd)
184         return XKB_ATOM_NONE;
185
186     nd->string = malloc(len + 1);
187     if (!nd->string) {
188         free(nd);
189         return XKB_ATOM_NONE;
190     }
191     strncpy(nd->string, string, len);
192     nd->string[len] = 0;
193
194     if ((table->last_atom + 1) >= table->table_length) {
195         struct atom_node **new_node_table;
196         int new_length;
197
198         if (table->table_length == 0)
199             new_length = INITIAL_TABLE_SIZE;
200         else
201             new_length = table->table_length * 2;
202
203         new_node_table = realloc(table->node_table,
204                                  new_length * sizeof(*new_node_table));
205         if (!new_node_table) {
206             if (nd->string != string)
207                 free(nd->string);
208             free(nd);
209             return XKB_ATOM_NONE;
210         }
211         new_node_table[XKB_ATOM_NONE] = NULL;
212
213         table->table_length = new_length;
214         table->node_table = new_node_table;
215     }
216
217     *np = nd;
218     nd->left = nd->right = NULL;
219     nd->fingerprint = fp;
220     nd->a = (++table->last_atom);
221     *(table->node_table + table->last_atom) = nd;
222
223     return nd->a;
224 }