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