remove id_decorate_win32_c_fkt
[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  */
25 #include "config.h"
26
27 #include <assert.h>
28 #include <ctype.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stddef.h>
32 #include <stdlib.h>
33
34 #include "ident_t.h"
35 #include "set.h"
36 #include "xmalloc.h"
37 #include "hashptr.h"
38
39 static set *id_set;
40
41 void init_ident(void)
42 {
43         /* it's ok to use memcmp here, we check only strings */
44         id_set = new_set(memcmp, 128);
45 }
46
47 ident *new_id_from_chars(const char *str, size_t len)
48 {
49         unsigned hash   = hash_data((const unsigned char*)str, len);
50         ident   *result = (ident*) set_hinsert0(id_set, str, len, hash);
51         return result;
52 }
53
54 ident *new_id_from_str(const char *str)
55 {
56         assert(str != NULL);
57         return new_id_from_chars(str, strlen(str));
58 }
59
60 const char *get_id_str(ident *id)
61 {
62         struct set_entry *entry = (struct set_entry*) id;
63         return (const char*) entry->dptr;
64 }
65
66 size_t get_id_strlen(ident *id)
67 {
68         struct set_entry *entry = (struct set_entry*) id;
69         return entry->size;
70 }
71
72 void finish_ident(void)
73 {
74         del_set(id_set);
75         id_set = NULL;
76 }
77
78 int id_is_prefix(ident *prefix, ident *id)
79 {
80         size_t prefix_len = get_id_strlen(prefix);
81         if (prefix_len > get_id_strlen(id))
82                 return 0;
83         return 0 == memcmp(get_id_str(prefix), get_id_str(id), prefix_len);
84 }
85
86 int id_is_suffix(ident *suffix, ident *id)
87 {
88         size_t suflen = get_id_strlen(suffix);
89         size_t idlen  = get_id_strlen(id);
90         const char *part;
91
92         if (suflen > idlen)
93                 return 0;
94
95         part = get_id_str(id);
96         part = part + (idlen - suflen);
97
98         return 0 == memcmp(get_id_str(suffix), part, suflen);
99 }
100
101 int id_contains_char(ident *id, char c)
102 {
103         return strchr(get_id_str(id), c) != NULL;
104 }
105
106 ident *id_unique(const char *tag)
107 {
108         static unsigned unique_id = 0;
109         char buf[256];
110
111         snprintf(buf, sizeof(buf), tag, unique_id);
112         unique_id++;
113         return new_id_from_str(buf);
114 }