1 /*---------------------------------------------------------------------------*
2   Project: Revolution ENC sample demo
3   File:    alternate.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: alternate.c,v $
14   Revision 1.2  2007/10/30 07:09:05  yoshioka_yasuhiro
15   Added a debug print at the end of demo.
16 
17   Revision 1.1  2006/12/26 00:52:44  yoshioka_yasuhiro
18   Initial commit.
19 
20   $NoKeywords: $
21  *---------------------------------------------------------------------------*/
22 
23 #include <revolution.h>
24 #include <revolution/enc.h>
25 
26 #define BUF_LEN 32
27 static u8 buffer[32];
28 
29 #define SRC_LEN 16
30 static const u16 wcs[] ={
31     0x0065, 0x0075, 0x0072, 0x006f, 0x0020, 0x0073, 0x0069, 0x0067,
32     0x006e, 0x0020, 0x20AC, 0x0020, 0x0074, 0x0065, 0x0073, 0x0074,
33     0x0000};
34 
main()35 int main()
36 {
37     u8* dstbuf = buffer;
38     const u16* srcbuf = wcs;
39     /* length of remaining buffers */
40     s32 dstbuflen = BUF_LEN;
41     s32 srcbuflen = SRC_LEN;
42 
43     /*
44        IN: length of remaining buffers
45        OUT: length of buffers read by the function
46      */
47     s32 dstlen = dstbuflen;
48     s32 srclen = srcbuflen;
49 
50     ENCResult result = ENCConvertStringUnicodeToLatin1(dstbuf, &dstlen, srcbuf, &srclen);
51     while (result != ENC_OK)
52     {
53         switch(result)
54         {
55         case ENC_ERR_NO_BUF_LEFT:
56             OSHalt("no buffer left\n");
57         case ENC_ERR_NO_MAP_RULE:
58             /* no map rule */
59             break;
60         case ENC_ERR_INVALID_PARAM:
61             OSHalt("invalid param\n");
62         case ENC_ERR_INVALID_FORMAT:
63             /* invalid format */
64             break;
65         default:
66             OSHalt("unknown result code\n");
67         }
68 
69         /* seek */
70         dstbuf += dstlen;
71         dstbuflen -= dstlen;
72         srcbuf += srclen;
73         srcbuflen -= srclen;
74 
75         if (dstbuflen > 0) /* some buffer left */
76         {
77             /* replaced with one alternative character */
78             *dstbuf = '?';
79             ++dstbuf;
80             --dstbuflen;
81         }
82         else
83         {
84             OSHalt("no buffer left\n");
85         }
86         dstlen = dstbuflen;
87 
88         // skip one unconvertible character
89         ++srcbuf;
90         --srcbuflen;
91         srclen = srcbuflen;
92 
93         result = ENCConvertStringUnicodeToLatin1(dstbuf, &dstlen, srcbuf, &srclen);
94     }
95 
96     OSReport("%s\n", buffer);
97 
98     OSHalt("Finish");
99 
100     return 0;
101 }