[09ababb7] | 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.
|
---|
[38aaacc2] | 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
|
---|
[09ababb7] | 36 | * more than one.
|
---|
[38aaacc2] | 37 | *
|
---|
| 38 | * Current implementation uses a linked list and thus it is slow.
|
---|
[09ababb7] | 39 | */
|
---|
| 40 |
|
---|
| 41 | #include <stdlib.h>
|
---|
| 42 | #include "mytypes.h"
|
---|
[94d484a] | 43 | #include "os/os.h"
|
---|
[09ababb7] | 44 | #include "list.h"
|
---|
| 45 |
|
---|
| 46 | #include "strtab.h"
|
---|
| 47 |
|
---|
| 48 | static list_t str_list;
|
---|
| 49 |
|
---|
[38aaacc2] | 50 | /** Initialize string table. */
|
---|
[09ababb7] | 51 | void strtab_init(void)
|
---|
| 52 | {
|
---|
| 53 | list_init(&str_list);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[38aaacc2] | 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 | */
|
---|
[d0febca] | 64 | sid_t strtab_get_sid(const char *str)
|
---|
[09ababb7] | 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;
|
---|
[94d484a] | 74 | if (os_str_cmp(str, list_node_data(node, char *)) == 0)
|
---|
[09ababb7] | 75 | return sid;
|
---|
| 76 |
|
---|
| 77 | node = list_next(&str_list, node);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | ++sid;
|
---|
[94d484a] | 81 | list_append(&str_list, os_str_dup(str));
|
---|
[09ababb7] | 82 |
|
---|
| 83 | return sid;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[38aaacc2] | 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 | */
|
---|
[09ababb7] | 94 | char *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);
|
---|
[94d484a] | 108 | abort();
|
---|
[09ababb7] | 109 | }
|
---|
| 110 |
|
---|
| 111 | return list_node_data(node, char *);
|
---|
| 112 | }
|
---|