home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / sys / mac / programm / 21098 < prev    next >
Encoding:
Internet Message Format  |  1993-01-09  |  3.9 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!uwm.edu!ogicse!news.u.washington.edu!stein.u.washington.edu!jesjones
  2. From: jesjones@stein.u.washington.edu (Jesse Jones)
  3. Newsgroups: comp.sys.mac.programmer
  4. Subject: Re: code for Finding Prefs File (long)
  5. Summary: some handy routines for resources
  6. Message-ID: <1invm7INN8rl@shelley.u.washington.edu>
  7. Date: 10 Jan 93 01:55:19 GMT
  8. Article-I.D.: shelley.1invm7INN8rl
  9. References: <D2150096.mv05jr@sproul.sproul.com> <1ilnplINNbck@shelley.u.washington.edu>
  10. Organization: University of Washington, Seattle
  11. Lines: 129
  12. NNTP-Posting-Host: stein.u.washington.edu
  13.  
  14.  
  15.   Here are some handy routines for dealing with resources. Several of these
  16. routines are used by the Prefs code I posted recently. A few of these routines
  17. replace Apple's standard routines with something that behaves a little better.
  18.  
  19.    --Jesse
  20.    
  21.    
  22. PROCEDURE Unique (theType: ResType): INTEGER; 
  23.     INLINE2(0A9C1H); 
  24.  
  25.    (* The toolbox trap, UniqueID, returns an unused positive resource ID
  26.       number. However, application resources need to be larger than 127 to
  27.       avoid conflicts with other resources used by the System. The below 
  28.       procedure ensures that the ID is  in the correct range. *)
  29. PROCEDURE UniqueID (theType: ResType; system: BOOLEAN): INTEGER; 
  30.    VAR 
  31.       ID  : INTEGER;
  32.       done: BOOLEAN;
  33. BEGIN
  34.    REPEAT
  35.       ID := Unique(theType);      (* always returns positive IDs *)
  36.       IF system THEN
  37.          done := ID < 128;
  38.       ELSE
  39.          done := ID > 127;
  40.       END;
  41.    UNTIL done;
  42.    RETURN ID;
  43. END UniqueID;
  44.  
  45. PROCEDURE Unique1 (theType: ResType): INTEGER; 
  46.     INLINE2(0A810H); 
  47.  
  48. PROCEDURE Unique1ID (theType: ResType; system: BOOLEAN): INTEGER; 
  49.    VAR 
  50.       ID  : INTEGER;
  51.       done: BOOLEAN;
  52. BEGIN
  53.    REPEAT
  54.       ID := Unique1(theType);      (* always returns positive IDs *)
  55.       IF system THEN
  56.          done := ID < 128;
  57.       ELSE
  58.          done := ID > 127;
  59.       END;
  60.    UNTIL done;
  61.    RETURN ID;
  62. END Unique1ID;
  63.  
  64. PROCEDURE GetResourceName (rsrc: HANDLE): Mstring;
  65.    VAR
  66.       rID  : INTEGER;
  67.       kind : ResType;
  68.       rName: Str255;
  69. BEGIN
  70.    GetResInfo(rsrc, rID, kind, rName);
  71.    RETURN PStrToStr(rName);
  72. END GetResourceName;
  73.  
  74. PROCEDURE GetResourceID (rsrc: HANDLE): INTEGER;
  75.    VAR
  76.       rID  : INTEGER;
  77.       kind : ResType;
  78.       rName: Str255;
  79. BEGIN
  80.    GetResInfo(rsrc, rID, kind, rName);
  81.    RETURN rID;
  82. END GetResourceID;
  83.  
  84.    (* resources can be specified using either name, or ID (if name is ""). *)
  85. PROCEDURE InsertResource (type, name: Mstring; ID: INTEGER; rsrc: HANDLE; purgeable: BOOLEAN): BOOLEAN;
  86.    VAR
  87.       rName : Str255;
  88.       rType : ResType;
  89.       refNum: INTEGER;
  90.       done  : BOOLEAN;
  91. BEGIN
  92.    done := FALSE;
  93.    rType := StrToOS(type);
  94.    IF name[0] = EOS THEN
  95.       rName := "";                     
  96.    ELSE
  97.       rName := StrToPStr(name);         
  98.       ID := Unique1ID(rType, FALSE);   
  99.    END;
  100.    AddResource(rsrc, rType, ID, rName);
  101.    IF ErrCheck(ResError()) THEN
  102.       IF purgeable THEN SetResAttrs(rsrc, resPurgeable) END;   (* clears resChanged bit! *)
  103.       IF ErrCheck(ResError()) THEN
  104.          ChangedResource(rsrc);
  105.          IF ErrCheck(ResError()) THEN
  106.             refNum := CurResFile();
  107.             UpdateResFile(refNum);
  108.             done := ErrCheck(ResError());
  109.          END;
  110.       END;
  111.    END;    
  112.    RETURN done;
  113. END InsertResource;
  114.    
  115. PROCEDURE ChangeResource (rsrc: HANDLE): BOOLEAN;
  116.    VAR
  117.       refNum: INTEGER;
  118.       done  : BOOLEAN;
  119. BEGIN
  120.    done := FALSE;
  121.    ChangedResource(rsrc);
  122.    IF ErrCheck(ResError()) THEN
  123.       refNum := CurResFile();
  124.       UpdateResFile(refNum);
  125.       done := ErrCheck(ResError());
  126.    END;
  127.    RETURN done;
  128. END ChangeResource;
  129.    
  130. PROCEDURE RemoveResource (rsrc: HANDLE);
  131.    VAR
  132.       refNum: INTEGER;
  133.       done  : BOOLEAN;
  134. BEGIN
  135.    RmveResource(rsrc);
  136.    IF ErrCheck(ResError()) THEN
  137.       refNum := CurResFile();
  138.       UpdateResFile(refNum);
  139.       KillHandle(rsrc);
  140.       done := ErrCheck(ResError());
  141.    END;    
  142. END RemoveResource;
  143.