Dec 91 Mousehole
Volume Number: 7
Issue Number: 2
Column Tag: Mousehole
STR# Hurdles
By Larry Nedry, Mousehole BBS SysOp
From: Jslee
Re: STR# Resource
From the frying pan, now into the fire. As you all know the Mac has a toolbox call
called GetIndString. This is really cool, but I need to do the same function in reverse,
that is, how can I add additional STR# resources to a list of resources? I have yet to
find a code fragment demonstrating this technique. What I want to do is add a series of
strings to the STR# resource via C (Think to be precise). Looking at IM I-476 I see
that STR# resource consists of 2 bytes containing the number of strings then m bytes
as the strings. Can I take this to mean:
short num_of_strings;
str63 the_strings[max_strings];
Is this right? If not, how would I do it? I need to have a handle to each string item so
that I can write it back to the resource. Any ideas would be great.
From: Mrteague
Re: STR# Resource
You have it partly wrong - because the “strings” are Pascal strings (and it wouldn’t
matter of they were C strings), they are NOT of fixed size, which means you can’t use a
struct as you have defined. There would be a number of different ways of doing what you
need to do, but generally it involves parsing the resource structure yourself - i.e.
{1}
tempHandle = GetResource('STR#',someID);
HLock(tempHandle); tempPtr = *tempHandle;
numStrings = *(short *)tempPtr;
tempPtr += sizeof(short);
for (i = 1; i <= numStrings; i++)
{
char tempchar[256];
short theLength = *tempPtr++;
strncpy(tempchar, tempPtr, theLength);
dosomethingwithstring(tempchar);
tempPtr += theLength;
}
Then you have to output the new resource - either you have to create a new resource
from scratch and add each string to it at a time (sort of the reverse of above), or you
can attempt to modify the existing resource in place (remember to detach resource
first) - this gets tricky when you insert strings, delete strings, or modify the length
of existing strings.
I wrote a HyperCard XMCD that did something similar to this a long time ago, so if you
are still stuck after trying my suggestions, I will see what I can do to help.
From: Jslee
Re: Writing to an STR# resource
Hurdle # 1: Parse out the strings from an STR# resource.
Hurdle # 2: Find out how the STR# is organized so you can write back to the resource
file.
Hurdle # 3: How can I write out the data in my string to the STR# resource.
I figured out hurdle #1 and #2. #3 is a bit more perplexing. What I did was to create
a string that looks like this: “/0/3/5Test1/5Test2/0”. The reason why the string is
formatted this way is because this is the way the STR#128 resource (my own resource
file in the THINK C project) looks. Now when I create a handle to this string, and try to
write out the resource using AddResource nothing is written. When I perform a
SizeResource to my handle it returns a -1 which means that the handle to the resource
does not exist. I would like to create a handle to my string, then write it out to the
STR# resource with a new id. Here is a code fragment:
/* 2 */
do_write()
{
OSErr oserr;
Handle dstHdl;
Ptr destPtr;
int newID;
int checkdigit;
int my_resfile;
OpenResFile("\pNewRes");
my_resfile = OpenResFile("\pNewRes");
UseResFile(my_resfile);
/*
newStr looks like this:
"/0/3/5Test1/5Test2/0
*/
dstHdl = NewHandle(sizeof((char *)&newStr));
oserr = PtrToHand((char *)&newStr,dstHdl,(long)sizeof(newStr));
if (oserr)
{
/*check os error here */
}
HLock(dstHdl);
newID = UniqueID('STR#');
AddResource(dstHdl,'STR#',newID,"\p");
WriteResource(dstHdl);
checkdigit = HomeResFile(dstHdl);
/* always shows up as a -1
meaning that this is not a handle to the resource
why?,why?,why?
*/
HUnlock(dstHdl);
CloseResFile(my_resfile);
}
From: Mikel
Re: OOP Ignorance
When you say “get at” the Button, are you referring to getting control when the
button’s smacked? If so (mind you, I’ve not used THINK C objects, just MacApp), odds
are you should declare a class descendent from the type of AcceptButton (TButton,
perhaps?) and override the appropriate method. Hope this makes sense.
From: Smug1
Re: OOP Ignorance
Thanks for the help, and I will try in that region. What I want to do is, When someone
clicks on that button, to be able to send the message to my routine to do “Whatever”.
Sort of like an “On MouseDown” script in HyperCard. I HAVE tried to make a Subclass;
maybe I am doing it wrong. Using the same code as before, When I declare a new class
(MyButton) and then try to access it, it tells me that “Acceptbutton is not declared. Do
I redo the “new(AcceptButton)” routine ?
From: Mikel
Re: OOP Ignorance