Update ooo320-m1
[ooovba.git] / sal / textenc / converter.c
blobce458b57b89d4474b34a2a037b96a8f07249fb38
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: converter.c,v $
10 * $Revision: 1.7 $
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 "converter.h"
32 #include "tenchelp.h"
33 #include "unichars.h"
34 #include "rtl/textcvt.h"
35 #include "sal/types.h"
37 ImplBadInputConversionAction ImplHandleBadInputTextToUnicodeConversion(
38 sal_Bool bUndefined, sal_Bool bMultiByte, sal_Char cByte, sal_uInt32 nFlags,
39 sal_Unicode ** pDestBufPtr, sal_Unicode * pDestBufEnd, sal_uInt32 * pInfo)
41 *pInfo |= bUndefined
42 ? (bMultiByte
43 ? RTL_TEXTTOUNICODE_INFO_MBUNDEFINED
44 : RTL_TEXTTOUNICODE_INFO_UNDEFINED)
45 : RTL_TEXTTOUNICODE_INFO_INVALID;
46 switch (nFlags
47 & (bUndefined
48 ? (bMultiByte
49 ? RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_MASK
50 : RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MASK)
51 : RTL_TEXTTOUNICODE_FLAGS_INVALID_MASK))
53 case RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR:
54 case RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR:
55 case RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR:
56 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR;
57 return IMPL_BAD_INPUT_STOP;
59 case RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_IGNORE:
60 case RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_IGNORE:
61 case RTL_TEXTTOUNICODE_FLAGS_INVALID_IGNORE:
62 return IMPL_BAD_INPUT_CONTINUE;
64 case RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MAPTOPRIVATE:
65 if (*pDestBufPtr != pDestBufEnd)
67 *(*pDestBufPtr)++ = RTL_TEXTCVT_BYTE_PRIVATE_START
68 | ((sal_uChar) cByte);
69 return IMPL_BAD_INPUT_CONTINUE;
71 else
72 return IMPL_BAD_INPUT_NO_OUTPUT;
74 default: /* RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_DEFAULT,
75 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT,
76 RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT */
77 if (*pDestBufPtr != pDestBufEnd)
79 *(*pDestBufPtr)++ = RTL_TEXTENC_UNICODE_REPLACEMENT_CHARACTER;
80 return IMPL_BAD_INPUT_CONTINUE;
82 else
83 return IMPL_BAD_INPUT_NO_OUTPUT;
87 ImplBadInputConversionAction
88 ImplHandleBadInputUnicodeToTextConversion(sal_Bool bUndefined,
89 sal_uInt32 nUtf32,
90 sal_uInt32 nFlags,
91 sal_Char ** pDestBufPtr,
92 sal_Char * pDestBufEnd,
93 sal_uInt32 * pInfo,
94 sal_Char const * pPrefix,
95 sal_Size nPrefixLen,
96 sal_Bool * pPrefixWritten)
98 /* TODO! RTL_UNICODETOTEXT_FLAGS_UNDEFINED_REPLACE
99 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_REPLACESTR */
101 sal_Char cReplace;
103 if (bUndefined)
105 if (ImplIsControlOrFormat(nUtf32))
107 if ((nFlags & RTL_UNICODETOTEXT_FLAGS_CONTROL_IGNORE) != 0)
108 nFlags = RTL_UNICODETOTEXT_FLAGS_UNDEFINED_IGNORE;
110 else if (ImplIsPrivateUse(nUtf32))
112 if ((nFlags & RTL_UNICODETOTEXT_FLAGS_PRIVATE_IGNORE) != 0)
113 nFlags = RTL_UNICODETOTEXT_FLAGS_UNDEFINED_IGNORE;
114 else if ((nFlags & RTL_UNICODETOTEXT_FLAGS_PRIVATE_MAPTO0) != 0)
115 nFlags = RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0;
117 else if (ImplIsZeroWidth(nUtf32))
119 if ((nFlags & RTL_UNICODETOTEXT_FLAGS_NONSPACING_IGNORE) != 0)
120 nFlags = RTL_UNICODETOTEXT_FLAGS_UNDEFINED_IGNORE;
123 *pInfo |= bUndefined ? RTL_UNICODETOTEXT_INFO_UNDEFINED :
124 RTL_UNICODETOTEXT_INFO_INVALID;
125 switch (nFlags & (bUndefined ? RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK :
126 RTL_UNICODETOTEXT_FLAGS_INVALID_MASK))
128 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR:
129 case RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR:
130 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR;
131 return IMPL_BAD_INPUT_STOP;
133 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_IGNORE:
134 case RTL_UNICODETOTEXT_FLAGS_INVALID_IGNORE:
135 if (pPrefixWritten)
136 *pPrefixWritten = sal_False;
137 return IMPL_BAD_INPUT_CONTINUE;
139 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0:
140 case RTL_UNICODETOTEXT_FLAGS_INVALID_0:
141 cReplace = 0;
142 break;
144 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_QUESTIONMARK:
145 case RTL_UNICODETOTEXT_FLAGS_INVALID_QUESTIONMARK:
146 default: /* RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT,
147 RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT */
148 cReplace = '?';
149 break;
151 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_UNDERLINE:
152 case RTL_UNICODETOTEXT_FLAGS_INVALID_UNDERLINE:
153 cReplace = '_';
154 break;
156 if ((sal_Size) (pDestBufEnd - *pDestBufPtr) > nPrefixLen)
158 while (nPrefixLen-- > 0)
159 *(*pDestBufPtr)++ = *pPrefix++;
160 *(*pDestBufPtr)++ = cReplace;
161 if (pPrefixWritten)
162 *pPrefixWritten = sal_True;
163 return IMPL_BAD_INPUT_CONTINUE;
165 else
166 return IMPL_BAD_INPUT_NO_OUTPUT;