1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - STD - include
3   File:     unicode.h
4 
5   Copyright 2006-2008 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   $Date:: 2008-09-18#$
14   $Rev: 8573 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef NITRO_STD_UNICODE_H_
19 #define NITRO_STD_UNICODE_H_
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 
26 /*****************************************************************************/
27 /* Constants */
28 
29 typedef enum STDResult
30 {
31     STD_RESULT_SUCCESS,
32     STD_RESULT_ERROR,
33     STD_RESULT_INVALID_PARAM,
34     STD_RESULT_CONVERSION_FAILED
35 }
36 STDResult;
37 
38 /*****************************************************************************/
39 /* Declarations */
40 
41 //---- ConvertString callback
42 typedef STDResult(*STDConvertUnicodeCallback) (u16 *dst, int *dst_len, const char *src,
43                                                int *src_len);
44 typedef STDResult(*STDConvertSjisCallback) (char *dst, int *dst_len, const u16 *src, int *src_len);
45 
46 
47 /*****************************************************************************/
48 /* Functions */
49 
50 /*---------------------------------------------------------------------------*
51   Name:         STD_IsSjisLeadByte
52 
53   Description:  Determines whether or not the specified character is the leading ShiftJIS byte.
54 
55   Arguments:    c:                Character to determine.
56 
57   Returns:      TRUE if ShiftJIS leading byte
58  *---------------------------------------------------------------------------*/
STD_IsSjisLeadByte(int c)59 SDK_INLINE BOOL STD_IsSjisLeadByte(int c)
60 {
61     return ((unsigned int)((((u8)c) ^ 0x20) - 0xA1) < 0x3C);
62 }
63 
64 /*---------------------------------------------------------------------------*
65   Name:         STD_IsSjisTrailByte
66 
67   Description:  Determines whether or not the specified character is the trailing ShiftJIS byte.
68 
69   Arguments:    c:                Character to determine.
70 
71   Returns:      TRUE if ShiftJIS trailing byte
72  *---------------------------------------------------------------------------*/
STD_IsSjisTrailByte(int c)73 SDK_INLINE BOOL STD_IsSjisTrailByte(int c)
74 {
75     return (c != 0x7F) && ((u8)(c - 0x40) <= (0xFC - 0x40));
76 }
77 
78 /*---------------------------------------------------------------------------*
79   Name:         STD_IsSjisCharacter
80 
81   Description:  Determines whether or not the specified character is a ShiftJIS character.
82 
83   Arguments:    s:                Character to determine.
84 
85   Returns:      TRUE if ShiftJIS character
86  *---------------------------------------------------------------------------*/
STD_IsSjisCharacter(const char * s)87 SDK_INLINE BOOL STD_IsSjisCharacter(const char *s)
88 {
89     return STD_IsSjisLeadByte(s[0]) && STD_IsSjisTrailByte(s[1]);
90 }
91 
92 /*---------------------------------------------------------------------------*
93   Name:         STD_ConvertStringSjisToUnicode
94 
95   Description:  Converts a ShiftJIS character string to a Unicode character string.
96 
97   Arguments:    dst:               Conversion destination buffer
98                                   The storage process is ignored if NULL is specified.
99                 dst_len:           Pointer which stores and passes the maximum number of characters for the conversion destination buffer, then receives the number of characters that were actually stored.
100 
101                                   Ignored when NULL is given.
102                 src:               Conversion source buffer
103                 src_len           Pointer which stores and passes the maximum number of characters to be converted, then receives the number actually converted.
104 
105                                   The end-of-string position takes priority over this specification.
106                                   When either a negative value is stored and passed, or NULL is given, the character count is revised to be the number of characters to the end of the string.
107 
108                 callback:          The callback to be called if there are any characters that can't be converted.
109                                   When NULL is specified, the conversion process ends at the position of the character that cannot be converted.
110 
111 
112   Returns:      Result of the conversion process.
113  *---------------------------------------------------------------------------*/
114 STDResult STD_ConvertStringSjisToUnicode(u16 *dst, int *dst_len,
115                                          const char *src, int *src_len,
116                                          STDConvertUnicodeCallback callback);
117 
118 /*---------------------------------------------------------------------------*
119   Name:         STD_ConvertCharSjisToUnicode
120 
121   Description:  Converts a ShiftJIS character to a Unicode character.
122 
123   Arguments:    dst:               Conversion destination buffer
124                 src:               Conversion source string.
125 
126   Returns:      The number of characters converted.
127                 Returns a -1 when conversion fails
128  *---------------------------------------------------------------------------*/
STD_ConvertCharSjisToUnicode(u16 * dst,const char * src)129 SDK_INLINE int STD_ConvertCharSjisToUnicode(u16 *dst, const char *src)
130 {
131     int     src_len = STD_IsSjisCharacter(src) ? 2 : 1;
132     int     dst_len = 1;
133     STDResult ret = STD_ConvertStringSjisToUnicode(dst, &dst_len, src, &src_len, NULL);
134     return (ret == STD_RESULT_SUCCESS) ? dst_len : -1;
135 }
136 
137 /*---------------------------------------------------------------------------*
138   Name:         STD_ConvertStringUnicodeToSjis
139 
140   Description:  Converts a Unicode character string into a ShiftJIS character string.
141 
142   Arguments:    dst:               Conversion destination buffer
143                                   The storage process is ignored if NULL is specified.
144                 dst_len:           Pointer which stores and passes the maximum number of characters for the conversion destination buffer, then receives the number of characters that were actually stored.
145 
146                                   Ignored when NULL is given.
147                 src:               Conversion source buffer
148                 src_len           Pointer which stores and passes the maximum number of characters to be converted, then receives the number actually converted.
149 
150                                   The end-of-string position takes priority over this specification.
151                                   When either a negative value is stored and passed, or NULL is given, the character count is revised to be the number of characters to the end of the string.
152 
153                 callback:          The callback to be called if there are any characters that can't be converted.
154                                   When NULL is specified, the conversion process ends at the position of the character that cannot be converted.
155 
156 
157   Returns:      Result of the conversion process.
158  *---------------------------------------------------------------------------*/
159 STDResult STD_ConvertStringUnicodeToSjis(char *dst, int *dst_len,
160                                          const u16 *src, int *src_len,
161                                          STDConvertSjisCallback callback);
162 
163 /*---------------------------------------------------------------------------*
164   Name:         STD_ConvertCharUnicodeToSjis
165 
166   Description:  Converts a Unicode string into a Shift JIS string.
167 
168   Arguments:    dst:               Conversion destination buffer
169                                   Requires two bytes to be secured.
170                 src:               Conversion source string.
171 
172   Returns:      The number of bytes converted.
173                 Returns a -1 when conversion fails
174  *---------------------------------------------------------------------------*/
STD_ConvertCharUnicodeToSjis(char * dst,u16 src)175 SDK_INLINE int STD_ConvertCharUnicodeToSjis(char *dst, u16 src)
176 {
177     int     src_len = 1;
178     int     dst_len = 2;
179     STDResult ret = STD_ConvertStringUnicodeToSjis(dst, &dst_len, &src, &src_len, NULL);
180     return (ret == STD_RESULT_SUCCESS) ? dst_len : -1;
181 }
182 
183 /*---------------------------------------------------------------------------*
184   Name:         STDi_GetUnicodeConversionTable
185 
186   Description:  Gets Unicode conversion tables.
187 
188   Arguments:    u2s: Location to store a pointer to the conversion table from Unicode to Shift JIS
189                 s2u: Location to store a pointer to the conversion table from Shift JIS to Unicode
190 
191   Returns:      None.
192  *---------------------------------------------------------------------------*/
193 void STDi_GetUnicodeConversionTable(const u8 **u2s, const u16 **s2u);
194 
195 /*---------------------------------------------------------------------------*
196   Name:         STDi_AttachUnicodeConversionTable
197 
198   Description:  Assigns Unicode conversion tables to the STD library.
199 
200   Arguments:    u2s: Conversion table from Unicode to Shift JIS
201                 s2u: Conversion table from Shift JIS to Unicode
202 
203   Returns:      None.
204  *---------------------------------------------------------------------------*/
205 void STDi_AttachUnicodeConversionTable(const u8 *u2s, const u16 *s2u);
206 
207 
208 #ifdef __cplusplus
209 } /* extern "C" */
210 #endif
211 
212 /* NITRO_STD_UNICODE_H_ */
213 #endif
214