1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %% Purpose: Overview of the wxMBConv classes in wxWindows
8 %% Copyright: (c) 2000 Ove Kaaven
9 %% Licence: wxWindows license
10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12 \section{wxMBConv classes overview
}\label{mbconvclasses
}
14 Classes:
\helpref{wxMBConv
}{wxmbconv
},
\helpref{wxMBConvFile
}{wxmbconvfile
},
15 \helpref{wxMBConvUTF7
}{wxmbconvutf7
},
\helpref{wxMBConvUTF8
}{wxmbconvutf8
},
16 \helpref{wxCSConv
}{wxcsconv
}
18 The wxMBConv classes in wxWindows enables an Unicode-aware application to
19 easily convert between Unicode and the variety of
8-bit encoding systems still
22 \subsection{Background: The need for conversion
}
24 As programs are becoming more and more globalized, and users exchange documents
25 across country boundaries as never before, applications increasingly need to
26 take into account all the different character sets in use around the world. It
27 is no longer enough to just depend on the default byte-sized character set that
28 computers have traditionally used.
30 A few years ago, a solution was proposed: the Unicode standard. Able to contain
31 the complete set of characters in use in one unified global coding system,
32 it would resolve the character set problems once and for all.
34 But it hasn't happened yet, and the migration towards Unicode has created new
35 challenges, resulting in "compatibility encodings" such as UTF-
8. A large
36 number of systems out there still depends on the old
8-bit encodings, hampered
37 by the huge amounts of legacy code still widely deployed. Even sending
38 Unicode data from one Unicode-aware system to another may need encoding to an
39 8-bit multibyte encoding (UTF-
7 or UTF-
8 is typically used for this purpose), to
40 pass unhindered through any traditional transport channels.
42 \subsection{Background: The wxString class
}
44 If you have compiled wxWindows in Unicode mode, the wxChar type will become
45 identical to wchar
\_t rather than char, and a wxString stores wxChars. Hence,
46 all wxString manipulation in your application will then operate on Unicode
47 strings, and almost as easily as working with ordinary char strings (you
48 just need to remember to use the wxT() macro to encapsulate any string
51 But often, your environment doesn't want Unicode strings. You could be sending
52 data over a network, or processing a text file for some other application. You
53 need a way to quickly convert your easily-handled Unicode data to and from a
54 traditional
8-bit-encoding. And this is what the wxMBConv classes do.
56 \subsection{wxMBConv classes
}
58 The base class for all these conversions is the wxMBConv class (which itself
59 implements standard libc locale conversion). Derived classes include
60 wxMBConvFile, wxMBConvUTF7, wxMBConvUTF8, and wxCSConv, which implement
61 different kinds of conversions. You can also derive your own class for your
62 own custom encoding and use it, should you need it. All you need to do is
63 override the MB2WC and WC2MB methods.
65 \subsection{wxMBConv objects
}
67 In C++, for a class to be useful and possible to pass around, it needs to be
68 instantiated. All of the wxWindows-provided wxMBConv classes have predefined
69 instances (wxConvLibc, wxConvFile, wxConvUTF7, wxConvUTF8, wxConvLocal).
70 You can use these predefined objects directly, or you can instantiate your own
73 A variable, wxConvCurrent, points to the conversion object that the user interface
74 is supposed to use, in the case that the user interface is not Unicode-based (like
75 with GTK+
1.2). By default, it points to wxConvLibc or wxConvLocal, depending on
76 which works best on the current platform.
80 The wxCSConv class is special because when it is instantiated, you can tell it
81 which character set it should use, which makes it meaningful to keep many
82 instances of them around, each with a different character set (or you can
83 create a wxCSConv instance on the fly).
85 The predefined wxCSConv instance, wxConvLocal, is preset to use the
86 default user character set, but you should rarely need to use it directly,
87 it is better to go through wxConvCurrent.
89 \subsection{Converting strings
}
91 Once you have chosen which object you want to use to convert your text,
92 here is how you would use them with wxString. These examples all assume
93 that you are using a Unicode build of wxWindows, although they will still
94 compile in a non-Unicode build (they just won't convert anything).
96 Example
1: Constructing a wxString from input in current encoding.
99 wxString str(input_data, *wxConvCurrent);
102 Example
2: Input in UTF-
8 encoding.
105 wxString str(input_data, wxConvUTF8);
108 Example
3: Input in KOI8-R. Construction of wxCSConv instance on the fly.
111 wxString str(input_data, wxCSConv(wxT("koi8-r")));
114 Example
4: Printing a wxString to stdout in UTF-
8 encoding.
117 puts(str.mb_str(wxConvUTF8));
120 Example
5: Printing a wxString to stdout in custom encoding.
121 Using preconstructed wxCSConv instance.
124 wxCSConv cust(user_encoding);
125 printf("Data:
%s\n", (const char*) str.mb_str(cust));
128 Note: Since mb_str() returns a temporary wxCharBuffer to hold the result
129 of the conversion, you need to explicitly cast it to const char* if you use
130 it in a vararg context (like with printf).
132 \subsection{Converting buffers
}
134 If you have specialized needs, or just don't want to use wxString, you
135 can also use the conversion methods of the conversion objects directly.
136 This can even be useful if you need to do conversion in a non-Unicode
137 build of wxWindows; converting a string from UTF-
8 to the current
138 encoding should be possible by doing this:
141 wxString str(wxConvUTF8.cMB2WC(input_data), *wxConvCurrent);
144 Here, cMB2WC of the UTF8 object returns a wxWCharBuffer containing a Unicode
145 string. The wxString constructor then converts it back to an
8-bit character
146 set using the passed conversion object, *wxConvCurrent. (In a Unicode build
147 of wxWindows, the constructor ignores the passed conversion object and
148 retains the Unicode data.)
150 This could also be done by first making a wxString of the original data:
153 wxString input_str(input_data);
154 wxString str(input_str.wc_str(wxConvUTF8), *wxConvCurrent);
157 To print a wxChar buffer to a non-Unicode stdout:
160 printf("Data:
%s\n", (const char*) wxConvCurrent->cWX2MB(unicode_data));
163 If you need to do more complex processing on the converted data, you
164 may want to store the temporary buffer in a local variable:
167 const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(unicode_data);
168 const char *tmp_str = (const char*) tmp_buf;
169 printf("Data:
%s\n", tmp_str);
170 process_data(tmp_str);
173 If a conversion had taken place in cWX2MB (i.e. in a Unicode build),
174 the buffer will be deallocated as soon as tmp_buf goes out of scope.
175 (The macro wxWX2MBbuf reflects the correct return value of cWX2MB
176 (either char* or wxCharBuffer), except for the const.)