//	Zinc Application Framework - Z_PRINTF.CPP
//	COPYRIGHT (C) 1990-1999.  All Rights Reserved.
//	Zinc Software Incorporated.  Pleasant Grove, Utah  USA

#include <z_int.hpp>
#include <z_real.hpp>
#include <z_str.hpp>
#include <z_ctype.hpp>
#include <z_stdio.hpp>
#include <z_stdarg.hpp>
#include <z_string.hpp>

#if !defined(ZAF_UNICODE)
#	define sprintf		ZafSPrintf
#	define sscanf		ZafSScanf
#endif

int sprintf(ZafIChar *dst, const ZafIChar *format, ...)
{
	va_list args;
	va_start(args, format);
#if defined(ZAF_REARRANGEARGS)
	ZafIChar *newFormat = new ZafIChar[ZAF_MAXPARAMLEN];
	char buff[ZAF_MAXPARAMLEN];	// Borland expects this in the stack.
	va_list newArgs;
	ZafStandardArg::RearrangeArgs(false, buff, format, args, newFormat, &newArgs);
	int i = ZafStandardArg::_vsprintf(dst, newFormat, &newArgs);
	delete []newFormat;
#else
	int i = ZafStandardArg::_vsprintf(dst, format, &args);
#endif
	va_end(args);
	return (i);
}

/* START BLOCK COMMENT
**	int ZafStandardArg::vsprintf(ZafIChar *dst, const ZafIChar *format, va_list *args)
**	{
**	#if defined(ZAF_REARRANGEARGS)
**		ZafIChar *newFormat = new ZafIChar[ZAF_MAXPARAMLEN];
**		char buff[ZAF_MAXPARAMLEN];	// Borland expects this in the stack.
**		va_list newArgs;
**		ZafStandardArg::RearrangeArgs(false, buff, format, args, newFormat, &newArgs);
**		int i = ZafStandardArg::_vsprintf(dst, newFormat, newArgs);
**		delete []newFormat;
**	#else
**		int i = ZafStandardArg::_vsprintf(dst, format, args);
**	#endif
**		return (i);
**	}
END BLOCK COMMENT */

static struct
{
	ZafIChar fmtCH;
	objectFormat Format;
} table[40];
static int tableEntries = 0;

void ZafStandardArg::SetSprintf(ZafIChar fmtch, objectFormat format)
{
	table[tableEntries].fmtCH = fmtch;
	table[tableEntries].Format = format;
	tableEntries++;
}

// -- Code below here was derived from BSD

#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)vfprintf.c	5.49 (Berkeley) 6/2/92";
#endif /* LIBC_SCCS and not lint */

/*
 * Actual printf innards.
 *
 * This code is large and complicated...
 */

typedef unsigned long U_QUAD_T;
typedef long QUAD_T;

/*
 * Macros for converting digits to letters and vice versa
 */
// For format string (always in Arabic numerals)
#define to_digit(c)	((c) - '0')
#define is_digit(c)	((unsigned int)to_digit(c) <= 9)

#define	BUF		40

int ZafStandardArg::_vsprintf(ZafIChar *fp, const ZafIChar *fmt, va_list *ap)
{
	ZafIntegerData::Bind();
	ZafStringData::Bind();
	ZafRealData::Bind();

	return (ZafFormatData::_vsprintf(fp, fmt, ap));
}

