mbconvclasses.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: mbconvclasses.h
  3. // Purpose: topic overview
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @page overview_mbconv wxMBConv Overview
  9. @tableofcontents
  10. The wxMBConv classes in wxWidgets enable an Unicode-aware application to easily
  11. convert between Unicode and the variety of 8-bit encoding systems still in use.
  12. @see @ref group_class_conv
  13. @section overview_mbconv_need Background: The Need for Conversion
  14. As programs are becoming more and more globalized, and users exchange documents
  15. across country boundaries as never before, applications increasingly need to
  16. take into account all the different character sets in use around the world. It
  17. is no longer enough to just depend on the default byte-sized character set that
  18. computers have traditionally used.
  19. A few years ago, a solution was proposed: the Unicode standard. Able to contain
  20. the complete set of characters in use in one unified global coding system, it
  21. would resolve the character set problems once and for all.
  22. But it hasn't happened yet, and the migration towards Unicode has created new
  23. challenges, resulting in "compatibility encodings" such as UTF-8. A large
  24. number of systems out there still depends on the old 8-bit encodings, hampered
  25. by the huge amounts of legacy code still widely deployed. Even sending Unicode
  26. data from one Unicode-aware system to another may need encoding to an 8-bit
  27. multibyte encoding (UTF-7 or UTF-8 is typically used for this purpose), to pass
  28. unhindered through any traditional transport channels.
  29. @section overview_mbconv_string Background: The wxString Class
  30. @todo rewrite this overview; it's not up2date with wxString changes
  31. If you have compiled wxWidgets in Unicode mode, the wxChar type will become
  32. identical to wchar_t rather than char, and a wxString stores wxChars. Hence,
  33. all wxString manipulation in your application will then operate on Unicode
  34. strings, and almost as easily as working with ordinary char strings (you just
  35. need to remember to use the wxT() macro to encapsulate any string literals).
  36. But often, your environment doesn't want Unicode strings. You could be sending
  37. data over a network, or processing a text file for some other application. You
  38. need a way to quickly convert your easily-handled Unicode data to and from a
  39. traditional 8-bit encoding. And this is what the wxMBConv classes do.
  40. @section overview_mbconv_classes wxMBConv Classes
  41. The base class for all these conversions is the wxMBConv class (which itself
  42. implements standard libc locale conversion). Derived classes include
  43. wxMBConvLibc, several different wxMBConvUTFxxx classes, and wxCSConv, which
  44. implement different kinds of conversions. You can also derive your own class
  45. for your own custom encoding and use it, should you need it. All you need to do
  46. is override the MB2WC and WC2MB methods.
  47. @section overview_mbconv_objects wxMBConv Objects
  48. Several of the wxWidgets-provided wxMBConv classes have predefined instances
  49. (wxConvLibc, wxConvFileName, wxConvUTF7, wxConvUTF8, wxConvLocal). You can use
  50. these predefined objects directly, or you can instantiate your own objects.
  51. A variable, wxConvCurrent, points to the conversion object that the user
  52. interface is supposed to use, in the case that the user interface is not
  53. Unicode-based (like with GTK+ 1.2). By default, it points to wxConvLibc or
  54. wxConvLocal, depending on which works best on the current platform.
  55. @section overview_mbconv_csconv wxCSConv
  56. The wxCSConv class is special because when it is instantiated, you can tell it
  57. which character set it should use, which makes it meaningful to keep many
  58. instances of them around, each with a different character set (or you can
  59. create a wxCSConv instance on the fly).
  60. The predefined wxCSConv instance, wxConvLocal, is preset to use the default
  61. user character set, but you should rarely need to use it directly, it is better
  62. to go through wxConvCurrent.
  63. @section overview_mbconv_converting Converting Strings
  64. Once you have chosen which object you want to use to convert your text, here is
  65. how you would use them with wxString. These examples all assume that you are
  66. using a Unicode build of wxWidgets, although they will still compile in a
  67. non-Unicode build (they just won't convert anything).
  68. Example 1: Constructing a wxString from input in current encoding.
  69. @code
  70. wxString str(input_data, *wxConvCurrent);
  71. @endcode
  72. Example 2: Input in UTF-8 encoding.
  73. @code
  74. wxString str(input_data, wxConvUTF8);
  75. @endcode
  76. Example 3: Input in KOI8-R. Construction of wxCSConv instance on the fly.
  77. @code
  78. wxString str(input_data, wxCSConv(wxT("koi8-r")));
  79. @endcode
  80. Example 4: Printing a wxString to stdout in UTF-8 encoding.
  81. @code
  82. puts(str.mb_str(wxConvUTF8));
  83. @endcode
  84. Example 5: Printing a wxString to stdout in custom encoding. Using
  85. preconstructed wxCSConv instance.
  86. @code
  87. wxCSConv cust(user_encoding);
  88. printf("Data: %s\n", (const char*) str.mb_str(cust));
  89. @endcode
  90. @note Since mb_str() returns a temporary wxCharBuffer to hold the result of the
  91. conversion, you need to explicitly cast it to const char* if you use it in a
  92. vararg context (like with printf).
  93. @section overview_mbconv_buffers Converting Buffers
  94. If you have specialized needs, or just don't want to use wxString, you can also
  95. use the conversion methods of the conversion objects directly. This can even be
  96. useful if you need to do conversion in a non-Unicode build of wxWidgets;
  97. converting a string from UTF-8 to the current encoding should be possible by
  98. doing this:
  99. @code
  100. wxString str(wxConvUTF8.cMB2WC(input_data), *wxConvCurrent);
  101. @endcode
  102. Here, cMB2WC of the UTF8 object returns a wxWCharBuffer containing a Unicode
  103. string. The wxString constructor then converts it back to an 8-bit character
  104. set using the passed conversion object, *wxConvCurrent. (In a Unicode build of
  105. wxWidgets, the constructor ignores the passed conversion object and retains the
  106. Unicode data.)
  107. This could also be done by first making a wxString of the original data:
  108. @code
  109. wxString input_str(input_data);
  110. wxString str(input_str.wc_str(wxConvUTF8), *wxConvCurrent);
  111. @endcode
  112. To print a wxChar buffer to a non-Unicode stdout:
  113. @code
  114. printf("Data: %s\n", (const char*) wxConvCurrent->cWX2MB(unicode_data));
  115. @endcode
  116. If you need to do more complex processing on the converted data, you may want
  117. to store the temporary buffer in a local variable:
  118. @code
  119. const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(unicode_data);
  120. const char *tmp_str = (const char*) tmp_buf;
  121. printf("Data: %s\n", tmp_str);
  122. process_data(tmp_str);
  123. @endcode
  124. If a conversion had taken place in cWX2MB (i.e. in a Unicode build), the buffer
  125. will be deallocated as soon as tmp_buf goes out of scope. The macro wxWX2MBbuf
  126. reflects the correct return value of cWX2MB (either char* or wxCharBuffer),
  127. except for the const.
  128. */