home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20cpp.zip / src / TStrListMaker.cpp < prev    next >
C/C++ Source or Header  |  1998-01-19  |  3KB  |  152 lines

  1. /*
  2.  * TStrListMaker.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_TStringList
  13. #define Uses_TStrIndexRec
  14. #define Uses_TStrListMaker
  15. #define Uses_opstream
  16. #define Uses_ipstream
  17. #include <tvision/tv.h>
  18.  
  19. #include <string.h>
  20.  
  21. const MAXKEYS = 16;
  22.  
  23. TStrIndexRec::TStrIndexRec() :
  24.     count(0)
  25. {
  26. }
  27.  
  28. TStrListMaker::TStrListMaker( ushort aStrSize, ushort aIndexSize ) :
  29.     strSize( aStrSize ),
  30.     indexSize( aIndexSize ),
  31.     strings( new char[aStrSize] ),
  32.     index( new TStrIndexRec[aIndexSize] ),
  33.     strPos( 0 ),
  34.     indexPos( 0 )
  35. {
  36. }
  37.  
  38. TStrListMaker::~TStrListMaker()
  39. {
  40.     delete strings;
  41.  
  42.     /* SS: prevent anachronistic stuff */
  43.  
  44.     delete [] index;
  45. //    delete [indexSize] index;
  46. }
  47.  
  48. void TStrListMaker::closeCurrent()
  49. {
  50.     if( cur.count != 0 )
  51.         {
  52.         index[indexPos++] = cur;
  53.         cur.count = 0;
  54.         }
  55. }
  56.  
  57. void TStrListMaker::put( ushort key, char *str )
  58. {
  59.     if( cur.count == MAXKEYS || key != cur.key + cur.count )
  60.         closeCurrent();
  61.     if( cur.count == 0 )
  62.         {
  63.         cur.key = key;
  64.         cur.offset = strPos;
  65.         }
  66.     int len = strlen( str );
  67.     strings[strPos] = len;
  68.     memcpy( strings+strPos+1, str, len);
  69.     strPos += len+1;
  70.     cur.count++;
  71. }
  72.  
  73. #if !defined(NO_STREAMABLE)
  74.  
  75. TStringList::TStringList( StreamableInit ) :
  76.     indexSize(0),
  77.     index(0),
  78.     basePos(0)
  79. {
  80. }
  81. #endif
  82.  
  83. TStringList::~TStringList()
  84. {
  85.     /* SS: prevent anachronistic stuff */
  86.  
  87.     delete [] index;
  88. //    delete [indexSize] index;
  89. }
  90.  
  91. void TStringList::get( char *dest, ushort key )
  92. {
  93.     if( indexSize == 0 )
  94.         {
  95.         *dest = EOS;
  96.         return;
  97.         }
  98.  
  99.     TStrIndexRec *cur = index;
  100.     while( cur->key + cur->count -1 < key && cur - index < indexSize )
  101.         cur++;
  102. #ifndef __UNPATCHED
  103.     if( cur->key + cur->count - 1 < key || cur->key > key )
  104. #else
  105.     if( cur->key + cur->count - 1 < key )
  106. #endif
  107.         {
  108.         *dest = EOS;
  109.         return;
  110.         }
  111.     ip->seekg( basePos + cur->offset );
  112.     int count = key - cur->key;
  113.     do  {
  114.         uchar sz = ip->readByte();
  115.         ip->readBytes( dest, sz );
  116.         dest[sz] = EOS;
  117.         } while( count-- > 0 );
  118. }
  119.  
  120. #if !defined(NO_STREAMABLE)
  121.  
  122. void TStrListMaker::write( opstream& os )
  123. {
  124.     closeCurrent();
  125.     os << strPos;
  126.     os.writeBytes( strings, strPos );
  127.     os << indexPos;
  128.     os.writeBytes( index, indexPos * sizeof( TStrIndexRec ) );
  129. }
  130.  
  131. void *TStringList::read( ipstream& is )
  132. {
  133.     ip = &is;
  134.  
  135.     ushort strSize;
  136.     is >> strSize;
  137.  
  138.     basePos = is.tellg();
  139.     is.seekg( basePos + strSize );
  140.     is >> indexSize;
  141.     index = new TStrIndexRec[indexSize];
  142.     is.readBytes( index, indexSize * sizeof( TStrIndexRec ) );
  143.     return this;
  144. }
  145.  
  146. TStreamable *TStringList::build()
  147. {
  148.     return new TStringList( streamableInit );
  149. }
  150.  
  151. #endif
  152.