home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / db02_src.zip / myobj.cc < prev    next >
C/C++ Source or Header  |  1993-11-05  |  3KB  |  160 lines

  1. /**************************************************************************
  2.  * Source Id :
  3.  *
  4.  * $Id: myobj.cc,v 1.9 1993/05/26 01:01:39 kevinl Exp $
  5.  *-------------------------------------------------------------------------
  6.  * Project Notes :
  7.  *
  8.  *  Diamond Base
  9.  *  ============
  10.  *      A solid database implementation, spurred on by the continuing
  11.  *  Metal (Lead) Base saga.
  12.  *
  13.  *  Project Team :
  14.  *        A. Davison
  15.  *        K. Lentin
  16.  *        D. Platt
  17.  *
  18.  *    Project Commenced : 05-02-1993
  19.  *
  20.  *-------------------------------------------------------------------------
  21.  *  Module Notes :
  22.  *
  23.  *    My test object class.
  24.  *
  25.  *
  26.  *  Original Author : Krazy Kev
  27.  *
  28.  *-------------------------------------------------------------------------
  29.  * Revision History:
  30.  *
  31.  * $Log: myobj.cc,v $
  32. // Revision 1.9  1993/05/26  01:01:39  kevinl
  33. // MALLOC_H_MISSING
  34. //
  35. // Revision 1.8  1993/03/29  08:20:09  darrenp
  36. // Added new malloc library support
  37. //
  38. // Revision 1.7  1993/03/20  14:12:16  kevinl
  39. // Added a char* cast for sdcanf and some {} in switch
  40. //
  41. // Revision 1.6  1993/03/18  04:38:28  kevinl
  42. // Simplified long comparison in compare()
  43. //
  44. // Revision 1.5  1993/03/17  14:33:10  kevinl
  45. // Added in second key.
  46. // Rewrote compare, getKey, getKeyLength
  47. // Removed isEqual and isLessThan
  48. //
  49. // Revision 1.4  1993/02/18  22:28:30  davison
  50. // Added numKey and default return values
  51. //
  52. // Revision 1.3  1993/02/17  12:16:43  kevinl
  53. // Added isEqual
  54. // fixed setKey
  55. //
  56. // Revision 1.2  1993/02/13  00:56:44  kevinl
  57. // added setKey and new constructor
  58. //
  59. // Revision 1.1  1993/02/08  14:41:47  darrenp
  60. // Initial revision
  61. //
  62.  **************************************************************************/
  63.  
  64. #include "myobj.h"
  65. #include <iostream.h>
  66. #include <stdlib.h>
  67. #include <string.h>
  68. #include <stdio.h>
  69. #ifndef MALLOC_H_MISSING
  70. #include <malloc.h>
  71. #endif
  72.  
  73. myObject::myObject() : object()
  74. {
  75.     theKey = rand();
  76.     strcpy(theData, "HelloWorld");
  77. }
  78.  
  79. myObject::myObject(pKeyType newKey, long numKey) : object()
  80. {
  81.     setKey(newKey, numKey);
  82. }
  83.  
  84. void myObject::setKey(pKeyType newKey, long keyNum)
  85. {
  86.     switch (keyNum)
  87.     {
  88.         case longKey:
  89.             bcopy(newKey, &theKey, sizeof(theKey));
  90.             break;
  91.         case charKey:
  92.             sscanf((char*)newKey, "%x", &theKey);
  93.             break;
  94.     }
  95. }
  96.  
  97. long myObject::getKeyLength(long keyNum)
  98. {
  99.     switch (keyNum)
  100.     {
  101.         case longKey:
  102.             return sizeof(theKey);
  103.             break;
  104.         case charKey:
  105.             return 10;
  106.             break;
  107.         default:
  108.             cerr << "getKeyLength AAGGHH" << endl;
  109.             exit (-1);
  110. //            return -1;
  111.     }
  112.     return -1;
  113. }
  114.  
  115. pKeyType myObject::getKey(long keyNum)
  116. {
  117.  
  118.     switch (keyNum)
  119.     {
  120.         case longKey:
  121.         {
  122.             long* copy = new long(theKey);
  123.             return (pKeyType)(copy);
  124.         }
  125.         case charKey:
  126.         {
  127.             char* key = new char[10];
  128.             sprintf(key, "%lx", theKey);
  129.             return (pKeyType)(key);
  130.         }
  131.         default:
  132.             cerr << "getKey AAGGHH" << endl;
  133.             exit (-1);
  134. //            return (pKeyType)0;
  135.     }
  136.     return (pKeyType)0;
  137. }
  138.  
  139. int myObject::compare(pKeyType key, long keyNum)
  140. {
  141.     char* myKey = (char*)getKey(keyNum);
  142.     int res;
  143.  
  144.     switch (keyNum)
  145.     {
  146.         case longKey:
  147.             res = (theKey - *(long*)key);
  148.             break;
  149.         case charKey:
  150.             res = strcmp(myKey, (char*)key);
  151.             break;
  152.         default:
  153.             cerr << "compare AAGGHH" << endl;
  154.             exit (-1);
  155.     }
  156.  
  157.     delete myKey;
  158.     return res;
  159. }
  160.