[comp.sys.mac.programmer] Preferences Folder

zebolskyd@yvax.byu.edu (10/04/89)

From what I have seen posted, the consensus seems to be that we ought to
use the preferences folder idea. Here's what I plan to do:

When using HFS, look for a folder named "Preferences" in BootDrive (which
I assume is still the working directory reference number for the current
System's directory). Open or create it. Keep your preference file in there
under the name of the application + Prefs (i.e., "Clarus Prefs"). If there
is already a file with that name and not your creator type, throw a fit.

I plan to keep the name of the Preferences folder in an 'STR ' resource
in the app. so furriners can change it to whatever is appropriate.

Is this the way to go, or the road to trouble?

--Lyle D. Gunderson    zebolskyd@yvax.byu.edu

nopuklic@ndsuvax.UUCP (Blayne Puklich) (10/05/89)

In article <835zebolskyd@yvax.byu.edu> zebolskyd@yvax.byu.edu writes:
>
>When using HFS, look for a folder named "Preferences" in BootDrive (which
>I assume is still the working directory reference number for the current
>System's directory). Open or create it. Keep your preference file in there
>under the name of the application + Prefs (i.e., "Clarus Prefs"). If there
>is already a file with that name and not your creator type, throw a fit.
>
>I plan to keep the name of the Preferences folder in an 'STR ' resource
>in the app. so furriners can change it to whatever is appropriate.
>
>--Lyle D. Gunderson    zebolskyd@yvax.byu.edu

Since you'll more than likely want the preferences file to be associated with
your application somewhat, I think a much better idea would be to have the
preferences filename in a FREF resource.  Then you can associate an ICN#
with it also.  And, if the user so wishes, the application can be launched
by opening its preferences file.

It's very easy to then pull the filename of that preference file out of
the FREF in the application resource fork by using a struct such as:
	typedef struct FREF
	{	OSType type;
		short localIconID;
		Str255 fileName;
	} FREF, **HFREF;

Let's discuss this...

||+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++||
|| Blayne Puklich      nopuklic@Plains.NoDak.EDU  "I think I'm going       ||
|| NDSU Student ACM    nopuklic@ndsuvax.BITNET          bald..."           ||
|| Vice-Chairperson    NU087763@NDSUVM1.BITNET     -- Rush, from Caress    ||
|| North Dakota State University, Fargo, ND           of Steel, 1975       ||
|| (701) 237-4408                                                          ||
||          "Corvettes are the best thing man has ever invented."          ||
||-------------------------------------------------------------------------||

chewy@apple.com (Paul Snively) (10/05/89)

In article <835zebolskyd@yvax.byu.edu> zebolskyd@yvax.byu.edu writes:
> When using HFS, look for a folder named "Preferences" in BootDrive (which
> I assume is still the working directory reference number for the current
> System's directory).

AIEEEEEEEEEEEEEEEEEEEEEEEEEEEE!  This isn't true now, nor has it ever been 
guaranteed to be so.  What BootDrive IS is the WDRefNum of WHATEVER the 
Startup Application is, regardless of where it is (go ahead, try it: Set 
Startup to be some app that's somewhere other than in the System Folder, 
and see what BootDrive tells you).

PLEASE use SysEnvirons to determine where the Blessed Folder is and look 
THERE for Preferences.

__________________________________________________________________________
Just because I work for Apple Computer, Inc. doesn't mean that they 
believe what I believe or vice-versa.
__________________________________________________________________________

bob@xanadu.com (Bob Schumaker -- "Software-in-a-bucket") (06/14/91)

I've noticed a few questions about the preference folder and how to
get it, so here is some code to make sure that your preference files
are all in the "right" place.  Note that this code does not use the
Folder Manager under System 7 (soon, though, I hope :-).  This code will
be included in an upcoming release of TransSkel.

/*
	some handy dandy file management routines
*/
  OSErr
OpenPreferencesFile (char *defaultsFileName, Boolean createIt, OSType creator,
				short *dataFork, short *rsrcFork)
{
	OSErr error;
	HParamBlockRec hpb;
	long dirID;
	short wdRefNum, curVol;
	char fName[256];
	SysEnvRec *env;

	/* get an environment record */
	SkelQuery (skelQEnvirons, (long *) &env);
	/* make the folder "Preferences" */
	error = FindPreferencesFolder (&dirID, &wdRefNum, true);
	*dataFork = 0;
	*rsrcFork = 0;
	if (error == noErr) {
		hpb.fileParam.ioCompletion = 0;
		PstrCpy (fName, defaultsFileName);
		hpb.fileParam.ioNamePtr = fName;
		hpb.fileParam.ioVRefNum = env->sysVRefNum;
		hpb.fileParam.ioFVersNum = 0;
		hpb.fileParam.ioFDirIndex = 0;
		hpb.fileParam.ioDirID = dirID;
		error = PBHGetFInfo (&hpb, false);
		if (error == fnfErr && createIt) {
			/* create the file in the right place */
			hpb.fileParam.ioCompletion = 0;
			PstrCpy (fName, defaultsFileName);
			hpb.fileParam.ioNamePtr = fName;
			hpb.fileParam.ioVRefNum = env->sysVRefNum;
			hpb.fileParam.ioDirID = dirID;
			error = PBHCreate (&hpb, false);
			hpb.fileParam.ioFlFndrInfo.fdType = 'defs';
			hpb.fileParam.ioFlFndrInfo.fdCreator = creator;
			error = PBHSetFInfo (&hpb, false);
		}
		if (error == noErr && dataFork != nil) {
			hpb.fileParam.ioCompletion = 0;
			PstrCpy (fName, defaultsFileName);
			hpb.fileParam.ioNamePtr = fName;
			hpb.fileParam.ioVRefNum = env->sysVRefNum;
			hpb.fileParam.ioDirID = dirID;
			hpb.fileParam.ioFVersNum = 0;
			hpb.ioParam.ioMisc = 0;
			hpb.ioParam.ioPermssn = fsRdWrPerm;
			error = PBHOpen (&hpb, false);
			*dataFork = hpb.ioParam.ioRefNum;
		}
		if (error == noErr && rsrcFork != nil) {
			/* resource manager calls suck!! */
			GetVol (0, &curVol);
			SetVol (0, wdRefNum);
			*rsrcFork = OpenResFile (defaultsFileName);
			error = ResError ();
			SetVol (0, curVol);
		}
	}
	return (error);
}

  OSErr
FindPreferencesFolder (long *dirID, short *wdRefNum, Boolean createIt)
{
	OSErr error;
	CInfoPBRec cpb;
	HParamBlockRec hpb;
	WDPBRec wd;	
	short vRefNum;
	long procid;
	long sysDirID;
	char fName[256];
	SysEnvRec *env;
	char *prefName = "\pPreferences";

	SkelQuery (skelQEnvirons, (long *) &env);
	/* find out if the folder "Preferences" exists */
	/* return the dir ID of the system folder regardless */
	error = GetWDInfo (env->sysVRefNum, &vRefNum, &sysDirID, &procid);
	*dirID = sysDirID;
	*wdRefNum = env->sysVRefNum;
	cpb.dirInfo.ioCompletion = 0;
	PstrCpy (fName, prefName);
	cpb.dirInfo.ioNamePtr = (StringPtr) fName;
	cpb.dirInfo.ioFDirIndex = 0;
	cpb.dirInfo.ioDrDirID = 0;
	cpb.dirInfo.ioVRefNum = env->sysVRefNum;
	error = PBGetCatInfo (&cpb, false);
	*dirID = cpb.dirInfo.ioDrDirID;
	if (error == fnfErr && createIt) {
		/* create the folder */
		hpb.fileParam.ioCompletion = 0;
		PstrCpy (fName, prefName);
		hpb.fileParam.ioNamePtr = (StringPtr) fName;
		hpb.fileParam.ioVRefNum = env->sysVRefNum;
		hpb.fileParam.ioDirID = sysDirID;
		error = PBDirCreate (&hpb, false);
		*dirID = hpb.fileParam.ioDirID;
	}
	if (error == noErr) {
		/* create a working directory */
		wd.ioCompletion = 0;
		wd.ioNamePtr = 0;
		wd.ioWDProcID = 'ERIK';
		wd.ioVRefNum = env->sysVRefNum;
		wd.ioWDDirID = *dirID;
		error = PBOpenWD (&wd, false);
		*wdRefNum = wd.ioVRefNum;
	}
	return (error);
}
-- 
------------------------------------------------------------------------
This life is a test.  It is only a test.  Had this been a real life, you
           would have been told where to go and what to do.
------------------------------------------------------------------------