home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / entrykey.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-14  |  5.1 KB  |  144 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- // 
  5. // C++ Source Code File Name: entrykey.cpp 
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer   
  8. // File Creation Date: 02/12/1997 
  9. // Date Last Modified: 03/15/1999
  10. // Copyright (c) 1997 Douglas M. Gaer
  11. // ----------------------------------------------------------- // 
  12. // ------------- Program Description and Details ------------- // 
  13. // ----------------------------------------------------------- // 
  14. /*
  15. The VBD C++ classes are copyright (c) 1997, by Douglas M. Gaer.
  16. All those who put this code or its derivatives in a commercial
  17. product MUST mention this copyright in their documentation for
  18. users of the products in which this code or its derivative
  19. classes are used. Otherwise, you have the freedom to redistribute
  20. verbatim copies of this source code, adapt it to your specific
  21. needs, or improve the code and release your improvements to the
  22. public provided that the modified files carry prominent notices
  23. stating that you changed the files and the date of any change.
  24.  
  25. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  26. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  27. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  28. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  29. CORRECTION.
  30.  
  31. The EntryKey class is used to identify objects in the database
  32. by a unique key name when an index file is used. Each key is
  33. fixed in length by the MAXKEYSIZE constant. An (E)ntry (K)ey
  34. is a key, along with its associated data address and branch
  35. fields.
  36.  
  37. The Multi-way node (E)ntryKey class and (M)ulti-way (n)ode
  38. classes are used to make up (B)alanced multi-way (T)rees.
  39. Each node in the tree will have a left branch that leads to
  40. all nodes with keys smaller than the smallest key. The right
  41. branches will be paried with a key, each branch leading to all
  42. nodes with keys greater than the given key, but less than the
  43. key to the right. 
  44. */
  45. // ----------------------------------------------------------- // 
  46. #include <string.h>
  47. #include "strutil.h"
  48. #include "entrykey.h"
  49.  
  50. EntryKey::EntryKey()
  51. {
  52.   // Set all the bytes of the entry key to zero
  53.   for(int i = 0; i < MAXKEYSIZE; i++) key[i] = 0;
  54. }
  55.  
  56. EntryKey::EntryKey(const EntryKey &s)
  57. {
  58.   for(int i = 0; i < MAXKEYSIZE; i++) key[i] = 0;
  59.   strcpy(key, s.key);
  60.   object_address = s.object_address;
  61.   class_id = s.class_id;
  62.   right = s.right;
  63. }
  64.  
  65. EntryKey::EntryKey(const char *s)
  66. {
  67.   for(int i = 0; i < MAXKEYSIZE; i++) key[i] = 0;
  68.   strncpy(key, s, MAXKEYSIZE); key[MAXKEYSIZE-1] = 0;
  69.   object_address = 0; class_id = 0; right = 0; 
  70. }
  71.  
  72. EntryKey::EntryKey(const char *s, INT32 oa, INT32 ci)
  73. {
  74.   for(int i = 0; i < MAXKEYSIZE; i++) key[i] = 0;
  75.   strncpy(key, s, MAXKEYSIZE); key[MAXKEYSIZE-1] = 0;
  76.   right = 0;
  77.   object_address = oa;
  78.   class_id = ci;
  79. }
  80.  
  81. void EntryKey::operator=(const EntryKey &s)
  82. {
  83.   if (this == &s) return;
  84.   for(int i = 0; i < MAXKEYSIZE; i++) key[i] = 0;
  85.   strcpy(key, s.key);
  86.   right = s.right;
  87.   object_address = s.object_address;
  88.   class_id = s.class_id;
  89. }
  90.  
  91. void EntryKey::operator=(const char *s)
  92. // Assigns the key portion, but no other fields
  93. {
  94.   for(int i = 0; i < MAXKEYSIZE; i++) key[i] = 0;
  95.   strncpy(key, s, MAXKEYSIZE); key[MAXKEYSIZE-1] = 0;
  96. }
  97.  
  98. int Compare(const EntryKey &a, const EntryKey &b)
  99. // Friend function that does a partial compare of a and b.
  100. // Compares only the key string, not the data.
  101. // Returns -1 if a < b, 0 if a == b, or 1 if a > b.
  102. {
  103.   // 02/10/1999: Modified to perform a case-insensitive comparison
  104.   // on the key member to prevent duplicate entries from appearing
  105.   // in the B-tree when objects are identified by their key name only.
  106.   return CaseICmp(a.key, b.key);
  107. }
  108.  
  109. int FullCompare(const EntryKey &a, const EntryKey &b)
  110. // Friend function that does a full compare of a and b.
  111. // Compares the key string as well as the data fields.
  112. // Returns -1 if a < b, 0 if a == b, or 1 if a > b.
  113. {
  114.   // 02/10/1999: Modified to perform a case-insensitive comparison
  115.   // on the key member to keep the B-tree entries in alphabetical
  116.   // order during insertions. As long Each object in the data file 
  117.   // is uniquely indentified by its address the case of the object's 
  118.   // key name is irrelevant during a full compare.
  119.   int rv = CaseICmp(a.key, b.key);
  120.   if (rv < 0) return -1;
  121.   if (rv > 0) return 1;
  122.   if (a.object_address > b.object_address) return 1;
  123.   if (a.object_address < b.object_address) return -1;
  124.   if (a.class_id > b.class_id) return 1;
  125.   if (a.class_id < b.class_id) return -1;
  126.   return 0;
  127. }
  128.  
  129. void EntryKey::SetStrKey(char *s)
  130. {
  131.   strncpy(key, s, MAXKEYSIZE);
  132.   key[MAXKEYSIZE-1] = 0; // Ensure null termination
  133. }  
  134.  
  135. void EntryKey::SetStrKey(const char *s)
  136. {
  137.   strncpy(key, s, MAXKEYSIZE);
  138.   key[MAXKEYSIZE-1] = 0; // Ensure null termination
  139. }  
  140. // ----------------------------------------------------------- //
  141. // ------------------------------- //
  142. // --------- End of File --------- //
  143. // ------------------------------- //
  144.