[comp.sys.mac.programmer] Drawstring Question

rg2c+@andrew.cmu.edu (Robert Nelson Gasch) (11/30/90)

Hi again,
First of all, thanx alot to all of you who responded to my questions
from a couple of days ago. I was able to get the I/O to work, using
FSOpen, FSRead, ect. The FS functions are defeniteley alot easier to
use than the PB functions. The update problam was a misunderstanding
on my part and has been solved. This Mac thing is starting to make me
feel like an idiot though.

Now there is a new problem:
Since I am using a drawing Window as my output medium, I can not use
the standard output functions (printf). I want to display the values of
some variables though. Since I have to use DrawString, this seems to be
a problem. For example, how do I draw a properly formatted floating
point number?? Drawstring does not give you formatting options and
seems to require a string. How do you get around this ?? Any ideas??

Thanx again --> Rob

rodman@hpcvlx.cv.hp.com (Steve Roderick) (12/01/90)

You can still use printf via sprintf, i.e.

	sprintf(myBuffer, "%3.2f", myDouble);

	DrawText(myBuffer, 0, strlen(myBuffer)); /* parameters correct? */

	or

	CtoPStr(myBuffer);
	DrawString(myBuffer);

Steve Roderick
rodman@hpcvlx.hp.cv.com

anders@verity.com (Anders Wallgren) (12/01/90)

In article <YbJQ_DO00Uzx40mkhy@andrew.cmu.edu>, rg2c+@andrew (Robert Nelson Gasch) writes:
>Now there is a new problem:
>Since I am using a drawing Window as my output medium, I can not use
>the standard output functions (printf). I want to display the values of
>some variables though. Since I have to use DrawString, this seems to be
>a problem. For example, how do I draw a properly formatted floating
>point number?? Drawstring does not give you formatting options and
>seems to require a string. How do you get around this ?? Any ideas??
>


How about:

fprintf(some_buffer, "%f", some_float);
drawstring(some_buffer);

(This assumes you have MPW C, which gives you 

void drawstring(char *s)

but you could also convert the string to pascal and use DrawString.

anders

chou@cs.washington.edu (Pai Chou) (12/01/90)

In article <1990Nov30.202807.6139@verity.com> anders@verity.com (Anders Wallgren) writes:
>In article <YbJQ_DO00Uzx40mkhy@andrew.cmu.edu>, rg2c+@andrew (Robert Nelson Gasch) writes:
>>Now there is a new problem:
>>Since I am using a drawing Window as my output medium, I can not use
>>the standard output functions (printf). I want to display the values of
>
>How about:
>
>fprintf(some_buffer, "%f", some_float);
>drawstring(some_buffer);

It should be sprintf, not fprintf.
sprintf "prints" to a char array.

Chris.Gehlker@p12.f56.n114.z1.fidonet.org (Chris Gehlker) (12/03/90)

> Now there is a new problem:
> Since I am using a drawing Window as my output medium, I can not use
> the standard output functions (printf). I want to display the values of
> some variables though. Since I have to use DrawString, this seems to be
> a problem. For example, how do I draw a properly formatted floating
> point number?? Drawstring does not give you formatting options and
> seems to require a string. How do you get around this ?? Any ideas??

I guess num2str() is a well kept secret. Apple documents it in the SANE
Manual rather than in IM. Anyhoo, it's:
void num2str(decform *, extended, void *); in SANE.h. A decform is a
struct that set the style to normal or scientific and tells the function how
many decimal points to use for rounding.  You pass it an Str255 in the final
argument and it fills it in for you.  Then you pass it to DrawString().

See the following fragment:
...
extended x = 1.2345;
Str255outString;
decformf;
...
f.style = FIXEDDECIMAL;
f. digits = 8;
num2str(&f, x, outString);
 DrawString(outString);
...


 

--  
Uucp: ...{gatech,ames,rutgers}!ncar!asuvax!stjhmc!56.12!Chris.Gehlker
Internet: Chris.Gehlker@p12.f56.n114.z1.fidonet.org

time@tbomb.ice.com (Tim Endres) (12/04/90)

In article <31268.275A511A@stjhmc.fidonet.org>, Chris.Gehlker@p12.f56.n114.z1.fidonet.org (Chris Gehlker) writes:
> > Now there is a new problem:
> > Since I am using a drawing Window as my output medium, I can not use
> > the standard output functions (printf). I want to display the values of
> > some variables though. Since I have to use DrawString, this seems to be
> > a problem. For example, how do I draw a properly formatted floating
> > point number?? Drawstring does not give you formatting options and
> > seems to require a string. How do you get around this ?? Any ideas??
> 
> I guess num2str() is a well kept secret. Apple documents it in the SANE
> Manual rather than in IM. Anyhoo, it's:
> void num2str(decform *, extended, void *); in SANE.h. A decform is a
> struct that set the style to normal or scientific and tells the function how
> many decimal points to use for rounding.  You pass it an Str255 in the final
> argument and it fills it in for you.  Then you pass it to DrawString().
> 
> See the following fragment:
> ...
> extended x = 1.2345;
> Str255outString;
> decformf;
> ...
> f.style = FIXEDDECIMAL;
> f. digits = 8;
> num2str(&f, x, outString);
>  DrawString(outString);
> ...

--- OR ---

static char		drawf_string[512];

/*
** NOTE: The limitation on the number of arguments.
**       You may specify more if you need them.
*/
drawf(format_str, arg0, arg1, arg2, arg3, arg4, arg5)
char	*format_str;
char	*arg0, *arg1, *arg2, *arg3, *arg4, *arg5;
{
	sprintf(drawf_string, format_str,
			arg0, arg1, arg2, arg3, arg4, arg5);
	c2pstr(drawf_string);
	DrawString(drawf_string);
	}

main()
{
	drawf("Print %s", "Hello World");
	}


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

peirce@outpost.UUCP (Michael Peirce) (12/04/90)

In article <1CE00001.ale2i0@tbomb.ice.com>, time@tbomb.ice.com (Tim Endres) writes:
> 
> 
> In article <31268.275A511A@stjhmc.fidonet.org>, Chris.Gehlker@p12.f56.n114.z1.fidonet.org (Chris Gehlker) writes:
> > > Now there is a new problem:
> > > Since I am using a drawing Window as my output medium, I can not use
> > > the standard output functions (printf). I want to display the values of
> > > some variables though. Since I have to use DrawString, this seems to be
> > > a problem. For example, how do I draw a properly formatted floating
> > > point number?? Drawstring does not give you formatting options and
> > > seems to require a string. How do you get around this ?? Any ideas??
> --- OR ---
> 
> static char		drawf_string[512];
> 
> /*
> ** NOTE: The limitation on the number of arguments.
> **       You may specify more if you need them.
> drawf(format_str, arg0, arg1, arg2, arg3, arg4, arg5)

Why not make use of a variable size parameter list, then use vsprintf?
K & R (at least the new ANSI version) discusses the use of these.
Also look in StdArg.h & StdIO.h

-- michael


--  Michael Peirce         --   {apple,decwrl}!claris!outpost!peirce
--  Peirce Software        --   Suite 301, 719 Hibiscus Place
--  Macintosh Programming  --   San Jose, California 95117
--         and Consulting  --   (408) 244-6554

vd09+@andrew.cmu.edu (Vincent M. Del Vecchio) (12/05/90)

Oh, I don't know.  I avoid printf and scanf whenever possible.  Among
other things, they're fairly large and relatively inefficient when the
format string is a constant.  On the other hand, I do wish there was a
more standard way to bypass them, perhaps itoa and ftoa, the reverses of
atoi and atof....