bump product version to 5.0.4.1
[LibreOffice.git] / sal / textenc / handleundefinedunicodetotextchar.cxx
blob164369fb3218b4c6044a7203b464253e851383fa
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/.
8 */
10 #include "sal/config.h"
12 #include "rtl/textcvt.h"
13 #include "sal/types.h"
15 #include "handleundefinedunicodetotextchar.hxx"
16 #include "tenchelp.hxx"
18 namespace {
20 bool ImplIsUnicodeIgnoreChar(sal_Unicode c, sal_uInt32 nFlags)
22 return
23 ((nFlags & RTL_UNICODETOTEXT_FLAGS_NONSPACING_IGNORE) != 0
24 && ImplIsZeroWidth(c))
25 || ((nFlags & RTL_UNICODETOTEXT_FLAGS_CONTROL_IGNORE) != 0
26 && ImplIsControlOrFormat(c))
27 || ((nFlags & RTL_UNICODETOTEXT_FLAGS_PRIVATE_IGNORE) != 0
28 && ImplIsPrivateUse(c));
31 bool ImplGetUndefinedAsciiMultiByte(sal_uInt32 nFlags,
32 char * pBuf,
33 sal_Size nMaxLen)
35 if (nMaxLen == 0)
36 return false;
37 switch (nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK)
39 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0:
40 *pBuf = 0x00;
41 break;
43 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_QUESTIONMARK:
44 default: /* RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT */
45 *pBuf = 0x3F;
46 break;
48 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_UNDERLINE:
49 *pBuf = 0x5F;
50 break;
52 return true;
55 bool ImplGetInvalidAsciiMultiByte(sal_uInt32 nFlags,
56 char * pBuf,
57 sal_Size nMaxLen)
59 if (nMaxLen == 0)
60 return false;
61 switch (nFlags & RTL_UNICODETOTEXT_FLAGS_INVALID_MASK)
63 case RTL_UNICODETOTEXT_FLAGS_INVALID_0:
64 *pBuf = 0x00;
65 break;
67 case RTL_UNICODETOTEXT_FLAGS_INVALID_QUESTIONMARK:
68 default: /* RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT */
69 *pBuf = 0x3F;
70 break;
72 case RTL_UNICODETOTEXT_FLAGS_INVALID_UNDERLINE:
73 *pBuf = 0x5F;
74 break;
76 return true;
81 bool sal::detail::textenc::handleUndefinedUnicodeToTextChar(
82 sal_Unicode const ** ppSrcBuf, sal_Unicode const * pEndSrcBuf,
83 char ** ppDestBuf, char const * pEndDestBuf, sal_uInt32 nFlags,
84 sal_uInt32 * pInfo)
86 sal_Unicode c = **ppSrcBuf;
88 /* Should the private character map to one byte */
89 if ( (c >= RTL_TEXTCVT_BYTE_PRIVATE_START) && (c <= RTL_TEXTCVT_BYTE_PRIVATE_END) )
91 if ( nFlags & RTL_UNICODETOTEXT_FLAGS_PRIVATE_MAPTO0 )
93 **ppDestBuf = (char)(unsigned char)(c-RTL_TEXTCVT_BYTE_PRIVATE_START);
94 (*ppDestBuf)++;
95 (*ppSrcBuf)++;
96 return true;
100 /* Should this character ignored (Private, Non Spacing, Control) */
101 if ( ImplIsUnicodeIgnoreChar( c, nFlags ) )
103 (*ppSrcBuf)++;
104 return true;
107 /* Surrogates Characters should result in */
108 /* one replacement character */
109 if (ImplIsHighSurrogate(c))
111 if ( ((*ppSrcBuf) + 1) == pEndSrcBuf )
113 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR | RTL_UNICODETOTEXT_INFO_SRCBUFFERTOSMALL;
114 return false;
117 c = *((*ppSrcBuf)+1);
118 if (ImplIsLowSurrogate(c))
119 (*ppSrcBuf)++;
120 else
122 *pInfo |= RTL_UNICODETOTEXT_INFO_INVALID;
123 if ( (nFlags & RTL_UNICODETOTEXT_FLAGS_INVALID_MASK) == RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR )
125 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR;
126 return false;
128 else if ( (nFlags & RTL_UNICODETOTEXT_FLAGS_INVALID_MASK) == RTL_UNICODETOTEXT_FLAGS_INVALID_IGNORE )
130 (*ppSrcBuf)++;
131 return true;
133 else if (ImplGetInvalidAsciiMultiByte(nFlags,
134 *ppDestBuf,
135 pEndDestBuf - *ppDestBuf))
137 ++*ppSrcBuf;
138 ++*ppDestBuf;
139 return true;
141 else
143 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR
144 | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
145 return false;
150 *pInfo |= RTL_UNICODETOTEXT_INFO_UNDEFINED;
151 if ( (nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK) == RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR )
153 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR;
154 return false;
156 else if ( (nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK) == RTL_UNICODETOTEXT_FLAGS_UNDEFINED_IGNORE )
157 (*ppSrcBuf)++;
158 else if (ImplGetUndefinedAsciiMultiByte(nFlags,
159 *ppDestBuf,
160 pEndDestBuf - *ppDestBuf))
162 ++*ppSrcBuf;
163 ++*ppDestBuf;
165 else
167 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR
168 | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
169 return false;
172 return true;
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */