1 // Created on: 1996-09-26
2 // Created by: Arnaud BOUZY
3 // Copyright (c) 1996-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License 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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <Resource_Unicode.ixx>
18 #include <Resource_ConvertUnicode.hxx>
19 #include <Resource_Manager.hxx>
20 #include <TCollection_AsciiString.hxx>
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))
26 #define issjis2(c) ((c)>=0x40 && (c)<=0xfc && (c)!=0x7f)
28 #define ishankana(c) ((c)>=0xa0 && (c)<=0xdf)
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; }
33 void Resource_Unicode::ConvertSJISToUnicode(const Standard_CString fromstr,TCollection_ExtendedString& tostr)
37 unsigned char* currentstr = ((unsigned char*) fromstr);
39 // BIG INDIAN USED HERE
40 while(*currentstr != '\0') {
41 if (issjis1(*currentstr)) {
43 ph = ((unsigned int) *currentstr);
44 // Be Carefull with first and second !!
48 pl = ((unsigned int) *currentstr);
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);
57 TCollection_ExtendedString curext(((char) *currentstr));
59 tostr.AssignCat(curext);
65 void Resource_Unicode::ConvertEUCToUnicode(const Standard_CString fromstr,TCollection_ExtendedString& tostr)
69 unsigned char* currentstr = ((unsigned char*) fromstr);
71 // BIG INDIAN USED HERE
72 while(*currentstr != '\0') {
73 if (iseuc(*currentstr)) {
75 ph = ((unsigned int) *currentstr);
76 // Be Carefull with first and second !!
80 pl = ((unsigned int) *currentstr);
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);
89 TCollection_ExtendedString curext(((char) *currentstr));
91 tostr.AssignCat(curext);
96 void Resource_Unicode::ConvertGBToUnicode(const Standard_CString fromstr,TCollection_ExtendedString& tostr)
100 unsigned char* currentstr = ((unsigned char*) fromstr);
102 // BIG INDIAN USED HERE
103 while(*currentstr != '\0') {
104 if (isshift(*currentstr)) {
106 ph = ((unsigned int) *currentstr);
107 // Be Carefull with first and second !!
111 pl = ((unsigned int) *currentstr);
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);
120 TCollection_ExtendedString curext(((char) *currentstr));
122 tostr.AssignCat(curext);
127 void Resource_Unicode::ConvertANSIToUnicode(const Standard_CString fromstr,TCollection_ExtendedString& tostr)
131 TCollection_ExtendedString curext(fromstr);
132 tostr.AssignCat(curext);
135 Standard_Boolean Resource_Unicode::ConvertUnicodeToSJIS(const TCollection_ExtendedString& fromstr,
136 Standard_PCharacter& tostr,
137 const Standard_Integer maxsize)
139 Standard_Integer nbtrans = 0;
140 Standard_Integer nbext = 1;
141 Standard_Boolean finished = Standard_False;
142 Standard_ExtCharacter curcar;
144 // BIG INDIAN USED HERE
147 if (nbext > fromstr.Length()) {
148 finished = Standard_True;
149 tostr[nbtrans] = '\0';
152 curcar = fromstr.Value(nbext);
154 ph = (((unsigned int) curcar) >> 8) & 0xFF;
155 pl = ((unsigned int) curcar) & 0xFF;
156 Resource_unicode_to_sjis(&ph,&pl);
158 if (nbtrans < (maxsize-3)) {
159 tostr[nbtrans] = ((char) ph);
161 tostr[nbtrans] = ((char) pl);
165 tostr[nbtrans] = '\0';
167 return Standard_False;
171 tostr[nbtrans] = ((char) pl);
174 if (nbtrans >= (maxsize - 1)) {
175 tostr[maxsize-1] = '\0';
176 finished = Standard_True;
177 return Standard_False;
181 return Standard_True;
184 Standard_Boolean Resource_Unicode::ConvertUnicodeToEUC(const TCollection_ExtendedString& fromstr,
185 Standard_PCharacter& tostr,
186 const Standard_Integer maxsize)
188 Standard_Integer nbtrans = 0;
189 Standard_Integer nbext = 1;
190 Standard_Boolean finished = Standard_False;
191 Standard_ExtCharacter curcar;
193 // BIG INDIAN USED HERE
196 if (nbext > fromstr.Length()) {
197 finished = Standard_True;
198 tostr[nbtrans] = '\0';
201 curcar = fromstr.Value(nbext);
203 ph = (((unsigned int) curcar) >> 8) & 0xFF;
204 pl = ((unsigned int) curcar) & 0xFF;
205 Resource_unicode_to_euc(&ph,&pl);
207 if (nbtrans < (maxsize-3)) {
208 tostr[nbtrans] = ((char) ph);
210 tostr[nbtrans] = ((char) pl);
214 tostr[nbtrans-1] = '\0';
216 return Standard_False;
220 tostr[nbtrans] = ((char) pl);
223 if (nbtrans >= (maxsize - 1)) {
224 tostr[maxsize-1] = '\0';
225 finished = Standard_True;
226 return Standard_False;
230 return Standard_True;
233 Standard_Boolean Resource_Unicode::ConvertUnicodeToGB(const TCollection_ExtendedString& fromstr,
234 Standard_PCharacter& tostr,
235 const Standard_Integer maxsize)
237 Standard_Integer nbtrans = 0;
238 Standard_Integer nbext = 1;
239 Standard_Boolean finished = Standard_False;
240 Standard_ExtCharacter curcar;
242 // BIG INDIAN USED HERE
245 if (nbext > fromstr.Length()) {
246 finished = Standard_True;
247 tostr[nbtrans] = '\0';
250 curcar = fromstr.Value(nbext);
252 ph = (((unsigned int) curcar) >> 8) & 0xFF;
253 pl = ((unsigned int) curcar) & 0xFF;
254 Resource_unicode_to_gb(&ph,&pl);
256 if (nbtrans < (maxsize-3)) {
257 tostr[nbtrans] = ((char) ph);
259 tostr[nbtrans] = ((char) pl);
263 tostr[nbtrans-1] = '\0';
265 return Standard_False;
269 tostr[nbtrans] = ((char) curcar) & 0xFF;
272 if (nbtrans >= (maxsize - 1)) {
273 tostr[maxsize-1] = '\0';
274 finished = Standard_True;
275 return Standard_False;
279 return Standard_True;
282 Standard_Boolean Resource_Unicode::ConvertUnicodeToANSI(const TCollection_ExtendedString& fromstr,
283 Standard_PCharacter& tostr,
284 const Standard_Integer maxsize)
286 Standard_Integer nbtrans = 0;
287 Standard_Integer nbext = 1;
288 Standard_Boolean finished = Standard_False;
289 Standard_ExtCharacter curcar;
291 // BIG INDIAN USED HERE
294 if (nbext > fromstr.Length()) {
295 finished = Standard_True;
296 tostr[nbtrans] = '\0';
299 curcar = fromstr.Value(nbext);
301 ph = ((unsigned int) curcar) >> 8;
302 pl = ((unsigned int) curcar) & 0xFF;
304 tostr[nbtrans] = ((char) pl);
307 tostr[nbtrans] = ' ';
311 if (nbtrans >= (maxsize - 1)) {
312 tostr[maxsize-1] = '\0';
313 finished = Standard_True;
314 return Standard_False;
317 return Standard_True;
320 static Standard_Boolean AlreadyRead = Standard_False;
322 static Resource_FormatType& Resource_Current_Format()
324 static Resource_FormatType theformat = Resource_ANSI;
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;
333 else if (form.IsEqual("EUC")) {
334 theformat = Resource_EUC;
336 else if (form.IsEqual("GB")) {
337 theformat = Resource_GB;
340 theformat = Resource_ANSI;
344 theformat = Resource_ANSI;
350 void Resource_Unicode::SetFormat(const Resource_FormatType typecode)
352 AlreadyRead = Standard_True;
353 Resource_Current_Format() = typecode;
356 Resource_FormatType Resource_Unicode::GetFormat()
358 return Resource_Current_Format();
362 void Resource_Unicode::ReadFormat()
364 AlreadyRead = Standard_False;
365 Resource_Unicode::GetFormat();
368 void Resource_Unicode::ConvertFormatToUnicode(const Standard_CString fromstr,
369 TCollection_ExtendedString& tostr)
371 Resource_FormatType theform = Resource_Unicode::GetFormat();
375 ConvertSJISToUnicode(fromstr,tostr);
380 ConvertEUCToUnicode(fromstr,tostr);
385 ConvertGBToUnicode(fromstr,tostr);
390 ConvertANSIToUnicode(fromstr,tostr);
396 Standard_Boolean Resource_Unicode::ConvertUnicodeToFormat(const TCollection_ExtendedString& fromstr,
397 Standard_PCharacter& tostr,
398 const Standard_Integer maxsize)
400 Resource_FormatType theform = Resource_Unicode::GetFormat();
404 return ConvertUnicodeToSJIS(fromstr,tostr,maxsize);
408 return ConvertUnicodeToEUC(fromstr,tostr,maxsize);
412 return ConvertUnicodeToGB(fromstr,tostr,maxsize);
416 return ConvertUnicodeToANSI(fromstr,tostr,maxsize);
419 return Standard_False;