shithub: pt2-clone

ref: fd5d4165735a0881ef0ccb9e52f22fb0f4f1bcc4
dir: /pt_pal_editor/src/unicode.c/

View raw version
#include <stdint.h>
#include <wchar.h>
#include "unicode.h"

// this is probably broken, but it "works" for now
uint32_t unicharToAnsi(char *dstBuffer, const UNICHAR *inputString, uint32_t maxDstLen)
{
	uint32_t i;
	UNICHAR wideChar;

	if (inputString == NULL || dstBuffer == NULL)
		return 0;

	i = 0;
	while (i < maxDstLen && inputString[i] != '\0')
	{
		wideChar = inputString[i];
#ifdef _WIN32
		if (wideChar >= 256)
#else
		if (wideChar < 0)
#endif
		{
			*dstBuffer++ = '?';

#ifdef _WIN32
			if ((wideChar & 0xFF00) == 0xD800) // lead surrogate, skip the next widechar (trail)
				i++;
#endif
		}
		else
		{
			*dstBuffer++ = (char)wideChar;
		}

		i++;
	}

	if (maxDstLen > 1)
		*dstBuffer = '\0';

	return i;
}