1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        wx/convauto.h
3 // Purpose:     wxConvAuto class declaration
4 // Author:      Vadim Zeitlin
5 // Created:     2006-04-03
6 // RCS-ID:      $Id: convauto.h 45893 2007-05-08 20:05:16Z VZ $
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
wxConvAuto()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
wxConvAuto(const wxConvAuto & WXUNUSED (other))30     wxConvAuto(const wxConvAuto& WXUNUSED(other)) : wxMBConv() { m_conv = NULL; }
31 
~wxConvAuto()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 
GetMBNulLen()41     virtual size_t GetMBNulLen() const { return m_conv->GetMBNulLen(); }
42 
Clone()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)
InitWithDefault()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 #else // !wxUSE_WCHAR_T
99 
100 // it doesn't matter how we define it in this case as it's unused anyhow, but
101 // do define it to allow the code using wxConvAuto() as default argument (this
102 // is done in many places) to compile
103 typedef wxMBConv wxConvAuto;
104 
105 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
106 
107 #endif // _WX_CONVAUTO_H_
108 
109