1 line
18 KiB
C
1 line
18 KiB
C
|
//============================================================================
//----------------------------------------------------------------------------
// Prefs.c
//----------------------------------------------------------------------------
//============================================================================
// This is a slick little file that I re-use and re-use. I wrote it to<74>
// seemlessly handle System 6 or System 7 with but a single call. You need<65>
// to define your own "prefs" struct, but these routines will read and write<74>
// it to the System folder.
#include "Externs.h"
#include <Folders.h> // Needed for creating a folder.
#include <GestaltEqu.h> // Needed for the Gestalt() call.
#include <Script.h> // I can't remember why I needed this.
#define kPrefCreatorType 'zade' // Change this to reflect your apps creator.
#define kPrefFileType 'zadP' // Change this to reflect your prefs type.
#define kPrefFileName "\pGlypha Prefs" // Change this to reflect the name for your prefs.
#define kDefaultPrefFName "\pPreferences" // Name of prefs folder (System 6 only).
#define kPrefsStringsID 160 // For easy localization.
#define kPrefsFNameIndex 1 // This one works with the previous constant.
Boolean CanUseFindFolder (void);
Boolean GetPrefsFPath (long *, short *);
Boolean CreatePrefsFolder (short *);
Boolean GetPrefsFPath6 (short *);
Boolean WritePrefs (long *, short *, prefsInfo *);
Boolean WritePrefs6 (short *, prefsInfo *);
OSErr ReadPrefs (long *, short *, prefsInfo *);
OSErr ReadPrefs6 (short *, prefsInfo *);
Boolean DeletePrefs (long *, short *);
Boolean DeletePrefs6 (short *);
//============================================================== Functions
//-------------------------------------------------------------- CanUseFindFolder
// Returns TRUE if we can use the FindFolder() call (a System 7 nicety).
Boolean CanUseFindFolder (void)
{
OSErr theErr;
long theFeature;
if (!DoWeHaveGestalt()) // Darn, have to check for Gestalt() first.
return(FALSE); // If no Gestalt(), probably don't have FindFolder().
theErr = Gestalt(gestaltFindFolderAttr, &theFeature);
if (theErr != noErr) // Use selector for FindFolder() attribute.
return(FALSE);
// Now do a bit test specifically for FindFolder().
if (!BitTst(&theFeature, 31 - gestaltFindFolderPresent))
return(FALSE);
else
return(TRUE);
}
//-------------------------------------------------------------- GetPrefsFPath
// This function gets the file path to the Preferences folder (for System 7).
// It is called only if we can use FindFolder() (see previous function).
Boolean GetPrefsFPath (long *prefDirID, short *systemVolRef)
{
OSErr theErr;
// Here's the wiley FindFolder() call.
theErr = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
systemVolRef, prefDirID); // It returns to us the directory and volume ref.<2E>
if (theErr != noErr) // Assuming it worked at all!
return(FALSE);
return(TRUE);
}
//-------------------------------------------------------------- CreatePrefsFolder
// This function won't be necessary for System 7, for System 6 though, it creates<65>
// a folder ("Preferences") in the System folder and returns whether or not it worked.
Boolean CreatePrefsFolder (short *systemVolRef)
{
HFileParam fileParamBlock;
Str255 folderName;
OSErr theErr;
// Here's our localization. Rather than<61>
// hard-code the name "Preferences" in the code<64>
// we pull up the text from a string resource.
GetIndString(folderName, kPrefsStringsID, kPrefsFNameIndex);
// Set up a file parameter block.
fileParamBlock.ioVRefNum = *systemVolRef;
fileParamBlock.ioDirID = 0;
fileParamBlock.ioNamePtr = folderName;
fileParamBlock.ioCompletion = 0L;
// And create a directory (folder).
theErr = PBDirCreate((HParmBlkPtr)&fileParamBlock, FALSE);
if (theErr != noErr) // See that it worked.
{
RedAlert("\pPrefs Creation Error");
return(FALSE);
}
return(TRUE);
}
//-----------------------------------
|