[comp.sys.mac.programmer] I want to write Add1ResMenu

engber@ils.nwu.edu (Mike Engber) (04/16/91)

I have need of Add1ResMenu(), a function which would behave
like AddResMenu(), but only search the current rsrc file.
Here`s my current attempt:

void Add1ResMenu(MenuHandle theMenu, ResType theType){
    short    i;
    short    resCount;
    
    resCount = Count1Resources(theType);
    for(i=1; i<=resCount; ++i){
        short    resId;
        ResType  resType;
        Str255   resName;
        Handle   resHandle;
        
        resHandle = Get1IndResource(theType,i);
        if(resHandle){
            GetResInfo(resHandle,&resId,&resType,resName);
            if(resName[0]>0 && resName[1]!='.' && resName[1]!='%'){
                AppendMenu(theMenu,resName);
            }
        }
    }
} 

The problem is that I'd like to call ReleaseResource on resources that
haven`t already been gotten (i.e. someone previously called GetResource
or GetNamedResource on it). But how do I know if a resource has already
been gotten?

And while your at it. There is one more resouce file question I have.
Is there any way I can get a list of all the currently opened resource
files and/or check if a particular resource file is already open.

Please respond via email if possible.

-ME

ech@cbnewsk.att.com (ned.horvath) (04/17/91)

From article <1387@anaxagoras.ils.nwu.edu>, by engber@ils.nwu.edu (Mike Engber):
> I have need of Add1ResMenu(), a function which would behave
> like AddResMenu(), but only search the current rsrc file.
> Here`s my current attempt:

> void Add1ResMenu(MenuHandle theMenu, ResType theType){
>     short    i;
>     short    resCount;
>     
>     resCount = Count1Resources(theType);
	SetResLoad (FALSE);
>     for(i=1; i<=resCount; ++i){
	...
>         resHandle = Get1IndResource(theType,i);
	...
>     }
	SetResLoad (TRUE);
> } 

> The problem is that I'd like to call ReleaseResource on resources that
> haven`t already been gotten (i.e. someone previously called GetResource
> or GetNamedResource on it). But how do I know if a resource has already
> been gotten?

Don't sweat the ReleaseResource calls, just insert the SetResLoad() calls
where indicated.  This may tie up some extra master pointers, but that's
it.  You could walk the internal resource map instead, but that is likely
to break someday.
 
> And while your at it. There is one more resouce file question I have.
> Is there any way I can get a list of all the currently opened resource
> files and/or check if a particular resource file is already open.

You can walk the list of resource files from TopMapHndl (Handle to resource
map of the most recently opened file; long at 0xA50).  The resource map is
described on p.I-129 of Inside Mac, but suffice to say that there's a
long at offset 16 bytes which is the handle of the next map, and a short at
offset 20 which is the refNum of the open resource file.  The refNum can
be used in a call to PBGetFCBInfo (IM IV-179) to find out which file
that is.  Yeah, that could break too, but I know of no "sanctioned" way
to get the info you crave...

Hope that helps.

=Ned Horvath=
ehorvath@attmail.com

cs483106@umbc5.umbc.edu (cs483106) (04/18/91)

In article <1991Apr16.180052.12246@cbnewsk.att.com> ech@cbnewsk.att.com (ned.horvath) writes:
>From article <1387@anaxagoras.ils.nwu.edu>, by engber@ils.nwu.edu (Mike Engber):
>> And while your at it. There is one more resouce file question I have.
>> Is there any way I can get a list of all the currently opened resource
>> files and/or check if a particular resource file is already open.
>
>You can walk the list of resource files from TopMapHndl (Handle to resource
>map of the most recently opened file; long at 0xA50).  The resource map is
>described on p.I-129 of Inside Mac, but suffice to say that there's a
>long at offset 16 bytes which is the handle of the next map, and a short at
>offset 20 which is the refNum of the open resource file.  The refNum can
>be used in a call to PBGetFCBInfo (IM IV-179) to find out which file
>that is.  Yeah, that could break too, but I know of no "sanctioned" way
>to get the info you crave...
>
>Hope that helps.
>
>=Ned Horvath=
>ehorvath@attmail.com

I don't know if it's sanctioned or not, but I have a simple technique for
finding out if a resource file is open or not. Simply do an OpenRF on the file
in question. If the resource file is open, then the Resource manager will have
write access to the file, and you will not be able to open it(you will get
an error). If the file isn't open, you can get access. Just remember to do
an FSClose if you do get access, and you're all set.
(note that this could fail when trying to open something that's on a server.
 If another machine, also attached to the server, were to do an OpenResFile
 in between your OpenRF and your OpenResFile, you could think the file was
 available, but then get an error when you try to open it. No big thing, in
 most cases.)

"Pope" Q.E.D
Michael Kohne
mikek@isis.ngs.noaa.gov

time@ice.com (Tim Endres) (04/18/91)

In article <1991Apr18.002228.25647@umbc3.umbc.edu>, cs483106@umbc5.umbc.edu (cs483106) writes:
> I don't know if it's sanctioned or not, but I have a simple technique for
> finding out if a resource file is open or not. Simply do an OpenRF on the file
> in question. If the resource file is open, then the Resource manager will have
> write access to the file, and you will not be able to open it(you will get
> an error). If the file isn't open, you can get access. Just remember to do
> an FSClose if you do get access, and you're all set.
> (note that this could fail when trying to open something that's on a server.
>  If another machine, also attached to the server, were to do an OpenResFile
>  in between your OpenRF and your OpenResFile, you could think the file was
>  available, but then get an error when you try to open it. No big thing, in
>  most cases.)


This could be dangerous, as a file opened as READONLY might succeed.

It is simpler to do a PBGetFInfo(). The ioFlAttrib
flags will tell if either or both forks are open and which.

tim.

-------------------------------------------------------------
Tim Endres                |  time@ice.com
ICE Engineering           |  uupsi!ice.com!time
8840 Main Street          |  Voice            FAX
Whitmore Lake MI. 48189   |  (313) 449 8288   (313) 449 9208

lsr@Apple.COM (Larry Rosenstein) (04/19/91)

In article <1991Apr18.002228.25647@umbc3.umbc.edu> cs483106@umbc5.umbc.edu (cs483106) writes:
>
>finding out if a resource file is open or not. Simply do an OpenRF on the file
>in question. If the resource file is open, then the Resource manager will have
>write access to the file, and you will not be able to open it(you will get

It is possible to open a resource file read-only, with OpenRFPerm, so this
technique won't work.  Ithink it is easier to just call PBGetFInfo and check
the file attributes (Inside Mac 4 p. 122).

-- 
Larry Rosenstein, Apple Computer, Inc.

lsr@apple.com
(or AppleLink: Rosenstein1)