Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / csa / cmsdata.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: cmsdata.c /main/1 1996/04/21 19:22:08 drk $ */
24 /*
25  *  (c) Copyright 1993, 1994 Hewlett-Packard Company
26  *  (c) Copyright 1993, 1994 International Business Machines Corp.
27  *  (c) Copyright 1993, 1994 Novell, Inc.
28  *  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
29  */
30
31 #include <EUSCompat.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include "csa.h"
36 #include "cmsdata.h"
37 #include "nametbl.h"
38 #include "attr.h"
39
40 /* standard calendar attr name and entry attr name table */
41 _DtCmNameTable *_DtCm_cal_name_tbl = NULL;
42 _DtCmNameTable *_DtCm_entry_name_tbl = NULL;
43
44 /*
45  * allocate a cms_entry structure and initialized with
46  * all the library defined attribute names
47  */
48 extern cms_entry *
49 _DtCm_make_cms_entry(_DtCmNameTable *tbl)
50 {
51         int     i;
52         cms_entry *ptr;
53
54         if ((ptr = (cms_entry *)calloc(1, sizeof(cms_entry))) == NULL)
55                 return (NULL);
56
57         /* initialize the entry with attribute names */
58
59         if ((ptr->attrs = (cms_attribute *)calloc(1,
60             sizeof(cms_attribute)*(tbl->size + 1))) == NULL) {
61                 free(ptr);
62                 return (NULL);
63         }
64
65         for (i = 1; i <= tbl->size; i++) {
66                 if ((ptr->attrs[i].name.name = strdup(tbl->names[i])) == NULL) {
67                         /* clean up */
68                         ptr->num_attrs = i - 1;
69                         _DtCm_free_cms_entry(ptr);
70                         return (NULL);
71                 }
72
73                 ptr->attrs[i].name.num = i;
74         }
75
76         ptr->num_attrs = tbl->size;
77
78         return (ptr);
79 }
80
81 extern CSA_return_code
82 _DtCm_copy_cms_entry(cms_entry *e, cms_entry **e_r)
83 {
84         cms_entry *ptr;
85         CSA_return_code stat;
86
87         if (e == NULL || e_r == NULL)
88                 return (CSA_E_INVALID_PARAMETER);
89
90         if ((ptr = (cms_entry *)calloc(1, sizeof(cms_entry))) == NULL)
91                 return (CSA_E_INSUFFICIENT_MEMORY);
92
93         if ((stat = _DtCm_copy_cms_attributes(e->num_attrs, e->attrs,
94             &ptr->num_attrs, &ptr->attrs)) != CSA_SUCCESS) {
95                 free(ptr);
96                 return (stat);
97         } else {
98                 ptr->key = e->key;
99                 *e_r = ptr;
100                 return (CSA_SUCCESS);
101         }
102 }
103
104 extern void
105 _DtCm_free_cms_entry(cms_entry *entry)
106 {
107         if (entry == NULL)
108                 return;
109
110         if (entry->num_attrs > 0) {
111                 _DtCm_free_cms_attributes(entry->num_attrs + 1, entry->attrs);
112                 free(entry->attrs);
113         }
114
115         free(entry);
116 }
117
118 extern void
119 _DtCm_free_cms_entries(cms_entry *entry)
120 {
121         cms_entry *ptr;
122
123         while (entry) {
124                 ptr = entry->next;
125
126                 _DtCm_free_cms_entry(entry);
127
128                 entry = ptr;
129         }
130 }
131
132 extern void
133 _DtCm_init_hash()
134 {
135         static  boolean_t       done = B_FALSE;
136
137         if (done == B_FALSE) {
138
139                 /* need to check whether table is actually created */
140                 _DtCm_cal_name_tbl = _DtCm_make_name_table(
141                                         _DtCM_DEFINED_CAL_ATTR_SIZE,
142                                         _CSA_calendar_attribute_names);
143                 _DtCm_entry_name_tbl = _DtCm_make_name_table(
144                                         _DtCM_DEFINED_ENTRY_ATTR_SIZE,
145                                         _CSA_entry_attribute_names);
146                 done = B_TRUE;
147         }
148 }
149
150 /*
151  * attr->name.num contains the correct index for the attribute
152  */
153 extern CSA_return_code
154 _DtCmGrowAttrArray(uint *num_attrs, cms_attribute **attrs, cms_attribute *attr)
155 {
156         cms_attribute   *newptr;
157         CSA_return_code stat;
158         int             index;
159
160         index = attr->name.num;
161
162         if ((newptr = (cms_attribute *)realloc(*attrs,
163             sizeof(cms_attribute) * (index + 1))) == NULL)
164                 return (CSA_E_INSUFFICIENT_MEMORY);
165         else {
166                 *attrs = newptr;
167                 memset((void *)&(*attrs)[*num_attrs+1], NULL,
168                         sizeof(cms_attribute) * (index - *num_attrs));
169         }
170
171         if ((stat = _DtCm_copy_cms_attribute(&(*attrs)[index], attr, B_TRUE))
172             == CSA_SUCCESS) {
173
174                 *num_attrs = index;
175         }
176
177         return (stat);
178 }
179