strconv.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: strconv.h
  3. // Purpose: interface of wxMBConvUTF7
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxMBConv
  9. This class is the base class of a hierarchy of classes capable of
  10. converting text strings between multibyte (SBCS or DBCS) encodings and
  11. Unicode.
  12. This is an abstract base class which defines the operations implemented by
  13. all different conversion classes. The derived classes don't add any new
  14. operations of their own (except, possibly, some non-default constructors)
  15. and so you should simply use this class ToWChar() and FromWChar() (or
  16. cMB2WC() and cWC2MB()) methods with the objects of the derived class.
  17. In the documentation for this and related classes please notice that
  18. length of the string refers to the number of characters in the string
  19. not counting the terminating @c NUL, if any. While the size of the string
  20. is the total number of bytes in the string, including any trailing @c NUL.
  21. Thus, length of wide character string @c L"foo" is 3 while its size can
  22. be either 8 or 16 depending on whether @c wchar_t is 2 bytes (as
  23. under Windows) or 4 (Unix).
  24. @library{wxbase}
  25. @category{conv}
  26. @see wxCSConv, wxEncodingConverter, @ref overview_mbconv
  27. */
  28. class wxMBConv
  29. {
  30. public:
  31. /**
  32. Trivial default constructor.
  33. */
  34. wxMBConv();
  35. /**
  36. This pure virtual function is overridden in each of the derived classes
  37. to return a new copy of the object it is called on.
  38. It is used for copying the conversion objects while preserving their
  39. dynamic type.
  40. */
  41. virtual wxMBConv* Clone() const = 0;
  42. /**
  43. This function returns 1 for most of the multibyte encodings in which the
  44. string is terminated by a single @c NUL, 2 for UTF-16 and 4 for UTF-32 for
  45. which the string is terminated with 2 and 4 @c NUL characters respectively.
  46. The other cases are not currently supported and @c wxCONV_FAILED
  47. (defined as -1) is returned for them.
  48. */
  49. virtual size_t GetMBNulLen() const;
  50. /**
  51. Returns the maximal value which can be returned by GetMBNulLen() for
  52. any conversion object.
  53. Currently this value is 4.
  54. This method can be used to allocate the buffer with enough space for the
  55. trailing @c NUL characters for any encoding.
  56. */
  57. static size_t GetMaxMBNulLen();
  58. /**
  59. Convert multibyte string to a wide character one.
  60. This is the most general function for converting a multibyte string to
  61. a wide string, cMB2WC() may be often more convenient, however this
  62. function is the most efficient one as it allows to avoid any
  63. unnecessary copying.
  64. The main case is when @a dst is not @NULL and @a srcLen is not
  65. @c wxNO_LEN (which is defined as @c (size_t)-1): then the function
  66. converts exactly @a srcLen bytes starting at @a src into wide string
  67. which it output to @e dst. If the length of the resulting wide
  68. string is greater than @e dstLen, an error is returned. Note that if
  69. @a srcLen bytes don't include @c NUL characters, the resulting wide
  70. string is not @c NUL-terminated neither.
  71. If @a srcLen is @c wxNO_LEN, the function supposes that the string is
  72. properly (i.e. as necessary for the encoding handled by this
  73. conversion) @c NUL-terminated and converts the entire string, including
  74. any trailing @c NUL bytes. In this case the wide string is also @c
  75. NUL-terminated.
  76. Finally, if @a dst is @NULL, the function returns the length of the
  77. needed buffer.
  78. Example of use of this function:
  79. @code
  80. size_t dstLen = conv.ToWChar(NULL, 0, src);
  81. if ( dstLen == wxCONV_FAILED )
  82. ... handle error ...
  83. wchar_t *dst = new wchar_t[dstLen];
  84. if ( conv.ToWChar(dst, dstLen, src) == wxCONV_FAILED )
  85. ... handle error ...
  86. @endcode
  87. Notice that when passing the explicit source length the output will
  88. @e not be @c NUL terminated if you pass @c strlen(str) as parameter.
  89. Either leave @a srcLen as default @c wxNO_LEN or add one to @c strlen
  90. result if you want the output to be @c NUL terminated.
  91. @param dst
  92. Pointer to output buffer of the size of at least @a dstLen or @NULL.
  93. @param dstLen
  94. Maximal number of characters to be written to the output buffer if
  95. @a dst is non-@NULL, unused otherwise.
  96. @param src
  97. Point to the source string, must not be @NULL.
  98. @param srcLen
  99. The number of characters of the source string to convert or
  100. @c wxNO_LEN (default parameter) to convert everything up to and
  101. including the terminating @c NUL character(s).
  102. @return
  103. The number of character written (or which would have been written
  104. if it were non-@NULL) to @a dst or @c wxCONV_FAILED on error.
  105. */
  106. virtual size_t ToWChar(wchar_t* dst, size_t dstLen, const char* src,
  107. size_t srcLen = wxNO_LEN) const;
  108. /**
  109. Converts wide character string to multibyte.
  110. This function has the same semantics as ToWChar() except that it
  111. converts a wide string to multibyte one. As with ToWChar(), it may be
  112. more convenient to use cWC2MB() when working with @c NUL terminated
  113. strings.
  114. @param dst
  115. Pointer to output buffer of the size of at least @a dstLen or @NULL.
  116. @param dstLen
  117. Maximal number of characters to be written to the output buffer if
  118. @a dst is non-@NULL, unused otherwise.
  119. @param src
  120. Point to the source string, must not be @NULL.
  121. @param srcLen
  122. The number of characters of the source string to convert or
  123. @c wxNO_LEN (default parameter) to convert everything up to and
  124. including the terminating @c NUL character.
  125. @return
  126. The number of character written (or which would have been written
  127. if it were non-@NULL) to @a dst or @c wxCONV_FAILED on error.
  128. */
  129. virtual size_t FromWChar(char* dst, size_t dstLen, const wchar_t* src,
  130. size_t srcLen = wxNO_LEN) const;
  131. /**
  132. Converts from multibyte encoding to Unicode by calling ToWChar() and
  133. allocating a temporary wxWCharBuffer to hold the result.
  134. This function is a convenient wrapper around ToWChar() as it takes care
  135. of allocating the buffer of the necessary size itself. Its parameters
  136. have the same meaning as for ToWChar(), in particular @a inLen can be
  137. specified explicitly in which case exactly that many characters are
  138. converted and @a outLen receives (if non-@NULL) exactly the
  139. corresponding number of wide characters, whether the last one of them
  140. is @c NUL or not. However if @c inLen is @c wxNO_LEN, then @c outLen
  141. doesn't count the trailing @c NUL even if it is always present in this
  142. case.
  143. Finally notice that if the conversion fails, the returned buffer is
  144. invalid and @a outLen is set to 0 (and not @c wxCONV_FAILED for
  145. compatibility concerns).
  146. */
  147. const wxWCharBuffer cMB2WC(const char* in,
  148. size_t inLen,
  149. size_t *outLen) const;
  150. /**
  151. Converts a char buffer to wide char one.
  152. This is the most convenient and safest conversion function as you
  153. don't have to deal with the buffer lengths directly. Use it if the
  154. input buffer is known not to be empty or if you are sure that the
  155. conversion is going to succeed -- otherwise, use the overload above to
  156. be able to distinguish between empty input and conversion failure.
  157. @return
  158. The buffer containing the converted text, empty if the input was
  159. empty or if the conversion failed.
  160. @since 2.9.1
  161. */
  162. const wxWCharBuffer cMB2WC(const wxCharBuffer& buf) const;
  163. //@{
  164. /**
  165. Converts from multibyte encoding to the current wxChar type (which
  166. depends on whether wxUSE_UNICODE is set to 1).
  167. If wxChar is char, it returns the parameter unaltered. If wxChar is
  168. wchar_t, it returns the result in a wxWCharBuffer. The macro wxMB2WXbuf
  169. is defined as the correct return type (without const).
  170. */
  171. const char* cMB2WX(const char* psz) const;
  172. const wxWCharBuffer cMB2WX(const char* psz) const;
  173. //@}
  174. /**
  175. Converts from Unicode to multibyte encoding by calling FromWChar() and
  176. allocating a temporary wxCharBuffer to hold the result.
  177. This function is a convenient wrapper around FromWChar() as it takes
  178. care of allocating the buffer of necessary size itself.
  179. Its parameters have the same meaning as the corresponding parameters of
  180. FromWChar(), please see the description of cMB2WC() for more details.
  181. */
  182. const wxCharBuffer cWC2MB(const wchar_t* in,
  183. size_t inLen,
  184. size_t *outLen) const;
  185. /**
  186. Converts a wide char buffer to char one.
  187. This is the most convenient and safest conversion function as you
  188. don't have to deal with the buffer lengths directly. Use it if the
  189. input buffer is known not to be empty or if you are sure that the
  190. conversion is going to succeed -- otherwise, use the overload above to
  191. be able to distinguish between empty input and conversion failure.
  192. @return
  193. The buffer containing the converted text, empty if the input was
  194. empty or if the conversion failed.
  195. @since 2.9.1
  196. */
  197. const wxCharBuffer cWC2MB(const wxWCharBuffer& buf) const;
  198. //@{
  199. /**
  200. Converts from Unicode to the current wxChar type.
  201. If wxChar is wchar_t, it returns the parameter unaltered. If wxChar is
  202. char, it returns the result in a wxCharBuffer. The macro wxWC2WXbuf is
  203. defined as the correct return type (without const).
  204. */
  205. const wchar_t* cWC2WX(const wchar_t* psz) const;
  206. const wxCharBuffer cWC2WX(const wchar_t* psz) const;
  207. //@}
  208. //@{
  209. /**
  210. Converts from the current wxChar type to multibyte encoding.
  211. If wxChar is char, it returns the parameter unaltered. If wxChar is
  212. wchar_t, it returns the result in a wxCharBuffer. The macro wxWX2MBbuf
  213. is defined as the correct return type (without const).
  214. */
  215. const char* cWX2MB(const wxChar* psz) const;
  216. const wxCharBuffer cWX2MB(const wxChar* psz) const;
  217. //@}
  218. //@{
  219. /**
  220. Converts from the current wxChar type to Unicode.
  221. If wxChar is wchar_t, it returns the parameter unaltered. If wxChar is
  222. char, it returns the result in a wxWCharBuffer. The macro wxWX2WCbuf is
  223. defined as the correct return type (without const).
  224. */
  225. const wchar_t* cWX2WC(const wxChar* psz) const;
  226. const wxWCharBuffer cWX2WC(const wxChar* psz) const;
  227. //@}
  228. /**
  229. @deprecated This function is deprecated, please use ToWChar() instead.
  230. Converts from a string @a in multibyte encoding to Unicode putting up to
  231. @a outLen characters into the buffer @e out.
  232. If @a out is @NULL, only the length of the string which would result
  233. from the conversion is calculated and returned. Note that this is the
  234. length and not size, i.e. the returned value does not include the
  235. trailing @c NUL. But when the function is called with a non-@NULL @a
  236. out buffer, the @a outLen parameter should be one more to allow to
  237. properly @c NUL-terminate the string.
  238. So to properly use this function you need to write:
  239. @code
  240. size_t lenConv = conv.MB2WC(NULL, in, 0);
  241. if ( lenConv == wxCONV_FAILED )
  242. ... handle error ...
  243. // allocate 1 more character for the trailing NUL and also pass
  244. // the size of the buffer to the function now
  245. wchar_t *out = new wchar_t[lenConv + 1];
  246. if ( conv.MB2WC(out, in, lenConv + 1) == wxCONV_FAILED )
  247. ... handle error ...
  248. @endcode
  249. For this and other reasons, ToWChar() is strongly recommended as a
  250. replacement.
  251. @param out
  252. The output buffer, may be @NULL if the caller is only
  253. interested in the length of the resulting string
  254. @param in
  255. The NUL-terminated input string, cannot be @NULL
  256. @param outLen
  257. The length of the output buffer but including
  258. NUL, ignored if out is @NULL
  259. @return The length of the converted string excluding the trailing NUL.
  260. */
  261. virtual size_t MB2WC(wchar_t* out, const char* in, size_t outLen) const;
  262. /**
  263. @deprecated This function is deprecated, please use FromWChar() instead.
  264. Converts from Unicode to multibyte encoding.
  265. The semantics of this function (including the return value meaning) is
  266. the same as for wxMBConv::MB2WC. Notice that when the function is
  267. called with a non-@NULL buffer, the @a n parameter should be the size
  268. of the buffer and so it should take into account the trailing @c NUL,
  269. which might take two or four bytes for some encodings (UTF-16 and
  270. UTF-32) and not one, i.e. GetMBNulLen().
  271. */
  272. virtual size_t WC2MB(char* buf, const wchar_t* psz, size_t n) const;
  273. };
  274. /**
  275. @class wxMBConvUTF7
  276. This class converts between the UTF-7 encoding and Unicode.
  277. It has one predefined instance, @b wxConvUTF7.
  278. Notice that, unlike all the other conversion objects, this converter is
  279. stateful, i.e. it remembers its state from the last call to its ToWChar()
  280. or FromWChar() and assumes it is called on the continuation of the same
  281. string when the same method is called again. This assumption is only made
  282. if an explicit length is specified as parameter to these functions as if an
  283. entire @c NUL terminated string is processed the state doesn't need to be
  284. remembered.
  285. This also means that, unlike the other predefined conversion objects,
  286. @b wxConvUTF7 is @em not thread-safe.
  287. @library{wxbase}
  288. @category{conv}
  289. @see wxMBConvUTF8, @ref overview_mbconv
  290. */
  291. class wxMBConvUTF7 : public wxMBConv
  292. {
  293. };
  294. /**
  295. @class wxMBConvUTF8
  296. This class converts between the UTF-8 encoding and Unicode.
  297. It has one predefined instance, @b wxConvUTF8.
  298. @library{wxbase}
  299. @category{conv}
  300. @see wxMBConvUTF7, @ref overview_mbconv
  301. */
  302. class wxMBConvUTF8 : public wxMBConv
  303. {
  304. };
  305. /**
  306. @class wxMBConvUTF16
  307. This class is used to convert between multibyte encodings and UTF-16 Unicode
  308. encoding (also known as UCS-2).
  309. Unlike UTF-8 encoding, UTF-16 uses words and not bytes and hence depends
  310. on the byte ordering: big or little endian. Hence this class is provided in
  311. two versions: wxMBConvUTF16LE and wxMBConvUTF16BE and wxMBConvUTF16 itself
  312. is just a typedef for one of them (native for the given platform, e.g. LE
  313. under Windows and BE under Mac).
  314. @library{wxbase}
  315. @category{conv}
  316. @see wxMBConvUTF8, wxMBConvUTF32, @ref overview_mbconv
  317. */
  318. class wxMBConvUTF16 : public wxMBConv
  319. {
  320. };
  321. /**
  322. @class wxMBConvUTF32
  323. This class is used to convert between multibyte encodings and UTF-32
  324. Unicode encoding (also known as UCS-4).
  325. Unlike UTF-8 encoding, UTF-32 uses (double) words and not bytes and hence
  326. depends on the byte ordering: big or little endian. Hence this class is
  327. provided in two versions: wxMBConvUTF32LE and wxMBConvUTF32BE and
  328. wxMBConvUTF32 itself is just a typedef for one of them (native for the
  329. given platform, e.g. LE under Windows and BE under Mac).
  330. @library{wxbase}
  331. @category{conv}
  332. @see wxMBConvUTF8, wxMBConvUTF16, @ref overview_mbconv
  333. */
  334. class wxMBConvUTF32 : public wxMBConv
  335. {
  336. };
  337. /**
  338. @class wxCSConv
  339. This class converts between any character set supported by the system and
  340. Unicode.
  341. Please notice that this class uses system-provided conversion functions,
  342. e.g. @c MultiByteToWideChar() and @c WideCharToMultiByte() under MSW and @c
  343. iconv(3) under Unix systems and as such may support different encodings and
  344. different encoding names on different platforms (although all relatively
  345. common encodings are supported should be supported everywhere).
  346. It has one predefined instance, @b wxConvLocal, for the default user
  347. character set.
  348. @library{wxbase}
  349. @category{conv}
  350. @see wxMBConv, wxEncodingConverter, @ref overview_mbconv
  351. */
  352. class wxCSConv : public wxMBConv
  353. {
  354. public:
  355. /**
  356. Constructor.
  357. You can specify the name of the character set you want to convert
  358. from/to. If the character set name is not recognized, ISO 8859-1 is
  359. used as fall back, use IsOk() to test for this.
  360. @param charset The name of the encoding, shouldn't be empty.
  361. */
  362. wxCSConv(const wxString& charset);
  363. /**
  364. Constructor.
  365. You can specify an encoding constant for the character set you want to
  366. convert from/to. Use IsOk() after construction to check whether the
  367. encoding is supported by the current system.
  368. @param encoding Any valid (i.e. not wxFONTENCODING_MAX) font encoding.
  369. */
  370. wxCSConv(wxFontEncoding encoding);
  371. /**
  372. Returns @true if the charset (or the encoding) given at constructor is
  373. really available to use.
  374. Returns @false if ISO 8859-1 will be used instead.
  375. Note this does not mean that a given string will be correctly
  376. converted. A malformed string may still make conversion functions
  377. return @c wxCONV_FAILED.
  378. @since 2.8.2
  379. */
  380. bool IsOk() const;
  381. };
  382. /**
  383. Conversion object used for converting file names from their external
  384. representation to the one used inside the program.
  385. @b wxConvFileName converts filenames between filesystem multibyte encoding
  386. and Unicode. @b wxConvFileName can also be set to a something else at
  387. run-time which is used e.g. by wxGTK to use an object which checks the
  388. environment variable @b G_FILESYSTEM_ENCODING indicating that filenames
  389. should not be interpreted as UTF8 and also for converting invalid UTF8
  390. characters (e.g. if there is a filename in iso8859_1) to strings with octal
  391. values.
  392. Since some platforms (such as Win32) use Unicode in the filenames,
  393. and others (such as Unix) use multibyte encodings, this object should only
  394. be used directly if wxMBFILES is defined to 1. A convenience macro,
  395. @c wxFNCONV, is defined to @c wxConvFileName->cWX2MB in this case. You
  396. could use it like this:
  397. @code
  398. wxChar *name = "rawfile.doc";
  399. FILE *fil = fopen(wxFNCONV(name), "r");
  400. @endcode
  401. (although it would be better to just use wxFopen(name, "r") in this
  402. particular case, you only need to use this object for functions taking file
  403. names not wrapped by wxWidgets.)
  404. @library{wxbase}
  405. @category{conv}
  406. @see @ref overview_mbconv
  407. */
  408. extern wxMBConv* wxConvFileName;