Merge branch 'master' into cde-next
[oweals/cde.git] / cde / lib / tt / mini_isam / iskeycmp.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: iskeycmp.c /main/3 1995/10/23 11:41:41 rswiston $                                                    */
28
29 /*
30  * Copyright (c) 1988 by Sun Microsystems, Inc.
31  */
32
33 /*
34  * iskeycmp.c
35  *
36  * Description:
37  *      ISAM index comparison functions
38  */
39
40 #include "isam_impl.h"
41
42 static struct keypart2 *_curtab;                     /* Current comparison */
43                                              /* descriptor table */
44 static int _ncurtab;                         /* Number of entries */
45
46 /*
47  * _iskeycmp_set()
48  *
49  * Set key decriptor and number of parts for subsequent key comparison.s
50  * nparts, Use only so many parts
51  */
52
53 void
54 _iskeycmp_set (Keydesc2 *pkeydesc2, int nparts)
55 {
56     _ncurtab = nparts;
57     _curtab = pkeydesc2->k2_part;
58     assert(_ncurtab <= pkeydesc2->k2_nparts + 1); /* + 1 for recno */
59 }
60
61 /*
62  * Return number that is > 0 if l > r,
63  *                       = 0 if l = r,
64  *                       < 0 if l < r.
65  */
66
67 int
68 _iskeycmp(char *lkey, char *rkey)
69 {
70     int                      i, ret;
71     struct keypart2 *p;
72       char           *l, *r;
73     long             llong, rlong;
74     double                   ldouble, rdouble;
75
76     ret = 0;
77     for (i = 0, p = _curtab; ret == 0 && i < _ncurtab;i++, p++) {
78         
79         l = lkey + p->kp2_offset;
80         r = rkey + p->kp2_offset;
81
82         switch (p->kp2_type) {
83         case CHARTYPE:
84         case BINTYPE:
85             ret = memcmp(l, r, p->kp2_leng);
86             break;
87
88         case LONGTYPE:
89             llong = ldlong(l);
90             rlong = ldlong(r);
91
92             if (llong > rlong)
93                 ret = 1;
94             else if (llong < rlong)
95                 ret = -1;
96             break;
97
98         case SHORTTYPE:
99             llong = (long)ldshort(l);
100             rlong = (long)ldshort(r);
101
102             if (llong > rlong)
103                 ret = 1;
104             else if (llong < rlong)
105                 ret = -1;
106             break;
107
108         case DOUBLETYPE:
109             ldouble = lddbl(l);
110             rdouble = lddbl(r);
111
112             if (ldouble > rdouble)
113                 ret = 1;
114             else if (ldouble < rdouble)
115                 ret = -1;
116             break;
117
118         case FLOATTYPE:
119             ldouble = (double)ldfloat(l);
120             rdouble = (double)ldfloat(r);
121
122             if (ldouble > rdouble)
123                 ret = 1;
124             else if (ldouble < rdouble)
125                 ret = -1;
126             break;
127
128         case CHARTYPE + ISDESC:
129         case BINTYPE + ISDESC:
130             ret = memcmp(r, l, p->kp2_leng);
131             break;
132
133         case LONGTYPE + ISDESC:
134             llong = ldlong(l);
135             rlong = ldlong(r);
136
137             if (llong > rlong)
138                 ret = -1;
139             else if (llong < rlong)
140                 ret = 1;
141             break;
142
143         case SHORTTYPE + ISDESC:
144             llong = (long)ldshort(l);
145             rlong = (long)ldshort(r);
146
147             if (llong > rlong)
148                 ret = -1;
149             else if (llong < rlong)
150                 ret = 1;
151             break;
152
153         case DOUBLETYPE + ISDESC:
154             ldouble = lddbl(l);
155             rdouble = lddbl(r);
156
157             if (ldouble > rdouble)
158                 ret = -1;
159             else if (ldouble < rdouble)
160                 ret = 1;
161             break;
162
163         case FLOATTYPE + ISDESC:
164             ldouble = (double)ldfloat(l);
165             rdouble = (double)ldfloat(r);
166
167             if (ldouble > rdouble)
168                 ret = -1;
169             else if (ldouble < rdouble)
170                 ret = 1;
171             break;
172
173         default:
174             _isfatal_error("Bad data conversion descriptor");
175             break;
176         }
177     }
178     return (ret);
179 }