[comp.sys.mac.programmer] How do you create new resources from within program?

gt4586c@prism.gatech.EDU (WILLETT,THOMAS CARTER) (02/22/91)

i'm trying to add a resource to a file being written out by my program.  i'm
creating the resource file associated with the data file already written out
and i want to put a 'STR ' resource into it.  i think my problem is at the
point of AddResource and the handle to the data.  what is the contents of the
handle supposed to be?  thanks in advance.


-- 
thomas willett 
Georgia Institute of Technology, Atlanta  
gt4586c@prism.gatech.edu
"Violence is the last refuge of the incompetent." - Salvor Hardin (Foundation)

jwinterm@jarthur.Claremont.EDU (Jim Wintermyre) (02/26/91)

To: gt4586c@prism.gatech.EDU
Subject: Re: How do you create new resources from within program?
Newsgroups: comp.sys.mac.programmer
In-Reply-To: <22599@hydra.gatech.EDU>
Organization: Harvey Mudd College, Claremont, CA 91711
Cc: 
Bcc: 

In article <22599@hydra.gatech.EDU> you write:
>i'm trying to add a resource to a file being written out by my program.  i'm
>creating the resource file associated with the data file already written out
>and i want to put a 'STR ' resource into it.  i think my problem is at the
>point of AddResource and the handle to the data.  what is the contents of the
>handle supposed to be?  thanks in advance.
>
>
>-- 
>thomas willett 
>Georgia Institute of Technology, Atlanta  
>gt4586c@prism.gatech.edu
>"Violence is the last refuge of the incompetent." - Salvor Hardin (Foundation)

The data for a 'STR ' resource should be a Pascal string (length byte at 
beginning). The StringHandle data type (IM 1-78) is particularly convenient
for adding resources of type 'STR '.  The following code fragment shows how
to add a resource of this type.  First you set a variable of type Str255 to 
whatever it is you want to put into the resource.  Then you can use the
Toolbox Utilities function NewString (IM 1-468) to allocate space for a 
StringHandle to the string.  A neat feature of this function is that the
amount of space it allocates is determined by the length of the string.  So, 
your resource won't take up 256 bytes when the string is only 2 characters
long.  

	procedure AddResStr;
	 var
  	  theText: Str255;
	  myString: StringHandle;
          resErr:integer;
	begin
         theText:= 'This is how you do it.'
	 myString := NewString(theText);
	 AddResource(Handle(myString), 'STR ', 200, 'String Resource');
	 resErr := ResError;
	 if resErr <> noErr then
	  DoResError;
	end;		{AddRes}


With other resource types it might not be so easy.  If the StringHandle data
type weren't defined, and there was no NewString function, you'd have to do
something more like this.  The first thing you would do is create a new handle
the size of the string + 1 byte for the length byte.  To make it easier to
manipulate the data, you could create your own data type to cast the handle to:
	
	type myString = array[0..0] of char;  {to allow for differing lengths}
 	     myStrPtr = ^myString;
	     myStrHandle = ^myPtr;

	var  theStuff:myStrHandle;

Then:	theStuff := myStrHandle(NewHandle({LENGTH OF STRING + 1}));
        theStuff^^[0]:=length;	{length of string}
        .... then fill out the rest of the array....
and call AddResource.

I hope that this made sense, and that it helped you at least a little.
P.S. This is the first time I've used rn, so I hope this makes it back!

- Jim Wintermyre

Ken.Knight@f421.n109.z1.fidonet.org (Ken Knight) (02/27/91)

jwinterm@jarthur.Claremont.EDU (Jim Wintermyre) writes
>The data for a 'STR ' resource should be a Pascal string (length byte
>at
>beginning). The StringHandle data type (IM 1-78) is particularly
>convenient
>for adding resources of type 'STR '.  The following code fragment
>shows how
>to add a resource of this type.  First you set a variable of type
>Str255 to
>whatever it is you want to put into the resource.  Then you can use
>the
>Toolbox Utilities function NewString (IM 1-468) to allocate space for
>a
>StringHandle to the string.  A neat feature of this function is that
>the
>amount of space it allocates is determined by the length of the
>string.  So,
>your resource won't take up 256 bytes when the string is only 2
>characters
>long.
>
>        procedure AddResStr;
>         var
>          theText: Str255;
>          myString: StringHandle;
>          resErr:integer;
>        begin
>         theText:= 'This is how you do it.'
>         myString := NewString(theText);
>         AddResource(Handle(myString), 'STR ', 200, 'String
>Resource');
>         resErr := ResError;
>         if resErr <> noErr then
>          DoResError;
>        end;            {AddRes}
>
> [deleted]
 
One thing to remember is that the resource isn't actually written out
to the resource fork of the file until a _WriteResource, _CloseResFile,
or _UpdateResFile call is made. If you many resources are being added
it is probably a good idea to do an _UpdateResFile so that everything
is saved. Also, AddResource will overwrite resources of the same type
of there are ID conflicts. So, if you want to play it COMPLETELY safe
calling _UniqueID isn't a bad idea.


--  

        Ken Knight, Ken.Knight@f421.n109.z1.fidonet.org
      via The Black Cat's Shack's FidoNet<->Usenet Gateway
          blkcat.fidonet.org   and   Fidonet 1:109/401