home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 9.ddi / TVSRC.ZIP / TSTRLIST.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  3.6 KB  |  156 lines

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