Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!apple!xanadu!bob From: bob@xanadu.com (Bob Schumaker -- "Software-in-a-bucket") Newsgroups: comp.sys.mac.programmer Subject: Preferences Folder Summary: Put preference files in the "right" place Keywords: Preferences Folder Message-ID: <1991Jun14.035617.17048@xanadu.com> Date: 14 Jun 91 03:56:17 GMT Sender: Bob Schumaker Followup-To: comp.sys.mac.programmer Organization: The AMIX Corporation, Palo Alto, CA Lines: 126 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. ------------------------------------------------------------------------