strconv.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/strconv.h
  3. // Purpose: conversion routines for char sets any Unicode
  4. // Author: Ove Kaaven, Robert Roebling, Vadim Zeitlin
  5. // Modified by:
  6. // Created: 29/01/98
  7. // Copyright: (c) 1998 Ove Kaaven, Robert Roebling
  8. // (c) 1998-2006 Vadim Zeitlin
  9. // Licence: wxWindows licence
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #ifndef _WX_STRCONV_H_
  12. #define _WX_STRCONV_H_
  13. #include "wx/defs.h"
  14. #include "wx/chartype.h"
  15. #include "wx/buffer.h"
  16. #ifdef __DIGITALMARS__
  17. #include "typeinfo.h"
  18. #endif
  19. #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
  20. # undef __BSEXCPT__
  21. #endif
  22. #include <stdlib.h>
  23. class WXDLLIMPEXP_FWD_BASE wxString;
  24. // the error value returned by wxMBConv methods
  25. #define wxCONV_FAILED ((size_t)-1)
  26. // ----------------------------------------------------------------------------
  27. // wxMBConv (abstract base class for conversions)
  28. // ----------------------------------------------------------------------------
  29. // When deriving a new class from wxMBConv you must reimplement ToWChar() and
  30. // FromWChar() methods which are not pure virtual only for historical reasons,
  31. // don't let the fact that the existing classes implement MB2WC/WC2MB() instead
  32. // confuse you.
  33. //
  34. // You also have to implement Clone() to allow copying the conversions
  35. // polymorphically.
  36. //
  37. // And you might need to override GetMBNulLen() as well.
  38. class WXDLLIMPEXP_BASE wxMBConv
  39. {
  40. public:
  41. // The functions doing actual conversion from/to narrow to/from wide
  42. // character strings.
  43. //
  44. // On success, the return value is the length (i.e. the number of
  45. // characters, not bytes) of the converted string including any trailing
  46. // L'\0' or (possibly multiple) '\0'(s). If the conversion fails or if
  47. // there is not enough space for everything, including the trailing NUL
  48. // character(s), in the output buffer, wxCONV_FAILED is returned.
  49. //
  50. // In the special case when dst is NULL (the value of dstLen is ignored
  51. // then) the return value is the length of the needed buffer but nothing
  52. // happens otherwise. If srcLen is wxNO_LEN, the entire string, up to and
  53. // including the trailing NUL(s), is converted, otherwise exactly srcLen
  54. // bytes are.
  55. //
  56. // Typical usage:
  57. //
  58. // size_t dstLen = conv.ToWChar(NULL, 0, src);
  59. // if ( dstLen == wxCONV_FAILED )
  60. // ... handle error ...
  61. // wchar_t *wbuf = new wchar_t[dstLen];
  62. // conv.ToWChar(wbuf, dstLen, src);
  63. // ... work with wbuf ...
  64. // delete [] wbuf;
  65. //
  66. virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
  67. const char *src, size_t srcLen = wxNO_LEN) const;
  68. virtual size_t FromWChar(char *dst, size_t dstLen,
  69. const wchar_t *src, size_t srcLen = wxNO_LEN) const;
  70. // Convenience functions for translating NUL-terminated strings: returns
  71. // the buffer containing the converted string or NULL pointer if the
  72. // conversion failed.
  73. const wxWCharBuffer cMB2WC(const char *in) const;
  74. const wxCharBuffer cWC2MB(const wchar_t *in) const;
  75. // Convenience functions for converting strings which may contain embedded
  76. // NULs and don't have to be NUL-terminated.
  77. //
  78. // inLen is the length of the buffer including trailing NUL if any or
  79. // wxNO_LEN if the input is NUL-terminated.
  80. //
  81. // outLen receives, if not NULL, the length of the converted string or 0 if
  82. // the conversion failed (returning 0 and not -1 in this case makes it
  83. // difficult to distinguish between failed conversion and empty input but
  84. // this is done for backwards compatibility). Notice that the rules for
  85. // whether outLen accounts or not for the last NUL are the same as for
  86. // To/FromWChar() above: if inLen is specified, outLen is exactly the
  87. // number of characters converted, whether the last one of them was NUL or
  88. // not. But if inLen == wxNO_LEN then outLen doesn't account for the last
  89. // NUL even though it is present.
  90. const wxWCharBuffer
  91. cMB2WC(const char *in, size_t inLen, size_t *outLen) const;
  92. const wxCharBuffer
  93. cWC2MB(const wchar_t *in, size_t inLen, size_t *outLen) const;
  94. // And yet more convenience functions for converting the entire buffers:
  95. // these are the simplest and least error-prone as you never need to bother
  96. // with lengths/sizes directly.
  97. const wxWCharBuffer cMB2WC(const wxScopedCharBuffer& in) const;
  98. const wxCharBuffer cWC2MB(const wxScopedWCharBuffer& in) const;
  99. // convenience functions for converting MB or WC to/from wxWin default
  100. #if wxUSE_UNICODE
  101. const wxWCharBuffer cMB2WX(const char *psz) const { return cMB2WC(psz); }
  102. const wxCharBuffer cWX2MB(const wchar_t *psz) const { return cWC2MB(psz); }
  103. const wchar_t* cWC2WX(const wchar_t *psz) const { return psz; }
  104. const wchar_t* cWX2WC(const wchar_t *psz) const { return psz; }
  105. #else // ANSI
  106. const char* cMB2WX(const char *psz) const { return psz; }
  107. const char* cWX2MB(const char *psz) const { return psz; }
  108. const wxCharBuffer cWC2WX(const wchar_t *psz) const { return cWC2MB(psz); }
  109. const wxWCharBuffer cWX2WC(const char *psz) const { return cMB2WC(psz); }
  110. #endif // Unicode/ANSI
  111. // this function is used in the implementation of cMB2WC() to distinguish
  112. // between the following cases:
  113. //
  114. // a) var width encoding with strings terminated by a single NUL
  115. // (usual multibyte encodings): return 1 in this case
  116. // b) fixed width encoding with 2 bytes/char and so terminated by
  117. // 2 NULs (UTF-16/UCS-2 and variants): return 2 in this case
  118. // c) fixed width encoding with 4 bytes/char and so terminated by
  119. // 4 NULs (UTF-32/UCS-4 and variants): return 4 in this case
  120. //
  121. // anything else is not supported currently and -1 should be returned
  122. virtual size_t GetMBNulLen() const { return 1; }
  123. // return the maximal value currently returned by GetMBNulLen() for any
  124. // encoding
  125. static size_t GetMaxMBNulLen() { return 4 /* for UTF-32 */; }
  126. #if wxUSE_UNICODE_UTF8
  127. // return true if the converter's charset is UTF-8, i.e. char* strings
  128. // decoded using this object can be directly copied to wxString's internal
  129. // storage without converting to WC and than back to UTF-8 MB string
  130. virtual bool IsUTF8() const { return false; }
  131. #endif
  132. // The old conversion functions. The existing classes currently mostly
  133. // implement these ones but we're in transition to using To/FromWChar()
  134. // instead and any new classes should implement just the new functions.
  135. // For now, however, we provide default implementation of To/FromWChar() in
  136. // this base class in terms of MB2WC/WC2MB() to avoid having to rewrite all
  137. // the conversions at once.
  138. //
  139. // On success, the return value is the length (i.e. the number of
  140. // characters, not bytes) not counting the trailing NUL(s) of the converted
  141. // string. On failure, (size_t)-1 is returned. In the special case when
  142. // outputBuf is NULL the return value is the same one but nothing is
  143. // written to the buffer.
  144. //
  145. // Note that outLen is the length of the output buffer, not the length of
  146. // the input (which is always supposed to be terminated by one or more
  147. // NULs, as appropriate for the encoding)!
  148. virtual size_t MB2WC(wchar_t *out, const char *in, size_t outLen) const;
  149. virtual size_t WC2MB(char *out, const wchar_t *in, size_t outLen) const;
  150. // make a heap-allocated copy of this object
  151. virtual wxMBConv *Clone() const = 0;
  152. // virtual dtor for any base class
  153. virtual ~wxMBConv();
  154. };
  155. // ----------------------------------------------------------------------------
  156. // wxMBConvLibc uses standard mbstowcs() and wcstombs() functions for
  157. // conversion (hence it depends on the current locale)
  158. // ----------------------------------------------------------------------------
  159. class WXDLLIMPEXP_BASE wxMBConvLibc : public wxMBConv
  160. {
  161. public:
  162. virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
  163. virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
  164. virtual wxMBConv *Clone() const { return new wxMBConvLibc; }
  165. #if wxUSE_UNICODE_UTF8
  166. virtual bool IsUTF8() const { return wxLocaleIsUtf8; }
  167. #endif
  168. };
  169. #ifdef __UNIX__
  170. // ----------------------------------------------------------------------------
  171. // wxConvBrokenFileNames is made for Unix in Unicode mode when
  172. // files are accidentally written in an encoding which is not
  173. // the system encoding. Typically, the system encoding will be
  174. // UTF8 but there might be files stored in ISO8859-1 on disk.
  175. // ----------------------------------------------------------------------------
  176. class WXDLLIMPEXP_BASE wxConvBrokenFileNames : public wxMBConv
  177. {
  178. public:
  179. wxConvBrokenFileNames(const wxString& charset);
  180. wxConvBrokenFileNames(const wxConvBrokenFileNames& conv)
  181. : wxMBConv(),
  182. m_conv(conv.m_conv ? conv.m_conv->Clone() : NULL)
  183. {
  184. }
  185. virtual ~wxConvBrokenFileNames() { delete m_conv; }
  186. virtual size_t MB2WC(wchar_t *out, const char *in, size_t outLen) const
  187. {
  188. return m_conv->MB2WC(out, in, outLen);
  189. }
  190. virtual size_t WC2MB(char *out, const wchar_t *in, size_t outLen) const
  191. {
  192. return m_conv->WC2MB(out, in, outLen);
  193. }
  194. virtual size_t GetMBNulLen() const
  195. {
  196. // cast needed to call a private function
  197. return m_conv->GetMBNulLen();
  198. }
  199. #if wxUSE_UNICODE_UTF8
  200. virtual bool IsUTF8() const { return m_conv->IsUTF8(); }
  201. #endif
  202. virtual wxMBConv *Clone() const { return new wxConvBrokenFileNames(*this); }
  203. private:
  204. // the conversion object we forward to
  205. wxMBConv *m_conv;
  206. wxDECLARE_NO_ASSIGN_CLASS(wxConvBrokenFileNames);
  207. };
  208. #endif // __UNIX__
  209. // ----------------------------------------------------------------------------
  210. // wxMBConvUTF7 (for conversion using UTF7 encoding)
  211. // ----------------------------------------------------------------------------
  212. class WXDLLIMPEXP_BASE wxMBConvUTF7 : public wxMBConv
  213. {
  214. public:
  215. wxMBConvUTF7() { }
  216. // compiler-generated copy ctor, assignment operator and dtor are ok
  217. // (assuming it's ok to copy the shift state -- not really sure about it)
  218. virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
  219. const char *src, size_t srcLen = wxNO_LEN) const;
  220. virtual size_t FromWChar(char *dst, size_t dstLen,
  221. const wchar_t *src, size_t srcLen = wxNO_LEN) const;
  222. virtual wxMBConv *Clone() const { return new wxMBConvUTF7; }
  223. private:
  224. // UTF-7 decoder/encoder may be in direct mode or in shifted mode after a
  225. // '+' (and until the '-' or any other non-base64 character)
  226. struct StateMode
  227. {
  228. enum Mode
  229. {
  230. Direct, // pass through state
  231. Shifted // after a '+' (and before '-')
  232. };
  233. };
  234. // the current decoder state: this is only used by ToWChar() if srcLen
  235. // parameter is not wxNO_LEN, when working on the entire NUL-terminated
  236. // strings we neither update nor use the state
  237. class DecoderState : private StateMode
  238. {
  239. private:
  240. // current state: this one is private as we want to enforce the use of
  241. // ToDirect/ToShifted() methods below
  242. Mode mode;
  243. public:
  244. // the initial state is direct
  245. DecoderState() { mode = Direct; }
  246. // switch to/from shifted mode
  247. void ToDirect() { mode = Direct; }
  248. void ToShifted() { mode = Shifted; accum = bit = 0; isLSB = false; }
  249. bool IsDirect() const { return mode == Direct; }
  250. bool IsShifted() const { return mode == Shifted; }
  251. // these variables are only used in shifted mode
  252. unsigned int accum; // accumulator of the bit we've already got
  253. unsigned int bit; // the number of bits consumed mod 8
  254. unsigned char msb; // the high byte of UTF-16 word
  255. bool isLSB; // whether we're decoding LSB or MSB of UTF-16 word
  256. };
  257. DecoderState m_stateDecoder;
  258. // encoder state is simpler as we always receive entire Unicode characters
  259. // on input
  260. class EncoderState : private StateMode
  261. {
  262. private:
  263. Mode mode;
  264. public:
  265. EncoderState() { mode = Direct; }
  266. void ToDirect() { mode = Direct; }
  267. void ToShifted() { mode = Shifted; accum = bit = 0; }
  268. bool IsDirect() const { return mode == Direct; }
  269. bool IsShifted() const { return mode == Shifted; }
  270. unsigned int accum;
  271. unsigned int bit;
  272. };
  273. EncoderState m_stateEncoder;
  274. };
  275. // ----------------------------------------------------------------------------
  276. // wxMBConvUTF8 (for conversion using UTF8 encoding)
  277. // ----------------------------------------------------------------------------
  278. // this is the real UTF-8 conversion class, it has to be called "strict UTF-8"
  279. // for compatibility reasons: the wxMBConvUTF8 class below also supports lossy
  280. // conversions if it is created with non default options
  281. class WXDLLIMPEXP_BASE wxMBConvStrictUTF8 : public wxMBConv
  282. {
  283. public:
  284. // compiler-generated default ctor and other methods are ok
  285. virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
  286. const char *src, size_t srcLen = wxNO_LEN) const;
  287. virtual size_t FromWChar(char *dst, size_t dstLen,
  288. const wchar_t *src, size_t srcLen = wxNO_LEN) const;
  289. virtual wxMBConv *Clone() const { return new wxMBConvStrictUTF8(); }
  290. #if wxUSE_UNICODE_UTF8
  291. // NB: other mapping modes are not, strictly speaking, UTF-8, so we can't
  292. // take the shortcut in that case
  293. virtual bool IsUTF8() const { return true; }
  294. #endif
  295. };
  296. class WXDLLIMPEXP_BASE wxMBConvUTF8 : public wxMBConvStrictUTF8
  297. {
  298. public:
  299. enum
  300. {
  301. MAP_INVALID_UTF8_NOT = 0,
  302. MAP_INVALID_UTF8_TO_PUA = 1,
  303. MAP_INVALID_UTF8_TO_OCTAL = 2
  304. };
  305. wxMBConvUTF8(int options = MAP_INVALID_UTF8_NOT) : m_options(options) { }
  306. virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
  307. const char *src, size_t srcLen = wxNO_LEN) const;
  308. virtual size_t FromWChar(char *dst, size_t dstLen,
  309. const wchar_t *src, size_t srcLen = wxNO_LEN) const;
  310. virtual wxMBConv *Clone() const { return new wxMBConvUTF8(m_options); }
  311. #if wxUSE_UNICODE_UTF8
  312. // NB: other mapping modes are not, strictly speaking, UTF-8, so we can't
  313. // take the shortcut in that case
  314. virtual bool IsUTF8() const { return m_options == MAP_INVALID_UTF8_NOT; }
  315. #endif
  316. private:
  317. int m_options;
  318. };
  319. // ----------------------------------------------------------------------------
  320. // wxMBConvUTF16Base: for both LE and BE variants
  321. // ----------------------------------------------------------------------------
  322. class WXDLLIMPEXP_BASE wxMBConvUTF16Base : public wxMBConv
  323. {
  324. public:
  325. enum { BYTES_PER_CHAR = 2 };
  326. virtual size_t GetMBNulLen() const { return BYTES_PER_CHAR; }
  327. protected:
  328. // return the length of the buffer using srcLen if it's not wxNO_LEN and
  329. // computing the length ourselves if it is; also checks that the length is
  330. // even if specified as we need an entire number of UTF-16 characters and
  331. // returns wxNO_LEN which indicates error if it is odd
  332. static size_t GetLength(const char *src, size_t srcLen);
  333. };
  334. // ----------------------------------------------------------------------------
  335. // wxMBConvUTF16LE (for conversion using UTF16 Little Endian encoding)
  336. // ----------------------------------------------------------------------------
  337. class WXDLLIMPEXP_BASE wxMBConvUTF16LE : public wxMBConvUTF16Base
  338. {
  339. public:
  340. virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
  341. const char *src, size_t srcLen = wxNO_LEN) const;
  342. virtual size_t FromWChar(char *dst, size_t dstLen,
  343. const wchar_t *src, size_t srcLen = wxNO_LEN) const;
  344. virtual wxMBConv *Clone() const { return new wxMBConvUTF16LE; }
  345. };
  346. // ----------------------------------------------------------------------------
  347. // wxMBConvUTF16BE (for conversion using UTF16 Big Endian encoding)
  348. // ----------------------------------------------------------------------------
  349. class WXDLLIMPEXP_BASE wxMBConvUTF16BE : public wxMBConvUTF16Base
  350. {
  351. public:
  352. virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
  353. const char *src, size_t srcLen = wxNO_LEN) const;
  354. virtual size_t FromWChar(char *dst, size_t dstLen,
  355. const wchar_t *src, size_t srcLen = wxNO_LEN) const;
  356. virtual wxMBConv *Clone() const { return new wxMBConvUTF16BE; }
  357. };
  358. // ----------------------------------------------------------------------------
  359. // wxMBConvUTF32Base: base class for both LE and BE variants
  360. // ----------------------------------------------------------------------------
  361. class WXDLLIMPEXP_BASE wxMBConvUTF32Base : public wxMBConv
  362. {
  363. public:
  364. enum { BYTES_PER_CHAR = 4 };
  365. virtual size_t GetMBNulLen() const { return BYTES_PER_CHAR; }
  366. protected:
  367. // this is similar to wxMBConvUTF16Base method with the same name except
  368. // that, of course, it verifies that length is divisible by 4 if given and
  369. // not by 2
  370. static size_t GetLength(const char *src, size_t srcLen);
  371. };
  372. // ----------------------------------------------------------------------------
  373. // wxMBConvUTF32LE (for conversion using UTF32 Little Endian encoding)
  374. // ----------------------------------------------------------------------------
  375. class WXDLLIMPEXP_BASE wxMBConvUTF32LE : public wxMBConvUTF32Base
  376. {
  377. public:
  378. virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
  379. const char *src, size_t srcLen = wxNO_LEN) const;
  380. virtual size_t FromWChar(char *dst, size_t dstLen,
  381. const wchar_t *src, size_t srcLen = wxNO_LEN) const;
  382. virtual wxMBConv *Clone() const { return new wxMBConvUTF32LE; }
  383. };
  384. // ----------------------------------------------------------------------------
  385. // wxMBConvUTF32BE (for conversion using UTF32 Big Endian encoding)
  386. // ----------------------------------------------------------------------------
  387. class WXDLLIMPEXP_BASE wxMBConvUTF32BE : public wxMBConvUTF32Base
  388. {
  389. public:
  390. virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
  391. const char *src, size_t srcLen = wxNO_LEN) const;
  392. virtual size_t FromWChar(char *dst, size_t dstLen,
  393. const wchar_t *src, size_t srcLen = wxNO_LEN) const;
  394. virtual wxMBConv *Clone() const { return new wxMBConvUTF32BE; }
  395. };
  396. // ----------------------------------------------------------------------------
  397. // wxCSConv (for conversion based on loadable char sets)
  398. // ----------------------------------------------------------------------------
  399. #include "wx/fontenc.h"
  400. class WXDLLIMPEXP_BASE wxCSConv : public wxMBConv
  401. {
  402. public:
  403. // we can be created either from charset name or from an encoding constant
  404. // but we can't have both at once
  405. wxCSConv(const wxString& charset);
  406. wxCSConv(wxFontEncoding encoding);
  407. wxCSConv(const wxCSConv& conv);
  408. virtual ~wxCSConv();
  409. wxCSConv& operator=(const wxCSConv& conv);
  410. virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
  411. const char *src, size_t srcLen = wxNO_LEN) const;
  412. virtual size_t FromWChar(char *dst, size_t dstLen,
  413. const wchar_t *src, size_t srcLen = wxNO_LEN) const;
  414. virtual size_t GetMBNulLen() const;
  415. #if wxUSE_UNICODE_UTF8
  416. virtual bool IsUTF8() const;
  417. #endif
  418. virtual wxMBConv *Clone() const { return new wxCSConv(*this); }
  419. void Clear();
  420. // return true if the conversion could be initialized successfully
  421. bool IsOk() const;
  422. private:
  423. // common part of all ctors
  424. void Init();
  425. // Creates the conversion to use, called from all ctors to initialize
  426. // m_convReal.
  427. wxMBConv *DoCreate() const;
  428. // Set the name (may be only called when m_name == NULL), makes copy of
  429. // the charset string.
  430. void SetName(const char *charset);
  431. // Set m_encoding field respecting the rules below, i.e. making sure it has
  432. // a valid value if m_name == NULL (thus this should be always called after
  433. // SetName()).
  434. //
  435. // Input encoding may be valid or not.
  436. void SetEncoding(wxFontEncoding encoding);
  437. // The encoding we use is specified by the two fields below:
  438. //
  439. // 1. If m_name != NULL, m_encoding corresponds to it if it's one of
  440. // encodings we know about (i.e. member of wxFontEncoding) or is
  441. // wxFONTENCODING_SYSTEM otherwise.
  442. //
  443. // 2. If m_name == NULL, m_encoding is always valid, i.e. not one of
  444. // wxFONTENCODING_{SYSTEM,DEFAULT,MAX}.
  445. char *m_name;
  446. wxFontEncoding m_encoding;
  447. // The conversion object for our encoding or NULL if we failed to create it
  448. // in which case we fall back to hard-coded ISO8859-1 conversion.
  449. wxMBConv *m_convReal;
  450. };
  451. // ----------------------------------------------------------------------------
  452. // declare predefined conversion objects
  453. // ----------------------------------------------------------------------------
  454. // Note: this macro is an implementation detail (see the comment in
  455. // strconv.cpp). The wxGet_XXX() and wxGet_XXXPtr() functions shouldn't be
  456. // used by user code and neither should XXXPtr, use the wxConvXXX macro
  457. // instead.
  458. #define WX_DECLARE_GLOBAL_CONV(klass, name) \
  459. extern WXDLLIMPEXP_DATA_BASE(klass*) name##Ptr; \
  460. extern WXDLLIMPEXP_BASE klass* wxGet_##name##Ptr(); \
  461. inline klass& wxGet_##name() \
  462. { \
  463. if ( !name##Ptr ) \
  464. name##Ptr = wxGet_##name##Ptr(); \
  465. return *name##Ptr; \
  466. }
  467. // conversion to be used with all standard functions affected by locale, e.g.
  468. // strtol(), strftime(), ...
  469. WX_DECLARE_GLOBAL_CONV(wxMBConv, wxConvLibc)
  470. #define wxConvLibc wxGet_wxConvLibc()
  471. // conversion ISO-8859-1/UTF-7/UTF-8 <-> wchar_t
  472. WX_DECLARE_GLOBAL_CONV(wxCSConv, wxConvISO8859_1)
  473. #define wxConvISO8859_1 wxGet_wxConvISO8859_1()
  474. WX_DECLARE_GLOBAL_CONV(wxMBConvStrictUTF8, wxConvUTF8)
  475. #define wxConvUTF8 wxGet_wxConvUTF8()
  476. WX_DECLARE_GLOBAL_CONV(wxMBConvUTF7, wxConvUTF7)
  477. #define wxConvUTF7 wxGet_wxConvUTF7()
  478. // conversion used for the file names on the systems where they're not Unicode
  479. // (basically anything except Windows)
  480. //
  481. // this is used by all file functions, can be changed by the application
  482. //
  483. // by default UTF-8 under Mac OS X and wxConvLibc elsewhere (but it's not used
  484. // under Windows normally)
  485. extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvFileName;
  486. // backwards compatible define
  487. #define wxConvFile (*wxConvFileName)
  488. // the current conversion object, may be set to any conversion, is used by
  489. // default in a couple of places inside wx (initially same as wxConvLibc)
  490. extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent;
  491. // the conversion corresponding to the current locale
  492. WX_DECLARE_GLOBAL_CONV(wxCSConv, wxConvLocal)
  493. #define wxConvLocal wxGet_wxConvLocal()
  494. // the conversion corresponding to the encoding of the standard UI elements
  495. //
  496. // by default this is the same as wxConvLocal but may be changed if the program
  497. // needs to use a fixed encoding
  498. extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvUI;
  499. #undef WX_DECLARE_GLOBAL_CONV
  500. // ----------------------------------------------------------------------------
  501. // endianness-dependent conversions
  502. // ----------------------------------------------------------------------------
  503. #ifdef WORDS_BIGENDIAN
  504. typedef wxMBConvUTF16BE wxMBConvUTF16;
  505. typedef wxMBConvUTF32BE wxMBConvUTF32;
  506. #else
  507. typedef wxMBConvUTF16LE wxMBConvUTF16;
  508. typedef wxMBConvUTF32LE wxMBConvUTF32;
  509. #endif
  510. // ----------------------------------------------------------------------------
  511. // filename conversion macros
  512. // ----------------------------------------------------------------------------
  513. // filenames are multibyte on Unix and widechar on Windows
  514. #if wxMBFILES && wxUSE_UNICODE
  515. #define wxFNCONV(name) wxConvFileName->cWX2MB(name)
  516. #define wxFNSTRINGCAST wxMBSTRINGCAST
  517. #else
  518. #if defined( __WXOSX_OR_COCOA__ ) && wxMBFILES
  519. #define wxFNCONV(name) wxConvFileName->cWC2MB( wxConvLocal.cWX2WC(name) )
  520. #else
  521. #define wxFNCONV(name) name
  522. #endif
  523. #define wxFNSTRINGCAST WXSTRINGCAST
  524. #endif
  525. // ----------------------------------------------------------------------------
  526. // macros for the most common conversions
  527. // ----------------------------------------------------------------------------
  528. #if wxUSE_UNICODE
  529. #define wxConvertWX2MB(s) wxConvCurrent->cWX2MB(s)
  530. #define wxConvertMB2WX(s) wxConvCurrent->cMB2WX(s)
  531. // these functions should be used when the conversions really, really have
  532. // to succeed (usually because we pass their results to a standard C
  533. // function which would crash if we passed NULL to it), so these functions
  534. // always return a valid pointer if their argument is non-NULL
  535. // this function safety is achieved by trying wxConvLibc first, wxConvUTF8
  536. // next if it fails and, finally, wxConvISO8859_1 which always succeeds
  537. extern WXDLLIMPEXP_BASE wxWCharBuffer wxSafeConvertMB2WX(const char *s);
  538. // this function uses wxConvLibc and wxConvUTF8(MAP_INVALID_UTF8_TO_OCTAL)
  539. // if it fails
  540. extern WXDLLIMPEXP_BASE wxCharBuffer wxSafeConvertWX2MB(const wchar_t *ws);
  541. #else // ANSI
  542. // no conversions to do
  543. #define wxConvertWX2MB(s) (s)
  544. #define wxConvertMB2WX(s) (s)
  545. #define wxSafeConvertMB2WX(s) (s)
  546. #define wxSafeConvertWX2MB(s) (s)
  547. #endif // Unicode/ANSI
  548. #endif // _WX_STRCONV_H_