Bump for 3.6-28
[LibreOffice.git] / sal / textenc / converter.cxx
blob24ebf486b7efb0615a7ec48feb9856fd078227f3
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"
32 #include "sal/types.h"
34 #include "converter.hxx"
35 #include "tenchelp.hxx"
36 #include "unichars.hxx"
38 sal::detail::textenc::BadInputConversionAction
39 sal::detail::textenc::handleBadInputTextToUnicodeConversion(
40 bool bUndefined, bool bMultiByte, char cByte, sal_uInt32 nFlags,
41 sal_Unicode ** pDestBufPtr, sal_Unicode * pDestBufEnd, sal_uInt32 * pInfo)
43 *pInfo |= bUndefined
44 ? (bMultiByte
45 ? RTL_TEXTTOUNICODE_INFO_MBUNDEFINED
46 : RTL_TEXTTOUNICODE_INFO_UNDEFINED)
47 : RTL_TEXTTOUNICODE_INFO_INVALID;
48 switch (nFlags
49 & (bUndefined
50 ? (bMultiByte
51 ? RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_MASK
52 : RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MASK)
53 : RTL_TEXTTOUNICODE_FLAGS_INVALID_MASK))
55 case RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR:
56 case RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR:
57 case RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR:
58 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR;
59 return BAD_INPUT_STOP;
61 case RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_IGNORE:
62 case RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_IGNORE:
63 case RTL_TEXTTOUNICODE_FLAGS_INVALID_IGNORE:
64 return BAD_INPUT_CONTINUE;
66 case RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MAPTOPRIVATE:
67 if (*pDestBufPtr != pDestBufEnd)
69 *(*pDestBufPtr)++ = RTL_TEXTCVT_BYTE_PRIVATE_START
70 | ((sal_uChar) cByte);
71 return BAD_INPUT_CONTINUE;
73 else
74 return BAD_INPUT_NO_OUTPUT;
76 default: // RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_DEFAULT,
77 // RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT,
78 // RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT
79 if (*pDestBufPtr != pDestBufEnd)
81 *(*pDestBufPtr)++ = RTL_TEXTENC_UNICODE_REPLACEMENT_CHARACTER;
82 return BAD_INPUT_CONTINUE;
84 else
85 return BAD_INPUT_NO_OUTPUT;
89 sal::detail::textenc::BadInputConversionAction
90 sal::detail::textenc::handleBadInputUnicodeToTextConversion(
91 bool bUndefined, sal_uInt32 nUtf32, sal_uInt32 nFlags, char ** pDestBufPtr,
92 char * pDestBufEnd, sal_uInt32 * pInfo, char const * pPrefix,
93 sal_Size nPrefixLen, bool * pPrefixWritten)
95 // TODO! RTL_UNICODETOTEXT_FLAGS_UNDEFINED_REPLACE
96 // RTL_UNICODETOTEXT_FLAGS_UNDEFINED_REPLACESTR
98 char cReplace;
100 if (bUndefined)
102 if (ImplIsControlOrFormat(nUtf32))
104 if ((nFlags & RTL_UNICODETOTEXT_FLAGS_CONTROL_IGNORE) != 0)
105 nFlags = RTL_UNICODETOTEXT_FLAGS_UNDEFINED_IGNORE;
107 else if (ImplIsPrivateUse(nUtf32))
109 if ((nFlags & RTL_UNICODETOTEXT_FLAGS_PRIVATE_IGNORE) != 0)
110 nFlags = RTL_UNICODETOTEXT_FLAGS_UNDEFINED_IGNORE;
111 else if ((nFlags & RTL_UNICODETOTEXT_FLAGS_PRIVATE_MAPTO0) != 0)
112 nFlags = RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0;
114 else if (ImplIsZeroWidth(nUtf32))
116 if ((nFlags & RTL_UNICODETOTEXT_FLAGS_NONSPACING_IGNORE) != 0)
117 nFlags = RTL_UNICODETOTEXT_FLAGS_UNDEFINED_IGNORE;
120 *pInfo |= bUndefined ? RTL_UNICODETOTEXT_INFO_UNDEFINED :
121 RTL_UNICODETOTEXT_INFO_INVALID;
122 switch (nFlags & (bUndefined ? RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK :
123 RTL_UNICODETOTEXT_FLAGS_INVALID_MASK))
125 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR:
126 case RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR:
127 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR;
128 return BAD_INPUT_STOP;
130 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_IGNORE:
131 case RTL_UNICODETOTEXT_FLAGS_INVALID_IGNORE:
132 if (pPrefixWritten)
133 *pPrefixWritten = false;
134 return BAD_INPUT_CONTINUE;
136 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0:
137 case RTL_UNICODETOTEXT_FLAGS_INVALID_0:
138 cReplace = 0;
139 break;
141 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_QUESTIONMARK:
142 case RTL_UNICODETOTEXT_FLAGS_INVALID_QUESTIONMARK:
143 default: // RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT,
144 // RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT
145 cReplace = '?';
146 break;
148 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_UNDERLINE:
149 case RTL_UNICODETOTEXT_FLAGS_INVALID_UNDERLINE:
150 cReplace = '_';
151 break;
153 if ((sal_Size) (pDestBufEnd - *pDestBufPtr) > nPrefixLen)
155 while (nPrefixLen-- > 0)
156 *(*pDestBufPtr)++ = *pPrefix++;
157 *(*pDestBufPtr)++ = cReplace;
158 if (pPrefixWritten)
159 *pPrefixWritten = true;
160 return BAD_INPUT_CONTINUE;
162 else
163 return BAD_INPUT_NO_OUTPUT;
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */