home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20os2.zip / src / TCollection.cpp < prev    next >
C/C++ Source or Header  |  1999-06-03  |  4KB  |  234 lines

  1. /*
  2.  * TCollection.cc
  3.  *
  4.  * Turbo Vision - Version 2.0
  5.  *
  6.  * Copyright (c) 1994 by Borland International
  7.  * All Rights Reserved.
  8.  *
  9.  * Modified by Sergio Sigala <ssigala@globalnet.it>
  10.  */
  11.  
  12. #define Uses_TNSCollection
  13. #define Uses_opstream
  14. #define Uses_ipstream
  15. #define Uses_TCollection
  16. #include <tvision/tv.h>
  17.  
  18. #include <stdlib.h>
  19. #include <string.h>
  20.  
  21. TNSCollection::TNSCollection( ccIndex aLimit, ccIndex aDelta ) :
  22.     count( 0 ),
  23.     items( 0 ),
  24.     limit( 0 ),
  25.     delta( aDelta ),
  26.     shouldDelete( True )
  27. {
  28.     setLimit( aLimit );
  29. }
  30.  
  31. TNSCollection::TNSCollection() :
  32.     count( 0 ),
  33.     items( 0 ),
  34.     limit( 0 ),
  35.     delta( 0 ),
  36.     shouldDelete( True )
  37. {
  38. }
  39.  
  40. TNSCollection::~TNSCollection()
  41. {
  42.     delete items;
  43. }
  44.  
  45. void TNSCollection::shutDown()
  46. {
  47.     if( shouldDelete )
  48.         freeAll();
  49.     else
  50.         removeAll();
  51.     setLimit(0);
  52.     TObject::shutDown();
  53. }
  54.  
  55. void *TNSCollection::at( ccIndex index )
  56. {
  57.     if( index < 0 || index >= count ) return NULL;
  58. //        error(1,0); // by Orlik
  59.     return items[index];
  60. }
  61.  
  62. void TNSCollection::atRemove( ccIndex index )
  63. {
  64.     if( index < 0 || index >= count ) return;
  65. //        error(1,0);
  66.  
  67.     count--;
  68.     memmove( &items[index], &items[index+1], (count-index)*sizeof(void *) );
  69. }
  70.  
  71. void TNSCollection::atFree( ccIndex index )
  72. {
  73.     void *item = at( index );
  74.     atRemove( index );
  75.     freeItem( item );
  76. }
  77.  
  78. void TNSCollection::atInsert(ccIndex index, void *item)
  79. {
  80.     if( index < 0 ) return;
  81. //        error(1,0);
  82.     if( count == limit )
  83.         setLimit(count + delta);
  84.  
  85.     memmove( &items[index+1], &items[index], (count-index)*sizeof(void *) );
  86.     count++;
  87.  
  88.     items[index] = item;
  89. }
  90.  
  91. void TNSCollection::atPut( ccIndex index, void *item )
  92. {
  93.     if( index >= count ) return;
  94. //        error(1,0);
  95.  
  96.     items[index] = item;
  97. }
  98.  
  99. void TNSCollection::remove( void *item )
  100. {
  101.     atRemove( indexOf(item) );
  102. }
  103.  
  104. void TNSCollection::removeAll()
  105. {
  106.     count = 0;
  107. }
  108.  
  109. void TNSCollection::error( ccIndex code, ccIndex )
  110. {
  111. //    exit(212 - code);
  112. }
  113.  
  114. void *TNSCollection::firstThat( ccTestFunc Test, void *arg )
  115. {
  116.     for( ccIndex i = 0; i < count; i++ )
  117.         {
  118.         if( Test( items[i], arg ) == True )
  119.             return items[i];
  120.         }
  121.     return 0;
  122. }
  123.  
  124. void *TNSCollection::lastThat( ccTestFunc Test, void *arg )
  125. {
  126.     for( ccIndex i = count; i > 0; i-- )
  127.         {
  128.         if( Test( items[i-1], arg ) == True )
  129.             return items[i-1];
  130.         }
  131.     return 0;
  132. }
  133.  
  134. void TNSCollection::forEach( ccAppFunc action, void *arg )
  135. {
  136.     for( ccIndex i = 0; i < count; i++ )
  137.         action( items[i], arg );
  138. }
  139.  
  140. void TNSCollection::free( void *item )
  141. {
  142.     remove( item );
  143.     freeItem( item );
  144. }
  145.  
  146. void TNSCollection::freeAll()
  147. {
  148.     for( ccIndex i =  0; i < count; i++ )
  149.         freeItem( at(i) );
  150.     count = 0;
  151. }
  152.  
  153. void TNSCollection::freeItem( void *item )
  154. {
  155.     delete item;
  156. }
  157.  
  158. ccIndex TNSCollection::indexOf(void *item)
  159. {
  160.     for( ccIndex i = 0; i < count; i++ )
  161.         if( item == items[i] )
  162.             return i;
  163.  
  164. //    error(1,0);
  165.     return (ccIndex)-1;    /* XXX */
  166. }
  167.  
  168. ccIndex TNSCollection::insert( void *item )
  169. {
  170.     ccIndex loc = count;
  171.     atInsert( count, item );
  172.     return loc;
  173. }
  174.  
  175. void TNSCollection::pack()
  176. {
  177.     void **curDst = items;
  178.     void **curSrc = items;
  179.     void **last = items + count;
  180.     while( curSrc < last )
  181.         {
  182.         if( *curSrc != 0 )
  183.             *curDst++ = *curSrc;
  184. //        *curSrc++;    /* XXX */
  185.     curSrc++;    /* XXX */
  186.         }
  187. }
  188.  
  189. void TNSCollection::setLimit(ccIndex aLimit)
  190. {
  191.     if( aLimit < count )
  192.         aLimit =  count;
  193.     if( aLimit > maxCollectionSize)
  194.         aLimit = maxCollectionSize;
  195.     if( aLimit != limit )
  196.         {
  197.         void **aItems;
  198.         if (aLimit == 0 )
  199.             aItems = 0;
  200.         else
  201.             {
  202.             aItems = new void *[aLimit];
  203.             if( count != 0 && aItems != 0 && items != 0 )
  204.                 memcpy( aItems, items, count*sizeof(void *) );
  205.             }
  206.         delete items;
  207.         items = aItems;
  208.         limit = aLimit;
  209.         }
  210. }
  211.  
  212.  
  213. void TCollection::write( opstream& os )
  214. {
  215.     os << count << limit << delta;
  216.     for( ccIndex idx = 0; idx < count; idx++ )
  217.         writeItem( items[idx], os );
  218. }
  219.  
  220. void *TCollection::read( ipstream& is )
  221. {
  222.     int savedLimit;
  223.     is >> count >> savedLimit >> delta;
  224.     setLimit(savedLimit);
  225.     for( ccIndex idx = 0; idx < count; idx++ )
  226.         items[idx] = readItem( is );
  227.     return this;
  228. }
  229.  
  230.  
  231. TCollection::TCollection( StreamableInit )
  232. {
  233. }
  234.