1 /*
2  * str_ht.c
3  *
4  * Single (char *) hash table
5  *
6  * Copyright (c) 1988, 89, 90, 91, 92, 93 Miguel Santana
7  * Copyright (c) 1995, 96, 97, 98 Akim Demaille, Miguel Santana
8  */
9 
10 /*
11  * This file is part of a2ps.
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2, or (at your option)
16  * any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; see the file COPYING.  If not, write to
25  * the Free Software Foundation, 59 Temple Place - Suite 330,
26  * Boston, MA 02111-1307, USA.
27  */
28 
29 /*
30  * $Id: str_ht.c,v 1.1 1998/02/17 17:54:53 demaille Exp $
31  */
32 
33 #include "system.h"
34 
35 /* Hack! */
36 #define hash_table_s string_htable
37 #include "str_ht.h"
38 #include "hashtab.h"
39 
40 /************************************************************************
41  * hash tables with one char * field					*
42  ************************************************************************/
43 /*
44  * Basic routines
45  */
46 static unsigned long
string_hash_1(char * string)47 string_hash_1 (char *string)
48 {
49   return_STRING_HASH_1 (string);
50 }
51 
52 static unsigned long
string_hash_2(char * string)53 string_hash_2 (char *string)
54 {
55   return_STRING_HASH_2 (string);
56 }
57 
58 static int
string_hash_cmp(char * x,char * y)59 string_hash_cmp (char *x, char *y)
60 {
61   return_STRING_COMPARE (x, y);
62 }
63 
64 /*
65  * For sorting them in alpha order
66  */
67 static int
string_hash_qcmp(char ** x,char ** y)68 string_hash_qcmp (char **x, char **y)
69 {
70   return_STRING_COMPARE (*x, *y);
71 }
72 
73 /*
74  * Create the structure that stores the list of strings
75  */
76 struct string_htable *
string_htable_new(void)77 string_htable_new (void)
78 {
79   struct string_htable * res;
80 
81   res = XMALLOC (struct string_htable, 1);
82   hash_init (res, 8,
83 	     (hash_func_t) string_hash_1,
84 	     (hash_func_t) string_hash_2,
85 	     (hash_cmp_func_t) string_hash_cmp);
86   return res;
87 }
88 
89 /*
90  * Free the whole structure
91  */
92 void
string_htable_free(struct string_htable * table)93 string_htable_free (struct string_htable * table)
94 {
95   hash_free (table, (hash_map_func_t) free);
96   free (table);
97 }
98 
99 /*
100  *  Add a string, with your own allocation for them.
101  */
102 void
string_htable_add(struct string_htable * table,const char * key)103 string_htable_add (struct string_htable * table, const char * key)
104 {
105   if (!hash_find_item (table, key))
106     hash_insert (table, xstrdup(key));
107 }
108 
109 /*
110  * Get the value associated to KEY in TABLE
111  * Return NULL upon error (this means that it is not
112  * valid to enter NULL as a value)
113  */
114 char *
string_htable_get(struct string_htable * table,const char * key)115 string_htable_get (struct string_htable * table, const char * key)
116 {
117   return (char *) hash_find_item (table, key);
118 }
119 
120 /*
121  * Mostly for debbuging
122  */
123 void
string_htable_self_print(struct string_htable * table,FILE * stream)124 string_htable_self_print (struct string_htable * table, FILE * stream)
125 {
126   int i;
127   char ** entries;
128   entries = (char **)
129     hash_dump (table, NULL,
130 	       (hash_cmp_func_t) string_hash_qcmp);
131 
132   for (i = 0 ; entries[i] ; i++)
133     fprintf (stream, "%s\n", entries[i]);
134 
135   putc ('\n', stream);
136   free (entries);
137 }
138 
139 /*
140  * Dump in a vector
141  */
142 char **
string_htable_dump_sorted(struct string_htable * table)143 string_htable_dump_sorted (struct string_htable * table)
144 {
145   return (char **) hash_dump (table, NULL, (qsort_cmp_t) string_hash_qcmp);
146 }
147