added doxygen comments
[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 typedef const struct set_entry ident_s;
27
28 /** The current ident module implementation. */
29 static ident_if_t impl;
30
31 /**
32  * Stores a string in the ident module and returns a handle for the string.
33  *
34  * @param handle   the handle for the set
35  * @param str      the string which shall be stored
36  *
37  * @return id - a handle for the generated ident
38  *
39  * Default implementation using libfirm sets.
40  */
41 static ident *set_new_id_from_chars(void *handle, const char *str, int len)
42 {
43   set *id_set = handle;
44
45   assert(len > 0);
46   return (ident *)set_hinsert0(id_set, str, len, ID_HASH(str, len));
47 }
48
49 /**
50  * Stores a string in the ident module and returns a handle for the string.
51  *
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
55  *
56  * Default implementation using libfirm sets.
57  */
58 static ident *set_new_id_from_str(void *handle, const char *str)
59 {
60   assert(str);
61   return (ident *)set_new_id_from_chars(handle, str, strlen(str));
62 }
63
64 /**
65  * Returns a string represented by an ident.
66  *
67  * @param handle   the handle for the set
68  * @param id       the ident
69  *
70  * Default implementation using libfirm sets.
71  */
72 static const char *set_get_id_str(void *handle, ident *id)
73 {
74   struct set_entry *entry = (struct set_entry *)id;
75
76   return (const char *)entry->dptr;
77 }
78
79 /**
80  * Returns the length of the string represented by an ident.
81  *
82  * @param handle   the handle for the set
83  * @param id       the ident
84  *
85  * Default implementation using libfirm sets.
86  */
87 static int set_get_id_strlen(void *handle, ident *id)
88 {
89   struct set_entry *entry = (struct set_entry *)id;
90
91   return entry->size;
92 }
93
94 /**
95  * Default implementation using libfirm sets.
96  */
97 void set_finish_ident(void *handle) {
98   set *id_set = handle;
99
100   del_set(id_set);
101 }
102
103 /**
104  * Default implementation if no new_id_from_str() is provided.
105  */
106 static ident *def_new_id_from_str(void *handle, const char *str)
107 {
108   return impl.new_id_from_chars(handle, str, strlen(str));
109 }
110
111 /**
112  * Default implementation if no get_id_strlen() is provided.
113  */
114 static int def_get_id_strlen(void *handle, ident *id)
115 {
116   return strlen(impl.get_id_str(handle, id));
117 }
118
119 /* Initialize the ident module. */
120 void init_ident(ident_if_t *id_if, int initial_n_idents)
121 {
122   if (id_if) {
123     memcpy(&impl, id_if, sizeof(impl));
124
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;
129   }
130   else {
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;
136
137    impl.handle = new_set(memcmp, initial_n_idents);
138   }
139 }
140
141 ident *new_id_from_str(const char *str)
142 {
143   assert(str);
144   return impl.new_id_from_str(impl.handle, str);
145 }
146
147 ident *new_id_from_chars(const char *str, int len)
148 {
149   assert(len > 0);
150   return impl.new_id_from_chars(impl.handle, str, len);
151 }
152
153 const char *get_id_str(ident *id)
154 {
155   return impl.get_id_str(impl.handle, id);
156 }
157
158 int get_id_strlen(ident *id)
159 {
160   return impl.get_id_strlen(impl.handle, id);
161 }
162
163 void finish_ident(void) {
164   if (impl.finish_ident)
165     impl.finish_ident(impl.handle);
166 }
167
168 int id_is_prefix(ident *prefix, ident *id)
169 {
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));
172 }
173
174 int id_is_suffix(ident *suffix, ident *id)
175 {
176   int suflen = get_id_strlen(suffix);
177   int idlen  = get_id_strlen(id);
178   const char *part;
179
180   if (suflen > idlen) return 0;
181
182   part = get_id_str(id);
183   part = part + (idlen - suflen);
184
185   return 0 == memcmp(get_id_str(suffix), part, suflen);
186 }
187
188 int id_contains_char(ident *id, char c)
189 {
190   return strchr(get_id_str(id), c) != NULL;
191 }
192
193 int print_id (ident *id)
194 {
195   return printf("%s", get_id_str(id));
196 }
197
198 int fprint_id (FILE *F, ident *id)
199 {
200   return fprintf(F, "%s", get_id_str(id));
201 }