[comp.sys.mac.programmer] Need help w/File Mgr: Preferences Folder

pfluegerm@gtephx.UUCP (Mike Pflueger) (06/21/91)

My understanding of the File Manager is a little fuzzy and I'd be
greatful for any help with this problem.

My routine which opens/creates preferences currently only looks in the
system folder, and I'm trying to add code for it to try to use the
Preferences folder as a default (system 7).

The problem is, at the point marked with XXXX below, it doesn't matter
whether an existing file is in the Preferences folder or just in the
System Folder - it opens.  When I get to the point marked ZZZZ, the
path (and WD) always indicates that the file is in the preferences
WD, even if it's really in the system folder.

Why is this?  I don't need to close the System Folder WD first, do I?

I'm using THINK Pascal.

Some info about my utility routines I call in this example:

  MoveToFolder - does a PBSetVol to the specified WD
  MoveToSysFolder - does a PBSetVol to the System Folder
  GetPath - returns the full path name up to (not including) the file

Thanks in advance
Mike


{**********************************************************}
 

  function OpenPrefsFile (FileName, FileCreator, FileType: str255;
      var FullPath: str255; var new: boolean; var FRef: integer): OSErr;

  { First look for the specified preferences file in the preferences folder  }
  { which is in the system folder.  If there is no preferences folder or the }
  { file doesn't exist, look for the file in the system folder.  If not      }
  { found there, and "new" is true, create a preferences folder if one does  }
  { not exist, then create the file in the preferences folder.  Return the   }
  { full path name and fref to the open file. }

    var
      Err: OSErr;
      foundWD, SysWD, PrefsWD: integer;
      sysDirID: longint;
      CIFR: CInfoPBRec;
      CIP: CInfoPBPtr;
      WDPB: WDPBRec;
      PBP: WDPBPtr;
      HPB: HParmBlkPtr;
      HPBR: HParamBlockRec;
      PrefsName: str255;
      prefsFldrFound: boolean;

  begin
    SysWD := MoveToSysFolder;  { SetVol to the system folder }

    GetIndString(PrefsName, ParamStrings, PrefsFolderName);

    CIP := @CIFR;
    PBP := @WDPB;
    HPB := @HPBR;

  { see if there's a folder called "Preferences" }

    with CIP^ do
      begin
        ioNamePtr := @PrefsName;
        ioCompletion := nil;
        iovrefNum := SysWD;
        ioFDirIndex := 0;
        ioDrDirID := 0;
        Err := PBGetCatInfo(CIP, false);
        sysDirID := ioDirID;
      end;

    if (Err = noErr) and (BTST(CIP^.ioFlAttrib, 4)) then { bit 4 set if folder }
      with PBP^ do
        begin  { preferences folder exists, open a WD for it }
          ioNamePtr := nil;
          ioCompletion := nil;
          ioVRefNum := SysWD;
          ioWDIndex := 0;
          ioWDProcID := 0;
          ioWDVRefNum := 0;
          Err := PBGetWDInfo(PBP, false);

          ioNamePtr := nil;
          ioCompletion := nil;
          ioVRefNum := SysWD;
          ioWDProcID := 0;
          ioWDDirID := CIP^.ioDrDirID;
          Err := PBOpenWD(PBP, false);

          MoveToFolder(ioVRefNum);
          prefsFldrFound := true;
          prefsWD := ioVRefNum;
          foundWD := prefsWD;

XXXX      Err := FSOpen(FileName, prefsWD, fRef);    { try to open prefs file }
        end
    else
      begin  { preferences doesn't exist, indicate file not found }
        prefsFldrFound := false;
        Err := fnfErr;
      end;

    if Err = fnfErr then  { not in system:preferences; try the system folder }
      begin
        MoveToFolder(SysWD);
        foundWD := SysWD;
        Err := FSOpen(FileName, SysWD, fRef);
      end;

    if (Err = fnfErr) and new then
      begin  { create new file in the preferences folder }

        if not prefsFldrFound then
          begin
            with HPB^ do
              begin  { create a Preferences folder }
                ioCompletion := nil;
                ioNamePtr := @PrefsName;
                ioVrefNum := SysWD;
                ioDirID := sysDirID;
                Err := PBDirCreate(HPB, false);
                prefsWD := ioWDDirID;
              end;

            with PBP^ do
              begin
                ioNamePtr := nil;
                ioCompletion := nil;
                ioVRefNum := SysWD;
                ioWDProcID := 0;
                ioWDDirID := HPB^.ioDirID;
                Err := PBOpenWD(PBP, false);
              end;
          end;

        MoveToFolder(prefsWD);
        Err := Create(FileName, prefsWD, FileCreator, FileType);
        foundWD := prefsWD;
        Err := FSOpen(FileName, prefsWD, fRef);

      { no previous preferences, set flag indicating we created it }

        new := true;
      end
    else
      new := false;  { preferences not found and not created }

    if Err = noErr then
      begin
ZZZZ    FullPath := GetPath(foundWD);
        if length(FullPath) > maxParamLength then
          FullPath := concat(copy(FullPath, 1, maxParamLength), '...');
      end
    else
      begin
        FullPath := '';
        fref := 0;
      end;

    OpenPrefsFile := Err;
  end;


-- 
Mike Pflueger @ AG Communication Systems (formerly GTE Comm. Sys.), Phoenix, AZ
  UUCP: {...!ames!ncar!noao!asuvax | uunet!hrc | att}!gtephx!pfluegerm
  Work: 602-582-7049        FAX: 602-582-7624     Home: 602-439-1978
Packet: WD8KPZ @ KB7TV.AZ.USA.NA  Internet: gtephx!pfluegerm@asuvax.eas.asu.edu