3d014a71293ad278613ba33382535b633fcbdbbc
[libfirm] / ir / ident / ident.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/common/ident.c
4  * Purpose:     Hash table to store names.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1999-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
16
17 #include <assert.h>
18 #include <ctype.h>
19 #include <string.h>
20 #include <stddef.h>
21 #include <stdlib.h>
22
23 #include "ident_t.h"
24 #include "set.h"
25
26 /** The current ident module implementation. */
27 static ident_if_t impl;
28
29 /**
30  * Stores a string in the ident module and returns a handle for the string.
31  *
32  * @param handle   the handle for the set
33  * @param str      the string which shall be stored
34  *
35  * @return id - a handle for the generated ident
36  *
37  * Default implementation using libfirm sets.
38  */
39 static ident *set_new_id_from_chars(void *handle, const char *str, int len)
40 {
41   set *id_set = handle;
42
43   assert(len > 0);
44   return (ident *)set_hinsert0(id_set, str, len, ID_HASH(str, len));
45 }
46
47 /**
48  * Stores a string in the ident module and returns a handle for the string.
49  *
50  * @param handle   the handle for the set
51  * @param str      the string (or whatever) which shall be stored
52  * @param len      the length of the data in bytes
53  *
54  * Default implementation using libfirm sets.
55  */
56 static ident *set_new_id_from_str(void *handle, const char *str)
57 {
58   assert(str);
59   return (ident *)set_new_id_from_chars(handle, str, strlen(str));
60 }
61
62 /**
63  * Returns a string represented by an ident.
64  *
65  * @param handle   the handle for the set
66  * @param id       the ident
67  *
68  * Default implementation using libfirm sets.
69  */
70 static const char *set_get_id_str(void *handle, ident *id)
71 {
72   struct set_entry *entry = (struct set_entry *)id;
73
74   return (const char *)entry->dptr;
75 }
76
77 /**
78  * Returns the length of the string represented by an ident.
79  *
80  * @param handle   the handle for the set
81  * @param id       the ident
82  *
83  * Default implementation using libfirm sets.
84  */
85 static int set_get_id_strlen(void *handle, ident *id)
86 {
87   struct set_entry *entry = (struct set_entry *)id;
88
89   return entry->size;
90 }
91
92 /**
93  * Default implementation using libfirm sets.
94  */
95 void set_finish_ident(void *handle) {
96   set *id_set = handle;
97
98   del_set(id_set);
99 }
100
101 /**
102  * Default implementation if no new_id_from_str() is provided.
103  */
104 static ident *def_new_id_from_str(void *handle, const char *str)
105 {
106   return impl.new_id_from_chars(handle, str, strlen(str));
107 }
108
109 /**
110  * Default implementation if no get_id_strlen() is provided.
111  */
112 static int def_get_id_strlen(void *handle, ident *id)
113 {
114   return strlen(impl.get_id_str(handle, id));
115 }
116
117 /* Initialize the ident module. */
118 void init_ident(ident_if_t *id_if, int initial_n_idents)
119 {
120   if (id_if) {
121     memcpy(&impl, id_if, sizeof(impl));
122
123     if (! impl.new_id_from_str)
124       impl.new_id_from_str = def_new_id_from_str;
125     if (! impl.get_id_strlen)
126       impl.get_id_strlen = def_get_id_strlen;
127   }
128   else {
129    impl.new_id_from_str   = set_new_id_from_str;
130    impl.new_id_from_chars = set_new_id_from_chars;
131    impl.get_id_str        = set_get_id_str;
132    impl.get_id_strlen     = set_get_id_strlen;
133    impl.finish_ident      = set_finish_ident;
134
135    impl.handle = new_set(memcmp, initial_n_idents);
136   }
137 }
138
139 ident *new_id_from_str(const char *str)
140 {
141   assert(str);
142   return impl.new_id_from_str(impl.handle, str);
143 }
144
145 ident *new_id_from_chars(const char *str, int len)
146 {
147   assert(len > 0);
148   return impl.new_id_from_chars(impl.handle, str, len);
149 }
150
151 const char *get_id_str(ident *id)
152 {
153   return impl.get_id_str(impl.handle, id);
154 }
155
156 int get_id_strlen(ident *id)
157 {
158   return impl.get_id_strlen(impl.handle, id);
159 }
160
161 void finish_ident(void) {
162   if (impl.finish_ident)
163     impl.finish_ident(impl.handle);
164 }
165
166 int id_is_prefix(ident *prefix, ident *id)
167 {
168   if (get_id_strlen(prefix) > get_id_strlen(id)) return 0;
169   return 0 == memcmp(get_id_str(prefix), get_id_str(id), get_id_strlen(prefix));
170 }
171
172 int id_is_suffix(ident *suffix, ident *id)
173 {
174   int suflen = get_id_strlen(suffix);
175   int idlen  = get_id_strlen(id);
176   const char *part;
177
178   if (suflen > idlen) return 0;
179
180   part = get_id_str(id);
181   part = part + (idlen - suflen);
182
183   return 0 == memcmp(get_id_str(suffix), part, suflen);
184 }
185
186 int id_contains_char(ident *id, char c)
187 {
188   return strchr(get_id_str(id), c) != NULL;
189 }
190
191 int print_id (ident *id)
192 {
193   return printf("%s", get_id_str(id));
194 }
195
196 int fprint_id (FILE *F, ident *id)
197 {
198   return fprintf(F, "%s", get_id_str(id));
199 }