Ejemplo n.º 1
0
/*! Returns converts input string from encoding to encoding
 * TODO: Locale depends transliterating
 */
char *natspec_convert(const char *in_str,
	const char *tocode, const char *fromcode, int transliterate)
{
	iconv_t frt;
	size_t lena = strlen(in_str)*6; /* FIXME see E2BIG for errno */
	size_t lenb = strlen(in_str);
	char *ansa = (char*)alloca(lena+1);
	char *ansbptr = (char*)in_str;
	char *ansaptr = ansa;

	frt = natspec_iconv_open(tocode, fromcode);
	if (frt == (iconv_t) -1)
	{
		char buf[200];
		snprintf(buf,199,"Broken encoding: '%s' (to) or '%s' (from) or UCS2. May be you forget setlocale in main or gconv-modules is missed?\n",
			tocode, fromcode);
		perror(buf);
		/* return dup of original string if converting is failed */
		ansaptr = strdup(in_str);
		return ansaptr;
	}
	(void)natspec_iconv(frt, &ansbptr, &lenb, &ansaptr, &lena, transliterate);
	natspec_iconv_close(frt);
	*ansaptr = '\0';
	return strdup(ansa);
}
Ejemplo n.º 2
0
void test_for_natspec_iconv()
{
	/*char *inbuf="Привет!";*/
	char *inbuf=tin;
	/*int insize=strlen(inbuf);*/
	size_t insize=2, size;
	char buf[100]; size_t outsize = 99;
	char *outbuf = buf;
	iconv_t tt = natspec_iconv_open("koi8-r","UCS-2BE");
	assert (tt != (iconv_t)-1);
	size = natspec_iconv(tt, &inbuf, &insize, &outbuf, &outsize, 0);
	*outbuf = 0;
	printf("natspec_iconv: insize=%d outsize=%d size=%d outbuf=%s\n", insize, outsize, size, buf);
}
Ejemplo n.º 3
0
void test_for_convert()
{
	iconv_t it; size_t li, lo, result;
	char *t, *ti;
	char toi[100],to[100], *tob, *toai;
	ti = "Test проверка";
	t = natspec_convert(ti, "ASCII", NULL, 1);
	printf("1 %s -> '%s'\n",ti,t);
	t = natspec_convert(ti, "UTF8", "", 1);
	printf("2 %s -> '%s'\n",ti,t);
	ti = natspec_convert(t, "", "UTF8", 1);
	printf("3 %s -> '%s'\n",t,ti);
	ti = "оПНБЕПЙЮ";
	t = natspec_convert(ti, "", "CP1251", 1);
	printf("%s -> '%s'\n",ti,t);
	ti = "оПНБЕПЙЮ";
	t = natspec_convert(ti, "", "CP1251", 0);
	printf("%s -> '%s'\n",ti,t);
	ti = "оПНБЕПЙЮ \xb9";
	t = natspec_convert(ti, "", "CP1251", 0);
	printf("%s -> '%s'\n",ti,t);
	ti = "Test Проверка";
	t = natspec_convert(ti, "IBM866", "KOI8R", 0);
	printf("%s -> '%s'\n",ti,t);
	
	it = natspec_iconv_open("aSCII", "");
	if ( it == (iconv_t)(-1)) {
		printf("natspec_iconv: some problems\n");
		exit (1);
	}
	strcpy(toi,"Test - Проверка ёлочных игрушек.");
	li = strlen(toi); lo = 99; tob = to; toai = toi;
	printf("Before natspec_iconv: it=%d, %s, len=%d\n", (int)it, toi, li);
	result = natspec_iconv(it, &toai, &li, &tob, &lo, 1);
	*tob = '\0';
	printf("Result natspec_iconv: %s (lo=%d), with result=%d\n", to, lo, result);
	natspec_iconv_close(it);
}