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