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