1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxConvAuto class declaration
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2006 Vadim Zeitlin
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_CONVAUTO_H_
12 #define _WX_CONVAUTO_H_
14 #include "wx/strconv.h"
15 #include "wx/fontenc.h"
17 // ----------------------------------------------------------------------------
18 // wxConvAuto: uses BOM to automatically detect input encoding
19 // ----------------------------------------------------------------------------
21 // All currently recognized BOM values.
33 class WXDLLIMPEXP_BASE wxConvAuto
: public wxMBConv
36 // default ctor, the real conversion will be created on demand
37 wxConvAuto(wxFontEncoding enc
= wxFONTENCODING_DEFAULT
)
44 // copy ctor doesn't initialize anything neither as conversion can only be
45 // deduced on first use
46 wxConvAuto(const wxConvAuto
& other
) : wxMBConv()
50 m_encDefault
= other
.m_encDefault
;
59 // get/set the fall-back encoding used when the input text doesn't have BOM
62 // special values are wxFONTENCODING_MAX meaning not to use any fall back
63 // at all (but just fail to convert in this case) and wxFONTENCODING_SYSTEM
64 // meaning to use the encoding of the system locale
65 static wxFontEncoding
GetFallbackEncoding() { return ms_defaultMBEncoding
; }
66 static void SetFallbackEncoding(wxFontEncoding enc
);
67 static void DisableFallbackEncoding()
69 SetFallbackEncoding(wxFONTENCODING_MAX
);
73 // override the base class virtual function(s) to use our m_conv
74 virtual size_t ToWChar(wchar_t *dst
, size_t dstLen
,
75 const char *src
, size_t srcLen
= wxNO_LEN
) const;
77 virtual size_t FromWChar(char *dst
, size_t dstLen
,
78 const wchar_t *src
, size_t srcLen
= wxNO_LEN
) const;
80 virtual size_t GetMBNulLen() const { return m_conv
->GetMBNulLen(); }
82 virtual wxMBConv
*Clone() const { return new wxConvAuto(*this); }
84 // return the BOM type of this buffer
85 static wxBOM
DetectBOM(const char *src
, size_t srcLen
);
87 // return the characters composing the given BOM.
88 static const char* GetBOMChars(wxBOM bomType
, size_t* count
);
96 // common part of all ctors
99 // We don't initialize m_encDefault here as different ctors do it
102 m_bomType
= wxBOM_Unknown
;
104 m_consumedBOM
= false;
107 // initialize m_conv with the UTF-8 conversion
110 m_conv
= &wxConvUTF8
;
114 // create the correct conversion object for the given BOM type
115 void InitFromBOM(wxBOM bomType
);
117 // create the correct conversion object for the BOM present in the
118 // beginning of the buffer
120 // return false if the buffer is too short to allow us to determine if we
122 bool InitFromInput(const char *src
, size_t len
);
124 // adjust src and len to skip over the BOM (identified by m_bomType) at the
125 // start of the buffer
126 void SkipBOM(const char **src
, size_t *len
) const;
129 // fall-back multibyte encoding to use, may be wxFONTENCODING_SYSTEM or
130 // wxFONTENCODING_MAX but not wxFONTENCODING_DEFAULT
131 static wxFontEncoding ms_defaultMBEncoding
;
133 // conversion object which we really use, NULL until the first call to
134 // either ToWChar() or FromWChar()
137 // the multibyte encoding to use by default if input isn't Unicode
138 wxFontEncoding m_encDefault
;
143 // true if we allocated m_conv ourselves, false if we just use an existing
147 // true if we already skipped BOM when converting (and not just calculating
152 wxDECLARE_NO_ASSIGN_CLASS(wxConvAuto
);
155 #endif // _WX_CONVAUTO_H_