1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: textcvt.c,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "rtl/textcvt.h"
32 #include "gettextencodingdata.h"
35 /* ======================================================================= */
37 static sal_Size
ImplDummyToUnicode( const sal_Char
* pSrcBuf
, sal_Size nSrcBytes
,
38 sal_Unicode
* pDestBuf
, sal_Size nDestChars
,
39 sal_uInt32 nFlags
, sal_uInt32
* pInfo
,
40 sal_Size
* pSrcCvtBytes
)
42 sal_Unicode
* pEndDestBuf
;
43 const sal_Char
* pEndSrcBuf
;
45 if ( ((nFlags
& RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MASK
) == RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR
) ||
46 ((nFlags
& RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_MASK
) == RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR
) )
48 *pInfo
|= RTL_TEXTTOUNICODE_INFO_ERROR
|
49 RTL_TEXTTOUNICODE_INFO_UNDEFINED
|
50 RTL_TEXTTOUNICODE_INFO_MBUNDEFINED
;
55 pEndDestBuf
= pDestBuf
+nDestChars
;
56 pEndSrcBuf
= pSrcBuf
+nSrcBytes
;
57 while ( pSrcBuf
< pEndSrcBuf
)
59 if ( pDestBuf
== pEndDestBuf
)
61 *pInfo
|= RTL_TEXTTOUNICODE_INFO_ERROR
| RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL
;
65 *pDestBuf
= (sal_Unicode
)(sal_uChar
)*pSrcBuf
;
70 *pSrcCvtBytes
= nSrcBytes
- (pEndSrcBuf
-pSrcBuf
);
71 return (nDestChars
- (pEndDestBuf
-pDestBuf
));
74 /* ----------------------------------------------------------------------- */
76 static sal_Size
ImplUnicodeToDummy( const sal_Unicode
* pSrcBuf
, sal_Size nSrcChars
,
77 sal_Char
* pDestBuf
, sal_Size nDestBytes
,
78 sal_uInt32 nFlags
, sal_uInt32
* pInfo
,
79 sal_Size
* pSrcCvtChars
)
81 sal_Char
* pEndDestBuf
;
82 const sal_Unicode
* pEndSrcBuf
;
84 if ( ((nFlags
& RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK
) == RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
) )
86 *pInfo
|= RTL_UNICODETOTEXT_INFO_ERROR
|
87 RTL_UNICODETOTEXT_INFO_UNDEFINED
;
92 pEndDestBuf
= pDestBuf
+nDestBytes
;
93 pEndSrcBuf
= pSrcBuf
+nSrcChars
;
94 while ( pSrcBuf
< pEndSrcBuf
)
96 if ( pDestBuf
== pEndDestBuf
)
98 *pInfo
|= RTL_UNICODETOTEXT_INFO_ERROR
| RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL
;
102 *pDestBuf
= (sal_Char
)(sal_uChar
)(*pSrcBuf
& 0x00FF);
107 *pSrcCvtChars
= nSrcChars
- (pEndSrcBuf
-pSrcBuf
);
108 return (nDestBytes
- (pEndDestBuf
-pDestBuf
));
111 /* ======================================================================= */
113 rtl_TextToUnicodeConverter SAL_CALL
rtl_createTextToUnicodeConverter( rtl_TextEncoding eTextEncoding
)
115 const ImplTextEncodingData
* pData
= Impl_getTextEncodingData( eTextEncoding
);
117 return (rtl_TextToUnicodeConverter
) &pData
->maConverter
;
122 /* ----------------------------------------------------------------------- */
124 void SAL_CALL
rtl_destroyTextToUnicodeConverter( rtl_TextToUnicodeConverter hContext
)
126 (void) hContext
; /* unused */
129 /* ----------------------------------------------------------------------- */
131 rtl_TextToUnicodeContext SAL_CALL
rtl_createTextToUnicodeContext( rtl_TextToUnicodeConverter hConverter
)
133 const ImplTextConverter
* pConverter
= (const ImplTextConverter
*)hConverter
;
136 else if ( pConverter
->mpCreateTextToUnicodeContext
)
137 return (rtl_TextToUnicodeContext
)pConverter
->mpCreateTextToUnicodeContext();
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( (void*)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( (void*)hContext
);
162 /* ----------------------------------------------------------------------- */
164 sal_Size SAL_CALL
rtl_convertTextToUnicode( rtl_TextToUnicodeConverter hConverter
,
165 rtl_TextToUnicodeContext hContext
,
166 const sal_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 */
177 return ImplDummyToUnicode( pSrcBuf
, nSrcBytes
,
178 pDestBuf
, nDestChars
,
179 nFlags
, pInfo
, pSrcCvtBytes
);
182 return pConverter
->mpConvertTextToUnicodeProc( pConverter
->mpConvertData
,
185 pDestBuf
, nDestChars
,
190 /* ======================================================================= */
192 rtl_UnicodeToTextConverter SAL_CALL
rtl_createUnicodeToTextConverter( rtl_TextEncoding eTextEncoding
)
194 const ImplTextEncodingData
* pData
= Impl_getTextEncodingData( eTextEncoding
);
196 return (rtl_TextToUnicodeConverter
) &pData
->maConverter
;
201 /* ----------------------------------------------------------------------- */
203 void SAL_CALL
rtl_destroyUnicodeToTextConverter( rtl_UnicodeToTextConverter hConverter
)
205 (void) hConverter
; /* unused */
208 /* ----------------------------------------------------------------------- */
210 rtl_UnicodeToTextContext SAL_CALL
rtl_createUnicodeToTextContext( rtl_UnicodeToTextConverter hConverter
)
212 const ImplTextConverter
* pConverter
= (const ImplTextConverter
*)hConverter
;
215 else if ( pConverter
->mpCreateUnicodeToTextContext
)
216 return (rtl_UnicodeToTextContext
)pConverter
->mpCreateUnicodeToTextContext();
218 return (rtl_UnicodeToTextContext
)1;
221 /* ----------------------------------------------------------------------- */
223 void SAL_CALL
rtl_destroyUnicodeToTextContext( rtl_UnicodeToTextConverter hConverter
,
224 rtl_UnicodeToTextContext hContext
)
226 const ImplTextConverter
* pConverter
= (const ImplTextConverter
*)hConverter
;
227 if ( pConverter
&& hContext
&& pConverter
->mpDestroyUnicodeToTextContext
)
228 pConverter
->mpDestroyUnicodeToTextContext( (void*)hContext
);
231 /* ----------------------------------------------------------------------- */
233 void SAL_CALL
rtl_resetUnicodeToTextContext( rtl_UnicodeToTextConverter hConverter
,
234 rtl_UnicodeToTextContext hContext
)
236 const ImplTextConverter
* pConverter
= (const ImplTextConverter
*)hConverter
;
237 if ( pConverter
&& hContext
&& pConverter
->mpResetUnicodeToTextContext
)
238 pConverter
->mpResetUnicodeToTextContext( (void*)hContext
);
241 /* ----------------------------------------------------------------------- */
243 sal_Size SAL_CALL
rtl_convertUnicodeToText( rtl_UnicodeToTextConverter hConverter
,
244 rtl_UnicodeToTextContext hContext
,
245 const sal_Unicode
* pSrcBuf
, sal_Size nSrcChars
,
246 sal_Char
* pDestBuf
, sal_Size nDestBytes
,
247 sal_uInt32 nFlags
, sal_uInt32
* pInfo
,
248 sal_Size
* pSrcCvtChars
)
250 const ImplTextConverter
* pConverter
= (const ImplTextConverter
*)hConverter
;
252 /* Only temporaer, because we don't want die, if we don't have a
253 converter, because not all converters are implemented yet */
256 return ImplUnicodeToDummy( pSrcBuf
, nSrcChars
,
257 pDestBuf
, nDestBytes
,
258 nFlags
, pInfo
, pSrcCvtChars
);
261 return pConverter
->mpConvertUnicodeToTextProc( pConverter
->mpConvertData
,
264 pDestBuf
, nDestBytes
,