Log files should have .txt extension.
[helenos.git] / uspace / app / sbi / src / strtab.c
blob106f8334624a555a1f67775099b8f4193cc4f5b0
1 /*
2 * Copyright (c) 2010 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
36 * more than one.
38 * Current implementation uses a linked list and thus it is slow.
41 #include <stdlib.h>
42 #include "mytypes.h"
43 #include "os/os.h"
44 #include "list.h"
46 #include "strtab.h"
48 static list_t str_list;
50 /** Initialize string table. */
51 void strtab_init(void)
53 list_init(&str_list);
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.
61 * @param str String
62 * @return SID of @a str.
64 sid_t strtab_get_sid(const char *str)
66 list_node_t *node;
67 sid_t sid;
69 sid = 0;
70 node = list_first(&str_list);
72 while (node != NULL) {
73 ++sid;
74 if (os_str_cmp(str, list_node_data(node, char *)) == 0)
75 return sid;
77 node = list_next(&str_list, node);
80 ++sid;
81 list_append(&str_list, os_str_dup(str));
83 return sid;
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)
96 list_node_t *node;
97 sid_t cnt;
99 node = list_first(&str_list);
100 cnt = 1;
101 while (node != NULL && cnt < sid) {
102 node = list_next(&str_list, node);
103 cnt += 1;
106 if (node == NULL) {
107 printf("Internal error: Invalid SID %d", sid);
108 abort();
111 return list_node_data(node, char *);