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