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

  1. /*--------------------------------------------------------------*/
  2. /* filename -       tresfile.cpp                                */
  3. /*                                                              */
  4. /* function(s)                                                  */
  5. /*                  TResourceFile 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_TResourceFile
  19. #define Uses_TResourceItem
  20. #define Uses_fpstream
  21. #define Uses_TCollection
  22. #define Uses_TStringCollection
  23. #define Uses_TResourceCollection
  24. #include <tv.h>
  25.  
  26. #if !defined( __IO_H )
  27. #include <io.h>
  28. #endif  // __IO_H
  29.  
  30. const long rStreamMagic = 0x52504246uL; // 'FBPR'
  31.  
  32. struct Count_type
  33. {
  34.     ushort lastCount;
  35.     ushort pageCount;
  36. };
  37.  
  38. struct Info_type
  39. {
  40.     ushort infoType;
  41.     long infoSize;
  42. };
  43.  
  44. struct THeader
  45. {
  46.     ushort signature;
  47.     union
  48.         {
  49.         Count_type count;
  50.         Info_type info;
  51.         };
  52. };
  53.  
  54.  
  55. TResourceFile::TResourceFile( fpstream *aStream ) : TObject()
  56. {
  57.     THeader *header;
  58.     int handle;
  59.     int found;
  60.     int repeat;
  61.     long streamSize;
  62.  
  63.     stream = aStream;
  64.     basePos = stream->tellp();
  65.     handle = stream->rdbuf()->fd();
  66.     streamSize = filelength(handle);
  67.     header = new THeader;
  68.     found = 0;
  69.     do {
  70.        repeat = 0;
  71.        if (basePos <= (streamSize - sizeof(THeader)))
  72.            {
  73.            stream->seekg(basePos, ios::beg);
  74.            stream->readBytes(header, sizeof(THeader));
  75.            if (header->signature == 0x5a4d)
  76.                {
  77.                basePos += ((header->count.pageCount * 512L) -
  78.                           (-header->count.lastCount & 511));
  79.                repeat = 1;
  80.                }
  81.            else if (header->signature == 0x4246)
  82.                {
  83.                if (header->info.infoType == 0x5250)
  84.                    found = 1;
  85.                else
  86.                    {
  87.                    basePos += 
  88.                       header->info.infoSize + 16 - (header->info.infoSize)%16;
  89.                    repeat = 1;
  90.                    }
  91.                }
  92.            }
  93.         } while (repeat);
  94.  
  95.     if (found)
  96.     {
  97.         stream->seekg(basePos + sizeof(long) * 2, ios::beg);
  98.         *stream >> indexPos;
  99.         stream->seekg(basePos + indexPos, ios::beg);
  100.         *stream >> index;
  101.     }
  102.     else
  103.     {
  104.         indexPos =  sizeof(long) * 3;
  105.         index = new TResourceCollection(0, 8);
  106.     }
  107. }
  108.  
  109. TResourceFile::~TResourceFile()
  110. {
  111.     flush();
  112.     destroy( (TCollection *)index );
  113.     delete stream;
  114. }
  115.  
  116. short TResourceFile::count()
  117. {
  118.     return index->getCount();
  119. }
  120.  
  121. void TResourceFile::remove( const char *key )
  122. {
  123.     int i;
  124.  
  125.     if (index->search( (char *)key, i))
  126.         {
  127.         index->free(index->at(i));
  128.         modified = True;
  129.         }
  130. }
  131.  
  132. void TResourceFile::flush()
  133. {
  134.     long lenRez;
  135.  
  136.     if (modified == True)
  137.     {
  138.         stream->seekg(basePos + indexPos, ios::beg);
  139.         *stream << index;
  140.         lenRez =  stream->tellp() - basePos -  sizeof(long) * 2;
  141.         stream->seekg(basePos, ios::beg);
  142.         *stream << rStreamMagic;
  143.         *stream << lenRez;
  144.         *stream << indexPos;
  145.         stream->flush();
  146.         modified = False;
  147.     }
  148. }
  149.  
  150. void *TResourceFile::get( const char *key)
  151. {
  152.     int i;
  153.     void *p;
  154.  
  155.     if (! index->search((char *)key, i))
  156.         return  0;
  157.     stream->seekg(basePos + ((TResourceItem*)(index->at(i)))->pos, ios::beg);
  158.     *stream >> p;
  159.     return p;
  160. }
  161.  
  162. const char *TResourceFile::keyAt(short i)
  163. {
  164.     return ((TResourceItem*)(index->at(i)))->key;
  165. }
  166.  
  167. void TResourceFile::put(TStreamable *item, const char *key)
  168. {
  169.     int i;
  170.     TResourceItem  *p;
  171.  
  172.     if (index->search( (char *)key, i))
  173.         p = (TResourceItem*)(index->at(i));
  174.     else
  175.     {
  176.         p = new TResourceItem;
  177.         p->key = newStr(key);
  178.         index->atInsert(i, p);
  179.     }
  180.     p->pos =  indexPos;
  181.     stream->seekp(basePos + indexPos, ios::beg);
  182.     *stream << item;
  183.     indexPos = stream->tellp() - basePos;
  184.     p->size  = indexPos - p->pos;
  185.     modified = True;
  186. }
  187.  
  188.