update dev300-m58
[ooovba.git] / rsc / source / tools / rscchar.cxx
blobfeff19dda792c69b243a23c51b757ba71419b653
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: rscchar.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_rsc.hxx"
33 /****************** I N C L U D E S **************************************/
34 #include <stdio.h>
35 #include <string.h>
36 #include <ctype.h>
38 #ifndef _TABLE_HXX //autogen
39 #include <tools/table.hxx>
40 #endif
42 // Solar Definitionen
43 #include <tools/solar.h>
44 #include <rsctools.hxx>
46 #include <rtl/textcvt.h>
47 #include <rtl/textenc.h>
48 #include <rtl/alloc.h>
50 /*************************************************************************
52 |* RscChar::MakeChar()
54 |* Beschreibung Der String wird nach C-Konvention umgesetzt
55 |* Ersterstellung MM 20.03.91
56 |* Letzte Aenderung MM 20.03.91
58 *************************************************************************/
59 char * RscChar::MakeUTF8( char * pStr, UINT16 nTextEncoding )
61 sal_Size nMaxUniCodeBuf = strlen( pStr ) + 1;
62 char * pOrgStr = new char[ nMaxUniCodeBuf ];
63 sal_uInt32 nOrgLen = 0;
65 if( nMaxUniCodeBuf * 6 > 0x0FFFFF )
66 RscExit( 10 );
68 char cOld = '1';
69 while( cOld != 0 )
71 char c;
73 if( *pStr == '\\' )
75 ++pStr;
76 switch( *pStr )
78 case 'a':
79 c = '\a';
80 break;
81 case 'b':
82 c = '\b';
83 break;
84 case 'f':
85 c = '\f';
86 break;
87 case 'n':
88 c = '\n';
89 break;
90 case 'r':
91 c = '\r';
92 break;
93 case 't':
94 c = '\t';
95 break;
96 case 'v':
97 c = '\v';
98 break;
99 case '\\':
100 c = '\\';
101 break;
102 case '?':
103 c = '\?';
104 break;
105 case '\'':
106 c = '\'';
107 break;
108 case '\"':
109 c = '\"';
110 break;
111 default:
113 if( '0' <= *pStr && '7' >= *pStr )
115 sal_uInt16 nChar = 0;
116 int i = 0;
117 while( '0' <= *pStr && '7' >= *pStr && i != 3 )
119 nChar = nChar * 8 + (BYTE)*pStr - (BYTE)'0';
120 ++pStr;
121 i++;
123 if( nChar > 255 )
125 rtl_freeMemory( pOrgStr );
127 // Wert zu gross, oder kein 3 Ziffern
128 return( NULL );
130 c = (char)nChar;
131 pStr--;
133 else if( 'x' == *pStr )
135 sal_uInt16 nChar = 0;
136 int i = 0;
137 ++pStr;
138 while( isxdigit( *pStr ) && i != 2 )
140 if( isdigit( *pStr ) )
141 nChar = nChar * 16 + (BYTE)*pStr - (BYTE)'0';
142 else if( isupper( *pStr ) )
143 nChar = nChar * 16 + (BYTE)*pStr - (BYTE)'A' +10;
144 else
145 nChar = nChar * 16 + (BYTE)*pStr - (BYTE)'a' +10;
146 ++pStr;
147 i++;
149 c = (char)nChar;
150 pStr--;
152 else
153 c = *pStr;
157 else
158 c = *pStr;
159 pOrgStr[ nOrgLen++ ] = c;
160 cOld = *pStr;
161 pStr++;
164 sal_Unicode * pUniCode = new sal_Unicode[ nMaxUniCodeBuf ];
165 rtl_TextToUnicodeConverter hConv = rtl_createTextToUnicodeConverter( nTextEncoding );
167 sal_uInt32 nInfo;
168 sal_Size nSrcCvtBytes;
169 sal_Size nUniSize = rtl_convertTextToUnicode( hConv, 0,
170 pOrgStr, nOrgLen,
171 pUniCode, nMaxUniCodeBuf,
172 RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_DEFAULT
173 | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT
174 | RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT
175 | RTL_TEXTTOUNICODE_FLAGS_FLUSH,
176 &nInfo,
177 &nSrcCvtBytes );
179 rtl_destroyTextToUnicodeConverter( hConv );
181 hConv = rtl_createUnicodeToTextConverter( RTL_TEXTENCODING_UTF8 );
182 // factor fo 6 is the maximum size of an UNICODE character as utf8
183 char * pUtf8 = (char *)rtl_allocateMemory( nUniSize * 6 );
184 rtl_convertUnicodeToText( hConv, 0,
185 pUniCode, nUniSize,
186 pUtf8, nUniSize * 6,
187 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT
188 | RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT
189 | RTL_UNICODETOTEXT_FLAGS_FLUSH,
190 &nInfo,
191 &nSrcCvtBytes );
193 rtl_destroyTextToUnicodeConverter( hConv );
195 delete[] pUniCode;
196 delete[] pOrgStr;
198 return pUtf8;
201 /*************************************************************************
203 |* RscChar::MakeChar()
205 |* Beschreibung Der String wird nach C-Konvention umgesetzt
206 |* Ersterstellung MM 20.03.91
207 |* Letzte Aenderung MM 20.03.91
209 *************************************************************************/
210 char * RscChar::MakeUTF8FromL( char * pStr )
212 sal_Size nUniPos = 0;
213 sal_Unicode * pUniCode = new sal_Unicode[ strlen( pStr ) + 1 ];
215 char cOld = '1';
216 while( cOld != 0 )
218 sal_Unicode c;
219 if( *pStr == '\\' )
221 ++pStr;
222 switch( *pStr )
224 case 'a':
225 c = '\a';
226 break;
227 case 'b':
228 c = '\b';
229 break;
230 case 'f':
231 c = '\f';
232 break;
233 case 'n':
234 c = '\n';
235 break;
236 case 'r':
237 c = '\r';
238 break;
239 case 't':
240 c = '\t';
241 break;
242 case 'v':
243 c = '\v';
244 break;
245 case '\\':
246 c = '\\';
247 break;
248 case '?':
249 c = '\?';
250 break;
251 case '\'':
252 c = '\'';
253 break;
254 case '\"':
255 c = '\"';
256 break;
257 default:
259 if( '0' <= *pStr && '7' >= *pStr )
261 UINT32 nChar = 0;
262 int i = 0;
263 while( '0' <= *pStr && '7' >= *pStr && i != 6 )
265 nChar = nChar * 8 + (BYTE)*pStr - (BYTE)'0';
266 ++pStr;
267 i++;
269 if( nChar > 0xFFFF )
270 // Wert zu gross, oder kein 3 Ziffern
271 return( FALSE );
272 c = (UINT16)nChar;
273 pStr--;
275 else if( 'x' == *pStr || 'X' == *pStr )
277 UINT32 nChar = 0;
278 int i = 0;
279 ++pStr;
280 while( isxdigit( *pStr ) && i != 4 )
282 if( isdigit( *pStr ) )
283 nChar = nChar * 16 + (BYTE)*pStr - (BYTE)'0';
284 else if( isupper( *pStr ) )
285 nChar = nChar * 16 + (BYTE)*pStr - (BYTE)'A' +10;
286 else
287 nChar = nChar * 16 + (BYTE)*pStr - (BYTE)'a' +10;
288 ++pStr;
289 i++;
291 c = (UINT16)nChar;
292 pStr--;
294 else
295 c = *pStr;
299 else
300 c = *pStr;
302 pUniCode[ nUniPos++ ] = c;
303 cOld = *pStr;
304 pStr++;
307 // factor fo 6 is the maximum size of an UNICODE character as utf8
308 sal_Size nMaxUtf8Len = nUniPos * 6;
309 if( nUniPos * 6 > 0x0FFFFF )
310 RscExit( 10 );
312 char * pUtf8 = (char *)rtl_allocateMemory( nMaxUtf8Len );
313 rtl_TextToUnicodeConverter hConv = rtl_createUnicodeToTextConverter( RTL_TEXTENCODING_UTF8 );
315 sal_uInt32 nInfo;
316 sal_Size nSrcCvtBytes;
317 rtl_convertUnicodeToText( hConv, 0,
318 pUniCode, nUniPos,
319 pUtf8, nMaxUtf8Len,
320 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT
321 | RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT
322 | RTL_UNICODETOTEXT_FLAGS_FLUSH,
323 &nInfo,
324 &nSrcCvtBytes );
326 rtl_destroyUnicodeToTextConverter( hConv );
328 delete[] pUniCode;
330 return pUtf8;