icu/internaltests/conversion-test3.c
changeset 39 65491902381c
equal deleted inserted replaced
38:6b6eddc571c7 39:65491902381c
       
     1 //   Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
       
     2 #include <stdio.h>
       
     3 #include <errno.h>
       
     4 #include <string.h>
       
     5 #include <stdlib.h>
       
     6 
       
     7 #include <unicode/utypes.h>
       
     8 #include <unicode/ucnv.h>
       
     9 #include <unicode/unistr.h>
       
    10 #include <unicode/translit.h>
       
    11 
       
    12 int main(int ac, char **av)
       
    13 {
       
    14     UBool ret = TRUE;
       
    15     UConverter *convfrom = 0;
       
    16     UConverter *convto = 0;
       
    17     UErrorCode err = U_ZERO_ERROR;
       
    18     UBool flush;
       
    19     const char *cbufp;
       
    20     char *bufp;
       
    21     char *buf = 0;
       
    22     int i;
       
    23     int32_t len32;
       
    24 
       
    25     const UChar *unibufbp;
       
    26     UChar *unibufp;
       
    27     UChar *unibuf = 0;
       
    28 
       
    29     if (ac != 2) {
       
    30 	fprintf(stderr, "Usage: %s [jis7 | iso-2022-jp | iso-2022-jp-2 ]\n", av[0]);
       
    31 	exit(-1);
       
    32     }
       
    33 
       
    34     printf("Conversion testing for %s:\n\n", av[1]);
       
    35     convto = ucnv_open(av[1], &err);
       
    36     if (U_FAILURE(err)) {
       
    37 	fprintf(stderr, "ucnv_open error - exiting.\n");
       
    38 	exit(-1);
       
    39     }
       
    40     buf = (char *)malloc(1024);
       
    41     memset(buf, 0, 1024);
       
    42     unibuf = (UChar *)malloc(sizeof(UChar) * 4);
       
    43     unibuf[0] = 0x3002;
       
    44     unibuf[1] = 0xff71;
       
    45     unibuf[2] = 0x3002;
       
    46     /*
       
    47     unibuf[0] = 0x4e02;
       
    48     unibuf[1] = 0x0041;
       
    49     unibuf[2] = 0xff71;
       
    50     */
       
    51 
       
    52 	bufp = buf;
       
    53         unibufp = unibuf;
       
    54 
       
    55 	printf("Checking ucnv_fromUnicode()...\n");
       
    56     	err = U_ZERO_ERROR;
       
    57 	ucnv_fromUnicode(convto, &bufp, (const char *)bufp + 1024,
       
    58                              (const UChar **)&unibufp,
       
    59                              (const UChar *)unibuf + 3,
       
    60                              NULL, (1 > 0), &err);
       
    61 	if (U_FAILURE(err)) {
       
    62 		fprintf(stderr, "conversion error!\n");
       
    63 		exit(-1);
       
    64             }
       
    65 
       
    66 	for (i = 0; buf[i]; i++)
       
    67 		if (buf[i] == 0x1b)
       
    68 			printf("%3d  %02x ESC\n", i, (unsigned char)buf[i]);
       
    69 		else
       
    70 			printf("%3d  %02x %c\n", i, (unsigned char)buf[i], (unsigned char)buf[i]);
       
    71 	
       
    72 	if (i > 2 && buf[i - 3] == '\x1b' && buf[i - 2] == '(' &&
       
    73 	    (buf[i - 1] == 'J' || buf[i - 1] == 'B'))
       
    74 		printf("Conversion ended with ESC ( J or ESC ( B - success.\n");
       
    75 	else
       
    76 		printf("Conversion didn't end with ESC ( J or ESC ( B - failed.\n");
       
    77 
       
    78 
       
    79     if (convto) ucnv_close(convto);
       
    80 
       
    81     if (buf) delete[] buf;
       
    82     if (unibuf) delete[] unibuf;
       
    83 }