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