3f92131adec32352fb506b92edcb65a022438e56
[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 "array.h"
25 #include "set.h"
26
27 static set *id_set;
28
29 void id_init(int initial_n_idents)
30 {
31   id_set = new_set(memcmp, initial_n_idents);
32 }
33
34 void id_finish (void) {
35   del_set(id_set);
36   id_set = NULL;
37 }
38
39 INLINE ident *id_from_str (const char *str, int len)
40 {
41   assert(len > 0);
42   return set_hinsert0(id_set, str, len, ID_HASH(str, len));
43 }
44
45 ident *new_id_from_str(const char *str)
46 {
47   assert(str);
48   return id_from_str(str, strlen(str));
49 }
50
51 INLINE const char *get_id_str(ident *id)
52 {
53   return (const char *)id->dptr;
54 }
55
56 INLINE int get_id_strlen(ident *id)
57 {
58   return id->size;
59 }
60
61 int id_is_prefix(ident *prefix, ident *id)
62 {
63   if (get_id_strlen(prefix) > get_id_strlen(id)) return 0;
64   return 0 == memcmp(prefix->dptr, id->dptr, get_id_strlen(prefix));
65 }
66
67 int id_is_suffix(ident *suffix, ident *id)
68 {
69   int suflen = get_id_strlen(suffix);
70   int idlen  = get_id_strlen(id);
71   char *part;
72
73   if (suflen > idlen) return 0;
74
75   part = (char *)id->dptr;
76   part = part + (idlen - suflen);
77
78   return 0 == memcmp(suffix->dptr, part, suflen);
79 }
80
81 int id_contains_char(ident *id, char c)
82 {
83   return strchr(get_id_str(id), c) != NULL;
84 }
85
86 int print_id (ident *id)
87 {
88   return printf("%s", get_id_str(id));
89 }
90
91 int fprint_id (FILE *F, ident *id)
92 {
93   return fprintf(F, "%s", get_id_str(id));
94 }