home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Internet / SLCyUtil.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  4.2 KB  |  164 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                SLCyUtil.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //    Cyberdog Utilities
  9. //
  10. //========================================================================================
  11.  
  12. #ifndef FWFRAMEW_HPP
  13. #include "FWFrameW.hpp"
  14. #endif
  15.  
  16. #ifndef SLCYUTIL_H
  17. #include "SLCyUtil.h"
  18. #endif
  19.  
  20. #ifndef FWMEMMGR_H
  21. #include "FWMemMgr.h"
  22. #endif
  23.  
  24. #ifndef SOM_FW_OSink_xh
  25. #include "SLASinks.xh"
  26. #endif
  27.  
  28. #ifndef FWSTREAM_H
  29. #include "FWStream.h"
  30. #endif
  31.  
  32. //
  33. // Cyberdog
  34. //
  35.  
  36. #ifndef __CYBERDOG__
  37. #include <Cyberdog.h>
  38. #endif
  39.  
  40. #ifndef SOM_CyberItem_xh
  41. #include <CyberItem.xh>
  42. #endif
  43.  
  44. #ifndef SOM_CyberSession_xh
  45. #include <CyberSession.xh>
  46. #endif
  47.  
  48. //========================================================================================
  49. // Class FW_CAcquiredMemory
  50. //========================================================================================
  51. #pragma mark Class FW_CAcquiredMemory
  52.  
  53. class FW_CAcquiredMemory {
  54. public:
  55.     FW_DECLARE_AUTO (FW_CAcquiredMemory)
  56.                     FW_CAcquiredMemory (void* memory)        :fMemory(memory){}
  57.                     ~FW_CAcquiredMemory ();
  58.     void*             operator & ()                         { return fMemory; }
  59. private:
  60.     void*             fMemory;
  61. };
  62.  
  63. //-----------------------------------------------------------------------------
  64. // FW_CAcquiredMemory Runtime Information
  65. //-----------------------------------------------------------------------------
  66.  
  67. FW_DEFINE_AUTO (FW_CAcquiredMemory)
  68.  
  69. //-----------------------------------------------------------------------------
  70. // FW_CAcquiredMemory::~FW_CAcquiredMemory
  71. //-----------------------------------------------------------------------------
  72.  
  73. FW_CAcquiredMemory::~FW_CAcquiredMemory ()
  74. {
  75.     if (fMemory)
  76.         FW_CMemoryManager::FreeBlock (fMemory);
  77. }
  78.  
  79. #pragma mark -
  80. //-----------------------------------------------------------------------------
  81. // FW_CyberdogIsInstalled
  82. //-----------------------------------------------------------------------------
  83.  
  84. FW_Boolean FW_CyberdogIsInstalled (Environment* ev)
  85. {
  86.     FW_UNUSED (ev);
  87.     
  88.     return (&GetCyberSession != 0);
  89. }
  90.  
  91. /*
  92.     Utilities for getting CyberItems into and out of ODF streams.
  93.     
  94.     Format of a CyberItem:
  95.     
  96.     long     length         (includes this field)
  97.     short     signature    ('cy')
  98.     short    version        
  99.     long     classid length
  100.     N        classid
  101.     <private data>
  102. */
  103.  
  104. //-----------------------------------------------------------------------------
  105. //    FW_WriteCyberItem
  106. //-----------------------------------------------------------------------------
  107.  
  108. extern "C"
  109. void FW_WriteCyberItem (Environment* ev, CyberItem* ci, FW_OSink* sink)
  110. {
  111.     long size = ci->GetFlatSize(ev);
  112.     FW_CAcquiredMemory tbuffer = FW_CMemoryManager::AllocateBlock (size);
  113.     char* buffer = (char*) &tbuffer;
  114.     size = ci->Flatten (ev, buffer, size);
  115.     sink->Write (ev, buffer, size);
  116. }
  117.  
  118. //-----------------------------------------------------------------------------
  119. //    FW_ReadCyberItem
  120. //-----------------------------------------------------------------------------
  121.  
  122. extern "C"
  123. CyberItem* FW_ReadCyberItem (Environment* ev, CyberSession* session, FW_OSink* sink)
  124. {
  125.     // First we read in the classname to create the CyberItem
  126.     FW_CReadableStream header (sink);
  127.     long size, classsize;
  128.     short signature, version;
  129.     header >> size >> signature >> version >> classsize;
  130.     if (signature != 'cy')
  131.         FW_Failure (FW_xCorruptArchiveStream);
  132.     
  133.     FW_CAcquiredMemory tclassname = FW_CMemoryManager::AllocateBlock (classsize+1);
  134.     char* classname = (char*) &tclassname;
  135.     header.Read (classname, classsize);
  136.     classname[classsize] = 0;
  137.     
  138.     // Create a buffer with the CyberItem header and data
  139.     FW_CAcquiredMemory tbuffer = FW_CMemoryManager::AllocateBlock (size);
  140.     char* buffer = (char*) &tbuffer;
  141.     FW_PMemorySink copysink (ev, buffer, size, 0);
  142.     FW_CWritableStream copy (copysink);
  143.     copy << size << signature << version << classsize;
  144.     copy.Write (classname, classsize);
  145.     long offset = copysink->GetPosition(ev);
  146.     sink->Read (ev, buffer + offset, size - offset);
  147.     
  148.     // Finally create the CyberItem and read it from the memory buffer. 
  149.     CyberItem* ci = session->NewCyberItem (ev, classname);
  150.     FW_TRY {
  151.         ci->ICyberItem (ev);
  152.         ci->Unflatten (ev, buffer);
  153.     }
  154.     FW_CATCH_BEGIN
  155.     FW_CATCH_EVERYTHING() {
  156.         delete ci;
  157.         FW_THROW_SAME();
  158.     }
  159.     FW_CATCH_END
  160.     
  161.     return ci;
  162. }
  163.  
  164.