[comp.sys.mac.programmer] ANSI library in THINK C 4.0

CXT105@psuvm.psu.edu (Christopher Tate) (03/10/90)

Does anyone know how to build a library containing the floating-point sprintf()
function, so that you don't have to include the entire ANSI library just to
get that one feature?

I know this was batted around quite a bit lately, but it still seems awfully
unwieldy to have to bring the whole kit and kaboodle along just to get a
particular little routine.  Should I just bite the bullet and write my own?

(Gack!  I can't believe I said that!)

-------
Christopher Tate                  |  "Hear perfect strangers call you by name,
cxt105@psuvm.psu.edu              |   Pay good money to hear "Fire and Rain"
 ..!psuvax1!psuvm.bitnet!cxt105   |   Again and again and again....
cxt105@psuvm.bitnet               |   That's why I'm here!"   -- James Taylor

oster@well.sf.ca.us (David Phillip Oster) (03/11/90)

In article <90068.135735CXT105@psuvm.psu.edu> cxt105@psuvm.psu.edu writes:
>Does anyone know how to build a library containing the floating-point sprintf()
>function, so that you don't have to include the entire ANSI library just to
>get that one feature?
Remember that every Mac has SANE built in. Check your SANE Numerics manual
for details, and remember that this is THINK C v4, but here is what I use:

#include <sane.h>
/* StringToFloat - convert a string to a floating point number.
 */
Boolean StringToFloat(s, fp)StringPtr s;float *fp;{
	short	index;
	double 	ff;
	Decimal	d;
	DecForm	df;
	Boolean	prefix;
	
	index = 1;
	Str2Dec(s, &index, &d, &prefix);
	df.style = FIXEDDECIMAL;
	df.digits = d.sig.length;
	fp68k(&df, &d, &ff, FOD2B);
	*fp = ff;
	return index != 1;
}

How script manager compatible is this? Is there a preferred way that 
will format numbers correctly with full script manager compatibility?

(Remember that in some countries, decimal point is spelled ",").
-- 
-- David Phillip Oster - Note new address. Old one has gone Bye Bye.
-- oster@well.sf.ca.us = {backbone}!well!oster

siegel@endor.harvard.edu (Rich Siegel) (03/12/90)

In article <90068.135735CXT105@psuvm.psu.edu> cxt105@psuvm.psu.edu writes:
>Does anyone know how to build a library containing the floating-point sprintf()
>function, so that you don't have to include the entire ANSI library just to
>get that one feature?

	If you duplicate the ANSI-small library, open the copy, and then
choose "ansi_config.h" from the project pop-up menu, you can comment
out the #define NO_FLOATING, rebuild the project, and you'll have a small
ANSI library with floating sprintf and scanf.

R.



~~~~~~~~~~~~~~~
 Rich Siegel
 Staff Software Developer
 Symantec Corporation, Language Products Group
 Internet: siegel@endor.harvard.edu
 UUCP: ..harvard!endor!siegel

"When someone who makes four hundred and fifty dollars an hour wants to
tell you something for free, it's a good idea to listen."

~~~~~~~~~~~~~~~

Christopher Tate <CXT105@psuvm.psu.edu> (03/13/90)

Just to let you all know, here's how I eventually built myself a fully-
functional (including floating point formats) sprintf() routine, without
having to include the entire ANSI library into my project:

I traced out all of the calling dependencies for the sprintf() function,
as defined in the ANSI source files (included with C).  Then, I created
my own working copies of the necessary files (stdio.c, printf.c, mem.c,
bufio.c, str.c, fwrite.c) and added them to a project (called sprintf.pi).

THEN, I eliminated from these working source files *all* routines that
could not be invoked by a call to sprintf(), and compiled the project.

This gave me a 3800-byte sprintf project that I could include in any
application that needed it, without having to include any form of ANSI
library to get it.  The full ANSI library is just under 28,000 bytes,
much of which does not get stripped away by the linker.  When I used
the ANSI library, my application linked (with Smart Link) to 37K.  When
I replaced ANSI with my sprintf project, it compiled to 21K.  (!)

Note:  Rich Siegel suggested that I simply rebuild the "ANSI-small"
library after commenting-out the "#define _NOFLOATING_" statement in
the ansi_config.h header file.  Thanks, Rich; that's a great idea if I
ever need to use more of the library, but don't want all of the extras
that come along with the full ANSI set.  For this application, though,
*all* I needed was a floating-point-capable sprintf() function.

-------
Christopher Tate                    |  "And as I watch the drops of rain
                                    |   Weave their weary paths and die,
cxt105@psuvm.psu.edu                |   I know that I am like the rain;
{...}!psuvax1!psuvm.bitnet!cxt105   |   There but for the grace of you go I."
cxt105@psuvm.bitnet                 |          -- Simon & Garfunkle