xref: /aoo41x/main/rsc/source/tools/rscchar.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_rsc.hxx"
30*cdf0e10cSrcweir /****************** I N C L U D E S **************************************/
31*cdf0e10cSrcweir #include <stdio.h>
32*cdf0e10cSrcweir #include <string.h>
33*cdf0e10cSrcweir #include <ctype.h>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #ifndef _TABLE_HXX //autogen
36*cdf0e10cSrcweir #include <tools/table.hxx>
37*cdf0e10cSrcweir #endif
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir // Solar Definitionen
40*cdf0e10cSrcweir #include <tools/solar.h>
41*cdf0e10cSrcweir #include <rsctools.hxx>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include <rtl/textcvt.h>
44*cdf0e10cSrcweir #include <rtl/textenc.h>
45*cdf0e10cSrcweir #include <rtl/alloc.h>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir /*************************************************************************
48*cdf0e10cSrcweir |*
49*cdf0e10cSrcweir |*    RscChar::MakeChar()
50*cdf0e10cSrcweir |*
51*cdf0e10cSrcweir |*    Beschreibung      Der String wird nach C-Konvention umgesetzt
52*cdf0e10cSrcweir |*    Ersterstellung    MM 20.03.91
53*cdf0e10cSrcweir |*    Letzte Aenderung  MM 20.03.91
54*cdf0e10cSrcweir |*
55*cdf0e10cSrcweir *************************************************************************/
56*cdf0e10cSrcweir char * RscChar::MakeUTF8( char * pStr, sal_uInt16 nTextEncoding )
57*cdf0e10cSrcweir {
58*cdf0e10cSrcweir 	sal_Size nMaxUniCodeBuf = strlen( pStr ) + 1;
59*cdf0e10cSrcweir 	if( nMaxUniCodeBuf * 6 > 0x0FFFFF )
60*cdf0e10cSrcweir         RscExit( 10 );
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 	char *			pOrgStr = new char[ nMaxUniCodeBuf ];
63*cdf0e10cSrcweir 	sal_uInt32		nOrgLen = 0;
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 	char cOld = '1';
66*cdf0e10cSrcweir 	while( cOld != 0 )
67*cdf0e10cSrcweir 	{
68*cdf0e10cSrcweir 		char c;
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 		if( *pStr == '\\' )
71*cdf0e10cSrcweir 		{
72*cdf0e10cSrcweir 			++pStr;
73*cdf0e10cSrcweir 			switch( *pStr )
74*cdf0e10cSrcweir 			{
75*cdf0e10cSrcweir 				case 'a':
76*cdf0e10cSrcweir 					c = '\a';
77*cdf0e10cSrcweir 					break;
78*cdf0e10cSrcweir 				case 'b':
79*cdf0e10cSrcweir 					c = '\b';
80*cdf0e10cSrcweir 					break;
81*cdf0e10cSrcweir 				case 'f':
82*cdf0e10cSrcweir 					c = '\f';
83*cdf0e10cSrcweir 					break;
84*cdf0e10cSrcweir 				case 'n':
85*cdf0e10cSrcweir 					c = '\n';
86*cdf0e10cSrcweir 					break;
87*cdf0e10cSrcweir 				case 'r':
88*cdf0e10cSrcweir 					c = '\r';
89*cdf0e10cSrcweir 					break;
90*cdf0e10cSrcweir 				case 't':
91*cdf0e10cSrcweir 					c = '\t';
92*cdf0e10cSrcweir 					break;
93*cdf0e10cSrcweir 				case 'v':
94*cdf0e10cSrcweir 					c = '\v';
95*cdf0e10cSrcweir 					break;
96*cdf0e10cSrcweir 				case '\\':
97*cdf0e10cSrcweir 					c = '\\';
98*cdf0e10cSrcweir 					break;
99*cdf0e10cSrcweir 				case '?':
100*cdf0e10cSrcweir 					c = '\?';
101*cdf0e10cSrcweir 					break;
102*cdf0e10cSrcweir 				case '\'':
103*cdf0e10cSrcweir 					c = '\'';
104*cdf0e10cSrcweir 					break;
105*cdf0e10cSrcweir 				case '\"':
106*cdf0e10cSrcweir 					c = '\"';
107*cdf0e10cSrcweir 					break;
108*cdf0e10cSrcweir 				default:
109*cdf0e10cSrcweir 				{
110*cdf0e10cSrcweir 					if( '0' <= *pStr && '7' >= *pStr )
111*cdf0e10cSrcweir 					{
112*cdf0e10cSrcweir 						sal_uInt16  nChar = 0;
113*cdf0e10cSrcweir 						int  i = 0;
114*cdf0e10cSrcweir 						while( '0' <= *pStr && '7' >= *pStr && i != 3 )
115*cdf0e10cSrcweir 						{
116*cdf0e10cSrcweir 							nChar = nChar * 8 + (sal_uInt8)*pStr - (sal_uInt8)'0';
117*cdf0e10cSrcweir 							++pStr;
118*cdf0e10cSrcweir 							i++;
119*cdf0e10cSrcweir 						}
120*cdf0e10cSrcweir 						if( nChar > 255 )
121*cdf0e10cSrcweir 						{
122*cdf0e10cSrcweir 							// Wert zu gross, oder kein 3 Ziffern
123*cdf0e10cSrcweir 							delete [] pOrgStr;
124*cdf0e10cSrcweir 							return( NULL );
125*cdf0e10cSrcweir 						}
126*cdf0e10cSrcweir 						c = (char)nChar;
127*cdf0e10cSrcweir 						pStr--;
128*cdf0e10cSrcweir 					}
129*cdf0e10cSrcweir 					else if( 'x' == *pStr )
130*cdf0e10cSrcweir 					{
131*cdf0e10cSrcweir 						sal_uInt16  nChar = 0;
132*cdf0e10cSrcweir 						int  i = 0;
133*cdf0e10cSrcweir 						++pStr;
134*cdf0e10cSrcweir 						while( isxdigit( *pStr ) && i != 2 )
135*cdf0e10cSrcweir 						{
136*cdf0e10cSrcweir 							if( isdigit( *pStr ) )
137*cdf0e10cSrcweir 								nChar = nChar * 16 + (sal_uInt8)*pStr - (sal_uInt8)'0';
138*cdf0e10cSrcweir 							else if( isupper( *pStr ) )
139*cdf0e10cSrcweir 								nChar = nChar * 16 + (sal_uInt8)*pStr - (sal_uInt8)'A' +10;
140*cdf0e10cSrcweir 							else
141*cdf0e10cSrcweir 								nChar = nChar * 16 + (sal_uInt8)*pStr - (sal_uInt8)'a' +10;
142*cdf0e10cSrcweir 							++pStr;
143*cdf0e10cSrcweir 							i++;
144*cdf0e10cSrcweir 						}
145*cdf0e10cSrcweir 						c = (char)nChar;
146*cdf0e10cSrcweir 						pStr--;
147*cdf0e10cSrcweir 					}
148*cdf0e10cSrcweir 					else
149*cdf0e10cSrcweir 						c = *pStr;
150*cdf0e10cSrcweir 				};
151*cdf0e10cSrcweir 			}
152*cdf0e10cSrcweir 		}
153*cdf0e10cSrcweir 		else
154*cdf0e10cSrcweir 			c = *pStr;
155*cdf0e10cSrcweir 		pOrgStr[ nOrgLen++ ] = c;
156*cdf0e10cSrcweir 		cOld = *pStr;
157*cdf0e10cSrcweir 		pStr++;
158*cdf0e10cSrcweir 	}
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 	sal_Unicode *	pUniCode = new sal_Unicode[ nMaxUniCodeBuf ];
161*cdf0e10cSrcweir 	rtl_TextToUnicodeConverter hConv = rtl_createTextToUnicodeConverter( nTextEncoding );
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 	sal_uInt32 nInfo;
164*cdf0e10cSrcweir 	sal_Size   nSrcCvtBytes;
165*cdf0e10cSrcweir 	sal_Size nUniSize = rtl_convertTextToUnicode( hConv, 0,
166*cdf0e10cSrcweir 												pOrgStr, nOrgLen,
167*cdf0e10cSrcweir 												pUniCode, nMaxUniCodeBuf,
168*cdf0e10cSrcweir 												RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_DEFAULT
169*cdf0e10cSrcweir 												| RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT
170*cdf0e10cSrcweir 												| RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT
171*cdf0e10cSrcweir 												| RTL_TEXTTOUNICODE_FLAGS_FLUSH,
172*cdf0e10cSrcweir 												&nInfo,
173*cdf0e10cSrcweir 												&nSrcCvtBytes );
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 	rtl_destroyTextToUnicodeConverter( hConv );
176*cdf0e10cSrcweir 	delete[] pOrgStr, pOrgStr = 0;
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir 	hConv = rtl_createUnicodeToTextConverter( RTL_TEXTENCODING_UTF8 );
179*cdf0e10cSrcweir 	// factor fo 6 is the maximum size of an UNICODE character as utf8
180*cdf0e10cSrcweir     char * pUtf8 = (char *)rtl_allocateMemory( nUniSize * 6 );
181*cdf0e10cSrcweir 	rtl_convertUnicodeToText( hConv, 0,
182*cdf0e10cSrcweir 							pUniCode, nUniSize,
183*cdf0e10cSrcweir 							pUtf8, nUniSize * 6,
184*cdf0e10cSrcweir 							RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT
185*cdf0e10cSrcweir 							| RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT
186*cdf0e10cSrcweir 							| RTL_UNICODETOTEXT_FLAGS_FLUSH,
187*cdf0e10cSrcweir 							&nInfo,
188*cdf0e10cSrcweir 							&nSrcCvtBytes );
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 	rtl_destroyTextToUnicodeConverter( hConv );
191*cdf0e10cSrcweir 	delete[] pUniCode, pUniCode = 0;
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 	return pUtf8;
194*cdf0e10cSrcweir };
195