Advanced Search
Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

Previous Book Contents Book Index Next

Inside Macintosh: More Macintosh Toolbox /
Chapter 1 - Resource Manager / Using the Resource Manager


Working With Partial Resources

Some resources, such as the 'snd ' and 'sfnt' resources, can be quite large--sometimes too large to fit in the available memory. The ReadPartialResource and WritePartialResource procedures, which are available in System 7 and later versions of system software, allow you to read a portion of the resource into memory or alter a section of the resource while it is still on disk. You can also use the SetResourceSize procedure to enlarge or reduce the size of a resource on disk. When you use ReadPartialResource and WritePartialResource, you specify how far into the resource you want to begin reading or writing and how many bytes you actually want to read or write at that spot, so you must be sure of the location of the data.

WARNING
Be aware that having a copy of a resource in memory when you are using the partial resource routines may cause problems. For example, if you read the resource into memory using GetResource, modify the resource in memory, and then access the resource on disk using either the ReadPartialResource or WritePartialResource procedure, note that these procedures work with the data in the buffer you specify, not the data referenced through the resource's handle.
To read or write any part of a resource, call the SetResLoad procedure specifying FALSE for its load parameter, then use the GetResource function to get an empty handle (that is, a handle whose master pointer is set to NIL) to the resource. (Because of the call to the SetResLoad procedure, the GetResource function does not load the entire resource into memory.) Then call SetResLoad specifying TRUE for its load parameter and use the partial resource routines to access portions of the resource.

Listing 1-12 illustrates one way to deal with partial resources. The application-defined procedure MyReadAPartial begins by calling SetResLoad (with the load parameter set to FALSE) to ensure that the Resource Manager will not attempt to read the entire resource into memory in the subsequent call to GetResource. After calling GetResource and checking for errors, MyReadAPartial calls SetResLoad (with the load parameter set to TRUE) to restore normal loading of resource data into memory. The procedure then calls ReadPartialResource, specifying as parameters the handle returned by GetResource, an offset to the beginning of the resource subsection to be read, a buffer into which to read the subsection, and the length of the subsection. The ReadPartialResource procedure reads the specified partial resource into the specified buffer.

Listing 1-12 Using partial resource routines

PROCEDURE MyReadAPartial(myRsrcType: ResType; myRsrcID: Integer; 
                         start: LongInt; count: LongInt; 
                         VAR putItHere: Ptr);
VAR
   myResHdl:         Handle;
   myErr:            OSErr;
BEGIN
   SetResLoad(FALSE);      {don't load resource}
   myResHdl := GetResource(myRsrcType, myRsrcID);
   myErr := ResError;
   SetResLoad(TRUE);       {reset to always load}
   IF myErr = noErr THEN 
   BEGIN
      ReadPartialResource(myResHdl, start, putItHere, count);
      myErr := ResError;
      {check and report error}
      IF myErr <> noErr THEN DoError(myErr);
   END
   ELSE {handle error from GetResource}
      DoError(myErr);
END;  


Previous Book Contents Book Index Next

© Apple Computer, Inc.
6 JUL 1996

Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2004 Apple Computer, Inc.
All rights reserved. | Terms of use | Privacy Notice