027f02df38693fe161b8fec47f3da15dbbdfa02c
[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 INLINE ident *id_from_str (const char *str, int len)
35 {
36   assert(len > 0);
37   return set_hinsert0(id_set, str, len, ID_HASH(str, len));
38 }
39
40 ident *new_id_from_str(const char *str)
41 {
42   assert(str);
43   return id_from_str(str, strlen(str));
44 }
45
46 INLINE const char *get_id_str(ident *id)
47 {
48   return (const char *)id->dptr;
49 }
50
51 INLINE int get_id_strlen(ident *id)
52 {
53   return id->size;
54 }
55
56 int id_is_prefix(ident *prefix, ident *id)
57 {
58   if (get_id_strlen(prefix) > get_id_strlen(id)) return 0;
59   return 0 == memcmp(prefix->dptr, id->dptr, get_id_strlen(prefix));
60 }
61
62 int id_is_suffix(ident *suffix, ident *id)
63 {
64   int suflen = get_id_strlen(suffix);
65   int idlen  = get_id_strlen(id);
66   char *part;
67
68   if (suflen > idlen) return 0;
69
70   part = (char *)id->dptr;
71   part = part + (idlen - suflen);
72
73   return 0 == memcmp(suffix->dptr, part, suflen);
74 }
75
76 int id_contains_char(ident *id, char c)
77 {
78   return strchr(get_id_str(id), c) != NULL;
79 }
80
81 int print_id (ident *id)
82 {
83   return printf("%s", get_id_str(id));
84 }
85
86 int fprint_id (FILE *F, ident *id)
87 {
88   return fprintf(F, "%s", get_id_str(id));
89 }