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