initial version of UTF-8 strings representation (still converting to wchar_t* a lot...
[wxWidgets.git] / src / common / strvararg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/strvararg.cpp
3 // Purpose: macros for implementing type-safe vararg passing of strings
4 // Author: Vaclav Slavik
5 // Created: 2007-02-19
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #include "wx/strvararg.h"
27 #include "wx/buffer.h"
28 #include "wx/strconv.h"
29 #include "wx/string.h"
30
31 // ============================================================================
32 // implementation
33 // ============================================================================
34
35 const wxChar *wxArgNormalizer<const wxCStrData&>::get() const
36 {
37 // FIXME-UTF8: use some way that doesn't involve implicit conversion,
38 // so that we deallocate any converted buffer immediately;
39 // can't use AsString() because it returns wxString and not
40 // const wxString&, unfortunately; use As[W]CharBuf() when
41 // available.
42 return m_value;
43 }
44
45 const wxChar *wxArgNormalizer<const wxString&>::get() const
46 {
47 #if wxUSE_UNICODE_UTF8 // FIXME-UTF8
48 return (const wxChar*)m_value;
49 #else
50 return m_value.wx_str();
51 #endif
52 }
53
54 #if wxUSE_UNICODE // FIXME-UTF8: should be wxUSE_UNICODE_WCHAR
55 wxArgNormalizer<const char*>::wxArgNormalizer(const char *value)
56 {
57 // FIXME-UTF8: move this to the header so that m_value doesn't have
58 // to be dynamically allocated
59 m_value = new wxWCharBuffer(wxConvLibc.cMB2WC(value));
60 }
61
62 wxArgNormalizer<const char*>::~wxArgNormalizer()
63 {
64 delete m_value;
65 }
66
67 const wchar_t *wxArgNormalizer<const char*>::get() const
68 {
69 return m_value->data();
70 }
71 #endif // wxUSE_UNICODE_WCHAR
72
73
74 #if /*wxUSE_UNICODE_UTF8 ||*/ !wxUSE_UNICODE // FIXME-UTF8
75 wxArgNormalizer<const wchar_t*>::wxArgNormalizer(const wchar_t *value)
76 {
77 #if wxUSE_UNICODE_UTF8 // FIXME-UTF8: this will be the only case
78 m_value = new wxCharBuffer(wxConvUTF8.cWC2MB(value));
79 #else
80 m_value = new wxCharBuffer(wxConvLibc.cWC2MB(value));
81 #endif
82 }
83
84 wxArgNormalizer<const wchar_t*>::~wxArgNormalizer()
85 {
86 delete m_value;
87 }
88
89 const char *wxArgNormalizer<const wchar_t*>::get() const
90 {
91 return m_value->data();
92 }
93 #endif // wxUSE_UNICODE_UTF8 || !wxUSE_UNICODE
94
95 #if 0 // wxUSE_UNICODE_UTF8 - FIXME-UTF8
96 wxArgNormalizer<const char*>::wxArgNormalizer(const char *value)
97 {
98 // FIXME-UTF8: move this to the header so that m_value doesn't have
99 // to be dynamically allocated
100 // FIXME-UTF8: optimize this if current locale is UTF-8 one
101
102 // convert to widechar string first:
103 wxWCharBuffer buf(wxConvLibc.cMB2WC(value));
104
105 if ( buf )
106 {
107 // then to UTF-8:
108 m_value = new wxCharBuffer(wxConvUTF8.cWC2MB(value));
109 }
110 else
111 {
112 m_value = new wxCharBuffer();
113 }
114 }
115
116 wxArgNormalizer<const char*>::~wxArgNormalizer()
117 {
118 delete m_value;
119 }
120
121 const char *wxArgNormalizer<const char*>::get() const
122 {
123 return m_value->data();
124 }
125 #endif // wxUSE_UNICODE_UTF8
126
127
128
129 // FIXME-UTF8: move this to the header once it's possible to include buffer.h
130 // without including wxcrt.h
131 wxArgNormalizer<wxCharBuffer>::wxArgNormalizer(const wxCharBuffer& buf)
132 : wxArgNormalizer<const char*>(buf.data())
133 {
134 }
135
136 wxArgNormalizer<wxWCharBuffer>::wxArgNormalizer(const wxWCharBuffer& buf)
137 : wxArgNormalizer<const wchar_t*>(buf.data())
138 {
139 }