From: jesjones@stein.u.washington.edu (Jesse Jones)
Newsgroups: comp.sys.mac.programmer
Subject: Re: code for Finding Prefs File (long)
Summary: some handy routines for resources
Date: 10 Jan 93 01:55:19 GMT
Article-I.D.: shelley.1invm7INN8rl
Organization: University of Washington, Seattle
NNTP-Posting-Host: stein.u.washington.edu


  Here are some handy routines for dealing with resources. Several of these
routines are used by the Prefs code I posted recently. A few of these routines
replace Apple's standard routines with something that behaves a little better.

   --Jesse
   
   
PROCEDURE Unique (theType: ResType): INTEGER; 
    INLINE2(0A9C1H); 

   (* The toolbox trap, UniqueID, returns an unused positive resource ID
      number. However, application resources need to be larger than 127 to
      avoid conflicts with other resources used by the System. The below 
      procedure ensures that the ID is  in the correct range. *)
PROCEDURE UniqueID (theType: ResType; system: BOOLEAN): INTEGER; 
   VAR 
      ID  : INTEGER;
      done: BOOLEAN;
BEGIN
   REPEAT
      ID := Unique(theType);      (* always returns positive IDs *)
      IF system THEN
         done := ID < 128;
      ELSE
         done := ID > 127;
      END;
   UNTIL done;
   RETURN ID;
END UniqueID;

PROCEDURE Unique1 (theType: ResType): INTEGER; 
    INLINE2(0A810H); 

PROCEDURE Unique1ID (theType: ResType; system: BOOLEAN): INTEGER; 
   VAR 
      ID  : INTEGER;
      done: BOOLEAN;
BEGIN
   REPEAT
      ID := Unique1(theType);      (* always returns positive IDs *)
      IF system THEN
         done := ID < 128;
      ELSE
         done := ID > 127;
      END;
   UNTIL done;
   RETURN ID;
END Unique1ID;

PROCEDURE GetResourceName (rsrc: HANDLE): Mstring;
   VAR
      rID  : INTEGER;
      kind : ResType;
      rName: Str255;
BEGIN
   GetResInfo(rsrc, rID, kind, rName);
   RETURN PStrToStr(rName);
END GetResourceName;

PROCEDURE GetResourceID (rsrc: HANDLE): INTEGER;
   VAR
      rID  : INTEGER;
      kind : ResType;
      rName: Str255;
BEGIN
   GetResInfo(rsrc, rID, kind, rName);
   RETURN rID;
END GetResourceID;

   (* resources can be specified using either name, or ID (if name is ""). *)
PROCEDURE InsertResource (type, name: Mstring; ID: INTEGER; rsrc: HANDLE; purgeable: BOOLEAN): BOOLEAN;
   VAR
      rName : Str255;
      rType : ResType;
      refNum: INTEGER;
      done  : BOOLEAN;
BEGIN
   done := FALSE;
   rType := StrToOS(type);
   IF name[0] = EOS THEN
      rName := "";                     
   ELSE
      rName := StrToPStr(name);         
      ID := Unique1ID(rType, FALSE);   
   END;
   AddResource(rsrc, rType, ID, rName);
   IF ErrCheck(ResError()) THEN
      IF purgeable THEN SetResAttrs(rsrc, resPurgeable) END;   (* clears resChanged bit! *)
      IF ErrCheck(ResError()) THEN
         ChangedResource(rsrc);
         IF ErrCheck(ResError()) THEN
            refNum := CurResFile();
            UpdateResFile(refNum);
            done := ErrCheck(ResError());
         END;
      END;
   END;    
   RETURN done;
END InsertResource;
   
PROCEDURE ChangeResource (rsrc: HANDLE): BOOLEAN;
   VAR
      refNum: INTEGER;
      done  : BOOLEAN;
BEGIN
   done := FALSE;
   ChangedResource(rsrc);
   IF ErrCheck(ResError()) THEN
      refNum := CurResFile();
      UpdateResFile(refNum);
      done := ErrCheck(ResError());
   END;
   RETURN done;
END ChangeResource;
   
PROCEDURE RemoveResource (rsrc: HANDLE);
   VAR
      refNum: INTEGER;
      done  : BOOLEAN;
BEGIN
   RmveResource(rsrc);
   IF ErrCheck(ResError()) THEN
      refNum := CurResFile();
      UpdateResFile(refNum);
      KillHandle(rsrc);
      done := ErrCheck(ResError());
   END;    
END RemoveResource;
Newsgroups: comp.sys.mac.programmer,comp.os.ms-windows.programmer.tools,comp.windows.ms.programmer