]>
git.saurik.com Git - wxWidgets.git/blob - src/common/convauto.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/convauto.cpp
3 // Purpose: implementation of wxConvAuto
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2006 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
32 #include "wx/convauto.h"
34 // we use latin1 by default as it seems the least bad choice: the files we need
35 // to detect input of don't always come from the user system (they are often
36 // received from other machines) and so using wxFONTENCODING_SYSTEM doesn't
37 // seem to be a good idea and there is no other reasonable alternative
38 wxFontEncoding
wxConvAuto::ms_defaultMBEncoding
= wxFONTENCODING_ISO8859_1
;
40 // ============================================================================
42 // ============================================================================
45 void wxConvAuto::SetFallbackEncoding(wxFontEncoding enc
)
47 wxASSERT_MSG( enc
!= wxFONTENCODING_DEFAULT
,
48 wxT("wxFONTENCODING_DEFAULT doesn't make sense here") );
50 ms_defaultMBEncoding
= enc
;
54 wxConvAuto::BOMType
wxConvAuto::DetectBOM(const char *src
, size_t srcLen
)
56 // examine the buffer for BOM presence
58 // quoting from http://www.unicode.org/faq/utf_bom.html#BOM:
60 // Bytes Encoding Form
62 // 00 00 FE FF UTF-32, big-endian
63 // FF FE 00 00 UTF-32, little-endian
64 // FE FF UTF-16, big-endian
65 // FF FE UTF-16, little-endian
68 // as some BOMs are prefixes of other ones we may need to read more bytes
69 // to disambiguate them
77 if ( src
[0] == '\x00' || src
[0] == '\xFF' ||
78 src
[0] == '\xFE' || src
[0] == '\xEF')
80 // this could be a BOM but we don't know yet
87 if ( src
[0] == '\xEF' && src
[1] == '\xBB' )
90 return src
[2] == '\xBF' ? BOM_UTF8
: BOM_None
;
95 if ( src
[0] == '\xFE' && src
[1] == '\xFF' )
98 if ( src
[0] == '\xFF' && src
[1] == '\xFE' )
100 // if the next byte is 0, it could be an UTF-32LE BOM but if it
101 // isn't we can be sure it's UTF-16LE
102 if ( srcLen
== 3 && src
[2] != '\x00' )
108 if ( src
[0] == '\x00' && src
[1] == '\x00' )
110 // this could only be UTF-32BE, check that the data we have so
112 if ( srcLen
== 3 && src
[2] != '\xFE' )
120 // we have at least 4 characters so we may finally decide whether
121 // we have a BOM or not
122 if ( src
[0] == '\xEF' && src
[1] == '\xBB' && src
[2] == '\xBF' )
125 if ( src
[0] == '\x00' && src
[1] == '\x00' &&
126 src
[2] == '\xFE' && src
[3] == '\xFF' )
129 if ( src
[0] == '\xFF' && src
[1] == '\xFE' &&
130 src
[2] == '\x00' && src
[3] == '\x00' )
133 if ( src
[0] == '\xFE' && src
[1] == '\xFF' )
136 if ( src
[0] == '\xFF' && src
[1] == '\xFE' )
143 void wxConvAuto::InitFromBOM(BOMType bomType
)
145 m_consumedBOM
= false;
150 wxFAIL_MSG( "shouldn't be called for this BOM type" );
158 m_conv
= new wxMBConvUTF32BE
;
163 m_conv
= new wxMBConvUTF32LE
;
168 m_conv
= new wxMBConvUTF16BE
;
173 m_conv
= new wxMBConvUTF16LE
;
182 wxFAIL_MSG( "unknown BOM type" );
187 // we end up here if there is no BOM or we didn't recognize it somehow
188 // (this shouldn't happen but still don't crash if it does), so use the
191 m_consumedBOM
= true; // as there is nothing to consume
195 void wxConvAuto::SkipBOM(const char **src
, size_t *len
) const
201 wxFAIL_MSG( "shouldn't be called for this BOM type" );
223 wxFAIL_MSG( "unknown BOM type" );
228 if ( *len
!= (size_t)-1 )
232 bool wxConvAuto::InitFromInput(const char *src
, size_t len
)
234 m_bomType
= DetectBOM(src
, len
);
235 if ( m_bomType
== BOM_Unknown
)
238 InitFromBOM(m_bomType
);
244 wxConvAuto::ToWChar(wchar_t *dst
, size_t dstLen
,
245 const char *src
, size_t srcLen
) const
247 // we check BOM and create the appropriate conversion the first time we're
248 // called but we also need to ensure that the BOM is skipped not only
249 // during this initial call but also during the first call with non-NULL
250 // dst as typically we're first called with NULL dst to calculate the
251 // needed buffer size
252 wxConvAuto
*self
= const_cast<wxConvAuto
*>(this);
257 if ( !self
->InitFromInput(src
, srcLen
) )
259 // there is not enough data to determine whether we have a BOM or
260 // not, so fail for now -- the caller is supposed to call us again
262 return wxCONV_FAILED
;
266 if ( !m_consumedBOM
)
268 SkipBOM(&src
, &srcLen
);
271 // there is nothing left except the BOM so we'd return 0 below but
272 // this is unexpected: decoding a non-empty string must either fail
273 // or return something non-empty, in particular this would break
274 // the code in wxTextInputStream::NextChar()
276 // so still return an error as we need some more data to be able to
278 return wxCONV_FAILED
;
282 // try to convert using the auto-detected encoding
283 size_t rc
= m_conv
->ToWChar(dst
, dstLen
, src
, srcLen
);
284 if ( rc
== wxCONV_FAILED
&& m_bomType
== BOM_None
)
286 // if the conversion failed but we didn't really detect anything and
287 // simply tried UTF-8 by default, retry it using the fall-back
288 if ( m_encDefault
!= wxFONTENCODING_MAX
)
293 self
->m_conv
= new wxCSConv(m_encDefault
== wxFONTENCODING_DEFAULT
294 ? GetFallbackEncoding()
296 self
->m_ownsConv
= true;
298 rc
= m_conv
->ToWChar(dst
, dstLen
, src
, srcLen
);
302 // don't skip the BOM again the next time if we really consumed it
303 if ( rc
!= wxCONV_FAILED
&& dst
&& !m_consumedBOM
)
304 self
->m_consumedBOM
= true;
310 wxConvAuto::FromWChar(char *dst
, size_t dstLen
,
311 const wchar_t *src
, size_t srcLen
) const
315 // default to UTF-8 for the multibyte output
316 const_cast<wxConvAuto
*>(this)->InitWithUTF8();
319 return m_conv
->FromWChar(dst
, dstLen
, src
, srcLen
);
322 #endif // wxUSE_WCHAR_T