Important: The information in this document is obsolete and should not be used for new development.
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. TheReadPartialResourceandWritePartialResourceprocedures, 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 theSetResourceSizeprocedure to enlarge or reduce the size of a resource on disk. When you useReadPartialResourceandWritePartialResource, 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.
To read or write any part of a resource, call the
- 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 theReadPartialResourceorWritePartialResourceprocedure, note that these procedures work with the data in the buffer you specify, not the data referenced through the resource's handle.![]()
SetResLoadprocedure specifyingFALSEfor itsloadparameter, then use theGetResourcefunction to get an empty handle (that is, a handle whose master pointer is set toNIL) to the resource. (Because of the call to theSetResLoadprocedure, theGetResourcefunction does not load the entire resource into memory.) Then callSetResLoadspecifyingTRUEfor itsloadparameter 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
MyReadAPartialbegins by callingSetResLoad(with theloadparameter set toFALSE) to ensure that the Resource Manager will not attempt to read the entire resource into memory in the subsequent call toGetResource. After callingGetResourceand checking for errors,MyReadAPartialcallsSetResLoad(with theloadparameter set toTRUE) to restore normal loading of resource data into memory. The procedure then callsReadPartialResource, specifying as parameters the handle returned byGetResource, 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. TheReadPartialResourceprocedure 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;