Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sal / textenc / textcvt.cxx
blobc84a9b607c399193a916832562d71cb2927fa54d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include "sal/config.h"
22 #include "rtl/textcvt.h"
24 #include "gettextencodingdata.hxx"
25 #include "tenchelp.hxx"
27 /* ======================================================================= */
29 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 )
34 sal_Unicode* pEndDestBuf;
35 const char* pEndSrcBuf;
37 if ( ((nFlags & RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MASK) == RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR) ||
38 ((nFlags & RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_MASK) == RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR) )
40 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR |
41 RTL_TEXTTOUNICODE_INFO_UNDEFINED |
42 RTL_TEXTTOUNICODE_INFO_MBUNDEFINED;
43 return 0;
46 *pInfo = 0;
47 pEndDestBuf = pDestBuf+nDestChars;
48 pEndSrcBuf = pSrcBuf+nSrcBytes;
49 while ( pSrcBuf < pEndSrcBuf )
51 if ( pDestBuf == pEndDestBuf )
53 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
54 break;
57 *pDestBuf = (sal_Unicode)(sal_uChar)*pSrcBuf;
58 pDestBuf++;
59 pSrcBuf++;
62 *pSrcCvtBytes = nSrcBytes - (pEndSrcBuf-pSrcBuf);
63 return (nDestChars - (pEndDestBuf-pDestBuf));
66 /* ----------------------------------------------------------------------- */
68 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 )
73 char* pEndDestBuf;
74 const sal_Unicode* pEndSrcBuf;
76 if ( ((nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK) == RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR) )
78 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR |
79 RTL_UNICODETOTEXT_INFO_UNDEFINED;
80 return 0;
83 *pInfo = 0;
84 pEndDestBuf = pDestBuf+nDestBytes;
85 pEndSrcBuf = pSrcBuf+nSrcChars;
86 while ( pSrcBuf < pEndSrcBuf )
88 if ( pDestBuf == pEndDestBuf )
90 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
91 break;
94 *pDestBuf = (char)(sal_uChar)(*pSrcBuf & 0x00FF);
95 pDestBuf++;
96 pSrcBuf++;
99 *pSrcCvtChars = nSrcChars - (pEndSrcBuf-pSrcBuf);
100 return (nDestBytes - (pEndDestBuf-pDestBuf));
103 /* ======================================================================= */
105 rtl_TextToUnicodeConverter SAL_CALL rtl_createTextToUnicodeConverter( rtl_TextEncoding eTextEncoding )
107 const ImplTextEncodingData* pData = Impl_getTextEncodingData( eTextEncoding );
108 if ( pData )
109 return (rtl_TextToUnicodeConverter) &pData->maConverter;
110 else
111 return 0;
114 /* ----------------------------------------------------------------------- */
116 void SAL_CALL rtl_destroyTextToUnicodeConverter(
117 SAL_UNUSED_PARAMETER rtl_TextToUnicodeConverter )
120 /* ----------------------------------------------------------------------- */
122 rtl_TextToUnicodeContext SAL_CALL rtl_createTextToUnicodeContext( rtl_TextToUnicodeConverter hConverter )
124 const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
125 if ( !pConverter )
126 return 0;
127 else if ( pConverter->mpCreateTextToUnicodeContext )
128 return (rtl_TextToUnicodeContext)pConverter->mpCreateTextToUnicodeContext();
129 else
130 return (rtl_TextToUnicodeContext)1;
133 /* ----------------------------------------------------------------------- */
135 void SAL_CALL rtl_destroyTextToUnicodeContext( rtl_TextToUnicodeConverter hConverter,
136 rtl_TextToUnicodeContext hContext )
138 const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
139 if ( pConverter && hContext && pConverter->mpDestroyTextToUnicodeContext )
140 pConverter->mpDestroyTextToUnicodeContext( hContext );
143 /* ----------------------------------------------------------------------- */
145 void SAL_CALL rtl_resetTextToUnicodeContext( rtl_TextToUnicodeConverter hConverter,
146 rtl_TextToUnicodeContext hContext )
148 const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
149 if ( pConverter && hContext && pConverter->mpResetTextToUnicodeContext )
150 pConverter->mpResetTextToUnicodeContext( hContext );
153 /* ----------------------------------------------------------------------- */
155 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 )
162 const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
164 /* Only temporaer, because we don't want die, if we don't have a
165 converter, because not all converters are implemented yet */
166 if ( !pConverter )
168 return ImplDummyToUnicode( pSrcBuf, nSrcBytes,
169 pDestBuf, nDestChars,
170 nFlags, pInfo, pSrcCvtBytes );
173 return pConverter->mpConvertTextToUnicodeProc( pConverter->mpConvertData,
174 hContext,
175 pSrcBuf, nSrcBytes,
176 pDestBuf, nDestChars,
177 nFlags, pInfo,
178 pSrcCvtBytes );
181 /* ======================================================================= */
183 rtl_UnicodeToTextConverter SAL_CALL rtl_createUnicodeToTextConverter( rtl_TextEncoding eTextEncoding )
185 const ImplTextEncodingData* pData = Impl_getTextEncodingData( eTextEncoding );
186 if ( pData )
187 return (rtl_TextToUnicodeConverter) &pData->maConverter;
188 else
189 return 0;
192 /* ----------------------------------------------------------------------- */
194 void SAL_CALL rtl_destroyUnicodeToTextConverter(
195 SAL_UNUSED_PARAMETER rtl_UnicodeToTextConverter )
198 /* ----------------------------------------------------------------------- */
200 rtl_UnicodeToTextContext SAL_CALL rtl_createUnicodeToTextContext( rtl_UnicodeToTextConverter hConverter )
202 const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
203 if ( !pConverter )
204 return 0;
205 else if ( pConverter->mpCreateUnicodeToTextContext )
206 return (rtl_UnicodeToTextContext)pConverter->mpCreateUnicodeToTextContext();
207 else
208 return (rtl_UnicodeToTextContext)1;
211 /* ----------------------------------------------------------------------- */
213 void SAL_CALL rtl_destroyUnicodeToTextContext( rtl_UnicodeToTextConverter hConverter,
214 rtl_UnicodeToTextContext hContext )
216 const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
217 if ( pConverter && hContext && pConverter->mpDestroyUnicodeToTextContext )
218 pConverter->mpDestroyUnicodeToTextContext( hContext );
221 /* ----------------------------------------------------------------------- */
223 void SAL_CALL rtl_resetUnicodeToTextContext( rtl_UnicodeToTextConverter hConverter,
224 rtl_UnicodeToTextContext hContext )
226 const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
227 if ( pConverter && hContext && pConverter->mpResetUnicodeToTextContext )
228 pConverter->mpResetUnicodeToTextContext( hContext );
231 /* ----------------------------------------------------------------------- */
233 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 )
240 const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
242 /* Only temporaer, because we don't want die, if we don't have a
243 converter, because not all converters are implemented yet */
244 if ( !pConverter )
246 return ImplUnicodeToDummy( pSrcBuf, nSrcChars,
247 pDestBuf, nDestBytes,
248 nFlags, pInfo, pSrcCvtChars );
251 return pConverter->mpConvertUnicodeToTextProc( pConverter->mpConvertData,
252 hContext,
253 pSrcBuf, nSrcChars,
254 pDestBuf, nDestBytes,
255 nFlags, pInfo,
256 pSrcCvtChars );
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */