2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
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 $ */
29 static char sccsid[] = "@(#)isfcbindex.c 1.3 89/07/17 Copyr 1988 Sun Micro";
32 * Copyright (c) 1988 by Sun Microsystems, Inc.
39 * Functions that deal with the key descriptors in FCB
44 #include "isam_impl.h"
46 Static int _keypart2cmp();
48 * _isfcb_primkeyadd(fcb, keydesc2)
50 * Add priamry key descriptor to FCB.
55 _isfcb_primkeyadd(Fcb *fcb, Keydesc2 *keydesc2)
60 keydesc2->k2_keyid = ++fcb->lastkeyid;
62 fcb->keys[0] = *keydesc2;
68 * _isfcb_primkeyel(fcb)
75 _isfcb_primkeydel(Fcb *fcb)
77 if (FCB_NOPRIMARY_KEY(fcb))
80 memset((char *)&fcb->keys[0], 0, sizeof(fcb->keys[0]));
86 * _isfcb_altkeyadd(fcb, keydesc2)
88 * Add alternate key descriptor to FCB.
93 _isfcb_altkeyadd(Fcb *fcb, Keydesc2 *keydesc2)
95 assert (fcb->nkeys < MAXNKEYS);
100 keydesc2->k2_keyid = ++fcb->lastkeyid;
103 * Reallocate fcb->keys table.
105 fcb->keys = (Keydesc2 *)
106 _isrealloc((char *)fcb->keys,
107 (unsigned) (sizeof(Keydesc2) * (fcb->nkeys + 1)));
109 fcb->keys[fcb->nkeys] = *keydesc2;
117 * pkeydesc2 = _isfcb_findkey(fcb, keydesc2)
119 * Find key descriptor.
124 _isfcb_findkey(Fcb *fcb, Keydesc2 *keydesc2)
126 int nkeys = fcb->nkeys;
131 for (i = 0; i < nkeys; i++) {
134 if (keydesc2->k2_nparts == kp2->k2_nparts) {
136 nparts = keydesc2->k2_nparts;
137 for (j = 0; j < nparts; j++) {
138 if (_keypart2cmp(keydesc2->k2_part + j, kp2->k2_part + j) != 0)
147 return ((struct keydesc2 *) 0); /* Key descriptor not found */
151 * pkeydesc2 = _isfcb_altkeydel(fcb, keydesc2)
153 * Delete key descriptor from FCB.
158 _isfcb_altkeydel(Fcb *fcb, Keydesc2 *keydesc2)
160 int nkeys = fcb->nkeys;
165 for (i = 0; i < nkeys; i++) {
168 if (keydesc2->k2_nparts == kp2->k2_nparts) {
170 nparts = keydesc2->k2_nparts;
171 for (j = 0; j < nparts; j++) {
172 if (_keypart2cmp(keydesc2->k2_part + j, kp2->k2_part + j) != 0)
177 break; /* Key found */
182 return (EBADKEY); /* Key descriptor not found */
185 return (EPRIMKEY); /* Cannot delete primary key */
189 * Shift the end of the table toward the beginning to delete the entry.
192 memcpy( (char *)(fcb->keys + i),(char *)(fcb->keys + i + 1),
193 (nkeys - 1 - i) * sizeof (fcb->keys[0]));
201 /* compare key parts */
203 _keypart2cmp(struct keypart2 *l, struct keypart2 *r)
205 return !(l->kp2_type == r->kp2_type && l->kp2_start == r->kp2_start &&
206 l->kp2_leng == r->kp2_leng);
210 * pkeydesc2 = _isfcb_indfindkey(fcb, keyind)
212 * Find key descriptor by its keyind value.
217 _isfcb_indfindkey(Fcb *fcb, int keyid)
219 int nkeys = fcb->nkeys;
220 Keydesc2 *keys = fcb->keys;
223 for (i = 0; i < nkeys; i++) {
224 if (keys[i].k2_keyid == keyid)
228 return ((i == nkeys) ? NULL : keys + i);