]>
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"
31 #include "wx/convauto.h"
33 // ============================================================================
35 // ============================================================================
38 wxConvAuto::BOMType
wxConvAuto::DetectBOM(const char *src
, size_t srcLen
)
42 // minimal BOM is 2 bytes so bail out immediately and simplify the code
43 // below which wouldn't need to check for length for UTF-16 cases
47 // examine the buffer for BOM presence
49 // see http://www.unicode.org/faq/utf_bom.html#BOM
53 // could only be big endian UTF-32 (00 00 FE FF)
64 // could only be big endian UTF-16 (FE FF)
65 if ( *src
++ == '\xff' )
72 // could be either little endian UTF-16 or UTF-32, both start
74 if ( *src
++ == '\xfe' )
76 return srcLen
>= 4 && src
[0] == '\0' && src
[1] == '\0'
83 // is this UTF-8 BOM (EF BB BF)?
84 if ( srcLen
>= 3 && src
[0] == '\xbb' && src
[1] == '\xbf' )
94 void wxConvAuto::InitFromBOM(BOMType bomType
)
96 m_consumedBOM
= false;
101 m_conv
= new wxMBConvUTF32BE
;
106 m_conv
= new wxMBConvUTF32LE
;
111 m_conv
= new wxMBConvUTF16BE
;
116 m_conv
= new wxMBConvUTF16LE
;
121 m_conv
= &wxConvUTF8
;
126 wxFAIL_MSG( _T("unexpected BOM type") );
127 // fall through: still need to create something
131 m_consumedBOM
= true; // as there is nothing to consume
135 void wxConvAuto::SkipBOM(const char **src
, size_t *len
) const
155 wxFAIL_MSG( _T("unexpected BOM type") );
156 // fall through: still need to create something
163 if ( *len
!= (size_t)-1 )
167 void wxConvAuto::InitFromInput(const char **src
, size_t *len
)
169 m_bomType
= DetectBOM(*src
, *len
);
170 InitFromBOM(m_bomType
);
175 wxConvAuto::ToWChar(wchar_t *dst
, size_t dstLen
,
176 const char *src
, size_t srcLen
) const
178 // we check BOM and create the appropriate conversion the first time we're
179 // called but we also need to ensure that the BOM is skipped not only
180 // during this initial call but also during the first call with non-NULL
181 // dst as typically we're first called with NULL dst to calculate the
182 // needed buffer size
183 wxConvAuto
*self
= wx_const_cast(wxConvAuto
*, this);
186 self
->InitFromInput(&src
, &srcLen
);
188 self
->m_consumedBOM
= true;
191 if ( !m_consumedBOM
&& dst
)
193 self
->m_consumedBOM
= true;
194 SkipBOM(&src
, &srcLen
);
197 return m_conv
->ToWChar(dst
, dstLen
, src
, srcLen
);
201 wxConvAuto::FromWChar(char *dst
, size_t dstLen
,
202 const wchar_t *src
, size_t srcLen
) const
206 // default to UTF-8 for the multibyte output
207 wx_const_cast(wxConvAuto
*, this)->InitWithDefault();
210 return m_conv
->FromWChar(dst
, dstLen
, src
, srcLen
);
213 #endif // wxUSE_WCHAR_T