xref: /AOO41X/main/rsc/source/tools/rscchar.cxx (revision 477794c15c6b4ddeee1d73cab1de4160cfe6efef)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_rsc.hxx"
26 /****************** I N C L U D E S **************************************/
27 #include <stdio.h>
28 #include <string.h>
29 #include <ctype.h>
30 
31 #ifndef _TABLE_HXX //autogen
32 #include <tools/table.hxx>
33 #endif
34 
35 // Solar Definitionen
36 #include <tools/solar.h>
37 #include <rsctools.hxx>
38 
39 #include <rtl/textcvt.h>
40 #include <rtl/textenc.h>
41 #include <rtl/alloc.h>
42 
43 /*************************************************************************
44 |*
45 |*    RscChar::MakeChar()
46 |*
47 |*    Beschreibung      Der String wird nach C-Konvention umgesetzt
48 |*    Ersterstellung    MM 20.03.91
49 |*    Letzte Aenderung  MM 20.03.91
50 |*
51 *************************************************************************/
MakeUTF8(char * pStr,sal_uInt16 nTextEncoding)52 char * RscChar::MakeUTF8( char * pStr, sal_uInt16 nTextEncoding )
53 {
54     sal_Size nMaxUniCodeBuf = strlen( pStr ) + 1;
55     if( nMaxUniCodeBuf * 6 > 0x0FFFFF )
56         RscExit( 10 );
57 
58     char *          pOrgStr = new char[ nMaxUniCodeBuf ];
59     sal_uInt32      nOrgLen = 0;
60 
61     char cOld = '1';
62     while( cOld != 0 )
63     {
64         char c;
65 
66         if( *pStr == '\\' )
67         {
68             ++pStr;
69             switch( *pStr )
70             {
71                 case 'a':
72                     c = '\a';
73                     break;
74                 case 'b':
75                     c = '\b';
76                     break;
77                 case 'f':
78                     c = '\f';
79                     break;
80                 case 'n':
81                     c = '\n';
82                     break;
83                 case 'r':
84                     c = '\r';
85                     break;
86                 case 't':
87                     c = '\t';
88                     break;
89                 case 'v':
90                     c = '\v';
91                     break;
92                 case '\\':
93                     c = '\\';
94                     break;
95                 case '?':
96                     c = '\?';
97                     break;
98                 case '\'':
99                     c = '\'';
100                     break;
101                 case '\"':
102                     c = '\"';
103                     break;
104                 default:
105                 {
106                     if( '0' <= *pStr && '7' >= *pStr )
107                     {
108                         sal_uInt16  nChar = 0;
109                         int  i = 0;
110                         while( '0' <= *pStr && '7' >= *pStr && i != 3 )
111                         {
112                             nChar = nChar * 8 + (sal_uInt8)*pStr - (sal_uInt8)'0';
113                             ++pStr;
114                             i++;
115                         }
116                         if( nChar > 255 )
117                         {
118                             // Wert zu gross, oder kein 3 Ziffern
119                             delete [] pOrgStr;
120                             return( NULL );
121                         }
122                         c = (char)nChar;
123                         pStr--;
124                     }
125                     else if( 'x' == *pStr )
126                     {
127                         sal_uInt16  nChar = 0;
128                         int  i = 0;
129                         ++pStr;
130                         while( isxdigit( *pStr ) && i != 2 )
131                         {
132                             if( isdigit( *pStr ) )
133                                 nChar = nChar * 16 + (sal_uInt8)*pStr - (sal_uInt8)'0';
134                             else if( isupper( *pStr ) )
135                                 nChar = nChar * 16 + (sal_uInt8)*pStr - (sal_uInt8)'A' +10;
136                             else
137                                 nChar = nChar * 16 + (sal_uInt8)*pStr - (sal_uInt8)'a' +10;
138                             ++pStr;
139                             i++;
140                         }
141                         c = (char)nChar;
142                         pStr--;
143                     }
144                     else
145                         c = *pStr;
146                 };
147             }
148         }
149         else
150             c = *pStr;
151         pOrgStr[ nOrgLen++ ] = c;
152         cOld = *pStr;
153         pStr++;
154     }
155 
156     sal_Unicode *   pUniCode = new sal_Unicode[ nMaxUniCodeBuf ];
157     rtl_TextToUnicodeConverter hConv = rtl_createTextToUnicodeConverter( nTextEncoding );
158 
159     sal_uInt32 nInfo;
160     sal_Size   nSrcCvtBytes;
161     sal_Size nUniSize = rtl_convertTextToUnicode( hConv, 0,
162                                                 pOrgStr, nOrgLen,
163                                                 pUniCode, nMaxUniCodeBuf,
164                                                 RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_DEFAULT
165                                                 | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT
166                                                 | RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT
167                                                 | RTL_TEXTTOUNICODE_FLAGS_FLUSH,
168                                                 &nInfo,
169                                                 &nSrcCvtBytes );
170 
171     rtl_destroyTextToUnicodeConverter( hConv );
172     delete[] pOrgStr, pOrgStr = 0;
173 
174     hConv = rtl_createUnicodeToTextConverter( RTL_TEXTENCODING_UTF8 );
175     // factor fo 6 is the maximum size of an UNICODE character as utf8
176     char * pUtf8 = (char *)rtl_allocateMemory( nUniSize * 6 );
177     rtl_convertUnicodeToText( hConv, 0,
178                             pUniCode, nUniSize,
179                             pUtf8, nUniSize * 6,
180                             RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT
181                             | RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT
182                             | RTL_UNICODETOTEXT_FLAGS_FLUSH,
183                             &nInfo,
184                             &nSrcCvtBytes );
185 
186     rtl_destroyTextToUnicodeConverter( hConv );
187     delete[] pUniCode, pUniCode = 0;
188 
189     return pUtf8;
190 };
191