Bump for 3.6-28
[LibreOffice.git] / sal / textenc / textcvt.cxx
blob9f0c54763e342d177704220200cc8a025bec2fbe
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "sal/config.h"
31 #include "rtl/textcvt.h"
33 #include "gettextencodingdata.hxx"
34 #include "tenchelp.hxx"
36 /* ======================================================================= */
38 static sal_Size ImplDummyToUnicode( const char* pSrcBuf, sal_Size nSrcBytes,
39 sal_Unicode* pDestBuf, sal_Size nDestChars,
40 sal_uInt32 nFlags, sal_uInt32* pInfo,
41 sal_Size* pSrcCvtBytes )
43 sal_Unicode* pEndDestBuf;
44 const char* pEndSrcBuf;
46 if ( ((nFlags & RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MASK) == RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR) ||
47 ((nFlags & RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_MASK) == RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR) )
49 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR |
50 RTL_TEXTTOUNICODE_INFO_UNDEFINED |
51 RTL_TEXTTOUNICODE_INFO_MBUNDEFINED;
52 return 0;
55 *pInfo = 0;
56 pEndDestBuf = pDestBuf+nDestChars;
57 pEndSrcBuf = pSrcBuf+nSrcBytes;
58 while ( pSrcBuf < pEndSrcBuf )
60 if ( pDestBuf == pEndDestBuf )
62 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
63 break;
66 *pDestBuf = (sal_Unicode)(sal_uChar)*pSrcBuf;
67 pDestBuf++;
68 pSrcBuf++;
71 *pSrcCvtBytes = nSrcBytes - (pEndSrcBuf-pSrcBuf);
72 return (nDestChars - (pEndDestBuf-pDestBuf));
75 /* ----------------------------------------------------------------------- */
77 static sal_Size ImplUnicodeToDummy( const sal_Unicode* pSrcBuf, sal_Size nSrcChars,
78 char* pDestBuf, sal_Size nDestBytes,
79 sal_uInt32 nFlags, sal_uInt32* pInfo,
80 sal_Size* pSrcCvtChars )
82 char* pEndDestBuf;
83 const sal_Unicode* pEndSrcBuf;
85 if ( ((nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK) == RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR) )
87 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR |
88 RTL_UNICODETOTEXT_INFO_UNDEFINED;
89 return 0;
92 *pInfo = 0;
93 pEndDestBuf = pDestBuf+nDestBytes;
94 pEndSrcBuf = pSrcBuf+nSrcChars;
95 while ( pSrcBuf < pEndSrcBuf )
97 if ( pDestBuf == pEndDestBuf )
99 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
100 break;
103 *pDestBuf = (char)(sal_uChar)(*pSrcBuf & 0x00FF);
104 pDestBuf++;
105 pSrcBuf++;
108 *pSrcCvtChars = nSrcChars - (pEndSrcBuf-pSrcBuf);
109 return (nDestBytes - (pEndDestBuf-pDestBuf));
112 /* ======================================================================= */
114 rtl_TextToUnicodeConverter SAL_CALL rtl_createTextToUnicodeConverter( rtl_TextEncoding eTextEncoding )
116 const ImplTextEncodingData* pData = Impl_getTextEncodingData( eTextEncoding );
117 if ( pData )
118 return (rtl_TextToUnicodeConverter) &pData->maConverter;
119 else
120 return 0;
123 /* ----------------------------------------------------------------------- */
125 void SAL_CALL rtl_destroyTextToUnicodeConverter(
126 SAL_UNUSED_PARAMETER rtl_TextToUnicodeConverter )
129 /* ----------------------------------------------------------------------- */
131 rtl_TextToUnicodeContext SAL_CALL rtl_createTextToUnicodeContext( rtl_TextToUnicodeConverter hConverter )
133 const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
134 if ( !pConverter )
135 return 0;
136 else if ( pConverter->mpCreateTextToUnicodeContext )
137 return (rtl_TextToUnicodeContext)pConverter->mpCreateTextToUnicodeContext();
138 else
139 return (rtl_TextToUnicodeContext)1;
142 /* ----------------------------------------------------------------------- */
144 void SAL_CALL rtl_destroyTextToUnicodeContext( rtl_TextToUnicodeConverter hConverter,
145 rtl_TextToUnicodeContext hContext )
147 const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
148 if ( pConverter && hContext && pConverter->mpDestroyTextToUnicodeContext )
149 pConverter->mpDestroyTextToUnicodeContext( hContext );
152 /* ----------------------------------------------------------------------- */
154 void SAL_CALL rtl_resetTextToUnicodeContext( rtl_TextToUnicodeConverter hConverter,
155 rtl_TextToUnicodeContext hContext )
157 const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
158 if ( pConverter && hContext && pConverter->mpResetTextToUnicodeContext )
159 pConverter->mpResetTextToUnicodeContext( hContext );
162 /* ----------------------------------------------------------------------- */
164 sal_Size SAL_CALL rtl_convertTextToUnicode( rtl_TextToUnicodeConverter hConverter,
165 rtl_TextToUnicodeContext hContext,
166 const char* pSrcBuf, sal_Size nSrcBytes,
167 sal_Unicode* pDestBuf, sal_Size nDestChars,
168 sal_uInt32 nFlags, sal_uInt32* pInfo,
169 sal_Size* pSrcCvtBytes )
171 const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
173 /* Only temporaer, because we don't want die, if we don't have a
174 converter, because not all converters are implemented yet */
175 if ( !pConverter )
177 return ImplDummyToUnicode( pSrcBuf, nSrcBytes,
178 pDestBuf, nDestChars,
179 nFlags, pInfo, pSrcCvtBytes );
182 return pConverter->mpConvertTextToUnicodeProc( pConverter->mpConvertData,
183 hContext,
184 pSrcBuf, nSrcBytes,
185 pDestBuf, nDestChars,
186 nFlags, pInfo,
187 pSrcCvtBytes );
190 /* ======================================================================= */
192 rtl_UnicodeToTextConverter SAL_CALL rtl_createUnicodeToTextConverter( rtl_TextEncoding eTextEncoding )
194 const ImplTextEncodingData* pData = Impl_getTextEncodingData( eTextEncoding );
195 if ( pData )
196 return (rtl_TextToUnicodeConverter) &pData->maConverter;
197 else
198 return 0;
201 /* ----------------------------------------------------------------------- */
203 void SAL_CALL rtl_destroyUnicodeToTextConverter(
204 SAL_UNUSED_PARAMETER rtl_UnicodeToTextConverter )
207 /* ----------------------------------------------------------------------- */
209 rtl_UnicodeToTextContext SAL_CALL rtl_createUnicodeToTextContext( rtl_UnicodeToTextConverter hConverter )
211 const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
212 if ( !pConverter )
213 return 0;
214 else if ( pConverter->mpCreateUnicodeToTextContext )
215 return (rtl_UnicodeToTextContext)pConverter->mpCreateUnicodeToTextContext();
216 else
217 return (rtl_UnicodeToTextContext)1;
220 /* ----------------------------------------------------------------------- */
222 void SAL_CALL rtl_destroyUnicodeToTextContext( rtl_UnicodeToTextConverter hConverter,
223 rtl_UnicodeToTextContext hContext )
225 const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
226 if ( pConverter && hContext && pConverter->mpDestroyUnicodeToTextContext )
227 pConverter->mpDestroyUnicodeToTextContext( hContext );
230 /* ----------------------------------------------------------------------- */
232 void SAL_CALL rtl_resetUnicodeToTextContext( rtl_UnicodeToTextConverter hConverter,
233 rtl_UnicodeToTextContext hContext )
235 const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
236 if ( pConverter && hContext && pConverter->mpResetUnicodeToTextContext )
237 pConverter->mpResetUnicodeToTextContext( hContext );
240 /* ----------------------------------------------------------------------- */
242 sal_Size SAL_CALL rtl_convertUnicodeToText( rtl_UnicodeToTextConverter hConverter,
243 rtl_UnicodeToTextContext hContext,
244 const sal_Unicode* pSrcBuf, sal_Size nSrcChars,
245 char* pDestBuf, sal_Size nDestBytes,
246 sal_uInt32 nFlags, sal_uInt32* pInfo,
247 sal_Size* pSrcCvtChars )
249 const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
251 /* Only temporaer, because we don't want die, if we don't have a
252 converter, because not all converters are implemented yet */
253 if ( !pConverter )
255 return ImplUnicodeToDummy( pSrcBuf, nSrcChars,
256 pDestBuf, nDestBytes,
257 nFlags, pInfo, pSrcCvtChars );
260 return pConverter->mpConvertUnicodeToTextProc( pConverter->mpConvertData,
261 hContext,
262 pSrcBuf, nSrcChars,
263 pDestBuf, nDestBytes,
264 nFlags, pInfo,
265 pSrcCvtChars );
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */