]> git.saurik.com Git - wxWidgets.git/blob - include/wx/convauto.h
moved wxSTRINGIZE to defs.h and made it work with macro argument, so that it can...
[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)) : wxMBConv() { 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 = wxNO_LEN) const;
37
38 virtual size_t FromWChar(char *dst, size_t dstLen,
39 const wchar_t *src, size_t srcLen = wxNO_LEN) const;
40
41 virtual size_t GetMBNulLen() const { return m_conv->GetMBNulLen(); }
42
43 virtual wxMBConv *Clone() const { return new wxConvAuto(*this); }
44
45 private:
46 // all currently recognized BOM values
47 enum BOMType
48 {
49 BOM_None,
50 BOM_UTF32BE,
51 BOM_UTF32LE,
52 BOM_UTF16BE,
53 BOM_UTF16LE,
54 BOM_UTF8
55 };
56
57 // return the BOM type of this buffer
58 static BOMType DetectBOM(const char *src, size_t srcLen);
59
60 // initialize m_conv with the conversion to use by default (UTF-8)
61 void InitWithDefault()
62 {
63 m_conv = &wxConvUTF8;
64 m_ownsConv = false;
65 }
66
67 // create the correct conversion object for the given BOM type
68 void InitFromBOM(BOMType bomType);
69
70 // create the correct conversion object for the BOM present in the
71 // beginning of the buffer; adjust the buffer to skip the BOM if found
72 void InitFromInput(const char **src, size_t *len);
73
74 // adjust src and len to skip over the BOM (identified by m_bomType) at the
75 // start of the buffer
76 void SkipBOM(const char **src, size_t *len) const;
77
78
79 // conversion object which we really use, NULL until the first call to
80 // either ToWChar() or FromWChar()
81 wxMBConv *m_conv;
82
83 // our BOM type
84 BOMType m_bomType;
85
86 // true if we allocated m_conv ourselves, false if we just use an existing
87 // global conversion
88 bool m_ownsConv;
89
90 // true if we already skipped BOM when converting (and not just calculating
91 // the size)
92 bool m_consumedBOM;
93
94
95 DECLARE_NO_ASSIGN_CLASS(wxConvAuto)
96 };
97
98 #endif // wxUSE_WCHAR_T
99
100 #endif // _WX_CONVAUTO_H_
101