3 * File name: ir/common/ident.c
4 * Purpose: Hash table to store names.
5 * Author: Goetz Lindenmaier
9 * Copyright: (c) 1999-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
26 typedef const struct set_entry ident_s;
28 /** The current ident module implementation. */
29 static ident_if_t impl;
32 * Stores a string in the ident module and returns a handle for the string.
34 * @param handle the handle for the set
35 * @param str the string which shall be stored
37 * @return id - a handle for the generated ident
39 * Default implementation using libfirm sets.
41 static ident *set_new_id_from_chars(void *handle, const char *str, int len)
46 return (ident *)set_hinsert0(id_set, str, len, ID_HASH(str, len));
50 * Stores a string in the ident module and returns a handle for the string.
52 * @param handle the handle for the set
53 * @param str the string (or whatever) which shall be stored
54 * @param len the length of the data in bytes
56 * Default implementation using libfirm sets.
58 static ident *set_new_id_from_str(void *handle, const char *str)
61 return (ident *)set_new_id_from_chars(handle, str, strlen(str));
65 * Returns a string represented by an ident.
67 * @param handle the handle for the set
70 * Default implementation using libfirm sets.
72 static const char *set_get_id_str(void *handle, ident *id)
74 struct set_entry *entry = (struct set_entry *)id;
76 return (const char *)entry->dptr;
80 * Returns the length of the string represented by an ident.
82 * @param handle the handle for the set
85 * Default implementation using libfirm sets.
87 static int set_get_id_strlen(void *handle, ident *id)
89 struct set_entry *entry = (struct set_entry *)id;
95 * Default implementation using libfirm sets.
97 void set_finish_ident(void *handle) {
104 * Default implementation if no new_id_from_str() is provided.
106 static ident *def_new_id_from_str(void *handle, const char *str)
108 return impl.new_id_from_chars(handle, str, strlen(str));
112 * Default implementation if no get_id_strlen() is provided.
114 static int def_get_id_strlen(void *handle, ident *id)
116 return strlen(impl.get_id_str(handle, id));
119 /* Initialize the ident module. */
120 void init_ident(ident_if_t *id_if, int initial_n_idents)
123 memcpy(&impl, id_if, sizeof(impl));
125 if (! impl.new_id_from_str)
126 impl.new_id_from_str = def_new_id_from_str;
127 if (! impl.get_id_strlen)
128 impl.get_id_strlen = def_get_id_strlen;
131 impl.new_id_from_str = set_new_id_from_str;
132 impl.new_id_from_chars = set_new_id_from_chars;
133 impl.get_id_str = set_get_id_str;
134 impl.get_id_strlen = set_get_id_strlen;
135 impl.finish_ident = set_finish_ident;
137 impl.handle = new_set(memcmp, initial_n_idents);
141 ident *new_id_from_str(const char *str)
144 return impl.new_id_from_str(impl.handle, str);
147 ident *new_id_from_chars(const char *str, int len)
150 return impl.new_id_from_chars(impl.handle, str, len);
153 const char *get_id_str(ident *id)
155 return impl.get_id_str(impl.handle, id);
158 int get_id_strlen(ident *id)
160 return impl.get_id_strlen(impl.handle, id);
163 void finish_ident(void) {
164 if (impl.finish_ident)
165 impl.finish_ident(impl.handle);
168 int id_is_prefix(ident *prefix, ident *id)
170 if (get_id_strlen(prefix) > get_id_strlen(id)) return 0;
171 return 0 == memcmp(get_id_str(prefix), get_id_str(id), get_id_strlen(prefix));
174 int id_is_suffix(ident *suffix, ident *id)
176 int suflen = get_id_strlen(suffix);
177 int idlen = get_id_strlen(id);
180 if (suflen > idlen) return 0;
182 part = get_id_str(id);
183 part = part + (idlen - suflen);
185 return 0 == memcmp(get_id_str(suffix), part, suflen);
188 int id_contains_char(ident *id, char c)
190 return strchr(get_id_str(id), c) != NULL;
193 int print_id (ident *id)
195 return printf("%s", get_id_str(id));
198 int fprint_id (FILE *F, ident *id)
200 return fprintf(F, "%s", get_id_str(id));