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

#include <z_idata.hpp>
#include <z_string.hpp>
#include <z_stdlib.hpp>
#include <z_stdio.hpp>
#include <z_cset.hpp>


#if defined(_MSC_VER)
#	if defined(DOS386)
#		include <pldos32.h>
#	elif defined(DOSX286)
#		define	_int86x	int86x
#		define	_REGS	REGS
#		define	_SREGS	SREGS
#	endif
#elif defined(__WATCOMC__)
#	include <i86.h>
#endif
#if !defined(ZAF_DISABLE_INT21)
#	include <dos.h>
#endif
#if defined(DOSX286)
#	include <phapi.h>
#elif	defined(DOS16M)
#	include <dos16.h>
#endif

#if !defined(ZAF_DISABLE_INT21)
Dos3Country dos3Info;
#endif

void LocalCopy(char *source, ZafIChar *destination, int max);

const ZafIChar *ZafI18nData::OSI18nName(void)
{
	static ZafIChar osI18nName[20];

	osI18nName[0] = '\0';
	int oemCountryCode = 1;

	// Check ZAF_LANG
#undef getenv
	char *zafLangName = getenv("ZAF_LANG");

	if (zafLangName && *zafLangName)
		LocalCopy(zafLangName, osI18nName, 15);
	else
	{
#if defined(ZAF_DISABLE_INT21)
	const ZafIChar *localeName= CountryCodeToZafLocale(oemCountryCode);
	if (localeName != ZAF_NULLP(ZafIChar))
	{
		const ZafIChar *languageName = ZafLocaleToZafLanguage(localeName);
		if (languageName != ZAF_NULLP(ZafIChar))
		{
			strcpy(osI18nName, languageName);
			strcat(osI18nName, ZAF_ITEXT("_"));
		}
		strcat(osI18nName, localeName);
	}

#else

#if defined(_MSC_VER)
#	if defined(DOS386)
		union _REGS regs;
		regs.d.edx = (unsigned int)&dos3Info;
		// Determine the country code.
		regs.w.ax = 0x3800;
		_int86(0x21, &regs, &regs);
		oemCountryCode = (regs.h.al == 0xFF) ? regs.w.bx : regs.h.al;
#	else
		union _REGS regs;
		struct _SREGS segregs;
		void far* ptr = (void *)&dos3Info;
		regs.x.dx = _FP_OFF(ptr);
		segregs.ds = _FP_SEG(ptr);
		segregs.es = 0;
		// Determine the country code.
		regs.x.ax = 0x3800;
		_int86x(0x21, &regs, &regs, &segregs);
		oemCountryCode = (regs.h.al == 0xFF) ? regs.x.bx : regs.h.al;
#	endif
#elif defined(__WATCOMC__)
	union REGS regs;
	// Determine the country code.
	regs.w.ax = 0x3800;

#if defined(__386__)
        regs.x.edx = (unsigned int)&dos3Info;
        int386(0x21, &regs, &regs);
#else
        regs.x.dx = (unsigned int)&dos3Info;
        int86(0x21, &regs, &regs);
#endif
        oemCountryCode = (regs.h.al == 0xFF) ? regs.w.bx : regs.h.al;
#elif defined(ZIL_CURSES)
	oemCountryCode = 01;
#elif defined(__ZTC__)
	union REGS regs;
	regs.x.dx = (unsigned int)&dos3Info;
	// Determine the country code.
	regs.x.ax = 0x3800;
	int86(0x21, &regs, &regs);
	oemCountryCode = (regs.h.al == 0xFF) ? regs.x.bx : regs.h.al;
#elif defined(__BCPLUSPLUS__) || defined(__TCPLUSPLUS__)
#	if defined(DOS32PP)
		union REGS regs;
		regs.x.edx = (unsigned int)&dos3Info;
		// Determine the country code.
		regs.w.ax = 0x3800;
		int386(0x21, &regs, &regs);
		oemCountryCode = (regs.h.al == 0xFF) ? regs.w.bx : regs.h.al;
#	else
		union REGS regs;
		struct SREGS segregs;
		void far* ptr = (void *)&dos3Info;
		regs.x.dx = FP_OFF(ptr);
		segregs.ds = FP_SEG(ptr);
		segregs.es = 0;
		// Determine the country code.
		regs.x.ax = 0x3800;
		int86x(0x21, &regs, &regs, &segregs);
		oemCountryCode = (regs.h.al == 0xFF) ? regs.x.bx : regs.h.al;
#	endif
#else
	???? who is this ????;
#endif
#if defined(ZIL_UNICODE)
	// It seems that MS-DOS/V 5.0J doesn't set the
	// country code to 1 when in "chev us" mode.
	int *table = I_CheckLeadIn();
	if (oemCountryCode == 81 && table && *table == 0)
		oemCountryCode = 1;
#endif

	const ZafIChar *localeName= CountryCodeToZafLocale(oemCountryCode);
	if (localeName != ZAF_NULLP(ZafIChar))
	{
		const ZafIChar *languageName = ZafLocaleToZafLanguage(localeName);
		if (languageName != ZAF_NULLP(ZafIChar))
		{
			strcpy(osI18nName, languageName);
			strcat(osI18nName, ZAF_ITEXT("_"));
		}
		strcat(osI18nName, localeName);
	}

#endif
	}

	// Check OS information
	return (osI18nName[0] ? osI18nName : ZAF_NULLP(ZafIChar));
}

void LocalCopy(char *source, ZafIChar *destination, int max)
{
	if (max < 0)
		max = strlen(source);
	int i;
	for (i = 0; i < max && source[i]; i++)
		destination[i] = source[i];
	if (i < max)
		destination[i] = '\0';
}
