1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
24 #include <rsctools.hxx>
26 #include <rtl/textcvt.h>
27 #include <rtl/textenc.h>
28 #include <rtl/alloc.h>
30 char * RscChar::MakeUTF8( char * pStr
, sal_uInt16 nTextEncoding
)
32 sal_Size nMaxUniCodeBuf
= strlen( pStr
) + 1;
33 if( nMaxUniCodeBuf
* 6 > 0x0FFFFF )
36 char * pOrgStr
= new char[ nMaxUniCodeBuf
];
37 sal_uInt32 nOrgLen
= 0;
84 if( '0' <= *pStr
&& '7' >= *pStr
)
88 while( '0' <= *pStr
&& '7' >= *pStr
&& i
!= 3 )
90 nChar
= nChar
* 8 + (sal_uInt8
)*pStr
- (sal_uInt8
)'0';
96 // value is too big, or more than 3 digits
103 else if( 'x' == *pStr
)
105 sal_uInt16 nChar
= 0;
108 while( isxdigit( *pStr
) && i
!= 2 )
110 if( isdigit( *pStr
) )
111 nChar
= nChar
* 16 + (sal_uInt8
)*pStr
- (sal_uInt8
)'0';
112 else if( isupper( *pStr
) )
113 nChar
= nChar
* 16 + (sal_uInt8
)*pStr
- (sal_uInt8
)'A' +10;
115 nChar
= nChar
* 16 + (sal_uInt8
)*pStr
- (sal_uInt8
)'a' +10;
129 pOrgStr
[ nOrgLen
++ ] = c
;
134 sal_Unicode
* pUniCode
= new sal_Unicode
[ nMaxUniCodeBuf
];
135 rtl_TextToUnicodeConverter hConv
= rtl_createTextToUnicodeConverter( nTextEncoding
);
138 sal_Size nSrcCvtBytes
;
139 sal_Size nUniSize
= rtl_convertTextToUnicode( hConv
, 0,
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
,
149 rtl_destroyTextToUnicodeConverter( hConv
);
150 delete[] pOrgStr
, pOrgStr
= 0;
152 hConv
= rtl_createUnicodeToTextConverter( RTL_TEXTENCODING_UTF8
);
153 // factor of 6 is the maximum size of an UNICODE character as utf8
154 char * pUtf8
= static_cast<char *>(rtl_allocateMemory( nUniSize
* 6 ));
155 rtl_convertUnicodeToText( hConv
, 0,
158 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT
159 | RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT
160 | RTL_UNICODETOTEXT_FLAGS_FLUSH
,
164 rtl_destroyTextToUnicodeConverter( hConv
);
165 delete[] pUniCode
, pUniCode
= 0;
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */