Update ooo320-m1
[ooovba.git] / sal / textenc / tenchelp.c
blob06edb35e35afbde8a7f16c77b8a979e4a1fa562b
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: tenchelp.c,v $
10 * $Revision: 1.6 $
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 "tenchelp.h"
32 #include "unichars.h"
33 #include "rtl/textcvt.h"
34 #include "sal/types.h"
36 static sal_Bool ImplGetUndefinedAsciiMultiByte(sal_uInt32 nFlags,
37 sal_Char * pBuf,
38 sal_Size nMaxLen);
40 static sal_Bool ImplGetInvalidAsciiMultiByte(sal_uInt32 nFlags,
41 sal_Char * pBuf,
42 sal_Size nMaxLen);
44 static int ImplIsUnicodeIgnoreChar(sal_Unicode c, sal_uInt32 nFlags);
46 sal_Bool ImplGetUndefinedAsciiMultiByte(sal_uInt32 nFlags,
47 sal_Char * pBuf,
48 sal_Size nMaxLen)
50 if (nMaxLen == 0)
51 return sal_False;
52 switch (nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK)
54 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0:
55 *pBuf = 0x00;
56 break;
58 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_QUESTIONMARK:
59 default: /* RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT */
60 *pBuf = 0x3F;
61 break;
63 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_UNDERLINE:
64 *pBuf = 0x5F;
65 break;
67 return sal_True;
70 sal_Bool ImplGetInvalidAsciiMultiByte(sal_uInt32 nFlags,
71 sal_Char * pBuf,
72 sal_Size nMaxLen)
74 if (nMaxLen == 0)
75 return sal_False;
76 switch (nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK)
78 case RTL_UNICODETOTEXT_FLAGS_INVALID_0:
79 *pBuf = 0x00;
80 break;
82 case RTL_UNICODETOTEXT_FLAGS_INVALID_QUESTIONMARK:
83 default: /* RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT */
84 *pBuf = 0x3F;
85 break;
87 case RTL_UNICODETOTEXT_FLAGS_INVALID_UNDERLINE:
88 *pBuf = 0x5F;
89 break;
91 return sal_True;
94 int ImplIsUnicodeIgnoreChar( sal_Unicode c, sal_uInt32 nFlags )
96 return
97 ((nFlags & RTL_UNICODETOTEXT_FLAGS_NONSPACING_IGNORE) != 0
98 && ImplIsZeroWidth(c))
99 || ((nFlags & RTL_UNICODETOTEXT_FLAGS_CONTROL_IGNORE) != 0
100 && ImplIsControlOrFormat(c))
101 || ((nFlags & RTL_UNICODETOTEXT_FLAGS_PRIVATE_IGNORE) != 0
102 && ImplIsPrivateUse(c));
105 /* ======================================================================= */
107 sal_Unicode ImplGetUndefinedUnicodeChar(sal_uChar cChar, sal_uInt32 nFlags)
109 return ((nFlags & RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MASK)
110 == RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MAPTOPRIVATE) ?
111 RTL_TEXTCVT_BYTE_PRIVATE_START + cChar :
112 RTL_TEXTENC_UNICODE_REPLACEMENT_CHARACTER;
115 /* ----------------------------------------------------------------------- */
117 sal_Bool
118 ImplHandleUndefinedUnicodeToTextChar(ImplTextConverterData const * pData,
119 sal_Unicode const ** ppSrcBuf,
120 sal_Unicode const * pEndSrcBuf,
121 sal_Char ** ppDestBuf,
122 sal_Char const * pEndDestBuf,
123 sal_uInt32 nFlags,
124 sal_uInt32 * pInfo)
126 sal_Unicode c = **ppSrcBuf;
128 (void) pData; /* unused */
130 /* Should the private character map to one byte */
131 if ( (c >= RTL_TEXTCVT_BYTE_PRIVATE_START) && (c <= RTL_TEXTCVT_BYTE_PRIVATE_END) )
133 if ( nFlags & RTL_UNICODETOTEXT_FLAGS_PRIVATE_MAPTO0 )
135 **ppDestBuf = (sal_Char)(sal_uChar)(c-RTL_TEXTCVT_BYTE_PRIVATE_START);
136 (*ppDestBuf)++;
137 (*ppSrcBuf)++;
138 return sal_True;
142 /* Should this character ignored (Private, Non Spacing, Control) */
143 if ( ImplIsUnicodeIgnoreChar( c, nFlags ) )
145 (*ppSrcBuf)++;
146 return sal_True;
149 /* Surrogates Characters should result in */
150 /* one replacement character */
151 if (ImplIsHighSurrogate(c))
153 if ( *ppSrcBuf == pEndSrcBuf )
155 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR | RTL_UNICODETOTEXT_INFO_SRCBUFFERTOSMALL;
156 return sal_False;
159 c = *((*ppSrcBuf)+1);
160 if (ImplIsLowSurrogate(c))
161 (*ppSrcBuf)++;
162 else
164 *pInfo |= RTL_UNICODETOTEXT_INFO_INVALID;
165 if ( (nFlags & RTL_UNICODETOTEXT_FLAGS_INVALID_MASK) == RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR )
167 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR;
168 return sal_False;
170 else if ( (nFlags & RTL_UNICODETOTEXT_FLAGS_INVALID_MASK) == RTL_UNICODETOTEXT_FLAGS_INVALID_IGNORE )
172 (*ppSrcBuf)++;
173 return sal_True;
175 else if (ImplGetInvalidAsciiMultiByte(nFlags,
176 *ppDestBuf,
177 pEndDestBuf - *ppDestBuf))
179 ++*ppSrcBuf;
180 ++*ppDestBuf;
181 return sal_True;
183 else
185 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR
186 | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
187 return sal_False;
192 *pInfo |= RTL_UNICODETOTEXT_INFO_UNDEFINED;
193 if ( (nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK) == RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR )
195 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR;
196 return sal_False;
198 else if ( (nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK) == RTL_UNICODETOTEXT_FLAGS_UNDEFINED_IGNORE )
199 (*ppSrcBuf)++;
200 else if (ImplGetUndefinedAsciiMultiByte(nFlags,
201 *ppDestBuf,
202 pEndDestBuf - *ppDestBuf))
204 ++*ppSrcBuf;
205 ++*ppDestBuf;
207 else
209 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR
210 | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
211 return sal_False;
214 return sal_True;