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