binkey.C 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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: binkey.C /main/3 1995/10/20 16:24:54 rswiston $
  28. /*
  29. *
  30. * binkey.cc
  31. *
  32. * Copyright (c) 1991 by Sun Microsystems, Inc.
  33. */
  34. #include "util/tt_string.h"
  35. #include "db/tt_db_key_utils.h"
  36. #include "binkey.h"
  37. #include <memory.h>
  38. static unsigned char sixteen_zeroes[OID_KEY_LENGTH] = {
  39. 0,0,0,0,
  40. 0,0,0,0,
  41. 0,0,0,0,
  42. 0,0,0,0};
  43. static unsigned char sixteen_foxes[OID_KEY_LENGTH] = {
  44. 255,255,255,255,
  45. 255,255,255,255,
  46. 255,255,255,255,
  47. 255,255,255,255};
  48. Binkey Binkey::smallest(sixteen_zeroes);
  49. Binkey Binkey::largest(sixteen_foxes);
  50. static int binkey_compare(const unsigned char *a, const unsigned char *b);
  51. Binkey::
  52. Binkey()
  53. {
  54. memset((char *)_binkey, 0, sizeof(_binkey));
  55. _key = (_Tt_db_key *)0;
  56. }
  57. Binkey::
  58. Binkey(const unsigned char *k)
  59. {
  60. _Tt_string bks(k, sizeof(_binkey));
  61. memcpy((char *)_binkey, k, sizeof(_binkey));
  62. _key = new _Tt_db_key(bks);
  63. }
  64. Binkey & Binkey::
  65. operator=(const Binkey &k)
  66. {
  67. memcpy((char *)_binkey, (char *)k._binkey, sizeof(_binkey));
  68. _key = new _Tt_db_key(k);
  69. return *this;
  70. }
  71. Binkey & Binkey::
  72. operator=(const unsigned char *k)
  73. {
  74. _Tt_string bks(k, sizeof(_binkey));
  75. memcpy((char *)_binkey, k, sizeof(_binkey));
  76. _key = new _Tt_db_key(bks);
  77. return *this;
  78. }
  79. int
  80. operator==(const Binkey &a, const _Tt_db_key &b)
  81. {
  82. return b==*a._key;
  83. }
  84. int
  85. operator==(const Binkey &a, const Binkey &b)
  86. {
  87. return binkey_compare(a._binkey,b._binkey)==0;
  88. }
  89. int
  90. operator<(const Binkey &a, const Binkey &b)
  91. {
  92. return binkey_compare(a._binkey,b._binkey)<0;
  93. }
  94. int
  95. operator>(const Binkey &a, const Binkey &b)
  96. {
  97. return binkey_compare(a._binkey,b._binkey)>0;
  98. }
  99. static int
  100. binkey_compare(const unsigned char *pa, const unsigned char *pb)
  101. {
  102. // No libc routines seem to guarantee to handle unsigned chars!
  103. int i = OID_KEY_LENGTH;
  104. while (i--) {
  105. if (*pa>*pb) return 1;
  106. if (*pa++<*pb++) return -1;
  107. }
  108. return 0;
  109. }
  110. void Binkey::
  111. print(FILE* f) const
  112. {
  113. if (_key.is_null()) {
  114. fprintf(f,"(null key)");
  115. } else {
  116. _key->print(f);
  117. }
  118. }