d734c67d02505bf2a20ff7783293ce0b37158232
[oweals/cde.git] / cde / lib / tt / mini_isam / iskeyconv.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: iskeyconv.c /main/3 1995/10/23 11:41:54 rswiston $                                                   */
28 #ifndef lint
29 static char sccsid[] = "@(#)iskeyconv.c 1.4 89/07/17 Copyr 1988 Sun Micro";
30 #endif
31
32 /*
33  * Copyright (c) 1988 by Sun Microsystems, Inc.
34  */
35
36 /*
37  * iskeyconv.c
38  *
39  * Description:
40  *      Conversion functions between internal and external key descriptor
41  *      
42  */
43
44 #include "isam_impl.h"
45
46 /* 
47  * _iskey_itox(pikdesc,pxkdesc) 
48  *
49  * Convert internal key desc. to X/OPEN key descriptor. 
50  *
51  * @param pikdesc NetISAM internal format
52  * @param pxkdesc X/OPEN format
53  */
54 void
55 _iskey_itox(struct keydesc2 *pikdesc, struct keydesc *pxkdesc)
56 {
57     int                 nparts;
58     int         i;
59
60     memset ((char *)pxkdesc, 0, sizeof (*pxkdesc));
61
62     pxkdesc->k_flags = pikdesc->k2_flags;
63     nparts = pxkdesc->k_nparts = pikdesc->k2_nparts;
64
65     for (i = 0; i < nparts;i++) {
66         pxkdesc->k_part[i].kp_start = pikdesc->k2_part[i].kp2_start;
67         pxkdesc->k_part[i].kp_leng = pikdesc->k2_part[i].kp2_leng;
68         pxkdesc->k_part[i].kp_type = pikdesc->k2_part[i].kp2_type;
69     }
70 }
71
72
73 /* 
74  * _iskey_xtoi()
75  *
76  * Convert X/OPEN key descriptor to internal key descriptor.
77  *
78  * @param pikdesc NetISAM internal format
79  * @param pxkdesc X/OPEN format
80  */
81 void
82 _iskey_xtoi(struct keydesc2 *pikdesc, struct keydesc *pxkdesc)
83 {
84     int                 nparts;
85     int         i;
86     int                 offset;              /* Keep track of offset in key */
87
88     memset ((char *)pikdesc, 0, sizeof (*pikdesc));
89
90     pikdesc->k2_flags = pxkdesc->k_flags;
91     nparts = pikdesc->k2_nparts = pxkdesc->k_nparts;
92
93     offset = 0;
94     /*
95      * Every key entry starts with record number.
96      */
97     offset += RECNOSIZE;
98
99     /*
100      * If index allows duplicates, the key is augmented with duplicate
101      * serial number.
102      */
103     if ((pxkdesc->k_flags & DUPSMASK) == ISDUPS)
104         offset += DUPIDSIZE;
105
106     for (i = 0; i < nparts; i++) {
107         pikdesc->k2_part[i].kp2_start = pxkdesc->k_part[i].kp_start;
108         pikdesc->k2_part[i].kp2_leng = pxkdesc->k_part[i].kp_leng;
109         pikdesc->k2_part[i].kp2_type = pxkdesc->k_part[i].kp_type;
110         pikdesc->k2_part[i].kp2_offset = offset;
111
112         offset += pxkdesc->k_part[i].kp_leng;
113     }
114
115     /* Append recno to key descriptors. */
116     pikdesc->k2_part[i].kp2_start = 0;       /* not used */
117     pikdesc->k2_part[i].kp2_leng = RECNOSIZE;
118     pikdesc->k2_part[i].kp2_type = RECNOTYPE;
119     pikdesc->k2_part[i].kp2_offset = KEY_RECNO_OFF; /* at the beginning of key */
120     
121     if ((pxkdesc->k_flags & DUPSMASK) == ISDUPS) {
122         /* Append duplicate serial number to key descriptors. */
123         pikdesc->k2_part[i].kp2_start = 0;   /* not used */
124         pikdesc->k2_part[i].kp2_leng = DUPIDSIZE;
125         pikdesc->k2_part[i].kp2_type = DUPIDTYPE;
126         pikdesc->k2_part[i].kp2_offset = KEY_DUPS_OFF; /* after recno field */
127     }
128
129     /* Round up to next multiple of 2. */
130     offset = (offset+1) & ~1;
131     
132     pikdesc->k2_len = offset;                /* Length of the entire key */
133 }