fix warning
[libfirm] / ir / ident / ident.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief     Hash table to store names.
23  * @author    Goetz Lindenmaier
24  * @version   $Id$
25  */
26 #include "config.h"
27
28 #include <assert.h>
29 #include <ctype.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stddef.h>
33 #include <stdlib.h>
34
35 #include "ident_t.h"
36 #include "set.h"
37 #include "xmalloc.h"
38 #include "hashptr.h"
39
40 static set *id_set;
41
42 void init_ident(void)
43 {
44         /* it's ok to use memcmp here, we check only strings */
45         id_set = new_set(memcmp, 128);
46 }
47
48 ident *new_id_from_chars(const char *str, size_t len)
49 {
50         unsigned hash   = HASH_STR(str, len);
51         ident   *result = (ident*) set_hinsert0(id_set, str, len, hash);
52         return result;
53 }
54
55 ident *new_id_from_str(const char *str)
56 {
57         assert(str != NULL);
58         return new_id_from_chars(str, strlen(str));
59 }
60
61 const char *get_id_str(ident *id)
62 {
63         struct set_entry *entry = (struct set_entry*) id;
64         return (const char*) entry->dptr;
65 }
66
67 size_t get_id_strlen(ident *id)
68 {
69         struct set_entry *entry = (struct set_entry*) id;
70         return entry->size;
71 }
72
73 void finish_ident(void)
74 {
75         del_set(id_set);
76         id_set = NULL;
77 }
78
79 int id_is_prefix(ident *prefix, ident *id)
80 {
81         size_t prefix_len = get_id_strlen(prefix);
82         if (prefix_len > get_id_strlen(id))
83                 return 0;
84         return 0 == memcmp(get_id_str(prefix), get_id_str(id), prefix_len);
85 }
86
87 int id_is_suffix(ident *suffix, ident *id)
88 {
89         size_t suflen = get_id_strlen(suffix);
90         size_t idlen  = get_id_strlen(id);
91         const char *part;
92
93         if (suflen > idlen)
94                 return 0;
95
96         part = get_id_str(id);
97         part = part + (idlen - suflen);
98
99         return 0 == memcmp(get_id_str(suffix), part, suflen);
100 }
101
102 int id_contains_char(ident *id, char c)
103 {
104         return strchr(get_id_str(id), c) != NULL;
105 }
106
107 ident *id_unique(const char *tag)
108 {
109         static unsigned unique_id = 0;
110         char buf[256];
111
112         snprintf(buf, sizeof(buf), tag, unique_id);
113         unique_id++;
114         return new_id_from_str(buf);
115 }