]> git.saurik.com Git - wxWidgets.git/blob - include/wx/convauto.h
ec32b93c431629baaa85853b48ba47464cf0846b
[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
16 #if wxUSE_WCHAR_T
17
18 // ----------------------------------------------------------------------------
19 // wxConvAuto: uses BOM to automatically detect input encoding
20 // ----------------------------------------------------------------------------
21
22 class WXDLLIMPEXP_BASE wxConvAuto : public wxMBConv
23 {
24 public:
25 // default ctor, the real conversion will be created on demand
26 wxConvAuto() { m_conv = NULL; /* the rest will be initialized later */ }
27
28 // copy ctor doesn't initialize anything neither as conversion can only be
29 // deduced on first use
30 wxConvAuto(const wxConvAuto& WXUNUSED(other)) { m_conv = NULL; }
31
32 virtual ~wxConvAuto() { if ( m_conv && m_ownsConv ) delete m_conv; }
33
34 // override the base class virtual function(s) to use our m_conv
35 virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
36 const char *src, size_t srcLen = -1) const;
37
38 virtual size_t FromWChar(char *dst, size_t dstLen,
39 const wchar_t *src, size_t srcLen = -1) const;
40
41 virtual size_t GetMBNulLen() const { return m_conv->GetMBNulLen(); }
42
43 private:
44 // all currently recognized BOM values
45 enum BOMType
46 {
47 BOM_None,
48 BOM_UTF32BE,
49 BOM_UTF32LE,
50 BOM_UTF16BE,
51 BOM_UTF16LE,
52 BOM_UTF8
53 };
54
55 // return the BOM type of this buffer
56 static BOMType DetectBOM(const char *src, size_t srcLen);
57
58 // initialize m_conv with the conversion to use by default (UTF-8)
59 void InitWithDefault()
60 {
61 m_conv = &wxConvUTF8;
62 m_ownsConv = false;
63 }
64
65 // create the correct conversion object for the given BOM type
66 void InitFromBOM(BOMType bomType);
67
68 // create the correct conversion object for the BOM present in the
69 // beginning of the buffer; adjust the buffer to skip the BOM if found
70 void InitFromInput(const char **src, size_t *len);
71
72 // adjust src and len to skip over the BOM (identified by m_bomType) at the
73 // start of the buffer
74 void SkipBOM(const char **src, size_t *len) const;
75
76
77 // conversion object which we really use, NULL until the first call to
78 // either ToWChar() or FromWChar()
79 wxMBConv *m_conv;
80
81 // our BOM type
82 BOMType m_bomType;
83
84 // true if we allocated m_conv ourselves, false if we just use an existing
85 // global conversion
86 bool m_ownsConv;
87
88 // true if we already skipped BOM when converting (and not just calculating
89 // the size)
90 bool m_consumedBOM;
91
92
93 DECLARE_NO_ASSIGN_CLASS(wxConvAuto);
94 };
95
96 #endif // wxUSE_WCHAR_T
97
98 #endif // _WX_CONVAUTO_H_
99