Bump for 3.6-28
[LibreOffice.git] / sal / textenc / tcvtbyte.cxx
blobd80229a45fee50148ddd8c620b9a4b9bccd63d79
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 "handleundefinedunicodetotextchar.hxx"
34 #include "tcvtbyte.hxx"
35 #include "tenchelp.hxx"
37 sal_Size ImplSymbolToUnicode( SAL_UNUSED_PARAMETER const void*,
38 SAL_UNUSED_PARAMETER void*,
39 const char* pSrcBuf, sal_Size nSrcBytes,
40 sal_Unicode* pDestBuf, sal_Size nDestChars,
41 SAL_UNUSED_PARAMETER sal_uInt32,
42 sal_uInt32* pInfo, sal_Size* pSrcCvtBytes )
44 sal_uChar c;
45 sal_Unicode* pEndDestBuf;
46 const char* pEndSrcBuf;
48 *pInfo = 0;
49 pEndDestBuf = pDestBuf+nDestChars;
50 pEndSrcBuf = pSrcBuf+nSrcBytes;
51 while ( pSrcBuf < pEndSrcBuf )
53 if ( pDestBuf == pEndDestBuf )
55 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
56 break;
59 /* 0-31 (all Control-Character get the same Unicode value) */
60 c = (sal_uChar)*pSrcBuf;
61 if ( c <= 0x1F )
62 *pDestBuf = (sal_Unicode)c;
63 else
64 *pDestBuf = ((sal_Unicode)c)+0xF000;
65 pDestBuf++;
66 pSrcBuf++;
69 *pSrcCvtBytes = nSrcBytes - (pEndSrcBuf-pSrcBuf);
70 return (nDestChars - (pEndDestBuf-pDestBuf));
73 sal_Size ImplUnicodeToSymbol( SAL_UNUSED_PARAMETER const void*,
74 SAL_UNUSED_PARAMETER void*,
75 const sal_Unicode* pSrcBuf, sal_Size nSrcChars,
76 char* pDestBuf, sal_Size nDestBytes,
77 sal_uInt32 nFlags, sal_uInt32* pInfo,
78 sal_Size* pSrcCvtChars )
80 sal_Unicode c;
81 char* pEndDestBuf;
82 const sal_Unicode* pEndSrcBuf;
84 *pInfo = 0;
85 pEndDestBuf = pDestBuf+nDestBytes;
86 pEndSrcBuf = pSrcBuf+nSrcChars;
87 while ( pSrcBuf < pEndSrcBuf )
89 if ( pDestBuf == pEndDestBuf )
91 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
92 break;
95 c = *pSrcBuf;
96 if ( (c >= 0xF000) && (c <= 0xF0FF) )
98 *pDestBuf = static_cast< char >(static_cast< unsigned char >(c-0xF000));
99 pDestBuf++;
100 pSrcBuf++;
102 // Normally 0x001F, but in many cases also symbol characters
103 // are stored in the first 256 bytes, so that we don't change
104 // these values
105 else if ( c <= 0x00FF )
107 *pDestBuf = static_cast< char >(static_cast< unsigned char >(c));
108 pDestBuf++;
109 pSrcBuf++;
111 else
113 if ( nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_REPLACE )
115 /* !!! */
116 /* Only ascii characters < 0x1F */
119 /* Handle undefined and surrogates characters */
120 /* (all surrogates characters are undefined) */
121 if (!sal::detail::textenc::handleUndefinedUnicodeToTextChar(
122 &pSrcBuf, pEndSrcBuf, &pDestBuf, pEndDestBuf, nFlags,
123 pInfo))
124 break;
128 *pSrcCvtChars = nSrcChars - (pEndSrcBuf-pSrcBuf);
129 return (nDestBytes - (pEndDestBuf-pDestBuf));
132 sal_Size ImplUpperCharToUnicode( const void* pData,
133 SAL_UNUSED_PARAMETER void*,
134 const char* pSrcBuf, sal_Size nSrcBytes,
135 sal_Unicode* pDestBuf, sal_Size nDestChars,
136 SAL_UNUSED_PARAMETER sal_uInt32, sal_uInt32* pInfo,
137 sal_Size* pSrcCvtBytes )
139 sal_uChar c;
140 sal_Unicode cConv;
141 const ImplByteConvertData* pConvertData = (const ImplByteConvertData*)pData;
142 sal_Unicode* pEndDestBuf;
143 const char* pEndSrcBuf;
145 *pInfo = 0;
146 pEndDestBuf = pDestBuf+nDestChars;
147 pEndSrcBuf = pSrcBuf+nSrcBytes;
148 if ( pDestBuf == pEndDestBuf )
150 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
151 *pSrcCvtBytes = 0;
152 return 0;
154 while ( pSrcBuf < pEndSrcBuf )
156 c = (sal_uChar)*pSrcBuf;
157 if (c < 0x80)
158 cConv = c;
159 else
160 // c <= 0xFF is implied.
161 cConv = pConvertData->mpToUniTab1[c - 0x80];
163 *pDestBuf = cConv;
164 pDestBuf++;
165 pSrcBuf++;
168 *pSrcCvtBytes = nSrcBytes - (pEndSrcBuf-pSrcBuf);
169 return (nDestChars - (pEndDestBuf-pDestBuf));
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */