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

#include <z_loc.hpp>
#include <z_string.hpp>
#include <z_cset.hpp>
#include <z_stdlib.hpp>
#include <locale.h>
#include <limits.h>

#if defined(ZAF_UNICODE)
#	define LocalCopy(x, y) zafCodeSet->ConvertToZafString(y, x, false)
#else
#	define LocalCopy(x, y) strcpy(x, y)
#endif

bool ZafLocaleData::OSLocaleInformation(ZafLocaleStruct &localeInfo)
{
#if defined(ZAF_DISABLE_INT21)
	// Get the locale information.  (Probably brain-damaged.)
	struct lconv *conv = localeconv();
	LocalCopy(localeInfo.decimalSeparator, conv->decimal_point);
	LocalCopy(localeInfo.monDecimalSeparator, conv->mon_decimal_point);
	LocalCopy(localeInfo.thousandsSeparator, conv->thousands_sep);
	LocalCopy(localeInfo.monThousandsSeparator, conv->mon_thousands_sep);
	LocalCopy(localeInfo.intCurrencySymbol, conv->int_curr_symbol);
	if (conv->p_cs_precedes != CHAR_MAX)
		localeInfo.posCurrencyPrecedes = conv->p_cs_precedes;
	if (conv->frac_digits != CHAR_MAX)
		localeInfo.fractionDigits = conv->frac_digits;
	if (conv->p_sep_by_space != CHAR_MAX)
		localeInfo.posSpaceSeparation = conv->p_sep_by_space;
	LocalCopy(localeInfo.negativeSign, conv->negative_sign);
	if (conv->n_sign_posn != CHAR_MAX)
		localeInfo.negSignPrecedes = conv->n_sign_posn;
	if (conv->n_sep_by_space != CHAR_MAX)
		localeInfo.negSpaceSeparation = conv->n_sep_by_space;

#	if defined(__ZTC__) && (__ZTC__ < 0x310)
	if (conv->n_sc_precedes != CHAR_MAX)
		localeInfo.negCurrencyPrecedes = conv->n_sc_precedes;
#	else	// !ZTC 3.1
	strcpy(localeInfo.grouping, conv->grouping);
	strcpy(localeInfo.monGrouping, conv->mon_grouping);
	if (conv->n_cs_precedes != CHAR_MAX)
		localeInfo.negCurrencyPrecedes = conv->n_cs_precedes;
	if (conv->int_frac_digits != CHAR_MAX)
		localeInfo.intFractionDigits = conv->int_frac_digits;
//???
#		if defined(USE_ZAF_CODE_SET_DATA)
	zafCodeSet->ConvertToZafString(conv->positive_sign, localeInfo.positiveSign);
#		else
	strcpy(conv->positive_sign, localeInfo.positiveSign);
#		endif
	if (conv->p_sign_posn != CHAR_MAX)
		localeInfo.posSignPrecedes = conv->p_sign_posn;
#	endif	// !ZTC 3.1
#else

	switch (dos3Info.dateFormat)
	{
	case 0:
		delete []localeInfo.dateStringOutputFormat;
		localeInfo.dateStringOutputFormat = strdup(ZAF_ITEXT("%v/%e/%Y"));
		break;
	case 1:
		delete []localeInfo.dateStringOutputFormat;
		localeInfo.dateStringOutputFormat = strdup(ZAF_ITEXT("%e/%v/%Y"));
		break;
	case 2:
		delete []localeInfo.dateStringOutputFormat;
		localeInfo.dateStringOutputFormat = strdup(ZAF_ITEXT("%EY/%v/%e"));
		break;
	}
	LocalCopy(localeInfo.currencySymbol, dos3Info.currencySymbol);
	LocalCopy(localeInfo.intCurrencySymbol, dos3Info.currencySymbol);
	LocalCopy(localeInfo.thousandsSeparator, dos3Info.thousandsSeparator);
	LocalCopy(localeInfo.monThousandsSeparator, dos3Info.thousandsSeparator);
	LocalCopy(localeInfo.decimalSeparator, dos3Info.decimalSeparator);
	LocalCopy(localeInfo.monDecimalSeparator, dos3Info.decimalSeparator);
	LocalCopy(localeInfo.dateSeparator, dos3Info.dateSeparator);
	LocalCopy(localeInfo.timeSeparator, dos3Info.timeSeparator);
	localeInfo.posSignPrecedes = localeInfo.negSignPrecedes = 1;
	static struct {
		int spce;
		int curr;
	} cstable[] = {
		{ 0, 1 },
		{ 0, 0 },
		{ 1, 1 },
		{ 1, 0 },
		{ 0, 1 }
	};
	if (dos3Info.currencyStyle <= 4)
	{
		localeInfo.posSpaceSeparation = localeInfo.negSpaceSeparation =
			cstable[dos3Info.currencyStyle].spce;
		localeInfo.posCurrencyPrecedes = localeInfo.negCurrencyPrecedes =
			cstable[dos3Info.currencyStyle].curr;
		if (dos3Info.currencyStyle == 4)
		{
			LocalCopy(localeInfo.monDecimalSeparator, dos3Info.currencySymbol);
			localeInfo.currencySymbol[0] = 0;
		}
	}
	localeInfo.intFractionDigits = localeInfo.fractionDigits =
		dos3Info.significantDigits;
	switch (dos3Info.timeFormat)
	{
	case 0:
		// Don't change the output format.
		break;
	case 1:
		delete []localeInfo.time12StringOutputFormat;
		localeInfo.time12StringOutputFormat = strdup(localeInfo.timeStringOutputFormat);
		break;
	}
#endif

	return (true);
}
