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