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