Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / lib / csa / cmsdata.c
1 /* $XConsortium: cmsdata.c /main/1 1996/04/21 19:22:08 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 <string.h>
12 #include <stdlib.h>
13 #include "csa.h"
14 #include "cmsdata.h"
15 #include "nametbl.h"
16 #include "attr.h"
17
18 /* standard calendar attr name and entry attr name table */
19 _DtCmNameTable *_DtCm_cal_name_tbl = NULL;
20 _DtCmNameTable *_DtCm_entry_name_tbl = NULL;
21
22 /*
23  * allocate a cms_entry structure and initialized with
24  * all the library defined attribute names
25  */
26 extern cms_entry *
27 _DtCm_make_cms_entry(_DtCmNameTable *tbl)
28 {
29         int     i;
30         cms_entry *ptr;
31
32         if ((ptr = (cms_entry *)calloc(1, sizeof(cms_entry))) == NULL)
33                 return (NULL);
34
35         /* initialize the entry with attribute names */
36
37         if ((ptr->attrs = (cms_attribute *)calloc(1,
38             sizeof(cms_attribute)*(tbl->size + 1))) == NULL) {
39                 free(ptr);
40                 return (NULL);
41         }
42
43         for (i = 1; i <= tbl->size; i++) {
44                 if ((ptr->attrs[i].name.name = strdup(tbl->names[i])) == NULL) {
45                         /* clean up */
46                         ptr->num_attrs = i - 1;
47                         _DtCm_free_cms_entry(ptr);
48                         return (NULL);
49                 }
50
51                 ptr->attrs[i].name.num = i;
52         }
53
54         ptr->num_attrs = tbl->size;
55
56         return (ptr);
57 }
58
59 extern CSA_return_code
60 _DtCm_copy_cms_entry(cms_entry *e, cms_entry **e_r)
61 {
62         cms_entry *ptr;
63         CSA_return_code stat;
64
65         if (e == NULL || e_r == NULL)
66                 return (CSA_E_INVALID_PARAMETER);
67
68         if ((ptr = (cms_entry *)calloc(1, sizeof(cms_entry))) == NULL)
69                 return (CSA_E_INSUFFICIENT_MEMORY);
70
71         if ((stat = _DtCm_copy_cms_attributes(e->num_attrs, e->attrs,
72             &ptr->num_attrs, &ptr->attrs)) != CSA_SUCCESS) {
73                 free(ptr);
74                 return (stat);
75         } else {
76                 ptr->key = e->key;
77                 *e_r = ptr;
78                 return (CSA_SUCCESS);
79         }
80 }
81
82 extern void
83 _DtCm_free_cms_entry(cms_entry *entry)
84 {
85         if (entry == NULL)
86                 return;
87
88         if (entry->num_attrs > 0) {
89                 _DtCm_free_cms_attributes(entry->num_attrs + 1, entry->attrs);
90                 free(entry->attrs);
91         }
92
93         free(entry);
94 }
95
96 extern void
97 _DtCm_free_cms_entries(cms_entry *entry)
98 {
99         cms_entry *ptr;
100
101         while (entry) {
102                 ptr = entry->next;
103
104                 _DtCm_free_cms_entry(entry);
105
106                 entry = ptr;
107         }
108 }
109
110 extern void
111 _DtCm_init_hash()
112 {
113         static  boolean_t       done = B_FALSE;
114
115         if (done == B_FALSE) {
116
117                 /* need to check whether table is actually created */
118                 _DtCm_cal_name_tbl = _DtCm_make_name_table(
119                                         _DtCM_DEFINED_CAL_ATTR_SIZE,
120                                         _CSA_calendar_attribute_names);
121                 _DtCm_entry_name_tbl = _DtCm_make_name_table(
122                                         _DtCM_DEFINED_ENTRY_ATTR_SIZE,
123                                         _CSA_entry_attribute_names);
124                 done = B_TRUE;
125         }
126 }
127
128 /*
129  * attr->name.num contains the correct index for the attribute
130  */
131 extern CSA_return_code
132 _DtCmGrowAttrArray(uint *num_attrs, cms_attribute **attrs, cms_attribute *attr)
133 {
134         cms_attribute   *newptr;
135         CSA_return_code stat;
136         int             index;
137
138         index = attr->name.num;
139
140         if ((newptr = (cms_attribute *)realloc(*attrs,
141             sizeof(cms_attribute) * (index + 1))) == NULL)
142                 return (CSA_E_INSUFFICIENT_MEMORY);
143         else {
144                 *attrs = newptr;
145                 memset((void *)&(*attrs)[*num_attrs+1], NULL,
146                         sizeof(cms_attribute) * (index - *num_attrs));
147         }
148
149         if ((stat = _DtCm_copy_cms_attribute(&(*attrs)[index], attr, B_TRUE))
150             == CSA_SUCCESS) {
151
152                 *num_attrs = index;
153         }
154
155         return (stat);
156 }
157