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