Saving a Document Containing Sections
When saving a document that contains sections, you should write out each
section record as a resource of type 'sect' and write out each alias record as a
resource of type 'alis' with the same ID as the section record. See the
If a user closes a document that contains newly created publishers without
attempting to save its contents, you should display an alert box similar to the
one shown in the Figure below.
The new publisher alert box
If you keep the section records and alias records for each publisher and
WriteResource function. If you detach the section records and alias records from each section, you need to clone the handles and use the AddResource function.
Use the PBExchangeFiles function to ensure that each time you save a
document that contains sections, the file ID remains the same. Saving a file
typically involves creating a new file (with a temporary name), writing data
to it, closing it, and then deleting the original file that you are replacing. You
rename the temporary file with the original filename, which leads to a new file
ID. The PBExchangeFiles function swaps the contents of the two files (even
if they are open) by getting both catalog entries and swapping the al location
pointers. If the files are open, the file control block (FCB) is updated so that
the reference numbers still access the same contents (under a new name).
The following listing illustrates how to save a file that contains sections. As
described earlier, you should write out the eligible section records and
alias records as resources to allow for future compatibility. There are
several different techniques for saving or adding resources; this listing
illustrates one technique. The section handles are still valid after using the
AddResource function because this listing illustrates just saving, not closing, the file.
Before you write out sections, you need to see if any publisher sections share
the same control block. Publishers that share the same control block share the
same edition.
If a user creates an identical copy of a file by choosing Save As from the File
menu and does not make any changes to this new file, you simply use the
document a section is located in.
// Listing. Saving a document containing sections
// Assuming inclusion of
#include <Editions.h>
// This is a sample declaration for the pointer to your document information.
typedef struct {
short resForkRefNum;
short nextSectionID;
} *MyDocumentInfoPtr;
// Prototype your function like this prior to calling it
void SaveDocument (MyDocumentInfoPtr, short);
void SaveDocument(MyDocumentInfoPtr thisDocument,
short numberOfSections)
{
SectionHandle aSectionH; // Handle containing information to write Handle copiedSectionH; // Temporary handle for copy of section handle // Temporary handle for copy of alias in section handle
short resID;
short thisone;
// Prototype user defined functions
SectionHandle GetSectionAliasPair(MyDocumentInfoPtr, short, short); void CheckForDupes (MyDocumentInfoPtr, short);
// Write contents of publishers that need to be written during save.
// The GetSectionAliasPair function returns a handle and
// resID to a section. The CheckForDataChanged function
// returns TRUE if the data in the section has changed. for ( thisone = 1; thisone <= numberOfSections; thisone++) {
if ( ((*aSectionH)->kind = stPublisher) && ((*aSectionH)->mode =
pumOnSave) && (CheckForDataChanged(aSectionH)) )
{
// The function DoWriteEdition simply writes the contents of
// aSectionH to thisDocument
DoWriteEdition(aSectionH, thisDocument);
}
}
// Set the curResFile to be the resource fork of thisDocument.
// Write all section and alias records to the document.
for ( thisone = 1; thisone <= numberOfSections; thisone++) {
// Given an index, get the next section handle and resID
// from your internal list of sections for this file.
aSectionH = GetSectionAliasPair( thisDocument, thisone, resID);
// Check for duplication of control block values.
CheckForDupes( thisDocument, numberOfSections);
// Save section record to disk.
copiedSectionH = (Handle)aSectionH; AddResource(copiedSectionH, rSectionType, resID, "\p"); // Save alias record to disk.
copiedAliasH = (Handle)(*aSectionH)-> alias; }
// Place code here to write rest of document to disk.
}