Fix typo in license headers
[oweals/cde.git] / cde / lib / tt / mini_isam / isfcbindex.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 libraries 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 /*%%  (c) Copyright 1993, 1994 Hewlett-Packard Company                   */
24 /*%%  (c) Copyright 1993, 1994 International Business Machines Corp.     */
25 /*%%  (c) Copyright 1993, 1994 Sun Microsystems, Inc.                    */
26 /*%%  (c) Copyright 1993, 1994 Novell, Inc.                              */
27 /*%%  $XConsortium: isfcbindex.c /main/3 1995/10/23 11:38:43 rswiston $                                                          */
28 #ifndef lint
29 static char sccsid[] = "@(#)isfcbindex.c 1.3 89/07/17 Copyr 1988 Sun Micro";
30 #endif
31 /*
32  * Copyright (c) 1988 by Sun Microsystems, Inc.
33  */
34
35 /*
36  * isfcbindex.c
37  *
38  * Description: 
39  *      Functions that deal with the key descriptors in FCB
40  *      
41  *
42  */
43
44 #include "isam_impl.h"
45
46 Static int _keypart2cmp();
47 /*
48  * _isfcb_primkeyadd(fcb, keydesc2)
49  *
50  * Add priamry key descriptor to FCB.
51  *
52  */
53
54 int
55 _isfcb_primkeyadd(fcb, keydesc2)
56     Fcb                 *fcb;
57     Keydesc2            *keydesc2;
58 {
59     /*
60      * Assign keyid.
61      */
62     keydesc2->k2_keyid = ++fcb->lastkeyid;
63
64     fcb->keys[0] = *keydesc2;
65
66     return (ISOK);
67 }
68
69 /*
70  * _isfcb_primkeyel(fcb)
71  *
72  * Delete primary key
73  *
74  */
75
76 int
77 _isfcb_primkeydel(fcb)
78     Fcb                 *fcb;
79 {
80     if (FCB_NOPRIMARY_KEY(fcb))
81         return (EBADARG);
82
83     memset((char *)&fcb->keys[0], 0, sizeof(fcb->keys[0]));
84
85     return (ISOK);
86 }
87
88 /*
89  * _isfcb_altkeyadd(fcb, keydesc2)
90  *
91  * Add alternate key descriptor to FCB.
92  *
93  */
94
95 int
96 _isfcb_altkeyadd(fcb, keydesc2)
97     Fcb                 *fcb;
98     Keydesc2            *keydesc2;
99 {
100     assert (fcb->nkeys < MAXNKEYS); 
101
102     /*
103      * Assign keyid.
104      */
105     keydesc2->k2_keyid = ++fcb->lastkeyid;
106
107     /*
108      * Reallocate fcb->keys table.
109      */
110     fcb->keys = (Keydesc2 *) 
111         _isrealloc((char *)fcb->keys, 
112                    (unsigned) (sizeof(Keydesc2) * (fcb->nkeys + 1)));
113
114     fcb->keys[fcb->nkeys] = *keydesc2;
115
116     fcb->nkeys++;
117     return (ISOK);
118 }
119
120
121 /*
122  * pkeydesc2 = _isfcb_findkey(fcb, keydesc2)
123  *
124  * Find key descriptor.
125  *
126  */
127
128 Keydesc2 *
129 _isfcb_findkey(fcb, keydesc2)
130     register Fcb        *fcb;
131     Keydesc2            *keydesc2;
132 {
133     int                 nkeys = fcb->nkeys;
134     register Keydesc2   *kp2;
135     register int        j, i;
136     int                 nparts;
137
138     for (i = 0; i < nkeys; i++) {
139         kp2 = fcb->keys + i;
140
141         if (keydesc2->k2_nparts == kp2->k2_nparts) {
142             
143             nparts = keydesc2->k2_nparts;
144             for (j = 0; j < nparts; j++) {
145                 if (_keypart2cmp(keydesc2->k2_part + j, kp2->k2_part + j) != 0)
146                     break;
147             }   
148             
149             if (j == nparts)
150                 return (kp2);
151         }
152     }  
153     
154     return ((struct keydesc2 *) 0);          /* Key descriptor not found */
155 }
156
157 /*
158  * pkeydesc2 = _isfcb_altkeydel(fcb, keydesc2)
159  *
160  * Delete key descriptor from FCB.
161  *
162  */
163
164 int
165 _isfcb_altkeydel(fcb, keydesc2)
166     register Fcb        *fcb;
167     Keydesc2            *keydesc2;
168 {
169     int                 nkeys = fcb->nkeys;
170     register int        i, j;
171     register Keydesc2   *kp2;
172     int                 nparts;
173
174     for (i = 0; i < nkeys; i++) {
175         kp2 = fcb->keys + i;
176
177         if (keydesc2->k2_nparts == kp2->k2_nparts) {
178             
179             nparts = keydesc2->k2_nparts;
180             for (j = 0; j < nparts; j++) {
181                 if (_keypart2cmp(keydesc2->k2_part + j, kp2->k2_part + j) != 0)
182                     break;
183             }   
184             
185             if (j == nparts)
186                 break;                       /* Key found */
187         }
188     }  
189
190     if (i >= nkeys)
191         return (EBADKEY);                    /* Key descriptor not found */
192
193     if (i == 0) {
194         return (EPRIMKEY);                   /* Cannot delete primary key */
195     }
196     
197     /*
198      * Shift the end of the table toward the beginning to delete the entry.
199      */
200     if (i < nkeys - 1) {
201         memcpy( (char *)(fcb->keys + i),(char *)(fcb->keys + i + 1), 
202               (nkeys - 1 - i) * sizeof (fcb->keys[0]));
203     }
204
205     fcb->nkeys--;
206
207     return (ISOK);
208 }
209
210 /* compare key parts */
211 Static int
212 _keypart2cmp(l,r)
213     register struct keypart2 *l, *r;
214 {
215     return !(l->kp2_type == r->kp2_type && l->kp2_start == r->kp2_start &&
216              l->kp2_leng == r->kp2_leng);
217 }
218
219 /*
220  * pkeydesc2 = _isfcb_indfindkey(fcb, keyind)
221  *
222  * Find key descriptor by its keyind value.
223  *
224  */
225
226 Keydesc2 *
227 _isfcb_indfindkey(fcb, keyid)
228     register Fcb        *fcb;
229     int                 keyid;
230 {
231     int                 nkeys = fcb->nkeys;
232     register Keydesc2   *keys = fcb->keys;
233     register int        i;
234     
235     for (i = 0; i < nkeys; i++) {
236         if (keys[i].k2_keyid == keyid)
237             break;
238     }  
239     
240     return ((i == nkeys) ? NULL : keys + i);
241 }