Date: Wed, 2 Dec 92 16:28:25 PST
From: macmod@SUMEX-AIM.Stanford.EDU (Info-Mac Moderator)

orrow.stanford.edu!stanford.edu!agate!ucbvax!hplabs!sdd.hp.com!elroy.j        pl.nasa.gov!usc!davidp 
From: davidp@calvin.usc.edu (David Peterson) 
Newsgroups: comp.sys.mac.programmer 
Subject: Just for you, an early Christmas present 
Message-Id: <1fj5ksINNo34@calvin.usc.edu> 
Date: 2 Dec 92 20:17:32 GMT 
Reply-To: davidp@usc.edu 
Organization: University of Southern California, Los Angeles, CA 
Lines: 99 
Nntp-Posting-Host: calvin.usc.edu 
Apparently-To: info-mac 
Resent-To: backmod
Resent-Date: Wed, 2 Dec 1992 16:28:22 PST
Resent-From: Info-Mac Moderator <macmod@sumex-aim.Stanford.EDU>



Now you, the Mac programmer, can have pages of useless strings effortlessly
vomited to your screen just like your DOS and UNIX friends do.

With this dandy new MBPrintf routine you can make up printf style diagnostic
messages and have them show up in MacsBug correctly interpreted and formatted.

Okay, so this is just a little hack I threw together with a liberal amount
copyright infringment (see pg 156 of K&R). After a few hours of using it
I found it extremely useful, and it has earned a permanent place in my 
personal library (not unlike every other snippet of code I come across).

Hopefully you will find it just as useful, but (as always) your mileage
may vary.

Handy tip:
	Instead of putting a '\n' at the end of your string try a semi-colon
	followed by Your Favorite MacsBug Command. ';g' is probably the most
	useful and ';s 3' will take you back to the instruction right after
	the JSR into this function. Combinations of ';dx off'/';dx on' could
	be good too.

Caveats:
	This will probably blow up if called from interupt. It makes calls
	into the StdCLib segment which may or may not be loaded.

	Probably won't work in stand alone code resources because the
	aforementioned code segment contains global data.

	I don't know what will happen if your string gets over 255 characters,
	but it probably won't be what you expected.


-dave.

---------------------------

#include <StdArg.h>
#include <StdIO.h>

void MBPrintf(char* form, ...);

void
MBPrintf(char* form, ...)
{
	va_list	ap;
	char*	p;

	char*	sval;
	int		ival;
	double	dval;

	char	string[256];
	char*	strp;
	char*	strb;
	
	strp = &string[1];
	strb = &string[1];
	
	va_start(ap, form);
	
	for (p = form; *p; p++) {
		if (*p != '%') {
			*strp++ = *p;
			continue;
		}
		
		switch (*++p) {
			case 'd':
				ival = va_arg(ap, int);
				sprintf(strp, "%d", ival);
				while (*++strp);
				break;
			case 'x':
				ival = va_arg(ap, int);
				sprintf(strp, "%x", ival);
				while (*++strp);
				break;
			case 'f':
				dval = va_arg(ap, double);
				sprintf(strp, "%f", dval);
				while (*++strp);
				break;
			case 's':
				for (sval = va_arg(ap, char*); *sval; sval++)
					*strp++ = *sval;
				break;
			default:
				*strp++ = *p;
				break;
		}
	}
	
	va_end(ap);
	
	string[0] = strp - strb;
	DebugStr((Str255) string);
}