]> git.saurik.com Git - wxWidgets.git/blob - include/wx/convauto.h
4de2bee9e74c4ca82affe73ebbffd99a93bcf407
[wxWidgets.git] / include / wx / convauto.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/convauto.h
3 // Purpose: wxConvAuto class declaration
4 // Author: Vadim Zeitlin
5 // Created: 2006-04-03
6 // RCS-ID: $Id$
7 // Copyright: (c) 2006 Vadim Zeitlin
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_CONVAUTO_H_
12 #define _WX_CONVAUTO_H_
13
14 #include "wx/strconv.h"
15 #include "wx/fontenc.h"
16
17 #if wxUSE_WCHAR_T
18
19 // ----------------------------------------------------------------------------
20 // wxConvAuto: uses BOM to automatically detect input encoding
21 // ----------------------------------------------------------------------------
22
23 class WXDLLIMPEXP_BASE wxConvAuto : public wxMBConv
24 {
25 public:
26 // default ctor, the real conversion will be created on demand
27 wxConvAuto(wxFontEncoding enc = wxFONTENCODING_DEFAULT)
28 {
29 Init();
30
31 m_encDefault = enc;
32 }
33
34 // copy ctor doesn't initialize anything neither as conversion can only be
35 // deduced on first use
36 wxConvAuto(const wxConvAuto& other) : wxMBConv()
37 {
38 Init();
39
40 m_encDefault = other.m_encDefault;
41 }
42
43 virtual ~wxConvAuto()
44 {
45 if ( m_ownsConv )
46 delete m_conv;
47 }
48
49 // get/set the fall-back encoding used when the input text doesn't have BOM
50 // and isn't UTF-8
51 //
52 // special values are wxFONTENCODING_MAX meaning not to use any fall back
53 // at all (but just fail to convert in this case) and wxFONTENCODING_SYSTEM
54 // meaning to use the encoding of the system locale
55 static wxFontEncoding GetFallbackEncoding() { return ms_defaultMBEncoding; }
56 static void SetFallbackEncoding(wxFontEncoding enc);
57 static void DisableFallbackEncoding()
58 {
59 SetFallbackEncoding(wxFONTENCODING_MAX);
60 }
61
62
63 // override the base class virtual function(s) to use our m_conv
64 virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
65 const char *src, size_t srcLen = wxNO_LEN) const;
66
67 virtual size_t FromWChar(char *dst, size_t dstLen,
68 const wchar_t *src, size_t srcLen = wxNO_LEN) const;
69
70 virtual size_t GetMBNulLen() const { return m_conv->GetMBNulLen(); }
71
72 virtual wxMBConv *Clone() const { return new wxConvAuto(*this); }
73
74 private:
75 // all currently recognized BOM values
76 enum BOMType
77 {
78 BOM_Unknown = -1,
79 BOM_None,
80 BOM_UTF32BE,
81 BOM_UTF32LE,
82 BOM_UTF16BE,
83 BOM_UTF16LE,
84 BOM_UTF8
85 };
86
87 // return the BOM type of this buffer
88 static BOMType DetectBOM(const char *src, size_t srcLen);
89
90 // common part of all ctors
91 void Init()
92 {
93 // no need to initialize m_bomType and m_consumedBOM here, this will be
94 // done when m_conv is created
95 m_conv = NULL;
96 m_ownsConv = false;
97 }
98
99 // initialize m_conv with the UTF-8 conversion
100 void InitWithUTF8()
101 {
102 m_conv = &wxConvUTF8;
103 m_ownsConv = false;
104 }
105
106 // create the correct conversion object for the given BOM type
107 void InitFromBOM(BOMType bomType);
108
109 // create the correct conversion object for the BOM present in the
110 // beginning of the buffer
111 //
112 // return false if the buffer is too short to allow us to determine if we
113 // have BOM or not
114 bool InitFromInput(const char *src, size_t len);
115
116 // adjust src and len to skip over the BOM (identified by m_bomType) at the
117 // start of the buffer
118 void SkipBOM(const char **src, size_t *len) const;
119
120
121 // fall-back multibyte encoding to use, may be wxFONTENCODING_SYSTEM or
122 // wxFONTENCODING_MAX but not wxFONTENCODING_DEFAULT
123 static wxFontEncoding ms_defaultMBEncoding;
124
125 // conversion object which we really use, NULL until the first call to
126 // either ToWChar() or FromWChar()
127 wxMBConv *m_conv;
128
129 // the multibyte encoding to use by default if input isn't Unicode
130 wxFontEncoding m_encDefault;
131
132 // our BOM type
133 BOMType m_bomType;
134
135 // true if we allocated m_conv ourselves, false if we just use an existing
136 // global conversion
137 bool m_ownsConv;
138
139 // true if we already skipped BOM when converting (and not just calculating
140 // the size)
141 bool m_consumedBOM;
142
143
144 wxDECLARE_NO_ASSIGN_CLASS(wxConvAuto);
145 };
146
147 #endif // wxUSE_WCHAR_T
148
149 #endif // _WX_CONVAUTO_H_
150