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 "sal/config.h"
21 :
22 : #include "rtl/textcvt.h"
23 :
24 : #include "gettextencodingdata.hxx"
25 : #include "tenchelp.hxx"
26 :
27 : /* ======================================================================= */
28 :
29 368 : static sal_Size ImplDummyToUnicode( const char* pSrcBuf, sal_Size nSrcBytes,
30 : sal_Unicode* pDestBuf, sal_Size nDestChars,
31 : sal_uInt32 nFlags, sal_uInt32* pInfo,
32 : sal_Size* pSrcCvtBytes )
33 : {
34 : sal_Unicode* pEndDestBuf;
35 : const char* pEndSrcBuf;
36 :
37 736 : if ( ((nFlags & RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MASK) == RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR) ||
38 368 : ((nFlags & RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_MASK) == RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR) )
39 : {
40 : *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR |
41 : RTL_TEXTTOUNICODE_INFO_UNDEFINED |
42 0 : RTL_TEXTTOUNICODE_INFO_MBUNDEFINED;
43 0 : return 0;
44 : }
45 :
46 368 : *pInfo = 0;
47 368 : pEndDestBuf = pDestBuf+nDestChars;
48 368 : pEndSrcBuf = pSrcBuf+nSrcBytes;
49 3142 : while ( pSrcBuf < pEndSrcBuf )
50 : {
51 2406 : if ( pDestBuf == pEndDestBuf )
52 : {
53 0 : *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
54 0 : break;
55 : }
56 :
57 2406 : *pDestBuf = (sal_Unicode)(unsigned char)*pSrcBuf;
58 2406 : pDestBuf++;
59 2406 : pSrcBuf++;
60 : }
61 :
62 368 : *pSrcCvtBytes = nSrcBytes - (pEndSrcBuf-pSrcBuf);
63 368 : return (nDestChars - (pEndDestBuf-pDestBuf));
64 : }
65 :
66 : /* ----------------------------------------------------------------------- */
67 :
68 18 : static sal_Size ImplUnicodeToDummy( const sal_Unicode* pSrcBuf, sal_Size nSrcChars,
69 : char* pDestBuf, sal_Size nDestBytes,
70 : sal_uInt32 nFlags, sal_uInt32* pInfo,
71 : sal_Size* pSrcCvtChars )
72 : {
73 : char* pEndDestBuf;
74 : const sal_Unicode* pEndSrcBuf;
75 :
76 18 : if ( ((nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK) == RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR) )
77 : {
78 : *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR |
79 0 : RTL_UNICODETOTEXT_INFO_UNDEFINED;
80 0 : return 0;
81 : }
82 :
83 18 : *pInfo = 0;
84 18 : pEndDestBuf = pDestBuf+nDestBytes;
85 18 : pEndSrcBuf = pSrcBuf+nSrcChars;
86 54 : while ( pSrcBuf < pEndSrcBuf )
87 : {
88 18 : if ( pDestBuf == pEndDestBuf )
89 : {
90 0 : *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
91 0 : break;
92 : }
93 :
94 18 : *pDestBuf = (char)(unsigned char)(*pSrcBuf & 0x00FF);
95 18 : pDestBuf++;
96 18 : pSrcBuf++;
97 : }
98 :
99 18 : *pSrcCvtChars = nSrcChars - (pEndSrcBuf-pSrcBuf);
100 18 : return (nDestBytes - (pEndDestBuf-pDestBuf));
101 : }
102 :
103 : /* ======================================================================= */
104 :
105 917247 : rtl_TextToUnicodeConverter SAL_CALL rtl_createTextToUnicodeConverter( rtl_TextEncoding eTextEncoding )
106 : {
107 917247 : const ImplTextEncodingData* pData = Impl_getTextEncodingData( eTextEncoding );
108 917247 : if ( pData )
109 916879 : return static_cast<rtl_TextToUnicodeConverter>(const_cast<ImplTextConverter *>(&pData->maConverter));
110 : else
111 368 : return 0;
112 : }
113 :
114 : /* ----------------------------------------------------------------------- */
115 :
116 942971 : void SAL_CALL rtl_destroyTextToUnicodeConverter(
117 : SAL_UNUSED_PARAMETER rtl_TextToUnicodeConverter )
118 942971 : {}
119 :
120 : /* ----------------------------------------------------------------------- */
121 :
122 854 : rtl_TextToUnicodeContext SAL_CALL rtl_createTextToUnicodeContext( rtl_TextToUnicodeConverter hConverter )
123 : {
124 854 : const ImplTextConverter* pConverter = static_cast<const ImplTextConverter*>(hConverter);
125 854 : if ( !pConverter )
126 0 : return 0;
127 854 : else if ( pConverter->mpCreateTextToUnicodeContext )
128 105 : return pConverter->mpCreateTextToUnicodeContext();
129 : else
130 749 : return reinterpret_cast<rtl_TextToUnicodeContext>(1);
131 : }
132 :
133 : /* ----------------------------------------------------------------------- */
134 :
135 654 : void SAL_CALL rtl_destroyTextToUnicodeContext( rtl_TextToUnicodeConverter hConverter,
136 : rtl_TextToUnicodeContext hContext )
137 : {
138 654 : const ImplTextConverter* pConverter = static_cast<const ImplTextConverter*>(hConverter);
139 654 : if ( pConverter && hContext && pConverter->mpDestroyTextToUnicodeContext )
140 104 : pConverter->mpDestroyTextToUnicodeContext( hContext );
141 654 : }
142 :
143 : /* ----------------------------------------------------------------------- */
144 :
145 12 : void SAL_CALL rtl_resetTextToUnicodeContext( rtl_TextToUnicodeConverter hConverter,
146 : rtl_TextToUnicodeContext hContext )
147 : {
148 12 : const ImplTextConverter* pConverter = static_cast<const ImplTextConverter*>(hConverter);
149 12 : if ( pConverter && hContext && pConverter->mpResetTextToUnicodeContext )
150 12 : pConverter->mpResetTextToUnicodeContext( hContext );
151 12 : }
152 :
153 : /* ----------------------------------------------------------------------- */
154 :
155 1472002 : sal_Size SAL_CALL rtl_convertTextToUnicode( rtl_TextToUnicodeConverter hConverter,
156 : rtl_TextToUnicodeContext hContext,
157 : const char* pSrcBuf, sal_Size nSrcBytes,
158 : sal_Unicode* pDestBuf, sal_Size nDestChars,
159 : sal_uInt32 nFlags, sal_uInt32* pInfo,
160 : sal_Size* pSrcCvtBytes )
161 : {
162 1472002 : const ImplTextConverter* pConverter = static_cast<const ImplTextConverter*>(hConverter);
163 :
164 : /* Only temporary, because we don't want die, if we don't have a
165 : converter, because not all converters are implemented yet */
166 1472002 : if ( !pConverter )
167 : {
168 : return ImplDummyToUnicode( pSrcBuf, nSrcBytes,
169 : pDestBuf, nDestChars,
170 368 : nFlags, pInfo, pSrcCvtBytes );
171 : }
172 :
173 : return pConverter->mpConvertTextToUnicodeProc( pConverter->mpConvertData,
174 : hContext,
175 : pSrcBuf, nSrcBytes,
176 : pDestBuf, nDestChars,
177 : nFlags, pInfo,
178 1471634 : pSrcCvtBytes );
179 : }
180 :
181 : /* ======================================================================= */
182 :
183 71988500 : rtl_UnicodeToTextConverter SAL_CALL rtl_createUnicodeToTextConverter( rtl_TextEncoding eTextEncoding )
184 : {
185 71988500 : const ImplTextEncodingData* pData = Impl_getTextEncodingData( eTextEncoding );
186 71988500 : if ( pData )
187 71988482 : return static_cast<rtl_TextToUnicodeConverter>(const_cast<ImplTextConverter *>(&pData->maConverter));
188 : else
189 18 : return 0;
190 : }
191 :
192 : /* ----------------------------------------------------------------------- */
193 :
194 71963654 : void SAL_CALL rtl_destroyUnicodeToTextConverter(
195 : SAL_UNUSED_PARAMETER rtl_UnicodeToTextConverter )
196 71963654 : {}
197 :
198 : /* ----------------------------------------------------------------------- */
199 :
200 2429 : rtl_UnicodeToTextContext SAL_CALL rtl_createUnicodeToTextContext( rtl_UnicodeToTextConverter hConverter )
201 : {
202 2429 : const ImplTextConverter* pConverter = static_cast<const ImplTextConverter*>(hConverter);
203 2429 : if ( !pConverter )
204 0 : return 0;
205 2429 : else if ( pConverter->mpCreateUnicodeToTextContext )
206 2360 : return pConverter->mpCreateUnicodeToTextContext();
207 : else
208 69 : return reinterpret_cast<rtl_UnicodeToTextContext>(1);
209 : }
210 :
211 : /* ----------------------------------------------------------------------- */
212 :
213 2629 : void SAL_CALL rtl_destroyUnicodeToTextContext( rtl_UnicodeToTextConverter hConverter,
214 : rtl_UnicodeToTextContext hContext )
215 : {
216 2629 : const ImplTextConverter* pConverter = static_cast<const ImplTextConverter*>(hConverter);
217 2629 : if ( pConverter && hContext && pConverter->mpDestroyUnicodeToTextContext )
218 2361 : pConverter->mpDestroyUnicodeToTextContext( hContext );
219 2629 : }
220 :
221 : /* ----------------------------------------------------------------------- */
222 :
223 0 : void SAL_CALL rtl_resetUnicodeToTextContext( rtl_UnicodeToTextConverter hConverter,
224 : rtl_UnicodeToTextContext hContext )
225 : {
226 0 : const ImplTextConverter* pConverter = static_cast<const ImplTextConverter*>(hConverter);
227 0 : if ( pConverter && hContext && pConverter->mpResetUnicodeToTextContext )
228 0 : pConverter->mpResetUnicodeToTextContext( hContext );
229 0 : }
230 :
231 : /* ----------------------------------------------------------------------- */
232 :
233 73250401 : sal_Size SAL_CALL rtl_convertUnicodeToText( rtl_UnicodeToTextConverter hConverter,
234 : rtl_UnicodeToTextContext hContext,
235 : const sal_Unicode* pSrcBuf, sal_Size nSrcChars,
236 : char* pDestBuf, sal_Size nDestBytes,
237 : sal_uInt32 nFlags, sal_uInt32* pInfo,
238 : sal_Size* pSrcCvtChars )
239 : {
240 73250401 : const ImplTextConverter* pConverter = static_cast<const ImplTextConverter*>(hConverter);
241 :
242 : /* Only temporary, because we don't want die, if we don't have a
243 : converter, because not all converters are implemented yet */
244 73250401 : if ( !pConverter )
245 : {
246 : return ImplUnicodeToDummy( pSrcBuf, nSrcChars,
247 : pDestBuf, nDestBytes,
248 18 : nFlags, pInfo, pSrcCvtChars );
249 : }
250 :
251 : return pConverter->mpConvertUnicodeToTextProc( pConverter->mpConvertData,
252 : hContext,
253 : pSrcBuf, nSrcChars,
254 : pDestBuf, nDestBytes,
255 : nFlags, pInfo,
256 73250383 : pSrcCvtChars );
257 : }
258 :
259 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|