shithub: libtags

ref: 0a1bab10da12e537aab4b7ea9679d7dc54a559c8
dir: libtags/437.c

View raw version
/* https://en.wikipedia.org/wiki/Code_page_437 */
#include "tagspriv.h"

#ifdef __unix__
int
cp437toutf8(char *o, int osz, const char *s, int sz)
{
	/* FIXME somebody come up with portable code */
	return snprint(o, osz, "%.*s", sz, s);
}
#else
static Rune rh[] =
	L"ΔÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧"
	L"ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│d┤╡╢╖╕╣║╗╝╜"
	L"╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌"
	L"▐▀αßeΓπfΣgσµhτΦΘΩiδj∞φkεl∩≡±≥≤⌠m"
	L"⌡÷≈°∙n·√oⁿ²■ ";

int
cp437toutf8(char *o, int osz, const char *s, int sz)
{
	char c[UTFmax];
	int i, n;
	Rune r;

	for(i = 0; i < sz && osz > 1 && s[i] != 0; i++){
		if((uchar)s[i] < 127){
			*o++ = s[i];
			osz--;
			continue;
		}
		r = rh[(uchar)s[i] - 127];
		if((n = runetochar(c, &r)) >= osz)
			break;
		memmove(o, c, n);
		o += n;
		osz -= n;
	}

	*o = 0;
	return i;
}
#endif