7fd59977 |
1 | // Copyright: Matra-Datavision 1996 |
2 | // File: Resource_Unicode.cxx |
3 | // Created: Thu Sep 26 14:39:46 1996 |
4 | // Author: Arnaud BOUZY |
5 | // <adn> |
6 | |
7 | #include <Resource_Unicode.ixx> |
8 | #include <Resource_ConvertUnicode.hxx> |
9 | #include <Resource_Manager.hxx> |
10 | #include <TCollection_AsciiString.hxx> |
11 | |
12 | #define isjis(c) (((c)>=0x21 && (c)<=0x7e)) |
13 | #define iseuc(c) (((c)>=0xa1 && (c)<=0xfe)) |
14 | #define issjis1(c) (((c)>=0x81 && (c)<=0x9f) || ((c)>=0xe0 && (c)<=0xef)) |
15 | |
16 | #define issjis2(c) ((c)>=0x40 && (c)<=0xfc && (c)!=0x7f) |
17 | |
18 | #define ishankana(c) ((c)>=0xa0 && (c)<=0xdf) |
19 | |
20 | static inline Standard_Boolean isshift (unsigned char c) { return c >= 0x80; } |
21 | static inline Standard_Boolean isshift (unsigned int c) { return c >= 0x80 && c <= 0xff; } |
22 | |
23 | void Resource_Unicode::ConvertSJISToUnicode(const Standard_CString fromstr,TCollection_ExtendedString& tostr) |
24 | { |
25 | tostr.Clear(); |
26 | |
27 | unsigned char* currentstr = ((unsigned char*) fromstr); |
28 | unsigned int pl,ph; |
29 | // BIG INDIAN USED HERE |
30 | while(*currentstr != '\0') { |
31 | if (issjis1(*currentstr)) { |
32 | |
33 | ph = ((unsigned int) *currentstr); |
34 | // Be Carefull with first and second !! |
35 | |
36 | currentstr++; |
37 | |
38 | pl = ((unsigned int) *currentstr); |
39 | currentstr++; |
40 | |
41 | Resource_sjis_to_unicode(&ph,&pl); |
42 | Standard_ExtCharacter curcar = ((Standard_ExtCharacter) ((ph << 8) | pl)); |
43 | TCollection_ExtendedString curext(curcar); |
44 | tostr.AssignCat(curext); |
45 | } |
46 | else { |
47 | TCollection_ExtendedString curext(((char) *currentstr)); |
48 | currentstr++; |
49 | tostr.AssignCat(curext); |
50 | } |
51 | } |
52 | } |
53 | |
54 | |
55 | void Resource_Unicode::ConvertEUCToUnicode(const Standard_CString fromstr,TCollection_ExtendedString& tostr) |
56 | { |
57 | tostr.Clear(); |
58 | |
59 | unsigned char* currentstr = ((unsigned char*) fromstr); |
60 | unsigned int pl,ph; |
61 | // BIG INDIAN USED HERE |
62 | while(*currentstr != '\0') { |
63 | if (iseuc(*currentstr)) { |
64 | |
65 | ph = ((unsigned int) *currentstr); |
66 | // Be Carefull with first and second !! |
67 | |
68 | currentstr++; |
69 | |
70 | pl = ((unsigned int) *currentstr); |
71 | currentstr++; |
72 | |
73 | Resource_euc_to_unicode(&ph,&pl); |
74 | Standard_ExtCharacter curcar = ((Standard_ExtCharacter) ((ph << 8) | pl)); |
75 | TCollection_ExtendedString curext(curcar); |
76 | tostr.AssignCat(curext); |
77 | } |
78 | else { |
79 | TCollection_ExtendedString curext(((char) *currentstr)); |
80 | currentstr++; |
81 | tostr.AssignCat(curext); |
82 | } |
83 | } |
84 | } |
85 | |
86 | void Resource_Unicode::ConvertGBToUnicode(const Standard_CString fromstr,TCollection_ExtendedString& tostr) |
87 | { |
88 | tostr.Clear(); |
89 | |
90 | unsigned char* currentstr = ((unsigned char*) fromstr); |
91 | unsigned int pl,ph; |
92 | // BIG INDIAN USED HERE |
93 | while(*currentstr != '\0') { |
94 | if (isshift(*currentstr)) { |
95 | |
96 | ph = ((unsigned int) *currentstr); |
97 | // Be Carefull with first and second !! |
98 | |
99 | currentstr++; |
100 | |
101 | pl = ((unsigned int) *currentstr); |
102 | currentstr++; |
103 | |
104 | Resource_gb_to_unicode(&ph,&pl); |
105 | Standard_ExtCharacter curcar = ((Standard_ExtCharacter) ((ph << 8) | pl)); |
106 | TCollection_ExtendedString curext(curcar); |
107 | tostr.AssignCat(curext); |
108 | } |
109 | else { |
110 | TCollection_ExtendedString curext(((char) *currentstr)); |
111 | currentstr++; |
112 | tostr.AssignCat(curext); |
113 | } |
114 | } |
115 | } |
116 | |
117 | void Resource_Unicode::ConvertANSIToUnicode(const Standard_CString fromstr,TCollection_ExtendedString& tostr) |
118 | { |
119 | tostr.Clear(); |
120 | |
121 | TCollection_ExtendedString curext(fromstr); |
122 | tostr.AssignCat(curext); |
123 | } |
124 | |
125 | Standard_Boolean Resource_Unicode::ConvertUnicodeToSJIS(const TCollection_ExtendedString& fromstr, |
126 | Standard_PCharacter& tostr, |
127 | const Standard_Integer maxsize) |
128 | { |
129 | Standard_Integer nbtrans = 0; |
130 | Standard_Integer nbext = 1; |
131 | Standard_Boolean finished = Standard_False; |
132 | Standard_ExtCharacter curcar; |
133 | unsigned int pl,ph; |
134 | // BIG INDIAN USED HERE |
135 | |
136 | while (!finished) { |
137 | if (nbext > fromstr.Length()) { |
138 | finished = Standard_True; |
139 | tostr[nbtrans] = '\0'; |
140 | } |
141 | else { |
142 | curcar = fromstr.Value(nbext); |
143 | nbext++; |
144 | ph = (((unsigned int) curcar) >> 8) & 0xFF; |
145 | pl = ((unsigned int) curcar) & 0xFF; |
146 | Resource_unicode_to_sjis(&ph,&pl); |
147 | if (issjis1(ph)) { |
148 | if (nbtrans < (maxsize-3)) { |
149 | tostr[nbtrans] = ((char) ph); |
150 | nbtrans++; |
151 | tostr[nbtrans] = ((char) pl); |
152 | nbtrans++; |
153 | } |
154 | else { |
155 | tostr[nbtrans] = '\0'; |
156 | nbtrans = maxsize-1; |
157 | return Standard_False; |
158 | } |
159 | } |
160 | else { |
161 | tostr[nbtrans] = ((char) pl); |
162 | nbtrans++; |
163 | } |
164 | if (nbtrans >= (maxsize - 1)) { |
165 | tostr[maxsize-1] = '\0'; |
166 | finished = Standard_True; |
167 | return Standard_False; |
168 | } |
169 | } |
170 | } |
171 | return Standard_True; |
172 | } |
173 | |
174 | Standard_Boolean Resource_Unicode::ConvertUnicodeToEUC(const TCollection_ExtendedString& fromstr, |
175 | Standard_PCharacter& tostr, |
176 | const Standard_Integer maxsize) |
177 | { |
178 | Standard_Integer nbtrans = 0; |
179 | Standard_Integer nbext = 1; |
180 | Standard_Boolean finished = Standard_False; |
181 | Standard_ExtCharacter curcar; |
182 | unsigned int pl,ph; |
183 | // BIG INDIAN USED HERE |
184 | |
185 | while (!finished) { |
186 | if (nbext > fromstr.Length()) { |
187 | finished = Standard_True; |
188 | tostr[nbtrans] = '\0'; |
189 | } |
190 | else { |
191 | curcar = fromstr.Value(nbext); |
192 | nbext++; |
193 | ph = (((unsigned int) curcar) >> 8) & 0xFF; |
194 | pl = ((unsigned int) curcar) & 0xFF; |
195 | Resource_unicode_to_euc(&ph,&pl); |
196 | if (iseuc(ph)) { |
197 | if (nbtrans < (maxsize-3)) { |
198 | tostr[nbtrans] = ((char) ph); |
199 | nbtrans++; |
200 | tostr[nbtrans] = ((char) pl); |
201 | nbtrans++; |
202 | } |
203 | else { |
204 | tostr[nbtrans-1] = '\0'; |
205 | nbtrans = maxsize-1; |
206 | return Standard_False; |
207 | } |
208 | } |
209 | else { |
210 | tostr[nbtrans] = ((char) pl); |
211 | nbtrans++; |
212 | } |
213 | if (nbtrans >= (maxsize - 1)) { |
214 | tostr[maxsize-1] = '\0'; |
215 | finished = Standard_True; |
216 | return Standard_False; |
217 | } |
218 | } |
219 | } |
220 | return Standard_True; |
221 | } |
222 | |
223 | Standard_Boolean Resource_Unicode::ConvertUnicodeToGB(const TCollection_ExtendedString& fromstr, |
224 | Standard_PCharacter& tostr, |
225 | const Standard_Integer maxsize) |
226 | { |
227 | Standard_Integer nbtrans = 0; |
228 | Standard_Integer nbext = 1; |
229 | Standard_Boolean finished = Standard_False; |
230 | Standard_ExtCharacter curcar; |
231 | unsigned int pl,ph; |
232 | // BIG INDIAN USED HERE |
233 | |
234 | while (!finished) { |
235 | if (nbext > fromstr.Length()) { |
236 | finished = Standard_True; |
237 | tostr[nbtrans] = '\0'; |
238 | } |
239 | else { |
240 | curcar = fromstr.Value(nbext); |
241 | nbext++; |
242 | ph = (((unsigned int) curcar) >> 8) & 0xFF; |
243 | pl = ((unsigned int) curcar) & 0xFF; |
244 | Resource_unicode_to_gb(&ph,&pl); |
245 | if (isshift(ph)) { |
246 | if (nbtrans < (maxsize-3)) { |
247 | tostr[nbtrans] = ((char) ph); |
248 | nbtrans++; |
249 | tostr[nbtrans] = ((char) pl); |
250 | nbtrans++; |
251 | } |
252 | else { |
253 | tostr[nbtrans-1] = '\0'; |
254 | nbtrans = maxsize-1; |
255 | return Standard_False; |
256 | } |
257 | } |
258 | else { |
259 | tostr[nbtrans] = ((char) curcar) & 0xFF; |
260 | nbtrans++; |
261 | } |
262 | if (nbtrans >= (maxsize - 1)) { |
263 | tostr[maxsize-1] = '\0'; |
264 | finished = Standard_True; |
265 | return Standard_False; |
266 | } |
267 | } |
268 | } |
269 | return Standard_True; |
270 | } |
271 | |
272 | Standard_Boolean Resource_Unicode::ConvertUnicodeToANSI(const TCollection_ExtendedString& fromstr, |
273 | Standard_PCharacter& tostr, |
274 | const Standard_Integer maxsize) |
275 | { |
276 | Standard_Integer nbtrans = 0; |
277 | Standard_Integer nbext = 1; |
278 | Standard_Boolean finished = Standard_False; |
279 | Standard_ExtCharacter curcar; |
280 | unsigned int pl,ph; |
281 | // BIG INDIAN USED HERE |
282 | |
283 | while (!finished) { |
284 | if (nbext > fromstr.Length()) { |
285 | finished = Standard_True; |
286 | tostr[nbtrans] = '\0'; |
287 | } |
288 | else { |
289 | curcar = fromstr.Value(nbext); |
290 | nbext++; |
291 | ph = ((unsigned int) curcar) >> 8; |
292 | pl = ((unsigned int) curcar) & 0xFF; |
293 | if (ph == 0) { |
294 | tostr[nbtrans] = ((char) pl); |
295 | } |
296 | else { |
297 | tostr[nbtrans] = ' '; |
298 | } |
299 | nbtrans++; |
300 | } |
301 | if (nbtrans >= (maxsize - 1)) { |
302 | tostr[maxsize-1] = '\0'; |
303 | finished = Standard_True; |
304 | return Standard_False; |
305 | } |
306 | } |
307 | return Standard_True; |
308 | } |
309 | |
310 | static Standard_Boolean AlreadyRead = Standard_False; |
311 | |
312 | static Resource_FormatType& Resource_Current_Format() |
313 | { |
314 | static Resource_FormatType theformat = Resource_ANSI; |
315 | if (!AlreadyRead) { |
316 | AlreadyRead = Standard_True ; |
317 | Handle(Resource_Manager) mgr = new Resource_Manager("CharSet"); |
318 | if (mgr->Find("FormatType")) { |
319 | TCollection_AsciiString form = mgr->Value("FormatType"); |
320 | if (form.IsEqual("SJIS")) { |
321 | theformat = Resource_SJIS; |
322 | } |
323 | else if (form.IsEqual("EUC")) { |
324 | theformat = Resource_EUC; |
325 | } |
326 | else if (form.IsEqual("GB")) { |
327 | theformat = Resource_GB; |
328 | } |
329 | else { |
330 | theformat = Resource_ANSI; |
331 | } |
332 | } |
333 | else { |
334 | theformat = Resource_ANSI; |
335 | } |
336 | } |
337 | return theformat; |
338 | } |
339 | |
340 | void Resource_Unicode::SetFormat(const Resource_FormatType typecode) |
341 | { |
342 | AlreadyRead = Standard_True; |
343 | Resource_Current_Format() = typecode; |
344 | } |
345 | |
346 | Resource_FormatType Resource_Unicode::GetFormat() |
347 | { |
348 | return Resource_Current_Format(); |
349 | } |
350 | |
351 | |
352 | void Resource_Unicode::ReadFormat() |
353 | { |
354 | AlreadyRead = Standard_False; |
355 | Resource_Unicode::GetFormat(); |
356 | } |
357 | |
358 | void Resource_Unicode::ConvertFormatToUnicode(const Standard_CString fromstr, |
359 | TCollection_ExtendedString& tostr) |
360 | { |
361 | Resource_FormatType theform = Resource_Unicode::GetFormat(); |
362 | switch (theform) { |
363 | case Resource_SJIS : |
364 | { |
365 | ConvertSJISToUnicode(fromstr,tostr); |
366 | break; |
367 | } |
368 | case Resource_EUC : |
369 | { |
370 | ConvertEUCToUnicode(fromstr,tostr); |
371 | break; |
372 | } |
373 | case Resource_GB : |
374 | { |
375 | ConvertGBToUnicode(fromstr,tostr); |
376 | break; |
377 | } |
378 | case Resource_ANSI : |
379 | { |
380 | ConvertANSIToUnicode(fromstr,tostr); |
381 | break; |
382 | } |
383 | } |
384 | } |
385 | |
386 | Standard_Boolean Resource_Unicode::ConvertUnicodeToFormat(const TCollection_ExtendedString& fromstr, |
387 | Standard_PCharacter& tostr, |
388 | const Standard_Integer maxsize) |
389 | { |
390 | Resource_FormatType theform = Resource_Unicode::GetFormat(); |
391 | switch (theform) { |
392 | case Resource_SJIS : |
393 | { |
394 | return ConvertUnicodeToSJIS(fromstr,tostr,maxsize); |
395 | } |
396 | case Resource_EUC : |
397 | { |
398 | return ConvertUnicodeToEUC(fromstr,tostr,maxsize); |
399 | } |
400 | case Resource_GB : |
401 | { |
402 | return ConvertUnicodeToGB(fromstr,tostr,maxsize); |
403 | } |
404 | case Resource_ANSI : |
405 | { |
406 | return ConvertUnicodeToANSI(fromstr,tostr,maxsize); |
407 | } |
408 | } |
409 | return Standard_False; |
410 | } |
411 | |