// P2CStrings.cpp
//
// Copyright 1996 Diehl Graphsoft, Inc.
//
// Contains functions that are very similar to existing callbacks except take
// C-strings instead of Pascal strings.
//
// SourceSafe information for this version:
//    $Revision: 1 $
//    $Date: 6/08/00 12:32p $
//    $Author: Dave $

#include "StdHeaders.h"

#include "MiniCadCallbacks.h"
#include "GSString.h"


static bool IsJapaneseSystem()
{
	CPINFO      cpInfo;

	return GetCPInfo(CP_ACP, &cpInfo) && (cpInfo.MaxCharSize>1);
}


///////////////////////////////////////////////////////////////////////////////
void GS_CALLBACK GS_Mac2WinString(CallBackPtr /*cbp*/, const unsigned char *ps, char *sz, int max)
// 'max' should be the length of the input string
{
	// check for double-byte characters (Japanese system)

	if (IsJapaneseSystem()) {
//	if (ASI_SystemValue(ASISV_CMD_GET, ASISV_gl_DBCS, 0)) {
		// Don't need to convert characters, just the string format

		// For the last parameter to StringCopy, we don't know the size of the output buffer
		// (WinMacToAnsiBuf has the same limitation), so just assume it is big enough
		StringCopy(ps, sz, 255);
	}
	else {
		if (max > ps[0])
			max = ps[0];

	#if _MINICAD_
		int len = WinMacToAnsiBuf(&ps[1], (unsigned char*)sz, max, 0);
	#else
		ASSERTN(kChao, max < 256);

		TAnsiMacBufDataStructAlturaRemoval data;
		data.feFunctionEnumeration	= eMacToAnsiBuf;
		data.fpInMacString			= &ps[1];
		data.fpOutAnsiString		= (unsigned char *)sz;
		data.fStringLength			= max;
		data.fConversionFlag		= 0;
		short len = 0;
		VERIFYN(kChao, GS_Kludge(gCBP, kKludgeAlturaBufFunctionAccess, &data, &len));
	#endif

		sz[len] = 0;
	}
}

///////////////////////////////////////////////////////////////////////////////
void GS_CALLBACK GS_Win2MacString(CallBackPtr /*cbp*/, const char *sz, unsigned char *ps, int max)
// 'max' should be the length of the input string
{
	// check for double-byte characters (Japanese system)
	if (IsJapaneseSystem()) {
//	if (ASI_SystemValue(ASISV_CMD_GET, ASISV_gl_DBCS, 0)) {
		// Don't need to convert characters, just the string format

		// For the last parameter to StringCopy, we don't know the size of the output buffer
		// (WinAnsiToMacBuf has the same limitation), so just assume it is big enough
		StringCopy(sz, ps, 255);
	}
	else {
		int len = strlen(sz);
		if (max > len)
			max = len;

	#if _MINICAD_
		len = WinAnsiToMacBuf((unsigned char*)sz, &ps[1], max);
	#else
		ASSERTN(kChao, max < 256);

		TAnsiMacBufDataStructAlturaRemoval data;
		data.feFunctionEnumeration	= eAnsiToMacBuf;
		data.fpInAnsiString			= (unsigned char*)sz;			
		data.fpOutMacString			= (unsigned char*)&ps[1];	
		data.fStringLength			= max;	
		
		short copySize = 0;
		VERIFYN(kChao, GS_Kludge(gCBP, kKludgeAlturaBufFunctionAccess, &data, &copySize));
		len = copySize;
	#endif

		ps[0] = len;
	}
}

///////////////////////////////////////////////////////////////////////////////
void GS_CALLBACK GS_AngleToCString(CallBackPtr cbp, double_param angle, char *sz, int max)
{
	Str255 ps;
	GS_AngleToStringN(cbp, angle, ps);
	GS_Mac2WinString(cbp, ps, sz, max);
}

///////////////////////////////////////////////////////////////////////////////
void GS_CALLBACK GS_CoordToDimCString(CallBackPtr cbp, const WorldCoord &c, char *sz, int max)
{
	Str255 ps;
	GS_CoordToDimString(cbp, c, ps);
	GS_Mac2WinString(cbp, ps, sz, max);
}

///////////////////////////////////////////////////////////////////////////////
Boolean GS_CALLBACK GS_CStringToAngle(CallBackPtr cbp, const char *sz, double &angle)
{
	Str255 ps;
	GS_Win2MacString(cbp, sz, ps, 256);
	return GS_StringToAngleN(cbp, ps, angle);
}

///////////////////////////////////////////////////////////////////////////////
Boolean GS_CALLBACK GS_DimCStringToCoord(CallBackPtr cbp, const char *sz, WorldCoord& c)
{
	Str255 ps;
	GS_Win2MacString(cbp, sz, ps, 256);
	return GS_DimStringToCoord(cbp, ps, c);
}

///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
