Debian Codegraph @

lib/database/src/so/upgraders/SoV2Text2.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 * Copyright (C) 1990,91,92 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.1.1.1 $
44 |
45 | Classes:
46 | SoV2Text2
47 |
48 | Author(s) : Gavin Bell
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54 #include "SoV2Text2.h"
55 #include <Inventor/nodes/SoText2.h>
56
57 #include <iconv.h>
58 #include <errno.h>
59
60
61 SO_NODE_SOURCE(SoV2Text2);
62
63
64 ////////////////////////////////////////////////////////////////////////
65 //
66 // Description:
67 // Default constructor
68 //
69 // Use: public
70
71 SoV2Text2::SoV2Text2()
72 //
73 ////////////////////////////////////////////////////////////////////////
74 {
75 SO_NODE_CONSTRUCTOR(SoV2Text2);
76
77 SO_NODE_ADD_FIELD(string, (""));
78 SO_NODE_ADD_FIELD(spacing, (1.0));
79 SO_NODE_ADD_FIELD(justification, (LEFT));
80
81 // Set up static info for enumerated type field
82 SO_NODE_DEFINE_ENUM_VALUE(Justification, LEFT);
83 SO_NODE_DEFINE_ENUM_VALUE(Justification, RIGHT);
84 SO_NODE_DEFINE_ENUM_VALUE(Justification, CENTER);
85
86 // Set up info in enumerated type field
87 SO_NODE_SET_SF_ENUM_TYPE(justification, Justification);
88
89 isBuiltIn = TRUE;
90 }
91
92 ////////////////////////////////////////////////////////////////////////
93 //
94 // Description:
95 // Destructor.
96 //
97 // Use: private
98
99 SoV2Text2::~SoV2Text2()
100 //
101 ////////////////////////////////////////////////////////////////////////
102 {
103 }
104
105 ////////////////////////////////////////////////////////////////////////
106 //
107 // Description:
108 // Create a version 2.0 SoText2.
109 //
110 // Use: private
111
112 SoNode *
113 SoV2Text2::createNewNode()
114 //
115 ////////////////////////////////////////////////////////////////////////
116 {
117 SoText2 *result = SO_UPGRADER_CREATE_NEW(SoText2);
118
119 // if european characters present they are converted to UTF-8
120 for (int i = 0; i < string.getNum(); i++) {
121 SbString str("");
122 if (convertToUTF8(string[i], str)) {
123 result->string.set1Value(i, str);
124 }
125 }
126 result->spacing.setValue(spacing.getValue());
127 result->justification.setValue(justification.getValue());
128
129 return result;
130 }
131 // Following is different for irix 6.2
132 static iconv_t codeConvert1 = NULL;
133 static iconv_t codeConvert2 = NULL;
134
135 ////////////////////////////////////////////////////////////////////////
136 //
137 // Description:
138 // Convert a string to UTF-8. If it's ascii, leave it alone.
139 // otherwise assume it's ISO-8859-1 (Western European)
140 //
141 // Use: static, public
142 SbBool
143 SoV2Text2::convertToUTF8(const SbString &inString,
144 SbString &outString )
145 //
146 ////////////////////////////////////////////////////////////////////////
147 {
148 const char* str = inString.getString();
149 SbBool ascii = TRUE;
150 for (int i= 0; i< inString.getLength(); i++){
151 if ( str[i]& 0x80) {
152 ascii = FALSE;
153 break;
154 }
155 }
156 if (ascii) {
157 outString = inString; // this does a copy.
158 return TRUE;
159 }
160
161 //Do a conversion first to UCS-2, then UTF-8. That's because
162 //not all Irix systems have iconv conversion table to go
163 //directly to UTF-8
164
165 if( codeConvert1 == NULL){
166 codeConvert1 = iconv_open("UCS-2", "ISO8859-1");
167 codeConvert2 = iconv_open("UTF-8", "UCS-2");
168 }
169
170 if ( codeConvert1 == (iconv_t)-1 ){
171 #ifdef DEBUG
172 SoDebugError::post("SoV2Text2::convertToUTF8",
173 "Invalid ISO8859-1 to UCS-2 conversion");
174 #endif /*DEBUG*/
175 return FALSE;
176 }
177 if ( codeConvert2 == (iconv_t)-1 ){
178 #ifdef DEBUG
179 SoDebugError::post("SoV2Text2::convertToUTF8",
180 "Invalid UCS-2 to UTF-8 conversion");
181 #endif /*DEBUG*/
182 return FALSE;
183 }
184 // allocate a sufficiently large buffer:
185 char * UCSBuf = new char[2*inString.getLength()+1];
186 char * UTFBuf = new char[2*inString.getLength()+1];
187
188 char* input = (char *)inString.getString();
189 size_t inbytes = inString.getLength();
190 size_t outbytes = 2*inbytes;
191 char* output = (char*)UCSBuf;
192 if (iconv(codeConvert1, &input, &inbytes, &output, &outbytes) == (size_t)-1){
193 #ifdef DEBUG
194 SoDebugError::post("SoV2Text2::convertToUTF8",
195 "Error converting text to UCS-2");
196 #endif /*DEBUG*/
197 }
198 input = (char *)UCSBuf;
199 outbytes = 2*inString.getLength()+1;
200 inbytes = 2*inString.getLength();
201 output = (char*)UTFBuf;
202 if ((iconv(codeConvert2, &input, &inbytes, &output, &outbytes) != 0)){
203 #ifdef DEBUG
204 switch(errno){
205 case EILSEQ: printf("EILSEQ\n");
206 break;
207 case EINVAL: printf("EINVAL\n");
208 break;
209 case E2BIG: printf("E2BIG\n");
210 break;
211 default: printf("errno %d\n", errno);
212 }
213 SoDebugError::post("SoV2Text2::convertToUTF8",
214 "Error converting text to UTF-8");
215
216 #endif /*DEBUG*/
217 delete [] UCSBuf;
218 delete [] UTFBuf;
219 return FALSE;
220 }
221
222 // make sure that there is a NULL at the end of the string
223 UTFBuf[2*inString.getLength()-outbytes+1] = '\0';
224
225 outString = UTFBuf;
226
227 // SbString* str1 = new SbString(UTFBuf, 0, 2*inString.getLength()-outbytes);
228
229 delete [] UCSBuf;
230 delete [] UTFBuf;
231
232 return TRUE;
233 }