home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWResour / Sources / FWPrvRAc.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  5.9 KB  |  215 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:        FWPrvRAc.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef   FWPRVRAC_H
  13. #include "FWPrvRAc.h"
  14. #endif
  15.  
  16. #ifndef   FWSTRS_H
  17. #include "FWStrs.h"
  18. #endif
  19.  
  20. #ifndef   FWFILESP_H
  21. #include "FWFileSp.h"
  22. #endif
  23.  
  24. #ifndef FWEXCDEF_H
  25. #include "FWExcDef.h"
  26. #endif
  27.  
  28. #ifndef   FWPRIDEB_H
  29. #include "FWPriDeb.h"
  30. #endif
  31.  
  32. #ifndef   FWMEMMGR_H
  33. #include "FWMemMgr.h"
  34. #endif
  35.  
  36. //----------------------------------------------------------------------------------------
  37. // Mac-only
  38. //----------------------------------------------------------------------------------------
  39. #ifdef FW_BUILD_MAC
  40.  
  41. #pragma segment ResourceFile
  42.  
  43. #ifndef __ERRORS__
  44. #include <errors.h>
  45. #endif
  46.  
  47. #ifndef __TOOLUTILS__
  48. #include <ToolUtils.h>
  49. #endif
  50.  
  51. #ifndef __RESOURCES__
  52. #include <Resources.h>
  53. #endif
  54.  
  55. #ifndef __MENUS__
  56. #include <Menus.h>
  57. #endif
  58.  
  59. #ifndef __STRING__
  60. #include <string.h>
  61. #endif
  62.  
  63. #ifndef __MEMORY__
  64. #include <Memory.h>
  65. #endif
  66.  
  67. #endif
  68.  
  69. #if FW_LIB_EXPORT_PRAGMAS
  70. #pragma lib_export on
  71. #endif
  72.  
  73. //========================================================================================
  74. // CLASS FW_PPrivResourceAccess
  75. //========================================================================================
  76.  
  77. //----------------------------------------------------------------------------------------
  78. // FW_PPrivResourceAccess::FW_PPrivResourceAccess
  79. //----------------------------------------------------------------------------------------
  80.  
  81. FW_PPrivResourceAccess::FW_PPrivResourceAccess(FW_CPrivResourceRep* privResourceAccessRep) :
  82.     FW_CCountedPtr(privResourceAccessRep)
  83. {
  84. }
  85.  
  86. //----------------------------------------------------------------------------------------
  87. // FW_PPrivResourceAccess::~FW_PPrivResourceAccess
  88. //----------------------------------------------------------------------------------------
  89.  
  90. FW_PPrivResourceAccess::~FW_PPrivResourceAccess()
  91. {
  92. }
  93.  
  94. //========================================================================================
  95. // CLASS FW_CPrivResourceRep
  96. //========================================================================================
  97.  
  98. //----------------------------------------------------------------------------------------
  99. // FW_CPrivResourceRep::FW_CPrivResourceRep 
  100. //----------------------------------------------------------------------------------------
  101.  
  102. FW_CPrivResourceRep::FW_CPrivResourceRep(const FW_CResourceFile& file,
  103.                                          FW_ResourceId resourceId,
  104.                                          FW_ResourceType resourceType) :
  105.     FW_CCountedPtrRep(),
  106.     fResourceFile(file),
  107.     fResourceID(resourceId),
  108.     fResourceType(resourceType),
  109.     fResourceHandle(0)
  110. {
  111. #ifdef FW_BUILD_MAC
  112.     fLockCount = 0;
  113.     fResourceData = 0;
  114.     {
  115.         FW_CMacResLoadFalse dontLoadResource;
  116.         fResourceHandle = file.GetResourceHandle(resourceId, resourceType);
  117.     }
  118. #elif defined FW_BUILD_WIN
  119.     fResourceHandle = file.GetResourceHandle(resourceId, resourceType);
  120. #endif
  121.  
  122.     fResourceSize = GetResourceSize(fResourceHandle);
  123. }
  124.  
  125. //----------------------------------------------------------------------------------------
  126. // FW_CPrivResourceRep::~FW_CPrivResourceRep 
  127. //----------------------------------------------------------------------------------------
  128.  
  129. FW_CPrivResourceRep::~FW_CPrivResourceRep()
  130. {
  131. #ifdef FW_BUILD_MAC
  132.     FW_ASSERT(fLockCount == 0);
  133.     if (fResourceData != 0)
  134.         FW_CMemoryManager::FreeSystemHandle(fResourceData);
  135. #endif
  136.     fResourceFile.ReleaseResourceHandle(fResourceHandle);
  137. }
  138.  
  139. //----------------------------------------------------------------------------------------
  140. // FW_CPrivResourceRep::GetResourceSize
  141. //----------------------------------------------------------------------------------------
  142.  
  143. unsigned long FW_CPrivResourceRep::GetResourceSize(FW_ResourceHandle handle)
  144. {
  145.     unsigned long resourceSize;
  146. #if defined FW_BUILD_MAC
  147.     resourceSize = ::GetResourceSizeOnDisk(handle);
  148. #elif defined FW_BUILD_WIN
  149.     resourceSize = ::SizeofResource(fResourceFile.PrivGetResourceFileID(), handle);
  150. #endif
  151.     return resourceSize;
  152. }
  153.  
  154. //----------------------------------------------------------------------------------------
  155. // FW_CPrivResourceRep::GetData
  156. //----------------------------------------------------------------------------------------
  157.  
  158. void* FW_CPrivResourceRep::GetData()
  159. {
  160.     void *result;
  161.     FW_ASSERT(fResourceHandle != 0);
  162. #if defined FW_BUILD_MAC
  163.     FW_ASSERT(fLockCount >= 0);
  164.     if (fLockCount == 0)
  165.     {
  166.         if (fResourceData == 0)
  167.         {
  168.             // ODNewHandle throws kODErrOutOfMemory if it fails
  169.             fResourceData = FW_CMemoryManager::AllocateSystemHandle(fResourceSize);
  170.  
  171.             // Lock the handle, read the resource data
  172.             FW_CMemoryManager::LockSystemHandle(fResourceData);
  173.             ::ReadPartialResource(fResourceHandle, 0, *fResourceData, fResourceSize);
  174.  
  175.             // Throw an exception if the read failed.
  176.             FW_FailOnError(::ResError());
  177.         } 
  178.         else
  179.         {
  180.             FW_CMemoryManager::LockSystemHandle(fResourceData);
  181.         }
  182.     }
  183.     ++fLockCount;
  184.     FW_ASSERT(*fResourceData != 0);
  185.     result = *fResourceData;
  186. #elif defined FW_BUILD_WIN
  187.     fLoadedHandle = ::LoadResource(fResourceFile.PrivGetResourceFileID(), fResourceHandle);
  188.     result = ::LockResource(fLoadedHandle);
  189. #endif
  190.  
  191.     return result;
  192. }
  193.  
  194. //----------------------------------------------------------------------------------------
  195. // FW_CPrivResourceRep::ReleaseData
  196. //----------------------------------------------------------------------------------------
  197.  
  198. void FW_CPrivResourceRep::ReleaseData()
  199. {
  200. #if defined FW_BUILD_MAC
  201.     FW_ASSERT(fLockCount > 0);
  202.     if (--fLockCount == 0)
  203.     {
  204.         FW_CMemoryManager::UnlockSystemHandle(fResourceData);
  205.         // (JEL) ??? Should we HPurge too?  My feeling is, tell developers they should
  206.         // generally set all resources to purgeable, but allow them to set a resource
  207.         // nonpurgeable for performance reasons.  If we always did an HPurge, we take
  208.         // control from the developer, for no good reason.
  209.     }
  210. #elif defined FW_BUILD_WIN16
  211.     ::UnlockResource(fLoadedHandle);
  212. #endif
  213. }
  214.  
  215.