]>
Commit | Line | Data |
---|---|---|
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 | m_conv = NULL; // the rest will be initialized later | |
30 | m_encDefault = enc; | |
31 | } | |
32 | ||
33 | // copy ctor doesn't initialize anything neither as conversion can only be | |
34 | // deduced on first use | |
35 | wxConvAuto(const wxConvAuto& other) : wxMBConv() | |
36 | { | |
37 | m_conv = NULL; | |
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 | } | |
59 | ||
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, | |
63 | const char *src, size_t srcLen = wxNO_LEN) const; | |
64 | ||
65 | virtual size_t FromWChar(char *dst, size_t dstLen, | |
66 | const wchar_t *src, size_t srcLen = wxNO_LEN) const; | |
67 | ||
68 | virtual size_t GetMBNulLen() const { return m_conv->GetMBNulLen(); } | |
69 | ||
70 | virtual wxMBConv *Clone() const { return new wxConvAuto(*this); } | |
71 | ||
72 | private: | |
73 | // all currently recognized BOM values | |
74 | enum BOMType | |
75 | { | |
76 | BOM_None, | |
77 | BOM_UTF32BE, | |
78 | BOM_UTF32LE, | |
79 | BOM_UTF16BE, | |
80 | BOM_UTF16LE, | |
81 | BOM_UTF8 | |
82 | }; | |
83 | ||
84 | // return the BOM type of this buffer | |
85 | static BOMType DetectBOM(const char *src, size_t srcLen); | |
86 | ||
87 | // initialize m_conv with the UTF-8 conversion | |
88 | void InitWithUTF8() | |
89 | { | |
90 | m_conv = &wxConvUTF8; | |
91 | m_ownsConv = false; | |
92 | } | |
93 | ||
94 | // create the correct conversion object for the given BOM type | |
95 | void InitFromBOM(BOMType bomType); | |
96 | ||
97 | // create the correct conversion object for the BOM present in the | |
98 | // beginning of the buffer; adjust the buffer to skip the BOM if found | |
99 | void InitFromInput(const char **src, size_t *len); | |
100 | ||
101 | // adjust src and len to skip over the BOM (identified by m_bomType) at the | |
102 | // start of the buffer | |
103 | void SkipBOM(const char **src, size_t *len) const; | |
104 | ||
105 | ||
106 | // fall-back multibyte encoding to use, may be wxFONTENCODING_SYSTEM or | |
107 | // wxFONTENCODING_MAX but not wxFONTENCODING_DEFAULT | |
108 | static wxFontEncoding ms_defaultMBEncoding; | |
109 | ||
110 | // conversion object which we really use, NULL until the first call to | |
111 | // either ToWChar() or FromWChar() | |
112 | wxMBConv *m_conv; | |
113 | ||
114 | // the multibyte encoding to use by default if input isn't Unicode | |
115 | wxFontEncoding m_encDefault; | |
116 | ||
117 | // our BOM type | |
118 | BOMType m_bomType; | |
119 | ||
120 | // true if we allocated m_conv ourselves, false if we just use an existing | |
121 | // global conversion | |
122 | bool m_ownsConv; | |
123 | ||
124 | // true if we already skipped BOM when converting (and not just calculating | |
125 | // the size) | |
126 | bool m_consumedBOM; | |
127 | ||
128 | ||
129 | DECLARE_NO_ASSIGN_CLASS(wxConvAuto) | |
130 | }; | |
131 | ||
132 | #endif // wxUSE_WCHAR_T | |
133 | ||
134 | #endif // _WX_CONVAUTO_H_ | |
135 |