iskeycmp.c 3.9 KB

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