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