source: mainline/uspace/app/sbi/src/strtab.c

Last change on this file was 38aaacc2, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Update SBI to rev. 207.

  • Property mode set to 100644
File size: 3.0 KB
Line 
1/*
2 * Copyright (c) 2010 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
16 *
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.
27 */
28
29/** @file String table.
30 *
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.)
34 *
35 * The string table is a singleton as there will never be a need for
36 * more than one.
37 *
38 * Current implementation uses a linked list and thus it is slow.
39 */
40
41#include <stdlib.h>
42#include "mytypes.h"
43#include "os/os.h"
44#include "list.h"
45
46#include "strtab.h"
47
48static list_t str_list;
49
50/** Initialize string table. */
51void strtab_init(void)
52{
53 list_init(&str_list);
54}
55
56/** Get SID of a string.
57 *
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.
60 *
61 * @param str String
62 * @return SID of @a str.
63 */
64sid_t strtab_get_sid(const char *str)
65{
66 list_node_t *node;
67 sid_t sid;
68
69 sid = 0;
70 node = list_first(&str_list);
71
72 while (node != NULL) {
73 ++sid;
74 if (os_str_cmp(str, list_node_data(node, char *)) == 0)
75 return sid;
76
77 node = list_next(&str_list, node);
78 }
79
80 ++sid;
81 list_append(&str_list, os_str_dup(str));
82
83 return sid;
84}
85
86/** Get string with the given SID.
87 *
88 * Returns string that has SID @a sid. If no such string exists, this
89 * causes a fatal error in the interpreter.
90 *
91 * @param sid SID of the string.
92 * @return Pointer to the string.
93 */
94char *strtab_get_str(sid_t sid)
95{
96 list_node_t *node;
97 sid_t cnt;
98
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;
104 }
105
106 if (node == NULL) {
107 printf("Internal error: Invalid SID %d", sid);
108 abort();
109 }
110
111 return list_node_data(node, char *);
112}
Note: See TracBrowser for help on using the repository browser.