Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtcm / server / cmsentry.c
1 /* $XConsortium: cmsentry.c /main/4 1995/11/09 12:41:44 rswiston $ */
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 "cmsentry.h"
14 #include "cmsdata.h"
15 #include "nametbl.h"
16 #include "attr.h"
17
18 /******************************************************************************
19  * forward declaration of static functions used within the file
20  ******************************************************************************/
21 static CSA_return_code
22 _ExtractEntryAttrsFromEntry(uint srcsize, cms_attribute *srcattrs,
23         uint *dstsize, cms_attribute **dstattrs, boolean_t time_only);
24
25 /*****************************************************************************
26  * extern functions
27  *****************************************************************************/
28
29 /*
30  * Given a hashed name table, initialize a new cms_entry with
31  * the given list of attribute values
32  */
33 extern CSA_return_code
34 _DtCmsMakeHashedEntry(
35         _DtCmsCalendar  *cal,
36         uint            num,
37         cms_attribute   *attrs,
38         cms_entry       **entry)
39 {
40         int             i, index;
41         cms_entry       *eptr;
42         CSA_return_code stat = CSA_SUCCESS;
43
44         if (cal == NULL || entry == NULL)
45                 return (CSA_E_INVALID_PARAMETER);
46
47         if ((eptr = _DtCm_make_cms_entry(cal->entry_tbl)) == NULL)
48                 return (CSA_E_INSUFFICIENT_MEMORY);
49
50         for (i = 0; i < num && stat == CSA_SUCCESS; i++) {
51                 index = _DtCm_get_index_from_table(cal->entry_tbl,
52                         attrs[i].name.name);
53
54                 if (index > 0) {
55                         /* check type */
56                         if (index > _DtCM_DEFINED_ENTRY_ATTR_SIZE &&
57                             attrs[i].value &&
58                             attrs[i].value->type != cal->types[index])
59                                 stat = CSA_E_INVALID_ATTRIBUTE_VALUE;
60                         else
61                                 stat = _DtCm_copy_cms_attr_val(attrs[i].value,
62                                         &eptr->attrs[index].value);
63                 } else if (attrs[i].value) {
64                         if ((stat = _DtCmExtendNameTable(attrs[i].name.name, 0,
65                             attrs[i].value->type, _DtCm_entry_name_tbl,
66                             _DtCM_DEFINED_ENTRY_ATTR_SIZE,
67                             _CSA_entry_attribute_names, &cal->entry_tbl,
68                             &cal->types)) == CSA_SUCCESS) {
69
70                                 attrs[i].name.num = cal->entry_tbl->size;
71
72                                 stat = _DtCmGrowAttrArray(&eptr->num_attrs,
73                                         &eptr->attrs, &attrs[i]);
74                         }
75                 }
76         }
77
78         if (stat != CSA_SUCCESS) {
79                 _DtCm_free_cms_entry(eptr);
80                 return (stat);
81         }
82
83         *entry = eptr;
84         return (CSA_SUCCESS);
85 }
86
87 extern void
88 _DtCmsFreeEntryAttrResItem(cms_get_entry_attr_res_item *elist)
89 {
90         cms_get_entry_attr_res_item *ptr;
91
92         while (elist) {
93                 ptr = elist->next;
94
95                 _DtCm_free_cms_attributes(elist->num_attrs, elist->attrs);
96                 free(elist);
97
98                 elist = ptr;
99         }
100 }
101
102 extern CSA_return_code
103 _DtCmsGetCmsEntryForClient(cms_entry *e, cms_entry **e_r, boolean_t time_only)
104 {
105         cms_entry *ptr;
106         CSA_return_code stat;
107
108         if (e == NULL || e_r == NULL)
109                 return (CSA_E_INVALID_PARAMETER);
110
111         if ((ptr = (cms_entry *)calloc(1, sizeof(cms_entry))) == NULL)
112                 return (CSA_E_INSUFFICIENT_MEMORY);
113
114         if ((stat = _ExtractEntryAttrsFromEntry(e->num_attrs, e->attrs,
115             &ptr->num_attrs, &ptr->attrs,time_only)) != CSA_SUCCESS) {
116                 free(ptr);
117                 return (stat);
118         } else {
119                 ptr->key = e->key;
120                 *e_r = ptr;
121                 return (CSA_SUCCESS);
122         }
123 }
124
125 /*****************************************************************************
126  * static functions used within the file
127  *****************************************************************************/
128
129 static CSA_return_code
130 _ExtractEntryAttrsFromEntry(
131         uint            srcsize,
132         cms_attribute   *srcattrs,
133         uint            *dstsize,
134         cms_attribute   **dstattrs,
135         boolean_t       time_only)
136 {
137         CSA_return_code stat;
138         int             i, j;
139         cms_attribute   *attrs;
140
141         if (dstsize == NULL || dstattrs == NULL)
142                 return (CSA_E_INVALID_PARAMETER);
143
144         *dstsize = 0;
145         *dstattrs = NULL;
146
147         if (srcsize == 0)
148                 return (CSA_SUCCESS);
149
150         if ((attrs = calloc(1, sizeof(cms_attribute) * srcsize)) == NULL)
151                 return (CSA_E_INSUFFICIENT_MEMORY);
152
153         for (i = 0, j = 0; i <= srcsize; i++) {
154                 if (srcattrs[i].value == NULL)
155                         continue;
156
157                 if ((stat = _DtCm_copy_cms_attribute(&attrs[j], &srcattrs[i],
158                     B_TRUE)) != CSA_SUCCESS)
159                         break;
160                 else {
161                         if ( (i == CSA_ENTRY_ATTR_SUMMARY_I) && (time_only == B_TRUE) ) {
162                                 if (attrs[j].value && attrs[j].value->item.string_value)
163                                         attrs[j].value->item.string_value[0] = '\0';
164                         }
165
166                         j++;
167                 }
168         }
169
170         if (stat == CSA_SUCCESS && j > 0) {
171                 *dstsize = j;
172                 *dstattrs = attrs;
173         } else {
174                 _DtCm_free_cms_attributes(j, attrs);
175                 free(attrs);
176         }
177
178         return (stat);
179 }
180
181