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 / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / Utilities / UseRsrcM.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  6.4 KB  |  327 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        UseRsrcM.cpp
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Owned by:    Doug Hill
  7.  
  8.     Copyright:    © 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <2>     6/21/96    DH        1353507: 1.0.x: Dragging Scrapbook OpenDoc
  13.                                     clipping to document can cause crash. Added
  14.                                     function to read a resource fork that takes
  15.                                     a fileRefNum
  16.  
  17.     To Do:
  18. */
  19.  
  20. /*
  21.     File:        UseRsrcM.cpp
  22.  
  23.     Contains:    Functions for using resources from the resource fork of a library
  24.  
  25.     Owned by:    Jens Alfke
  26.  
  27.     Copyright:    © 1994 - 1995 by Apple Computer, Inc., all rights reserved.
  28.  
  29. */
  30.  
  31.  
  32. #ifndef _USERSRCM_
  33. #include "UseRsrcM.h"
  34. #endif
  35.  
  36. #ifndef __TEXTUTILS__
  37. #include <TextUtils.h>
  38. #endif
  39.  
  40. #ifndef _EXCEPT_
  41. #include "Except.h"
  42. #endif
  43.  
  44. #ifndef _ODDEBUG_
  45. #include "ODDebug.h"
  46. #endif
  47.  
  48. #ifndef _ODMEMORY_
  49. #include <ODMemory.h>
  50. #endif
  51.  
  52. #ifndef _PASCLSTR_
  53. #include <PasclStr.h>
  54. #endif
  55.  
  56. #ifndef __FRAGRSRC__
  57. #include <FragRsrc.h>
  58. #endif
  59.  
  60. #ifndef __RESOURCES__
  61. #include <Resources.h>
  62. #endif
  63.  
  64. #ifndef __ERRORS__
  65. #include <Errors.h>
  66. #endif
  67.  
  68.  
  69. static CFragResFileRef    gFragRef = kODNULL;        // Reference to resource map
  70. static short            gRefNum = -1;            // RefNum of resource file
  71. static ODSLong            gNesting = 0;            // Nesting level (>0 means in use)
  72.  
  73.  
  74. OSErr
  75. InitLibraryResources( CFragInitBlockPtr init )
  76. {
  77.     return InitCFragResources(init,&gFragRef);
  78. }
  79.  
  80.  
  81. void
  82. CloseLibraryResources( )
  83. {
  84.     if( gNesting>0 )
  85.         WARN("Resource file still in use while being closed!");
  86.     OSErr err= TermCFragResources(gFragRef);
  87.     if( err )
  88.         WARN("TermCFragResources returned %hd",err);
  89.     
  90.     gFragRef = kODNULL;
  91.     gRefNum = -1;
  92.     gNesting = 0;
  93. }
  94.  
  95.  
  96. ODSLong BeginUsingLibraryResources( )
  97. {
  98.     short ref = CurResFile();
  99.     if( gNesting > 0 )
  100.         UseResFile(gRefNum);
  101.     else {
  102.         THROW_IF_ERROR( BeginCFragResources(gFragRef) );
  103.         gRefNum = CurResFile();
  104.     }
  105.     gNesting++;
  106.     return ref;
  107. }
  108.  
  109.  
  110. void EndUsingLibraryResources( ODSLong ref )
  111. {
  112.     if( gNesting > 0 )
  113.         if( --gNesting == 0 )
  114.             THROW_IF_ERROR( EndCFragResources(gFragRef) );
  115.     UseResFile(ref);
  116. }
  117.  
  118.  
  119. CUsingLibraryResources::~CUsingLibraryResources( )
  120. {
  121.     ODSLong ref = fRefNum;
  122.     if( ref != -1 ) {                        // Make sure it's done only once
  123.         fRefNum = -1;
  124.         EndUsingLibraryResources(ref);
  125.     }
  126. }
  127.  
  128.  
  129. // RESOURCE UTILITIES:
  130.  
  131.  
  132. static void*
  133. BasicReadResource( ResType type, short id, ConstStr255Param name, ODBoolean asHandle )
  134. {
  135.     Handle rsrc;
  136.     void *result = kODNULL; ODVolatile(result);
  137.     
  138.     ODSLong savedRef = BeginUsingLibraryResources();
  139.     
  140.     SetResLoad(false);
  141.     if( name )
  142.         rsrc = Get1NamedResource(type,name);
  143.     else
  144.         rsrc = Get1Resource(type,id);
  145.     SetResLoad(true);
  146.     if( rsrc==kODNULL ) {
  147.         OSErr err = ResError();
  148.         EndUsingLibraryResources(savedRef);
  149.         THROW( err ?err :resNotFound );
  150.     }
  151.     
  152.     TRY{
  153.         ODSize size = (*rsrc) ?GetHandleSize(rsrc) :GetResourceSizeOnDisk(rsrc);
  154.         void *dst;
  155.         if( asHandle ) {
  156.             result = (void*) ODNewHandle(size);
  157.             dst = ODLockHandle((ODHandle)result);
  158.         } else
  159.             result = dst = ODNewPtr(size);
  160.     
  161.         if( *rsrc == kODNULL ) {
  162.             // Resource is not in memory, use partial-resource call:
  163.             ReadPartialResource( rsrc, 0, dst, size );
  164.             THROW_IF_ERROR( ResError() );
  165.             ReleaseResource(rsrc);
  166.         } else {
  167.             // Resource is already in memory; just copy it:
  168.             ODBlockMove(*rsrc,dst,size);
  169.         }
  170.  
  171.         if( asHandle )
  172.             ODUnlockHandle((ODHandle)result);
  173.  
  174.     }CATCH_ALL{
  175.         if( *rsrc == kODNULL )
  176.             ReleaseResource(rsrc);
  177.         EndUsingLibraryResources(savedRef);
  178.         if( result )
  179.             if( asHandle )
  180.                 ODDisposeHandle((ODHandle)result);
  181.             else
  182.                 ODDisposePtr(result);
  183.         RERAISE;
  184.     }ENDTRY
  185.     
  186.     EndUsingLibraryResources(savedRef);
  187.     return result;
  188. }
  189.  
  190.  
  191. ODHandle
  192. ODReadResource( ResType type, short id )
  193. {
  194.     return (ODHandle) BasicReadResource(type,id,kODNULL,kODTrue);
  195. }
  196.  
  197. ODHandle
  198. ODReadNamedResource( ResType type, ConstStr255Param name )
  199. {
  200.     if( name==kODNULL )
  201.         THROW(kODErrIllegalNullInput);
  202.     return (ODHandle) BasicReadResource(type,0,name,kODTrue);
  203. }
  204.  
  205. ODPtr
  206. ODReadResourceToPtr( ResType type, short id )
  207. {
  208.     return BasicReadResource(type,id,kODNULL,kODFalse);
  209. }
  210.  
  211. ODPtr
  212. ODReadNamedResourceToPtr( ResType type, ConstStr255Param name )
  213. {
  214.     if( name==kODNULL )
  215.         THROW(kODErrIllegalNullInput);
  216.     return BasicReadResource(type,0,name,kODFalse);
  217. }
  218.  
  219.  
  220. void
  221. ODGetString( Str255 str, short id )
  222. {
  223.     str[0] = 0;
  224.     ODSLong savedRef = BeginUsingLibraryResources();
  225.     Handle h = Get1Resource('STR ',id);
  226.     OSErr result = ResError();
  227.     if( h ) {
  228.         CopyPascalString(str,(StringPtr)*h);
  229.         ReleaseResource(h);
  230.     }
  231.     EndUsingLibraryResources(savedRef);
  232.     if( !h ) {
  233.         THROW(result);
  234.         THROW(resNotFound);
  235.     }
  236. }
  237.  
  238.  
  239. void
  240. ODGetIndString( Str255 str, short id, short index )
  241. {
  242.     str[0] = 0;
  243.     ODSLong savedRef = BeginUsingLibraryResources();
  244.     Handle h = Get1Resource('STR#',id);
  245.     OSErr result = ResError();
  246.     if( h ) {
  247. #if ODDebug
  248.         if( index<1 || index>**(short**)h ) {
  249.             WARN("ODGetIndString(%hd,%hd): invalid index",id,index);
  250.             result= kODErrValueOutOfRange;
  251.         } else
  252. #endif
  253.         GetIndString(str,id,index);
  254.         ReleaseResource(h);
  255.     }
  256.     EndUsingLibraryResources(savedRef);
  257.     if( result ) {
  258.         THROW(result);
  259.         THROW(resNotFound);
  260.     }
  261. }
  262.  
  263.  
  264. void*
  265. BasicReadResource( short ResRefNum, ResType type, short id, ConstStr255Param name, ODBoolean asHandle )
  266. {
  267.     Handle rsrc;
  268.     void *result = kODNULL; ODVolatile(result);
  269.     
  270.     short oldResRefnum; ODVolatile( oldResRefnum );
  271.     oldResRefnum = CurResFile();        // save the current start of the resource map.
  272.     UseResFile( ResRefNum );
  273.  
  274.     SetResLoad(false);
  275.     if( name )
  276.         rsrc = Get1NamedResource(type,name);
  277.     else
  278.         rsrc = Get1IndResource(type,id);
  279.     SetResLoad(true);
  280.     if( rsrc == kODNULL ) {
  281.         OSErr err = ResError();
  282.         UseResFile( oldResRefnum );        // restore the old start of the resource map
  283.         THROW( err ? err : resNotFound );
  284.     }
  285.     
  286.     TRY{
  287.         ODSize size = (*rsrc) ?GetHandleSize(rsrc) :GetResourceSizeOnDisk(rsrc);
  288.         void *dst;
  289.         if( asHandle ) {
  290.             result = (void*) ODNewHandle(size);
  291.             dst = ODLockHandle((ODHandle)result);
  292.         } else
  293.             result = dst = ODNewPtr(size);
  294.     
  295.         if( *rsrc == kODNULL ) {
  296.             // Resource is not in memory, use partial-resource call:
  297.             ReadPartialResource( rsrc, 0, dst, size );
  298.             THROW_IF_ERROR( ResError() );
  299.             ReleaseResource(rsrc);
  300.         } else {
  301.             // Resource is already in memory; just copy it:
  302.             ODBlockMove(*rsrc,dst,size);
  303.         }
  304.  
  305.         if( asHandle )
  306.             ODUnlockHandle((ODHandle)result);
  307.  
  308.     }CATCH_ALL{
  309.         if( *rsrc == kODNULL )
  310.             ReleaseResource(rsrc);
  311.         UseResFile( oldResRefnum );        // restore the old start of the resource map
  312.         if( result )
  313.             if( asHandle )
  314.                 ODDisposeHandle((ODHandle)result);
  315.             else
  316.                 ODDisposePtr(result);
  317.         RERAISE;
  318.     }ENDTRY
  319.     
  320.     UseResFile( oldResRefnum );        // restore the old start of the resource map
  321.     
  322.     return result;
  323. }
  324.  
  325.  
  326.  
  327.