1 /*---------------------------------------------------------------------------*
2   Project: Revolution ENC sample demo
3   File:    sample.c
4 
5   Copyright 2006 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Log: sample.c,v $
14   Revision 1.9  2007/02/05 08:01:20  yoshioka_yasuhiro
15   Modified the character encoding.
16 
17   Revision 1.8  2006/12/26 00:54:01  yoshioka_yasuhiro
18   Deleted unused codes.
19 
20   Revision 1.7  2006/10/27 12:20:16  yoshioka_yasuhiro
21   Divided into unicode.c, latin.c and japanese.c.
22   Added auto converter demo.
23 
24   Revision 1.6  2006/08/14 04:31:35  yoshioka_yasuhiro
25   Added demos for ENCConvertStringJisToUnicode and ENCConvertStringUnicodeToJis.
26 
27   Revision 1.4  2006/08/09 10:37:37  yoshioka_yasuhiro
28   Specification change.
29   ENCConverString* functions returns a result code,
30   and read/write dstlen and srclen.
31 
32   Revision 1.3  2006/08/08 10:48:23  yoshioka_yasuhiro
33   Fix for specification change.
34   Modified termination check.
35 
36   Revision 1.1  2006/08/07 06:43:14  yoshioka_yasuhiro
37   Initial commit.
38 
39   $NoKeywords: $
40  *---------------------------------------------------------------------------*/
41 
42 #include <revolution.h>
43 #include <revolution/enc.h>
44 
45 static const u16 wcs[] = {
46     0x002A, 0x0045, 0x004E, 0x0043, 0x0020, 0x0044, 0x0045, 0x004D,
47     0x004F, 0x002A, 0x000A, 0x0075, 0x006E, 0x006B, 0x006E, 0x006F,
48     0x0077, 0x006E, 0x0020, 0x0063, 0x0068, 0x0061, 0x0072, 0x0061,
49     0x0063, 0x0074, 0x0065, 0x0072, 0x0073, 0x000A, 0x0100, 0x0110,
50     0x0120, 0x0130, 0x0140, 0x0150, 0x0160, 0x0170, 0x000A, 0x0061,
51     0x0072, 0x0065, 0x0020, 0x0073, 0x006B, 0x0069, 0x0070, 0x0070,
52     0x0065, 0x0064, 0x0020, 0x006F, 0x0072, 0x0020, 0x0063, 0x006F,
53     0x006E, 0x0076, 0x0065, 0x0072, 0x0074, 0x0065, 0x0064, 0x0020,
54     0x0074, 0x006F, 0x0020, 0x0027, 0x003F, 0x0027, 0x0000
55 };
56 
57 static const u8 mbs[] = {
58     0x75, 0x6E, 0x6B, 0x6E, 0x6F, 0x77, 0x6E, 0x20,
59     0x63, 0x68, 0x61, 0x72, 0x61, 0x63, 0x74, 0x65,
60     0x72, 0x73, 0x0A, 0x1B, 0x24, 0x42, 0x22, 0x32,
61     0x22, 0x33, 0x22, 0x34, 0x22, 0x35, 0x22, 0x36,
62     0x1B, 0x28, 0x42, 0x0A, 0x61, 0x72, 0x65, 0x20,
63     0x73, 0x6B, 0x69, 0x70, 0x70, 0x65, 0x64, 0x20,
64     0x6F, 0x72, 0x20, 0x63, 0x6F, 0x6E, 0x76, 0x65,
65     0x72, 0x74, 0x65, 0x64, 0x20, 0x74, 0x6F, 0x20,
66     0x27, 0x3F, 0x27, 0x00
67 };
68 
69 #define LENGTH 0x400
70 
71 /* these characters must be convertible. */
72 static const u16 altNomap = L'?';
73 static const u16 altInvalid = L'@';
74 
main()75 int main()
76 {
77     s32 dstlen, srclen;
78     u8 buffer8[LENGTH];
79     u16 buffer16[LENGTH];
80     ENCResult result;
81     ENCContext lenContext, convContext;
82 
83     /* convert from internal encoding */
84     {
85         srclen = -1;
86 
87         ENCInitContext(&lenContext);
88         ENCSetExternalEncoding(&lenContext, (const u8*)"Shift_JIS");
89         ENCSetBreakType(&lenContext, ENC_BR_KEEP);
90         ENCSetAlternativeCharacter(&lenContext, altNomap, altInvalid);
91 
92         /* ENCContext has the state of a conversion sequence. */
93         ENCDuplicateContext(&convContext, &lenContext);
94 
95         result = ENCConvertFromInternalEncoding(&lenContext, NULL, &dstlen, wcs, &srclen);
96         OSReport("result: %d, dstlen: %d, srclen: %d\n", result, dstlen, srclen);
97         result = ENCConvertFromInternalEncoding(&convContext, buffer8, &dstlen, wcs, &srclen);
98         OSReport("result: %d, dstlen: %d, srclen: %d\n", result, dstlen, srclen);
99         buffer8[dstlen] = 0;
100 
101         OSReport("\n%s\n\n", buffer8);
102     }
103 
104     /* convert to internal encoding */
105     {
106         srclen = -1;
107 
108         ENCInitContext(&lenContext);
109         ENCSetExternalEncoding(&lenContext, (const u8*)"ISO-2022-JP");
110         ENCSetBreakType(&lenContext, ENC_BR_KEEP);
111         ENCSetAlternativeCharacter(&lenContext, altNomap, altInvalid);
112 
113         /* ENCContext has the state of a conversion sequence. */
114         ENCDuplicateContext(&convContext, &lenContext);
115 
116         result = ENCConvertToInternalEncoding(&lenContext, NULL, &dstlen, mbs, &srclen);
117         OSReport("result: %d, dstlen: %d, srclen: %d\n", result, dstlen, srclen);
118         result = ENCConvertToInternalEncoding(&convContext, buffer16, &dstlen, mbs, &srclen);
119         OSReport("result: %d, dstlen: %d, srclen: %d\n", result, dstlen, srclen);
120         buffer16[dstlen] = 0;
121 
122         OSReport("\n");
123         {
124             u32 i;
125             u32 length = dstlen / ENC_INTERNAL_CHAR_WIDTH + 1;
126             for (i = 0; i < length; ++i)
127             {
128                 OSReport("0x%04X\n", buffer16[i]);
129             }
130         }
131         OSReport("\n\n");
132     }
133 
134 
135     OSHalt("Finish");
136 
137     return 0;
138 }
139