2 * Copyright (c) 2010 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @file String table.
31 * Converts strings to more compact SID (string ID, integer) and back.
32 * (The point is that this deduplicates the strings. Using SID might actually
33 * not be such a big win.)
35 * The string table is a singleton as there will never be a need for
38 * Current implementation uses a linked list and thus it is slow.
48 static list_t str_list
;
50 /** Initialize string table. */
51 void strtab_init(void)
56 /** Get SID of a string.
58 * Return SID of @a str. If @a str is not in the string table yet,
59 * it is added and thus a new SID is assigned.
62 * @return SID of @a str.
64 sid_t
strtab_get_sid(const char *str
)
70 node
= list_first(&str_list
);
72 while (node
!= NULL
) {
74 if (os_str_cmp(str
, list_node_data(node
, char *)) == 0)
77 node
= list_next(&str_list
, node
);
81 list_append(&str_list
, os_str_dup(str
));
86 /** Get string with the given SID.
88 * Returns string that has SID @a sid. If no such string exists, this
89 * causes a fatal error in the interpreter.
91 * @param sid SID of the string.
92 * @return Pointer to the string.
94 char *strtab_get_str(sid_t sid
)
99 node
= list_first(&str_list
);
101 while (node
!= NULL
&& cnt
< sid
) {
102 node
= list_next(&str_list
, node
);
107 printf("Internal error: Invalid SID %d", sid
);
111 return list_node_data(node
, char *);