Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / lib / csa / nametbl.c
1 /* $XConsortium: nametbl.c /main/1 1996/04/21 19:24:03 drk $ */
2 /*
3  *  (c) Copyright 1993, 1994 Hewlett-Packard Company
4  *  (c) Copyright 1993, 1994 International Business Machines Corp.
5  *  (c) Copyright 1993, 1994 Novell, Inc.
6  *  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
7  */
8
9 #include <EUSCompat.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "nametbl.h"
14 #include "hash.h"
15
16 #define MAX_CAL_TBL_SIZE        23
17 #define MAX_ENTRY_TBL_SIZE      37
18
19 extern _DtCmNameTable *
20 _DtCm_make_name_table(int size, char **names)
21 {
22         _DtCmNameTable *tbl;
23         int i;
24
25         if ((tbl = (_DtCmNameTable *)calloc(1, sizeof(_DtCmNameTable))) == NULL)
26                 return (NULL);
27
28         if ((tbl->tbl = _DtCmMakeHash(MAX_CAL_TBL_SIZE)) == NULL) {
29                 free(tbl);
30                 return (NULL);
31         }
32
33         if ((tbl->names = (char **)malloc(sizeof(char *) * (size+1))) == NULL) {
34                 _DtCm_free_name_table(tbl);
35                 return (NULL);
36         }
37
38         for (i = 1; i <= size; i++) {
39                 if ((tbl->names[i] = strdup(names[i])) == NULL) {
40                         tbl->size = i;
41                         _DtCm_free_name_table(tbl);
42                         return (NULL);
43                 }
44
45                 *(int *)_DtCmGetHash(tbl->tbl, (unsigned char *)names[i]) = i;
46         }
47
48         tbl->size = i - 1;
49
50         return (tbl);
51 }
52
53 extern void
54 _DtCm_free_name_table(_DtCmNameTable *tbl)
55 {
56         int     i;
57
58         if (tbl == NULL) return;
59
60         if (tbl->tbl)
61                 _DtCmDestroyHash(tbl->tbl, NULL, NULL);
62
63         for (i = 1; i <= tbl->size; i++)
64                 free(tbl->names[i]);
65
66         if (tbl->names)
67                 free(tbl->names);
68
69         free(tbl);
70 }
71
72 /*
73  * if index == 0, then add one more element to the table
74  * otherwise use the index to add the new name and extend the
75  * table by (index - size)
76  */
77 extern CSA_return_code
78 _DtCm_add_name_to_table(_DtCmNameTable *tbl, int index, char *newname)
79 {
80         int     *ptr;
81         char    **newptr;
82
83         if (index > 0 && index <= tbl->size && tbl->names[index])
84                 return (CSA_E_INVALID_PARAMETER);
85
86         if (index == 0)
87                 index = tbl->size + 1;
88
89         /* add new name to table */
90         ptr = (int *)_DtCmGetHash(tbl->tbl, (unsigned char *)newname);
91         if (ptr)
92                 *ptr = index;
93
94         if (index > tbl->size) {
95                 if ((newptr = (char **)realloc(tbl->names,
96                     sizeof(char *)*(index + 1))) == NULL) {
97                         *ptr = -1;
98                         return (CSA_E_INSUFFICIENT_MEMORY);
99                 } else {
100                         tbl->names = newptr;
101                         memset((void *)&tbl->names[tbl->size+1], NULL,
102                                 sizeof(char *)*(index - tbl->size));
103                 }
104         }
105
106         if ((tbl->names[index] = strdup(newname)) == NULL) {
107
108                 *ptr = -1;
109                 return (CSA_E_INSUFFICIENT_MEMORY);
110
111         } else {
112                 if (index > tbl->size)
113                         tbl->size = index;
114         }
115
116         return (CSA_SUCCESS);
117 }
118
119 extern int
120 _DtCm_get_index_from_table(_DtCmNameTable *tbl, char *name)
121 {
122         int *ptr;
123
124         ptr = (int *)_DtCmFindHash(tbl->tbl, (unsigned char *)name);
125
126         if (ptr)
127                 return (*ptr);
128         else
129                 return (-1);
130 }
131
132 /*
133  * if index == 0, then add one more element to the table
134  * other use the index to add the new name and extend the
135  * table by (index - size)
136  */
137 extern CSA_return_code
138 _DtCmExtendNameTable(
139         char            *name,
140         int             index,
141         int             type,
142         _DtCmNameTable  *base,
143         int             basesize,
144         char            **basenames,
145         _DtCmNameTable  **tbl,
146         int             **types)
147 {
148         _DtCmNameTable  *ntbl;
149         int             *newarray;
150         int             newindex;
151
152         if (index > 0 && index <= (*tbl)->size)
153                 return (CSA_E_INVALID_PARAMETER);
154
155         if (*tbl == base) {
156                 if ((ntbl = _DtCm_make_name_table(basesize, basenames)) == NULL)
157                 {
158
159                         return (CSA_E_INSUFFICIENT_MEMORY);
160                 } else
161                         *tbl = ntbl;
162         }
163
164         if (types) {
165                 newindex = (index == 0) ? (*tbl)->size+1 : index;
166                 if ((newarray = (int *)realloc(*types,
167                     sizeof(int) * (newindex+1))) == NULL) {
168                         return (CSA_E_INSUFFICIENT_MEMORY);
169                 } else {
170                         *types = newarray;
171                         (*types)[newindex] = type;
172                 }
173         }
174
175         return (_DtCm_add_name_to_table(*tbl, index, name));
176 }
177