Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <stdio.h>
21 : #include <string.h>
22 : #include <ctype.h>
23 :
24 : #include <rsctools.hxx>
25 :
26 : #include <rtl/textcvt.h>
27 : #include <rtl/textenc.h>
28 : #include <rtl/alloc.h>
29 :
30 0 : char * RscChar::MakeUTF8( char * pStr, sal_uInt16 nTextEncoding )
31 : {
32 0 : sal_Size nMaxUniCodeBuf = strlen( pStr ) + 1;
33 0 : if( nMaxUniCodeBuf * 6 > 0x0FFFFF )
34 0 : RscExit( 10 );
35 :
36 0 : char * pOrgStr = new char[ nMaxUniCodeBuf ];
37 0 : sal_uInt32 nOrgLen = 0;
38 :
39 0 : char cOld = '1';
40 0 : while( cOld != 0 )
41 : {
42 : char c;
43 :
44 0 : if( *pStr == '\\' )
45 : {
46 0 : ++pStr;
47 0 : switch( *pStr )
48 : {
49 : case 'a':
50 0 : c = '\a';
51 0 : break;
52 : case 'b':
53 0 : c = '\b';
54 0 : break;
55 : case 'f':
56 0 : c = '\f';
57 0 : break;
58 : case 'n':
59 0 : c = '\n';
60 0 : break;
61 : case 'r':
62 0 : c = '\r';
63 0 : break;
64 : case 't':
65 0 : c = '\t';
66 0 : break;
67 : case 'v':
68 0 : c = '\v';
69 0 : break;
70 : case '\\':
71 0 : c = '\\';
72 0 : break;
73 : case '?':
74 0 : c = '\?';
75 0 : break;
76 : case '\'':
77 0 : c = '\'';
78 0 : break;
79 : case '\"':
80 0 : c = '\"';
81 0 : break;
82 : default:
83 : {
84 0 : if( '0' <= *pStr && '7' >= *pStr )
85 : {
86 0 : sal_uInt16 nChar = 0;
87 0 : int i = 0;
88 0 : while( '0' <= *pStr && '7' >= *pStr && i != 3 )
89 : {
90 0 : nChar = nChar * 8 + (sal_uInt8)*pStr - (sal_uInt8)'0';
91 0 : ++pStr;
92 0 : i++;
93 : }
94 0 : if( nChar > 255 )
95 : {
96 : // value is too big, or more than 3 digits
97 0 : delete [] pOrgStr;
98 0 : return( NULL );
99 : }
100 0 : c = (char)nChar;
101 0 : pStr--;
102 : }
103 0 : else if( 'x' == *pStr )
104 : {
105 0 : sal_uInt16 nChar = 0;
106 0 : int i = 0;
107 0 : ++pStr;
108 0 : while( isxdigit( *pStr ) && i != 2 )
109 : {
110 0 : if( isdigit( *pStr ) )
111 0 : nChar = nChar * 16 + (sal_uInt8)*pStr - (sal_uInt8)'0';
112 0 : else if( isupper( *pStr ) )
113 0 : nChar = nChar * 16 + (sal_uInt8)*pStr - (sal_uInt8)'A' +10;
114 : else
115 0 : nChar = nChar * 16 + (sal_uInt8)*pStr - (sal_uInt8)'a' +10;
116 0 : ++pStr;
117 0 : i++;
118 : }
119 0 : c = (char)nChar;
120 0 : pStr--;
121 : }
122 : else
123 0 : c = *pStr;
124 : };
125 : }
126 : }
127 : else
128 0 : c = *pStr;
129 0 : pOrgStr[ nOrgLen++ ] = c;
130 0 : cOld = *pStr;
131 0 : pStr++;
132 : }
133 :
134 0 : sal_Unicode * pUniCode = new sal_Unicode[ nMaxUniCodeBuf ];
135 0 : rtl_TextToUnicodeConverter hConv = rtl_createTextToUnicodeConverter( nTextEncoding );
136 :
137 : sal_uInt32 nInfo;
138 : sal_Size nSrcCvtBytes;
139 : sal_Size nUniSize = rtl_convertTextToUnicode( hConv, 0,
140 : pOrgStr, nOrgLen,
141 : pUniCode, nMaxUniCodeBuf,
142 : RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_DEFAULT
143 : | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT
144 : | RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT
145 : | RTL_TEXTTOUNICODE_FLAGS_FLUSH,
146 : &nInfo,
147 0 : &nSrcCvtBytes );
148 :
149 0 : rtl_destroyTextToUnicodeConverter( hConv );
150 0 : delete[] pOrgStr, pOrgStr = 0;
151 :
152 0 : hConv = rtl_createUnicodeToTextConverter( RTL_TEXTENCODING_UTF8 );
153 : // factor of 6 is the maximum size of an UNICODE character as utf8
154 0 : char * pUtf8 = (char *)rtl_allocateMemory( nUniSize * 6 );
155 : rtl_convertUnicodeToText( hConv, 0,
156 : pUniCode, nUniSize,
157 : pUtf8, nUniSize * 6,
158 : RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT
159 : | RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT
160 : | RTL_UNICODETOTEXT_FLAGS_FLUSH,
161 : &nInfo,
162 0 : &nSrcCvtBytes );
163 :
164 0 : rtl_destroyTextToUnicodeConverter( hConv );
165 0 : delete[] pUniCode, pUniCode = 0;
166 :
167 0 : return pUtf8;
168 : };
169 :
170 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|