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

#include <z_int.hpp>
#include <z_str.hpp>
#include <z_real.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 sscanf(const 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(true, buff, format, args, newFormat, &newArgs);
	int i = ZafStandardArg::_vsscanf(dst, newFormat, newArgs);
	delete []newFormat;
#else
	int i = ZafStandardArg::_vsscanf(dst, format, &args);
#endif
	va_end(args);
	return i;
}

int ZafStandardArg::vsscanf(const 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(true, buff, format, args, newFormat, &newArgs);
	int i = ZafStandardArg::_vsscanf(dst, newFormat, newArgs);
	delete []newFormat;
#else
	int i = ZafStandardArg::_vsscanf(dst, format, args);
#endif
	return (i);
}

static struct
{
	ZafIChar fmtCH;
	objectParse Parse;
} table[40];
static int tableEntries = 0;

void ZafStandardArg::SetSscanf(ZafIChar fmtch, objectParse parse)
{
	table[tableEntries].fmtCH = fmtch;
	table[tableEntries].Parse = parse;
	tableEntries++;
}

// -- Code below here was derived from BSD

#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)vfscanf.c	5.6 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */

#if defined(__SC__) && !defined(macintosh)
#	undef va_arg
#	define va_arg(ap,type)	   (*(type *)((*(char **)&(ap)+=__va_size(type))-(__va_size(type))))
#endif

#define	BUF	40

/*
 * Conversion types.
 */
#define	CT_CCL		1	/* %[...] conversion */

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

	return (ZafFormatData::_vsscanf(fp, fmt, ap));

}

