]> git.saurik.com Git - wxWidgets.git/blob - src/common/unichar.cpp
fixed wxSVGFileDC::write() to output UTF-8 even in ANSI build
[wxWidgets.git] / src / common / unichar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/unichar.cpp
3 // Purpose: wxUniChar and wxUniCharRef classes
4 // Author: Vaclav Slavik
5 // Created: 2007-03-19
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ===========================================================================
12 // headers
13 // ===========================================================================
14
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 #include "wx/strconv.h" // wxConvLibc
24 #endif
25
26 #include "wx/unichar.h"
27 #include "wx/stringops.h"
28
29 // ===========================================================================
30 // implementation
31 // ===========================================================================
32
33 // ---------------------------------------------------------------------------
34 // wxUniChar
35 // ---------------------------------------------------------------------------
36
37 /* static */
38 wxUniChar::value_type wxUniChar::From8bit(char c)
39 {
40 // all supported charsets have the first 128 characters same as ASCII:
41 if ( (unsigned char)c < 0x80 )
42 return c;
43
44 #if wxUSE_UTF8_LOCALE_ONLY
45 wxFAIL_MSG( _T("invalid UTF-8 character") );
46 return wxT('?'); // FIXME-UTF8: what to use as failure character?
47 #else
48 wchar_t buf[2];
49 if ( wxConvLibc.ToWChar(buf, 2, &c, 1) != 2 )
50 return wxT('?'); // FIXME-UTF8: what to use as failure character?
51 return buf[0];
52 #endif
53 }
54
55 /* static */
56 char wxUniChar::To8bit(wxUniChar::value_type c)
57 {
58 // all supported charsets have the first 128 characters same as ASCII:
59 if ( c < 0x80 )
60 return c;
61
62 #if wxUSE_UTF8_LOCALE_ONLY
63 wxFAIL_MSG( _T("character cannot be converted to single UTF-8 byte") );
64 return '?'; // FIXME-UTF8: what to use as failure character?
65 #else
66 wchar_t in = c;
67 char buf[2];
68 if ( wxConvLibc.FromWChar(buf, 2, &in, 1) != 2 )
69 return '?'; // FIXME-UTF8: what to use as failure character?
70 return buf[0];
71 #endif
72 }
73
74
75 // ---------------------------------------------------------------------------
76 // wxUniCharRef
77 // ---------------------------------------------------------------------------
78
79 #if wxUSE_UNICODE_UTF8
80 wxUniChar wxUniCharRef::UniChar() const
81 {
82 return wxStringOperations::DecodeChar(m_pos);
83 }
84
85 wxUniCharRef& wxUniCharRef::operator=(const wxUniChar& c)
86 {
87 wxStringOperations::Utf8CharBuffer utf(wxStringOperations::EncodeChar(c));
88 size_t lenOld = wxStringOperations::GetUtf8CharLength(*m_pos);
89 size_t lenNew = wxStringOperations::GetUtf8CharLength(utf[0]);
90
91 if ( lenNew == lenOld )
92 {
93 iterator pos(m_pos);
94 for ( size_t i = 0; i < lenNew; ++i, ++pos )
95 *pos = utf[i];
96 }
97 else
98 {
99 size_t idx = m_pos - m_str.begin();
100
101 m_str.replace(m_pos, m_pos + lenOld, utf, lenNew);
102
103 // this is needed to keep m_pos valid:
104 m_pos = m_str.begin() + idx;
105 }
106
107 return *this;
108 }
109 #endif // wxUSE_UNICODE_UTF8