Important: The information in this document is obsolete and should not be used for new development.
Reading Collections From and Writing Collections to Disk
The Collection Manager provides a number of methods for storing collections on disk:
- You can store the collection's contents as a collection (
'cltn') resource and read the information into a collection object using theGetNewCollectionfunction. For more information about the'cltn'resource, see "The Collection Resource" beginning on page 5-102, and for more information about theGetNewCollectionfunction, see the description of that function on page 5-99. For an example of reading a collection object from a collection resource, see the next section, "Reading a Collection From a Collection Resource."- You can flatten a collection using the
FlattenCollectionfunction and provide a callback function that writes the blocks of flattened data to a file. You can unflatten this collection using theUnflattenCollectionfunction, providing a callback function that reads blocks of data from the file. For more information about these functions, see "Flattening and Unflattening a Collection" beginning on page 5-37 and the description of theFlattenCollectionfunction on page 5-88 and the description of theUnflattenCollectionfunction on page 5-90.- You can flatten a collection to a handle using the
FlattenCollectionToHdlfunction and write the contents of the handle to the resource fork of a file (using the Macintosh functionAddResource) or to the data fork of a file (using the Macintosh functionFSWrite). You can then read the contents of this file into a handle (using the Macintosh functionsGetResourceorFSRead) and unflatten the result using theUnflattenCollectionFromHdlfunction.
Listing 5-22 shows how to flatten a collection to a handle and then write the contents of the handle to the resource fork of a disk file.
- IMPORTANT
- Although you may create a resource containing a flattened collection using the
FlattenCollectionToHdlandAddResourcefunctions, you cannot recreate the collection from this resource using theGetNewCollectionfunction. The resource format created by theFlattenCollectionToHdlandAddResourcefunctions is incompatible with the resource format expected by theGetNewCollectionfunction.![]()
Listing 5-22 Flattening a collection to a disk file as a resource
OSErr anErr; Handle flattened; /* write the collection out as a resource */ flattened = NewHandle(0); anErr = FlattenCollectionToHdl(myCollection, flattened); if (anErr == noErr) { AddResource(flattened, myType, myID, myName); anErr = ResError(); }Listing 5-23 shows how to flatten a collection to a handle and then write the contents of the handle to the data fork of a disk file.Listing 5-23 Flattening a collection to a data fork of a disk file
OSErr anErr; Handle flattened; long theSize; /* write the collection out to the data fork */ flattened = NewHandle(0); anErr = FlattenCollectionToHdl(myCollection, flattened); if (anErr == noErr) { theSize = GetHandleSize(flattened); anErr = FSWrite(refNum, theSize, *flattened); }Listing 5-24 shows how to read a flattened collection from the resource fork of a disk file into a block of memory referenced by a Macintosh Memory Manager handle and then unflatten the information in that block of memory into a collection object.Listing 5-24 Unflattening a collection from a disk file as a resource
Handle flattened; Collection myCollection; if (myCollection = NewCollection()) { /* read the collection in as a resource */ flattened = GetResource(myType, myID); if ((anErr = ResError()) == noErr) { anErr = UnflattenCollectionFromHdl(myCollection, flattened); ReleaseResource(flattened); if (anErr == noErr) anErr = ResError(); } }Listing 5-25 shows how to read a flattened collection from the data fork of a disk file into a block of memory referenced by a Macintosh Memory Manager handle and then unflatten the information in that block of memory into a collection object.Listing 5-25 Unflattening a collection from the data fork of a disk file
OSErr anErr; Handle flattened; Collection myCollection; if (myCollection = NewCollection()) { /* read the collection in from the data fork */ flattened = NewHandle(theSize); if ((anErr = MemError()) == noErr) { if ((anErr = FSRead(refNum, theSize, *flattened)) == noErr) anErr = UnflattenCollectionFromHdl(myCollection, flattened); DisposHandle(flattened); } }To unflatten a collection using Listing 5-25, you must know the size of the collection before you can unflatten it. If you don't know the size of the collection, you unflatten a collection using the callback function mechanism described in "Flattening and Unflattening a Collection" beginning on page 5-37.For more information about the
FlattenCollectionToHdlfunction and theUnflattenCollectionFromHdlfunction, see "Flattening and Unflattening a Collection" beginning on page 5-37 as well as the descriptions of these functions starting on page 5-97.For information about the Macintosh functions
AddResourceandGetResource, see the chapter "Resource Manager" in Inside Macintosh: More Macintosh Toolbox. For information about the Macintosh functionsFSReadandFSWrite, see the chapter "File Manager" in Inside Macintosh: Files.