convauto.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: convauto.h
  3. // Purpose: interface of wxConvAuto
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. Constants representing various BOM types.
  9. BOM is an abbreviation for "Byte Order Mark", a special Unicode character
  10. which may be inserted into the beginning of a text stream to indicate its
  11. encoding.
  12. @since 2.9.3
  13. */
  14. enum wxBOM
  15. {
  16. /**
  17. Unknown BOM.
  18. This is returned if BOM presence couldn't be determined and normally
  19. happens because not enough bytes of input have been analysed.
  20. */
  21. wxBOM_Unknown = -1,
  22. /**
  23. No BOM.
  24. The stream doesn't contain BOM character at all.
  25. */
  26. wxBOM_None,
  27. /**
  28. UTF-32 big endian BOM.
  29. The stream is encoded in big endian variant of UTF-32.
  30. */
  31. wxBOM_UTF32BE,
  32. /**
  33. UTF-32 little endian BOM.
  34. The stream is encoded in little endian variant of UTF-32.
  35. */
  36. wxBOM_UTF32LE,
  37. /**
  38. UTF-16 big endian BOM.
  39. The stream is encoded in big endian variant of UTF-16.
  40. */
  41. wxBOM_UTF16BE,
  42. /**
  43. UTF-16 little endian BOM.
  44. The stream is encoded in little endian variant of UTF-16.
  45. */
  46. wxBOM_UTF16LE,
  47. /**
  48. UTF-8 BOM.
  49. The stream is encoded in UTF-8.
  50. Notice that contrary to a popular belief, it's perfectly possible and,
  51. n fact, common under Microsoft Windows systems, to have a BOM in an
  52. UTF-8 stream: while it's not used to indicate the endianness of UTF-8
  53. stream (as it's byte-oriented), the BOM can still be useful just as an
  54. unambiguous indicator of UTF-8 being used.
  55. */
  56. wxBOM_UTF8
  57. };
  58. /**
  59. @class wxConvAuto
  60. This class implements a Unicode to/from multibyte converter capable of
  61. automatically recognizing the encoding of the multibyte text on input. The
  62. logic used is very simple: the class uses the BOM (byte order mark) if it's
  63. present and tries to interpret the input as UTF-8 otherwise. If this fails,
  64. the input is interpreted as being in the default multibyte encoding which
  65. can be specified in the constructor of a wxConvAuto instance and, in turn,
  66. defaults to the value of GetFallbackEncoding() if not explicitly given.
  67. For the conversion from Unicode to multibyte, the same encoding as was
  68. previously used for multibyte to Unicode conversion is reused. If there had
  69. been no previous multibyte to Unicode conversion, UTF-8 is used by default.
  70. Notice that once the multibyte encoding is automatically detected, it
  71. doesn't change any more, i.e. it is entirely determined by the first use of
  72. wxConvAuto object in the multibyte-to-Unicode direction. However creating a
  73. copy of wxConvAuto object, either via the usual copy constructor or
  74. assignment operator, or using wxMBConv::Clone(), resets the automatically
  75. detected encoding so that the new copy will try to detect the encoding of
  76. the input on first use.
  77. This class is used by default in wxWidgets classes and functions reading
  78. text from files such as wxFile, wxFFile, wxTextFile, wxFileConfig and
  79. various stream classes so the encoding set with its SetFallbackEncoding()
  80. method will affect how these classes treat input files. In particular, use
  81. this method to change the fall-back multibyte encoding used to interpret
  82. the contents of the files whose contents isn't valid UTF-8 or to disallow
  83. it completely.
  84. @library{wxbase}
  85. @category{data}
  86. @see @ref overview_mbconv
  87. */
  88. class wxConvAuto : public wxMBConv
  89. {
  90. public:
  91. /**
  92. Constructs a new wxConvAuto instance. The object will try to detect the
  93. input of the multibyte text given to its wxMBConv::ToWChar() method
  94. automatically but if the automatic detection of Unicode encodings
  95. fails, the fall-back encoding @a enc will be used to interpret it as
  96. multibyte text.
  97. The default value of @a enc, @c wxFONTENCODING_DEFAULT, means that the
  98. global default value (which can be set using SetFallbackEncoding())
  99. should be used. As with that method, passing @c wxFONTENCODING_MAX
  100. inhibits using this encoding completely so the input multibyte text
  101. will always be interpreted as UTF-8 in the absence of BOM and the
  102. conversion will fail if the input doesn't form valid UTF-8 sequence.
  103. Another special value is @c wxFONTENCODING_SYSTEM which means to use
  104. the encoding currently used on the user system, i.e. the encoding
  105. returned by wxLocale::GetSystemEncoding(). Any other encoding will be
  106. used as is, e.g. passing @c wxFONTENCODING_ISO8859_1 ensures that
  107. non-UTF-8 input will be treated as latin1.
  108. */
  109. wxConvAuto(wxFontEncoding enc = wxFONTENCODING_DEFAULT);
  110. /**
  111. Return the detected BOM type.
  112. The BOM type is detected after sufficiently many initial bytes have
  113. passed through this conversion object so it will always return
  114. wxBOM_Unknown immediately after the object creation but may return a
  115. different value later.
  116. @since 2.9.3
  117. */
  118. wxBOM GetBOM() const;
  119. /**
  120. Return a pointer to the characters that makes up this BOM.
  121. The returned character count is 2, 3 or 4, or undefined if the return
  122. value is NULL.
  123. @param bom
  124. A valid BOM type, i.e. not wxBOM_Unknown or wxBOM_None.
  125. @param count
  126. A non-@NULL pointer receiving the number of characters in this BOM.
  127. @return
  128. Pointer to characters composing the BOM or @NULL if BOM is unknown
  129. or invalid. Notice that the returned string is not NUL-terminated
  130. and may contain embedded NULs so @a count must be used to handle it
  131. correctly.
  132. @since 2.9.3
  133. */
  134. const char* GetBOMChars(wxBOM bom, size_t* count);
  135. /**
  136. Disable the use of the fall back encoding: if the input doesn't have a
  137. BOM and is not valid UTF-8, the conversion will fail.
  138. */
  139. static void DisableFallbackEncoding();
  140. /**
  141. Returns the encoding used by default by wxConvAuto if no other encoding
  142. is explicitly specified in constructor. By default, returns
  143. @c wxFONTENCODING_ISO8859_1 but can be changed using
  144. SetFallbackEncoding().
  145. */
  146. static wxFontEncoding GetFallbackEncoding();
  147. /**
  148. Changes the encoding used by default by wxConvAuto if no other encoding
  149. is explicitly specified in constructor. The default value, which can be
  150. retrieved using GetFallbackEncoding(), is @c wxFONTENCODING_ISO8859_1.
  151. Special values of @c wxFONTENCODING_SYSTEM or @c wxFONTENCODING_MAX can
  152. be used for the @a enc parameter to use the encoding of the current
  153. user locale as fall back or not use any encoding for fall back at all,
  154. respectively (just as with the similar constructor parameter). However,
  155. @c wxFONTENCODING_DEFAULT can't be used here.
  156. */
  157. static void SetFallbackEncoding(wxFontEncoding enc);
  158. /**
  159. Return the BOM type of this buffer.
  160. This is a helper function which is normally only used internally by
  161. wxConvAuto but provided for convenience of the code that wants to
  162. detect the encoding of a stream by checking it for BOM presence on its
  163. own.
  164. @since 2.9.3
  165. */
  166. static wxBOM DetectBOM(const char *src, size_t srcLen);
  167. };