0022627: Change OCCT memory management defaults
[occt.git] / src / Resource / Resource_ConvertUnicode.c
CommitLineData
7fd59977 1#include <stdio.h>
2#include <string.h>
3#include <sys/types.h>
4
5typedef unsigned short char16 ;
6
7#include <Resource_Shiftjis.h>
8#include <Resource_gb2312.h>
9
10#define isjis(c) (((c)>=0x21 && (c)<=0x7e))
11#define iseuc(c) (((c)>=0xa1 && (c)<=0xfe))
12#define issjis1(c) (((c)>=0x81 && (c)<=0x9f) || ((c)>=0xe0 && (c)<=0xef))
13#define issjis2(c) ((c)>=0x40 && (c)<=0xfc && (c)!=0x7f)
14#define ishankana(c) ((c)>=0xa0 && (c)<=0xdf)
15#define isshift(c) (((c)>=0x80 && (c)<=0xff))
16
17
18static void sjis_to_jis (unsigned int *ph, unsigned int *pl)
19{
20
21 if ( ! issjis1 ( *ph ) || ! issjis2 ( *pl ) ) {
22 return ;
23 }
24
25 if (*ph <= 0x9f)
26 {
27 if (*pl < 0x9f)
28 *ph = (*ph << 1) - 0xe1;
29 else
30 *ph = (*ph << 1) - 0xe0;
31 }
32 else
33 {
34 if (*pl < 0x9f)
35 *ph = (*ph << 1) - 0x161;
36 else
37 *ph = (*ph << 1) - 0x160;
38 }
39 if (*pl < 0x7f)
40 *pl -= 0x1f;
41 else if (*pl < 0x9f)
42 *pl -= 0x20;
43 else
44 *pl -= 0x7e;
45}
46
47static void jis_to_sjis (unsigned int *ph, unsigned int *pl)
48{
49 if (*ph & 1)
50 {
51 if (*pl < 0x60)
52 *pl += 0x1f;
53 else
54 *pl += 0x20;
55 }
56 else
57 *pl += 0x7e;
58 if (*ph < 0x5f)
59 *ph = (*ph + 0xe1) >> 1;
60 else
61 *ph = (*ph + 0x161) >> 1;
62}
63
64static void euc_to_sjis (unsigned int *ph, unsigned int *pl)
65{
66 if ( (*ph & 0xFFFFFF00) || (*pl & 0xFFFFFF00) ) {
67 *ph = 0 ;
68 *pl = 0 ;
69 return ;
70 }
71
72 if ( ! iseuc ( *ph ) || ! iseuc ( *pl ) ) {
73 return ;
74 }
75
76
77 *ph &= 0x7F ;
78 *pl &= 0x7F ;
79
80 jis_to_sjis ( ph , pl ) ;
81
82}
83
84static void sjis_to_euc (unsigned int *ph, unsigned int *pl)
85{
86 if ( (*ph & 0xFFFFFF00) || (*pl & 0xFFFFFF00) ) {
87 *ph = 0 ;
88 *pl = 0 ;
89 return ;
90 }
91
92 if ( ! issjis1 ( *ph ) || ! issjis2 ( *pl ) ) {
93 return ;
94 }
95
96 if ( *ph == 0 && *pl == 0 )
97 return ;
98
99 sjis_to_jis ( ph , pl ) ;
100
101 *ph |= 0x80 ;
102 *pl |= 0x80 ;
103
104}
105
106void Resource_sjis_to_unicode (unsigned int *ph, unsigned int *pl)
107{
108 char16 sjis ;
109 char16 uni ;
110
111 if ( (*ph & 0xFFFFFF00) || (*pl & 0xFFFFFF00) ) {
112 *ph = 0 ;
113 *pl = 0 ;
114 return ;
115 }
116
117 if ( ! issjis1 ( *ph ) || ! issjis2 ( *pl ) ) {
118 return ;
119 }
120
121 sjis = ((*ph) << 8) | (*pl) ;
122 uni = sjisuni [sjis] ;
123 *ph = uni >> 8 ;
124 *pl = uni & 0xFF ;
125}
126
127void Resource_unicode_to_sjis (unsigned int *ph, unsigned int *pl)
128{
129 char16 sjis ;
130 char16 uni ;
131
132 if ( (*ph & 0xFFFFFF00) || (*pl & 0xFFFFFF00) ) {
133 *ph = 0 ;
134 *pl = 0 ;
135 return ;
136 }
137 if ( *ph == 0 && *pl == 0 )
138 return ;
139
140 uni = ((*ph) << 8) | (*pl) ;
141 sjis = unisjis [uni] ;
142 *ph = sjis >> 8 ;
143 *pl = sjis & 0xFF ;
144}
145
146void Resource_unicode_to_euc (unsigned int *ph, unsigned int *pl)
147{
148
149 if ( *ph == 0 && *pl == 0 )
150 return ;
151
152 Resource_unicode_to_sjis ( ph , pl ) ;
153 if (issjis1(*ph)) { /* let's believe it is ANSI code if it is not sjis*/
154 sjis_to_euc ( ph , pl ) ;
155 }
156
157}
158
159void Resource_euc_to_unicode (unsigned int *ph, unsigned int *pl)
160{
161
162 if ( ! iseuc ( *ph ) || ! iseuc ( *pl ) ) {
163 return ;
164 }
165
166
167 if ( *ph == 0 && *pl == 0 )
168 return ;
169
170 euc_to_sjis ( ph , pl ) ;
171 Resource_sjis_to_unicode ( ph , pl ) ;
172
173}
174
175
176void Resource_gb_to_unicode (unsigned int *ph, unsigned int *pl)
177{
178 char16 gb ;
179 char16 uni ;
180
181
182 if ( (*ph & 0xFFFFFF00) || (*pl & 0xFFFFFF00) ) {
183 *ph = 0 ;
184 *pl = 0 ;
185 return ;
186 }
187
188 if ( ! isshift ( *ph ) || ! isshift ( *pl ) ) {
189 return ;
190 }
191
192 *ph = (*ph) & 0x7f ;
193 *pl = (*pl) & 0x7f ;
194
195 gb = ((*ph) << 8) | (*pl) ;
196 uni = gbuni [gb] ;
197 *ph = uni >> 8 ;
198 *pl = uni & 0xFF ;
199}
200
201void Resource_unicode_to_gb (unsigned int *ph, unsigned int *pl)
202{
203 char16 gb ;
204 char16 uni ;
205
206 if ( (*ph & 0xFFFFFF00) || (*pl & 0xFFFFFF00) ) {
207 *ph = 0 ;
208 *pl = 0 ;
209 return ;
210 }
211 if ( *ph == 0 && *pl == 0 )
212 return ;
213
214 uni = ((*ph) << 8) | (*pl) ;
215 gb = unigb [uni] ;
216 if (gb != 0) {
217 *ph = ( gb >> 8 ) | 0x80 ;
218 *pl = ( gb & 0xFF ) | 0x80 ;
219 }
220 else {
221 *ph = 0;
222 *pl = 0 ;
223 }
224}