// Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unicode/utypes.h>
#include <unicode/ucnv.h>
#include <unicode/unistr.h>
#include <unicode/translit.h>
main()
{
UBool ret = TRUE;
UConverter *convfrom = 0;
UConverter *convto = 0;
UErrorCode err = U_ZERO_ERROR;
UBool flush;
const char *cbufp;
char *bufp;
char *buf = 0;
int i;
int32_t len32;
const UChar *unibufbp;
UChar *unibufp;
UChar *unibuf = 0;
printf("Conversion testing for ISO-2022-JP:\n\n");
convto = ucnv_open("iso-2022-jp", &err);
if (U_FAILURE(err)) {
fprintf(stderr, "ucnv_open error - exiting.\n");
exit(-1);
}
buf = (char *)malloc(1024);
memset(buf, 0, 1024);
unibuf = (UChar *)malloc(sizeof(UChar) * 4);
unibuf[0] = 0x611f;
unibuf[1] = 0x000d;
unibuf[2] = 0x000a;
unibuf[3] = 0x611f;
bufp = buf;
unibufp = unibuf;
printf("Checking ucnv_fromUnicode()...\n");
ucnv_fromUnicode(convto, &bufp, (const char *)bufp + 1024,
(const UChar **)&unibufp,
(const UChar *)unibuf + 4,
NULL, (1 > 0), &err);
bufp += 15;
unibufp = unibuf + 4;
ucnv_fromUnicode(convto, &bufp, (const char *)bufp + 1024 - 15,
(const UChar **)&unibufp,
(const UChar *)unibufp,
NULL, (1 > 0), &err);
if (U_FAILURE(err)) {
fprintf(stderr, "conversion error!\n");
exit(-1);
}
for (i = 0; buf[i]; i++)
if (buf[i] == 0x1b)
printf("%3d %02x ESC\n", i, (unsigned char)buf[i]);
else
printf("%3d %02x\n", i, (unsigned char)buf[i]);
if (i > 2 && buf[i - 3] == '\x1b' && buf[i - 2] == '(' &&
(buf[i - 1] == 'J' || buf[i - 1] == 'B'))
printf("Conversion ended with ESC ( J or ESC ( B - success.\n");
else
printf("Conversion didn't end with ESC ( J or ESC ( B - failed.\n");
ucnv_resetFromUnicode(convto);
memset(buf, 0, 1024);
unibuf[0] = 0x611f;
unibuf[1] = 0x000d;
unibuf[2] = 0x000a;
unibuf[3] = 0x611f;
bufp = buf;
unibufp = unibuf;
printf("\nChecking ucnv_fromUChars()...\n");
len32 = ucnv_fromUChars(convto, bufp, 1024, unibufp, 4, &err);
for (i = 0; len32 > i; i++)
if (buf[i] == 0x1b)
printf("%3d %02x ESC\n", i, (unsigned char)buf[i]);
else
printf("%3d %02x\n", i, (unsigned char)buf[i]);
if (i > 2 && buf[i - 3] == '\x1b' && buf[i - 2] == '(' &&
(buf[i - 1] == 'J' || buf[i - 1] == 'B'))
printf("Conversion ended with ESC ( J or ESC ( B - success.\n");
else
printf("Conversion didn't end with ESC ( J or ESC ( B - failed.\n");
if (convto) ucnv_close(convto);
if (buf) delete[] buf;
if (unibuf) delete[] unibuf;
}