[comp.sys.mac] Resource file HELP

sn0v+@andrew.cmu.edu (Shoichi Nakai) (09/18/88)

Hello,
I am a novice C programmer and am writing my first C program for the
Macintosh.  What this is going to do is to read a text from a resource
file and to display it in a window.  I got a window created successfully,
but I couldn't get a right text in a window instead I got a garbage.
Would someone help me?

I created in advance a resource file by using ResEdit and wrote a code
something like

   TEHandle teh;
   Handle   hte;
   long     len;

   teh = TENew(&dest_rect, &view_rect);

   hte = GetIndResource((ResType)"TEXT", (int)256);
   len = SizeResource(hte);
   TESetText(*hte, len, teh);

According to the debugger, after calling GetIndResource, hte still
pointed to 0x000000.  I have been struggling with this problem for a
couple of weeks but still I couldn't get out of it.
I am using LightspeedC 3.0 on Macintosh SE.
Any suggestion would be greatly appreciated.  Thank you in advance.

Shoichi Nakai
sn0v@andrew.cmu.edu

levin@bbn.com (Joel B Levin) (09/19/88)

In article <MXAyfcy00Xc=MAOEQe@andrew.cmu.edu> sn0v+@andrew.cmu.edu (Shoichi Nakai) writes:
(I am a novice C programmer and am writing my first C program for the
(Macintosh.  What this is going to do is to read a text from a resource
(file and to display it in a window.  I got a window created successfully,
(but I couldn't get a right text in a window instead I got a garbage.
( . . .
(   hte = GetIndResource((ResType)"TEXT", (int)256);
( . . .
(According to the debugger, after calling GetIndResource, hte still
(pointed to 0x000000. . . .

Try . . . (ResType)'TEXT' . . .  in the above line, using single
quotes.  "TEXT" gives a pointer to a string; 'TEXT' gives the four
ascii characters in a long, which is what you need.

	/JBL
UUCP:     {backbone}!bbn!levin		POTS: (617) 873-3463
INTERNET: levin@bbn.com

chrisj@ut-emx.UUCP (Chris Johnson) (09/20/88)

A big part of your problem is that you're using GetIndResource() instead of
GetResource(), which, I suspect, is really what you had in mind.  
GetIndResource('TEXT', 256) will attempt to get the 256th resource of type
text in your resource file.  GetResource('TEXT', 256) will simply attempt
to get a resource of type 'TEXT' whose ID is 256.

Also, if all you want to do is display some text in a window, you may find it
easier to use the TextBox() procedure.

The following source should work, assuming that the window you want to draw 
into is visible and is also the current port.

Handle 				TextHand;
long				TextSize;

TextHand = GetResource('TEXT', 256);
if (TextHand != noErr) {

	LoadResource(TextHand);
	if (ResErr == noErr) {
		
		TextSize = GetHandleSize(TextHand);
		if (MemErr == noErr) {
		
			HLock(TextHand);
		
			TextBox(*TextHand, TextSize, &view_rect, teJustLeft);
			
			HUnlock(TextHand);
		}
	}
}

Hope this helps.

----Chris

chrisj@ut-emx.UUCP (Chris Johnson) (09/20/88)

A brief note on the source I posted.

The line:  if (TextHand != noErr) {

Should actually read:  if (TextHand != NULL) {

By coincidence, noErr and NULL both equal zero so the example
works anyway, but it isn't really correct.  With most resource
manager calls you would check the global ResErr to see if the
call completed properly, but there's a bug in GetResourse() that
permits it to return in noErr even in cases where it was unable to
get a resource, so with GetResource() it's best to check the handle
it returns to see if it is equal to NULL or not.  (If it is NULL,
the call failed.)

I was typing faster than I was thinking, sorry.

----Chris

shane@chianti.cc.umich.edu (Shane Looker) (09/20/88)

In article <MXAyfcy00Xc=MAOEQe@andrew.cmu.edu> sn0v+@andrew.cmu.edu (Shoichi Nakai) writes:
>   hte = GetIndResource((ResType)"TEXT", (int)256);
                                  ^    ^
  These should be single quotes.  Insead of "TEXT", you should have 'TEXT'.
The compiler is trying to push the address of the string "TEXT\0" onto the
stack instead of the four characters TEXT.

>
>Shoichi Nakai
>sn0v@andrew.cmu.edu

p.s.  You should post this to comp.sys.mac.programmer instead of comp.sys.mac.
 
Shane Looker
Shane Looker   |  Looker@um.cc.umich.edu
"Subtle as a chainsaw, lacking all the social graces..."

clive@drutx.ATT.COM (Clive Steward) (09/20/88)

From article <29813@bbn.COM>, by levin@bbn.com (Joel B Levin):
> 
> Try . . . (ResType)'TEXT' . . .  in the above line, using single
> quotes.  "TEXT" gives a pointer to a string; 'TEXT' gives the four
> ascii characters in a long, which is what you need.
> 

Good advice, and one more thing to do is to use the error function
after the line where you try to get it:

 hte = GetIndResource('TEXT', 256);

 if ((theError = ResError ()) == noErr) {

     ...note what happened with an alert, and skip 
        without trying to use the handle
 }

 This way, you won't get bombs in your program, no matter what (like 
 trying this on a file with no 'TEXT' resource of the right number, 
 for example.
 
 Notice above, by the way, that no cast is necessary for 'TEXT' if it's
 a 4-character constant as required, nor for the int.  With LSC,
 you only have to worry where a long (4 bytes) needs to be passed, and you 
 begin with something which looks like an int or short (2 bytes).

 Welcome to the wonderful world of Mac programming.


 Clive Steward