home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / TVDEMOS.ZIP / DATACOLL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  3.1 KB  |  132 lines

  1. /*-------------------------------------------------------*/
  2. /*                                                       */
  3. /*   Turbo Vision 1.0                                    */
  4. /*   Turbo Vision Forms Demo                             */
  5. /*   Copyright (c) 1991 by Borland International         */
  6. /*                                                       */
  7. /*   Datacoll.cpp: Support source file for TVFORMS demo  */
  8. /*-------------------------------------------------------*/
  9.  
  10. #define Uses_TStreamableClass
  11. #define Uses_TStringCollection
  12. #include <tv.h>
  13. __link( RStringCollection )
  14.  
  15. #if !defined( __DATACOLL_H )
  16. #include "datacoll.h"
  17. #endif  // __DATACOLL_H
  18.  
  19. #if !defined( __STRING_H )
  20. #include <string.h>
  21. #endif  // __STRING_H
  22.  
  23. const char * const TDataCollection::name = "TDataCollection";
  24.  
  25. void TDataCollection::write( opstream& os )
  26. {
  27.     os << itemSize;
  28.     TStringCollection::write(os);
  29.     int temp = int(keyType);
  30.     os << temp;
  31. }
  32.  
  33. void *TDataCollection::read( ipstream& is )
  34. {
  35.     is >> itemSize;
  36.     TStringCollection::read( is );
  37.     int temp;
  38.     is >> temp;
  39.     keyType = KeyTypes(temp);
  40.     status = 0;
  41.     return this;
  42. }
  43.  
  44. TStreamable *TDataCollection::build()
  45. {
  46.     return new TDataCollection( streamableInit );
  47. }
  48.  
  49. void TDataCollection::writeItem( void *obj, opstream& os )
  50. {
  51.     os.writeBytes( obj, itemSize );
  52. }
  53.  
  54. void *TDataCollection::readItem( ipstream& is )
  55. {
  56.     void *obj;
  57.  
  58.     obj = new char[itemSize];
  59.     is.readBytes(obj, itemSize);
  60.     return obj;
  61. }
  62.  
  63. TStreamableClass RDataCollection( TDataCollection::name,
  64.                                   TDataCollection::build,
  65.                                   __DELTA(TDataCollection)
  66.                                 );
  67.  
  68. TDataCollection::TDataCollection( int aLimit, int aDelta,
  69.                  int anItemSize,
  70.                  KeyTypes aKeyType) :
  71.     TStringCollection(aLimit, aDelta),
  72.     itemSize( anItemSize ),
  73.     keyType( aKeyType )
  74. {
  75. }
  76.  
  77. int TDataCollection::compare( void *key1, void *key2 )
  78. {
  79.  
  80.     if (keyType == stringKey)
  81.         return stricmp((char*)key1, (char*) key2);
  82.     else 
  83.         {
  84.         if (*(long *)key1 < *(long *)key2)
  85.             return -1;
  86.         else if (*(long *)key1 == *(long *)key2)
  87.             return 0;
  88.         else
  89.             return 1;
  90.         }
  91. }
  92.  
  93. void TDataCollection::error( int code )
  94. // Save error status instead of giving a runtime error
  95. {
  96.     status = code;
  97. }
  98.  
  99. void TDataCollection::freeItem( void *item )
  100. {
  101.     if (item != NULL) 
  102.         delete (item);   
  103. }
  104.  
  105. void TDataCollection::setLimit( int aLimit )
  106. {
  107.     void **aItems;
  108.  
  109.     if (aLimit < count)
  110.     aLimit = count;
  111.     if (aLimit > maxCollectionSize)
  112.     aLimit = maxCollectionSize;
  113.     if (aLimit != limit)
  114.     {
  115.     if (aLimit == 0)
  116.         aItems = NULL;
  117.     else
  118.         {
  119.         // Restrict collection: don't allow it to eat into safety pool.
  120.         // Requires careful checking for success at point of insertion.
  121.         aItems = new void *[aLimit];
  122.         if (count != 0)
  123.         memcpy(aItems, items, count * sizeof(void *));
  124.         }
  125.     if (limit != 0)
  126.         delete (items); 
  127.     items = aItems;
  128.     limit = aLimit;
  129.     }
  130. }
  131.  
  132.