|
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 main() |
|
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 UChar *saveunibuf; |
|
29 |
|
30 printf("Conversion test for ISO-2022-JP's JIS X 0201 C0 characters:\n\n"); |
|
31 convto = ucnv_open("iso-2022-jp", &err); |
|
32 if (U_FAILURE(err)) { |
|
33 fprintf(stderr, "ucnv_open error - exiting.\n"); |
|
34 exit(-1); |
|
35 } |
|
36 buf = (char *)malloc(1024); |
|
37 memset(buf, 0, 1024); |
|
38 |
|
39 /* ESC $ B ?? ESC ( J A \n for JIS X 0201 |
|
40 */ |
|
41 strcpy(buf, "\x1b\x24\x42\x4a\x52\x1b\x28\x4a\x41\x0a"); |
|
42 |
|
43 /* ESC $ B ?? ESC ( J \n for JIS X 0201 |
|
44 strcpy(buf, "\x1b\x24\x42\x4a\x52\x1b\x28\x4a\x0a"); |
|
45 */ |
|
46 |
|
47 /* ESC $ B ?? ESC ( B \n for ASCII |
|
48 strcpy(buf, "\x1b\x24\x42\x4a\x52\x1b\x28\x42\x0a"); |
|
49 */ |
|
50 |
|
51 saveunibuf = unibuf = (UChar *)malloc(sizeof(UChar) * 100); |
|
52 memset(unibuf, 0, sizeof(UChar) * 100); |
|
53 |
|
54 bufp = buf; |
|
55 unibufp = unibuf; |
|
56 |
|
57 printf("Converting by using ucnv_toUnicode()...\n"); |
|
58 ucnv_toUnicode(convto, &unibufp, (const UChar *)(unibuf + 100), |
|
59 (const char **)&bufp, (const char *)(bufp + strlen(buf)), NULL, |
|
60 TRUE, &err); |
|
61 |
|
62 if (U_FAILURE(err)) { |
|
63 fprintf(stderr, "conversion error!\n"); |
|
64 exit(-1); |
|
65 } |
|
66 |
|
67 for (i = 0; unibuf < unibufp; unibuf++) |
|
68 printf("%3d U+%04X\n", i++, *unibuf); |
|
69 |
|
70 if (i > 2 && *(unibuf - 3) == 0x7247 && *(unibuf - 2) == 0x41 && |
|
71 *(unibuf - 1) == 0xa) |
|
72 printf("Conversion done correctly - success.\n"); |
|
73 else |
|
74 printf("Conversion done incorrectly - failed.\n"); |
|
75 if (convto) ucnv_close(convto); |
|
76 |
|
77 if (buf) delete[] buf; |
|
78 if (saveunibuf) delete[] saveunibuf; |
|
79 } |