isfcbindex.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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: isfcbindex.c /main/3 1995/10/23 11:38:43 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * isfcbindex.c
  33. *
  34. * Description:
  35. * Functions that deal with the key descriptors in FCB
  36. *
  37. *
  38. */
  39. #include "isam_impl.h"
  40. Static int _keypart2cmp();
  41. /*
  42. * _isfcb_primkeyadd(fcb, keydesc2)
  43. *
  44. * Add priamry key descriptor to FCB.
  45. *
  46. */
  47. int
  48. _isfcb_primkeyadd(Fcb *fcb, Keydesc2 *keydesc2)
  49. {
  50. /*
  51. * Assign keyid.
  52. */
  53. keydesc2->k2_keyid = ++fcb->lastkeyid;
  54. fcb->keys[0] = *keydesc2;
  55. return (ISOK);
  56. }
  57. /*
  58. * _isfcb_primkeyel(fcb)
  59. *
  60. * Delete primary key
  61. *
  62. */
  63. int
  64. _isfcb_primkeydel(Fcb *fcb)
  65. {
  66. if (FCB_NOPRIMARY_KEY(fcb))
  67. return (EBADARG);
  68. memset((char *)&fcb->keys[0], 0, sizeof(fcb->keys[0]));
  69. return (ISOK);
  70. }
  71. /*
  72. * _isfcb_altkeyadd(fcb, keydesc2)
  73. *
  74. * Add alternate key descriptor to FCB.
  75. *
  76. */
  77. int
  78. _isfcb_altkeyadd(Fcb *fcb, Keydesc2 *keydesc2)
  79. {
  80. assert (fcb->nkeys < MAXNKEYS);
  81. /*
  82. * Assign keyid.
  83. */
  84. keydesc2->k2_keyid = ++fcb->lastkeyid;
  85. /*
  86. * Reallocate fcb->keys table.
  87. */
  88. fcb->keys = (Keydesc2 *)
  89. _isrealloc((char *)fcb->keys,
  90. (unsigned) (sizeof(Keydesc2) * (fcb->nkeys + 1)));
  91. fcb->keys[fcb->nkeys] = *keydesc2;
  92. fcb->nkeys++;
  93. return (ISOK);
  94. }
  95. /*
  96. * pkeydesc2 = _isfcb_findkey(fcb, keydesc2)
  97. *
  98. * Find key descriptor.
  99. *
  100. */
  101. Keydesc2 *
  102. _isfcb_findkey(Fcb *fcb, Keydesc2 *keydesc2)
  103. {
  104. int nkeys = fcb->nkeys;
  105. Keydesc2 *kp2;
  106. int j, i;
  107. int nparts;
  108. for (i = 0; i < nkeys; i++) {
  109. kp2 = fcb->keys + i;
  110. if (keydesc2->k2_nparts == kp2->k2_nparts) {
  111. nparts = keydesc2->k2_nparts;
  112. for (j = 0; j < nparts; j++) {
  113. if (_keypart2cmp(keydesc2->k2_part + j, kp2->k2_part + j) != 0)
  114. break;
  115. }
  116. if (j == nparts)
  117. return (kp2);
  118. }
  119. }
  120. return ((struct keydesc2 *) 0); /* Key descriptor not found */
  121. }
  122. /*
  123. * pkeydesc2 = _isfcb_altkeydel(fcb, keydesc2)
  124. *
  125. * Delete key descriptor from FCB.
  126. *
  127. */
  128. int
  129. _isfcb_altkeydel(Fcb *fcb, Keydesc2 *keydesc2)
  130. {
  131. int nkeys = fcb->nkeys;
  132. int i, j;
  133. Keydesc2 *kp2;
  134. int nparts;
  135. for (i = 0; i < nkeys; i++) {
  136. kp2 = fcb->keys + i;
  137. if (keydesc2->k2_nparts == kp2->k2_nparts) {
  138. nparts = keydesc2->k2_nparts;
  139. for (j = 0; j < nparts; j++) {
  140. if (_keypart2cmp(keydesc2->k2_part + j, kp2->k2_part + j) != 0)
  141. break;
  142. }
  143. if (j == nparts)
  144. break; /* Key found */
  145. }
  146. }
  147. if (i >= nkeys)
  148. return (EBADKEY); /* Key descriptor not found */
  149. if (i == 0) {
  150. return (EPRIMKEY); /* Cannot delete primary key */
  151. }
  152. /*
  153. * Shift the end of the table toward the beginning to delete the entry.
  154. */
  155. if (i < nkeys - 1) {
  156. memcpy( (char *)(fcb->keys + i),(char *)(fcb->keys + i + 1),
  157. (nkeys - 1 - i) * sizeof (fcb->keys[0]));
  158. }
  159. fcb->nkeys--;
  160. return (ISOK);
  161. }
  162. /* compare key parts */
  163. Static int
  164. _keypart2cmp(struct keypart2 *l, struct keypart2 *r)
  165. {
  166. return !(l->kp2_type == r->kp2_type && l->kp2_start == r->kp2_start &&
  167. l->kp2_leng == r->kp2_leng);
  168. }
  169. /*
  170. * pkeydesc2 = _isfcb_indfindkey(fcb, keyind)
  171. *
  172. * Find key descriptor by its keyind value.
  173. *
  174. */
  175. Keydesc2 *
  176. _isfcb_indfindkey(Fcb *fcb, int keyid)
  177. {
  178. int nkeys = fcb->nkeys;
  179. Keydesc2 *keys = fcb->keys;
  180. int i;
  181. for (i = 0; i < nkeys; i++) {
  182. if (keys[i].k2_keyid == keyid)
  183. break;
  184. }
  185. return ((i == nkeys) ? NULL : keys + i);
  186. }