wxcrt.h 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/wxcrt.h
  3. // Purpose: Type-safe ANSI and Unicode builds compatible wrappers for
  4. // CRT functions
  5. // Author: Joel Farley, Ove Kaaven
  6. // Modified by: Vadim Zeitlin, Robert Roebling, Ron Lee, Vaclav Slavik
  7. // Created: 1998/06/12
  8. // Copyright: (c) 1998-2006 wxWidgets dev team
  9. // Licence: wxWindows licence
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #ifndef _WX_WXCRT_H_
  12. #define _WX_WXCRT_H_
  13. #include "wx/wxcrtbase.h"
  14. #include "wx/string.h"
  15. #ifndef __WX_SETUP_H__
  16. // For non-configure builds assume vsscanf is available, if not Visual C or DMC
  17. #if !defined (__VISUALC__) && !defined (__DMC__)
  18. #define HAVE_VSSCANF 1
  19. #endif
  20. #endif
  21. // ============================================================================
  22. // misc functions
  23. // ============================================================================
  24. /* checks whether the passed in pointer is NULL and if the string is empty */
  25. inline bool wxIsEmpty(const char *s) { return !s || !*s; }
  26. inline bool wxIsEmpty(const wchar_t *s) { return !s || !*s; }
  27. inline bool wxIsEmpty(const wxScopedCharBuffer& s) { return wxIsEmpty(s.data()); }
  28. inline bool wxIsEmpty(const wxScopedWCharBuffer& s) { return wxIsEmpty(s.data()); }
  29. inline bool wxIsEmpty(const wxString& s) { return s.empty(); }
  30. inline bool wxIsEmpty(const wxCStrData& s) { return s.AsString().empty(); }
  31. /* multibyte to wide char conversion functions and macros */
  32. /* multibyte<->widechar conversion */
  33. WXDLLIMPEXP_BASE size_t wxMB2WC(wchar_t *buf, const char *psz, size_t n);
  34. WXDLLIMPEXP_BASE size_t wxWC2MB(char *buf, const wchar_t *psz, size_t n);
  35. #if wxUSE_UNICODE
  36. #define wxMB2WX wxMB2WC
  37. #define wxWX2MB wxWC2MB
  38. #define wxWC2WX wxStrncpy
  39. #define wxWX2WC wxStrncpy
  40. #else
  41. #define wxMB2WX wxStrncpy
  42. #define wxWX2MB wxStrncpy
  43. #define wxWC2WX wxWC2MB
  44. #define wxWX2WC wxMB2WC
  45. #endif
  46. // RN: We could do the usual tricky compiler detection here,
  47. // and use their variant (such as wmemchr, etc.). The problem
  48. // is that these functions are quite rare, even though they are
  49. // part of the current POSIX standard. In addition, most compilers
  50. // (including even MSC) inline them just like we do right in their
  51. // headers.
  52. //
  53. #include <string.h>
  54. #if wxUSE_UNICODE
  55. //implement our own wmem variants
  56. inline wxChar* wxTmemchr(const wxChar* s, wxChar c, size_t l)
  57. {
  58. for(;l && *s != c;--l, ++s) {}
  59. if(l)
  60. return const_cast<wxChar*>(s);
  61. return NULL;
  62. }
  63. inline int wxTmemcmp(const wxChar* sz1, const wxChar* sz2, size_t len)
  64. {
  65. for(; *sz1 == *sz2 && len; --len, ++sz1, ++sz2) {}
  66. if(len)
  67. return *sz1 < *sz2 ? -1 : *sz1 > *sz2;
  68. else
  69. return 0;
  70. }
  71. inline wxChar* wxTmemcpy(wxChar* szOut, const wxChar* szIn, size_t len)
  72. {
  73. return (wxChar*) memcpy(szOut, szIn, len * sizeof(wxChar));
  74. }
  75. inline wxChar* wxTmemmove(wxChar* szOut, const wxChar* szIn, size_t len)
  76. {
  77. return (wxChar*) memmove(szOut, szIn, len * sizeof(wxChar));
  78. }
  79. inline wxChar* wxTmemset(wxChar* szOut, const wxChar cIn, size_t len)
  80. {
  81. wxChar* szRet = szOut;
  82. while (len--)
  83. *szOut++ = cIn;
  84. return szRet;
  85. }
  86. #endif /* wxUSE_UNICODE */
  87. // provide trivial wrappers for char* versions for both ANSI and Unicode builds
  88. // (notice that these intentionally return "char *" and not "void *" unlike the
  89. // standard memxxx() for symmetry with the wide char versions):
  90. inline char* wxTmemchr(const char* s, char c, size_t len)
  91. { return (char*)memchr(s, c, len); }
  92. inline int wxTmemcmp(const char* sz1, const char* sz2, size_t len)
  93. { return memcmp(sz1, sz2, len); }
  94. inline char* wxTmemcpy(char* szOut, const char* szIn, size_t len)
  95. { return (char*)memcpy(szOut, szIn, len); }
  96. inline char* wxTmemmove(char* szOut, const char* szIn, size_t len)
  97. { return (char*)memmove(szOut, szIn, len); }
  98. inline char* wxTmemset(char* szOut, const char cIn, size_t len)
  99. { return (char*)memset(szOut, cIn, len); }
  100. // ============================================================================
  101. // wx wrappers for CRT functions in both char* and wchar_t* versions
  102. // ============================================================================
  103. // A few notes on implementation of these wrappers:
  104. //
  105. // We need both char* and wchar_t* versions of functions like wxStrlen() for
  106. // compatibility with both ANSI and Unicode builds.
  107. //
  108. // This makes passing wxString or c_str()/mb_str()/wc_str() result to them
  109. // ambiguous, so we need to provide overrides for that as well (in cases where
  110. // it makes sense).
  111. //
  112. // We can do this without problems for some functions (wxStrlen()), but in some
  113. // cases, we can't stay compatible with both ANSI and Unicode builds, e.g. for
  114. // wxStrcpy(const wxString&), which can only return either char* or wchar_t*.
  115. // In these cases, we preserve ANSI build compatibility by returning char*.
  116. // ----------------------------------------------------------------------------
  117. // locale functions
  118. // ----------------------------------------------------------------------------
  119. // NB: we can't provide const wchar_t* (= wxChar*) overload, because calling
  120. // wxSetlocale(category, NULL) -- which is a common thing to do -- would be
  121. // ambiguous
  122. WXDLLIMPEXP_BASE char* wxSetlocale(int category, const char *locale);
  123. inline char* wxSetlocale(int category, const wxScopedCharBuffer& locale)
  124. { return wxSetlocale(category, locale.data()); }
  125. inline char* wxSetlocale(int category, const wxString& locale)
  126. { return wxSetlocale(category, locale.mb_str()); }
  127. inline char* wxSetlocale(int category, const wxCStrData& locale)
  128. { return wxSetlocale(category, locale.AsCharBuf()); }
  129. // ----------------------------------------------------------------------------
  130. // string functions
  131. // ----------------------------------------------------------------------------
  132. /* safe version of strlen() (returns 0 if passed NULL pointer) */
  133. // NB: these are defined in wxcrtbase.h, see the comment there
  134. // inline size_t wxStrlen(const char *s) { return s ? strlen(s) : 0; }
  135. // inline size_t wxStrlen(const wchar_t *s) { return s ? wxCRT_Strlen_(s) : 0; }
  136. inline size_t wxStrlen(const wxScopedCharBuffer& s) { return wxStrlen(s.data()); }
  137. inline size_t wxStrlen(const wxScopedWCharBuffer& s) { return wxStrlen(s.data()); }
  138. inline size_t wxStrlen(const wxString& s) { return s.length(); }
  139. inline size_t wxStrlen(const wxCStrData& s) { return s.AsString().length(); }
  140. // this is a function new in 2.9 so we don't care about backwards compatibility and
  141. // so don't need to support wxScopedCharBuffer/wxScopedWCharBuffer overloads
  142. #if defined(wxCRT_StrnlenA)
  143. inline size_t wxStrnlen(const char *str, size_t maxlen) { return wxCRT_StrnlenA(str, maxlen); }
  144. #else
  145. inline size_t wxStrnlen(const char *str, size_t maxlen)
  146. {
  147. size_t n;
  148. for ( n = 0; n < maxlen; n++ )
  149. if ( !str[n] )
  150. break;
  151. return n;
  152. }
  153. #endif
  154. #if defined(wxCRT_StrnlenW)
  155. inline size_t wxStrnlen(const wchar_t *str, size_t maxlen) { return wxCRT_StrnlenW(str, maxlen); }
  156. #else
  157. inline size_t wxStrnlen(const wchar_t *str, size_t maxlen)
  158. {
  159. size_t n;
  160. for ( n = 0; n < maxlen; n++ )
  161. if ( !str[n] )
  162. break;
  163. return n;
  164. }
  165. #endif
  166. // NB: these are defined in wxcrtbase.h, see the comment there
  167. // inline char* wxStrdup(const char *s) { return wxStrdupA(s); }
  168. // inline wchar_t* wxStrdup(const wchar_t *s) { return wxStrdupW(s); }
  169. inline char* wxStrdup(const wxScopedCharBuffer& s) { return wxStrdup(s.data()); }
  170. inline wchar_t* wxStrdup(const wxScopedWCharBuffer& s) { return wxStrdup(s.data()); }
  171. inline char* wxStrdup(const wxString& s) { return wxStrdup(s.mb_str()); }
  172. inline char* wxStrdup(const wxCStrData& s) { return wxStrdup(s.AsCharBuf()); }
  173. inline char *wxStrcpy(char *dest, const char *src)
  174. { return wxCRT_StrcpyA(dest, src); }
  175. inline wchar_t *wxStrcpy(wchar_t *dest, const wchar_t *src)
  176. { return wxCRT_StrcpyW(dest, src); }
  177. inline char *wxStrcpy(char *dest, const wxString& src)
  178. { return wxCRT_StrcpyA(dest, src.mb_str()); }
  179. inline char *wxStrcpy(char *dest, const wxCStrData& src)
  180. { return wxCRT_StrcpyA(dest, src.AsCharBuf()); }
  181. inline char *wxStrcpy(char *dest, const wxScopedCharBuffer& src)
  182. { return wxCRT_StrcpyA(dest, src.data()); }
  183. inline wchar_t *wxStrcpy(wchar_t *dest, const wxString& src)
  184. { return wxCRT_StrcpyW(dest, src.wc_str()); }
  185. inline wchar_t *wxStrcpy(wchar_t *dest, const wxCStrData& src)
  186. { return wxCRT_StrcpyW(dest, src.AsWCharBuf()); }
  187. inline wchar_t *wxStrcpy(wchar_t *dest, const wxScopedWCharBuffer& src)
  188. { return wxCRT_StrcpyW(dest, src.data()); }
  189. inline char *wxStrcpy(char *dest, const wchar_t *src)
  190. { return wxCRT_StrcpyA(dest, wxConvLibc.cWC2MB(src)); }
  191. inline wchar_t *wxStrcpy(wchar_t *dest, const char *src)
  192. { return wxCRT_StrcpyW(dest, wxConvLibc.cMB2WC(src)); }
  193. inline char *wxStrncpy(char *dest, const char *src, size_t n)
  194. { return wxCRT_StrncpyA(dest, src, n); }
  195. inline wchar_t *wxStrncpy(wchar_t *dest, const wchar_t *src, size_t n)
  196. { return wxCRT_StrncpyW(dest, src, n); }
  197. inline char *wxStrncpy(char *dest, const wxString& src, size_t n)
  198. { return wxCRT_StrncpyA(dest, src.mb_str(), n); }
  199. inline char *wxStrncpy(char *dest, const wxCStrData& src, size_t n)
  200. { return wxCRT_StrncpyA(dest, src.AsCharBuf(), n); }
  201. inline char *wxStrncpy(char *dest, const wxScopedCharBuffer& src, size_t n)
  202. { return wxCRT_StrncpyA(dest, src.data(), n); }
  203. inline wchar_t *wxStrncpy(wchar_t *dest, const wxString& src, size_t n)
  204. { return wxCRT_StrncpyW(dest, src.wc_str(), n); }
  205. inline wchar_t *wxStrncpy(wchar_t *dest, const wxCStrData& src, size_t n)
  206. { return wxCRT_StrncpyW(dest, src.AsWCharBuf(), n); }
  207. inline wchar_t *wxStrncpy(wchar_t *dest, const wxScopedWCharBuffer& src, size_t n)
  208. { return wxCRT_StrncpyW(dest, src.data(), n); }
  209. inline char *wxStrncpy(char *dest, const wchar_t *src, size_t n)
  210. { return wxCRT_StrncpyA(dest, wxConvLibc.cWC2MB(src), n); }
  211. inline wchar_t *wxStrncpy(wchar_t *dest, const char *src, size_t n)
  212. { return wxCRT_StrncpyW(dest, wxConvLibc.cMB2WC(src), n); }
  213. // this is a function new in 2.9 so we don't care about backwards compatibility and
  214. // so don't need to support wchar_t/char overloads
  215. inline size_t wxStrlcpy(char *dest, const char *src, size_t n)
  216. {
  217. const size_t len = wxCRT_StrlenA(src);
  218. if ( n )
  219. {
  220. if ( n-- > len )
  221. n = len;
  222. wxCRT_StrncpyA(dest, src, n);
  223. dest[n] = '\0';
  224. }
  225. return len;
  226. }
  227. inline size_t wxStrlcpy(wchar_t *dest, const wchar_t *src, size_t n)
  228. {
  229. const size_t len = wxCRT_StrlenW(src);
  230. if ( n )
  231. {
  232. if ( n-- > len )
  233. n = len;
  234. wxCRT_StrncpyW(dest, src, n);
  235. dest[n] = L'\0';
  236. }
  237. return len;
  238. }
  239. inline char *wxStrcat(char *dest, const char *src)
  240. { return wxCRT_StrcatA(dest, src); }
  241. inline wchar_t *wxStrcat(wchar_t *dest, const wchar_t *src)
  242. { return wxCRT_StrcatW(dest, src); }
  243. inline char *wxStrcat(char *dest, const wxString& src)
  244. { return wxCRT_StrcatA(dest, src.mb_str()); }
  245. inline char *wxStrcat(char *dest, const wxCStrData& src)
  246. { return wxCRT_StrcatA(dest, src.AsCharBuf()); }
  247. inline char *wxStrcat(char *dest, const wxScopedCharBuffer& src)
  248. { return wxCRT_StrcatA(dest, src.data()); }
  249. inline wchar_t *wxStrcat(wchar_t *dest, const wxString& src)
  250. { return wxCRT_StrcatW(dest, src.wc_str()); }
  251. inline wchar_t *wxStrcat(wchar_t *dest, const wxCStrData& src)
  252. { return wxCRT_StrcatW(dest, src.AsWCharBuf()); }
  253. inline wchar_t *wxStrcat(wchar_t *dest, const wxScopedWCharBuffer& src)
  254. { return wxCRT_StrcatW(dest, src.data()); }
  255. inline char *wxStrcat(char *dest, const wchar_t *src)
  256. { return wxCRT_StrcatA(dest, wxConvLibc.cWC2MB(src)); }
  257. inline wchar_t *wxStrcat(wchar_t *dest, const char *src)
  258. { return wxCRT_StrcatW(dest, wxConvLibc.cMB2WC(src)); }
  259. inline char *wxStrncat(char *dest, const char *src, size_t n)
  260. { return wxCRT_StrncatA(dest, src, n); }
  261. inline wchar_t *wxStrncat(wchar_t *dest, const wchar_t *src, size_t n)
  262. { return wxCRT_StrncatW(dest, src, n); }
  263. inline char *wxStrncat(char *dest, const wxString& src, size_t n)
  264. { return wxCRT_StrncatA(dest, src.mb_str(), n); }
  265. inline char *wxStrncat(char *dest, const wxCStrData& src, size_t n)
  266. { return wxCRT_StrncatA(dest, src.AsCharBuf(), n); }
  267. inline char *wxStrncat(char *dest, const wxScopedCharBuffer& src, size_t n)
  268. { return wxCRT_StrncatA(dest, src.data(), n); }
  269. inline wchar_t *wxStrncat(wchar_t *dest, const wxString& src, size_t n)
  270. { return wxCRT_StrncatW(dest, src.wc_str(), n); }
  271. inline wchar_t *wxStrncat(wchar_t *dest, const wxCStrData& src, size_t n)
  272. { return wxCRT_StrncatW(dest, src.AsWCharBuf(), n); }
  273. inline wchar_t *wxStrncat(wchar_t *dest, const wxScopedWCharBuffer& src, size_t n)
  274. { return wxCRT_StrncatW(dest, src.data(), n); }
  275. inline char *wxStrncat(char *dest, const wchar_t *src, size_t n)
  276. { return wxCRT_StrncatA(dest, wxConvLibc.cWC2MB(src), n); }
  277. inline wchar_t *wxStrncat(wchar_t *dest, const char *src, size_t n)
  278. { return wxCRT_StrncatW(dest, wxConvLibc.cMB2WC(src), n); }
  279. #define WX_STR_DECL(name, T1, T2) name(T1 s1, T2 s2)
  280. #define WX_STR_CALL(func, a1, a2) func(a1, a2)
  281. // This macro defines string function for all possible variants of arguments,
  282. // except for those taking wxString or wxCStrData as second argument.
  283. // Parameters:
  284. // rettype - return type
  285. // name - name of the (overloaded) function to define
  286. // crtA - function to call for char* versions (takes two arguments)
  287. // crtW - ditto for wchar_t* function
  288. // forString - function to call when the *first* argument is wxString;
  289. // the second argument can be any string type, so this is
  290. // typically a template
  291. #define WX_STR_FUNC_NO_INVERT(rettype, name, crtA, crtW, forString) \
  292. inline rettype WX_STR_DECL(name, const char *, const char *) \
  293. { return WX_STR_CALL(crtA, s1, s2); } \
  294. inline rettype WX_STR_DECL(name, const char *, const wchar_t *) \
  295. { return WX_STR_CALL(forString, wxString(s1), wxString(s2)); } \
  296. inline rettype WX_STR_DECL(name, const char *, const wxScopedCharBuffer&) \
  297. { return WX_STR_CALL(crtA, s1, s2.data()); } \
  298. inline rettype WX_STR_DECL(name, const char *, const wxScopedWCharBuffer&) \
  299. { return WX_STR_CALL(forString, wxString(s1), s2.data()); } \
  300. \
  301. inline rettype WX_STR_DECL(name, const wchar_t *, const wchar_t *) \
  302. { return WX_STR_CALL(crtW, s1, s2); } \
  303. inline rettype WX_STR_DECL(name, const wchar_t *, const char *) \
  304. { return WX_STR_CALL(forString, wxString(s1), wxString(s2)); } \
  305. inline rettype WX_STR_DECL(name, const wchar_t *, const wxScopedWCharBuffer&) \
  306. { return WX_STR_CALL(crtW, s1, s2.data()); } \
  307. inline rettype WX_STR_DECL(name, const wchar_t *, const wxScopedCharBuffer&) \
  308. { return WX_STR_CALL(forString, wxString(s1), s2.data()); } \
  309. \
  310. inline rettype WX_STR_DECL(name, const wxScopedCharBuffer&, const char *) \
  311. { return WX_STR_CALL(crtA, s1.data(), s2); } \
  312. inline rettype WX_STR_DECL(name, const wxScopedCharBuffer&, const wchar_t *) \
  313. { return WX_STR_CALL(forString, wxString(s1), wxString(s2)); } \
  314. inline rettype WX_STR_DECL(name, const wxScopedCharBuffer&, const wxScopedCharBuffer&)\
  315. { return WX_STR_CALL(crtA, s1.data(), s2.data()); } \
  316. inline rettype WX_STR_DECL(name, const wxScopedCharBuffer&, const wxScopedWCharBuffer&) \
  317. { return WX_STR_CALL(forString, wxString(s1), wxString(s2)); } \
  318. \
  319. inline rettype WX_STR_DECL(name, const wxScopedWCharBuffer&, const wchar_t *) \
  320. { return WX_STR_CALL(crtW, s1.data(), s2); } \
  321. inline rettype WX_STR_DECL(name, const wxScopedWCharBuffer&, const char *) \
  322. { return WX_STR_CALL(forString, wxString(s1), wxString(s2)); } \
  323. inline rettype WX_STR_DECL(name, const wxScopedWCharBuffer&, const wxScopedWCharBuffer&) \
  324. { return WX_STR_CALL(crtW, s1.data(), s2.data()); } \
  325. inline rettype WX_STR_DECL(name, const wxScopedWCharBuffer&, const wxScopedCharBuffer&) \
  326. { return WX_STR_CALL(forString, wxString(s1), wxString(s2)); } \
  327. \
  328. inline rettype WX_STR_DECL(name, const wxString&, const char*) \
  329. { return WX_STR_CALL(forString, s1, s2); } \
  330. inline rettype WX_STR_DECL(name, const wxString&, const wchar_t*) \
  331. { return WX_STR_CALL(forString, s1, s2); } \
  332. inline rettype WX_STR_DECL(name, const wxString&, const wxScopedCharBuffer&) \
  333. { return WX_STR_CALL(forString, s1, s2); } \
  334. inline rettype WX_STR_DECL(name, const wxString&, const wxScopedWCharBuffer&) \
  335. { return WX_STR_CALL(forString, s1, s2); } \
  336. inline rettype WX_STR_DECL(name, const wxString&, const wxString&) \
  337. { return WX_STR_CALL(forString, s1, s2); } \
  338. inline rettype WX_STR_DECL(name, const wxString&, const wxCStrData&) \
  339. { return WX_STR_CALL(forString, s1, s2); } \
  340. \
  341. inline rettype WX_STR_DECL(name, const wxCStrData&, const char*) \
  342. { return WX_STR_CALL(forString, s1.AsString(), s2); } \
  343. inline rettype WX_STR_DECL(name, const wxCStrData&, const wchar_t*) \
  344. { return WX_STR_CALL(forString, s1.AsString(), s2); } \
  345. inline rettype WX_STR_DECL(name, const wxCStrData&, const wxScopedCharBuffer&) \
  346. { return WX_STR_CALL(forString, s1.AsString(), s2); } \
  347. inline rettype WX_STR_DECL(name, const wxCStrData&, const wxScopedWCharBuffer&) \
  348. { return WX_STR_CALL(forString, s1.AsString(), s2); } \
  349. inline rettype WX_STR_DECL(name, const wxCStrData&, const wxString&) \
  350. { return WX_STR_CALL(forString, s1.AsString(), s2); } \
  351. inline rettype WX_STR_DECL(name, const wxCStrData&, const wxCStrData&) \
  352. { return WX_STR_CALL(forString, s1.AsString(), s2); }
  353. // This defines strcmp-like function, i.e. one returning the result of
  354. // comparison; see WX_STR_FUNC_NO_INVERT for explanation of the arguments
  355. #define WX_STRCMP_FUNC(name, crtA, crtW, forString) \
  356. WX_STR_FUNC_NO_INVERT(int, name, crtA, crtW, forString) \
  357. \
  358. inline int WX_STR_DECL(name, const char *, const wxCStrData&) \
  359. { return -WX_STR_CALL(forString, s2.AsString(), s1); } \
  360. inline int WX_STR_DECL(name, const char *, const wxString&) \
  361. { return -WX_STR_CALL(forString, s2, s1); } \
  362. \
  363. inline int WX_STR_DECL(name, const wchar_t *, const wxCStrData&) \
  364. { return -WX_STR_CALL(forString, s2.AsString(), s1); } \
  365. inline int WX_STR_DECL(name, const wchar_t *, const wxString&) \
  366. { return -WX_STR_CALL(forString, s2, s1); } \
  367. \
  368. inline int WX_STR_DECL(name, const wxScopedCharBuffer&, const wxCStrData&) \
  369. { return -WX_STR_CALL(forString, s2.AsString(), s1.data()); } \
  370. inline int WX_STR_DECL(name, const wxScopedCharBuffer&, const wxString&) \
  371. { return -WX_STR_CALL(forString, s2, s1.data()); } \
  372. \
  373. inline int WX_STR_DECL(name, const wxScopedWCharBuffer&, const wxCStrData&) \
  374. { return -WX_STR_CALL(forString, s2.AsString(), s1.data()); } \
  375. inline int WX_STR_DECL(name, const wxScopedWCharBuffer&, const wxString&) \
  376. { return -WX_STR_CALL(forString, s2, s1.data()); }
  377. // This defines a string function that is *not* strcmp-like, i.e. doesn't
  378. // return the result of comparison and so if the second argument is a string,
  379. // it has to be converted to char* or wchar_t*
  380. #define WX_STR_FUNC(rettype, name, crtA, crtW, forString) \
  381. WX_STR_FUNC_NO_INVERT(rettype, name, crtA, crtW, forString) \
  382. \
  383. inline rettype WX_STR_DECL(name, const char *, const wxCStrData&) \
  384. { return WX_STR_CALL(crtA, s1, s2.AsCharBuf()); } \
  385. inline rettype WX_STR_DECL(name, const char *, const wxString&) \
  386. { return WX_STR_CALL(crtA, s1, s2.mb_str()); } \
  387. \
  388. inline rettype WX_STR_DECL(name, const wchar_t *, const wxCStrData&) \
  389. { return WX_STR_CALL(crtW, s1, s2.AsWCharBuf()); } \
  390. inline rettype WX_STR_DECL(name, const wchar_t *, const wxString&) \
  391. { return WX_STR_CALL(crtW, s1, s2.wc_str()); } \
  392. \
  393. inline rettype WX_STR_DECL(name, const wxScopedCharBuffer&, const wxCStrData&) \
  394. { return WX_STR_CALL(crtA, s1.data(), s2.AsCharBuf()); } \
  395. inline rettype WX_STR_DECL(name, const wxScopedCharBuffer&, const wxString&) \
  396. { return WX_STR_CALL(crtA, s1.data(), s2.mb_str()); } \
  397. \
  398. inline rettype WX_STR_DECL(name, const wxScopedWCharBuffer&, const wxCStrData&) \
  399. { return WX_STR_CALL(crtW, s1.data(), s2.AsWCharBuf()); } \
  400. inline rettype WX_STR_DECL(name, const wxScopedWCharBuffer&, const wxString&) \
  401. { return WX_STR_CALL(crtW, s1.data(), s2.wc_str()); }
  402. template<typename T>
  403. inline int wxStrcmp_String(const wxString& s1, const T& s2)
  404. { return s1.compare(s2); }
  405. WX_STRCMP_FUNC(wxStrcmp, wxCRT_StrcmpA, wxCRT_StrcmpW, wxStrcmp_String)
  406. template<typename T>
  407. inline int wxStricmp_String(const wxString& s1, const T& s2)
  408. { return s1.CmpNoCase(s2); }
  409. WX_STRCMP_FUNC(wxStricmp, wxCRT_StricmpA, wxCRT_StricmpW, wxStricmp_String)
  410. #if defined(wxCRT_StrcollA) && defined(wxCRT_StrcollW)
  411. // GCC 3.4 and other compilers have a bug that causes it to fail compilation if
  412. // the template's implementation uses overloaded function declared later (see
  413. // the wxStrcoll() call in wxStrcoll_String<T>()), so we have to
  414. // forward-declare the template and implement it below WX_STRCMP_FUNC. OTOH,
  415. // this fails to compile with VC6, so don't do it for VC. It also causes
  416. // problems with GCC visibility in newer GCC versions.
  417. #if !(defined(__VISUALC__) || (wxCHECK_GCC_VERSION(3,5) && !wxCHECK_GCC_VERSION(4,7))) || defined(__clang__)
  418. #define wxNEEDS_DECL_BEFORE_TEMPLATE
  419. #endif
  420. #ifdef wxNEEDS_DECL_BEFORE_TEMPLATE
  421. template<typename T>
  422. inline int wxStrcoll_String(const wxString& s1, const T& s2);
  423. WX_STRCMP_FUNC(wxStrcoll, wxCRT_StrcollA, wxCRT_StrcollW, wxStrcoll_String)
  424. #endif // wxNEEDS_DECL_BEFORE_TEMPLATE
  425. template<typename T>
  426. inline int wxStrcoll_String(const wxString& s1, const T& s2)
  427. {
  428. #if wxUSE_UNICODE
  429. // NB: strcoll() doesn't work correctly on UTF-8 strings, so we have to use
  430. // wc_str() even if wxUSE_UNICODE_UTF8; the (const wchar_t*) cast is
  431. // there just as optimization to avoid going through
  432. // wxStrcoll<wxScopedWCharBuffer>:
  433. return wxStrcoll((const wchar_t*)s1.wc_str(), s2);
  434. #else
  435. return wxStrcoll((const char*)s1.mb_str(), s2);
  436. #endif
  437. }
  438. #ifndef wxNEEDS_DECL_BEFORE_TEMPLATE
  439. // this is exactly the same WX_STRCMP_FUNC line as above, insde the
  440. // wxNEEDS_DECL_BEFORE_TEMPLATE case
  441. WX_STRCMP_FUNC(wxStrcoll, wxCRT_StrcollA, wxCRT_StrcollW, wxStrcoll_String)
  442. #endif
  443. #endif // defined(wxCRT_Strcoll[AW])
  444. template<typename T>
  445. inline size_t wxStrspn_String(const wxString& s1, const T& s2)
  446. {
  447. size_t pos = s1.find_first_not_of(s2);
  448. return pos == wxString::npos ? s1.length() : pos;
  449. }
  450. WX_STR_FUNC(size_t, wxStrspn, wxCRT_StrspnA, wxCRT_StrspnW, wxStrspn_String)
  451. template<typename T>
  452. inline size_t wxStrcspn_String(const wxString& s1, const T& s2)
  453. {
  454. size_t pos = s1.find_first_of(s2);
  455. return pos == wxString::npos ? s1.length() : pos;
  456. }
  457. WX_STR_FUNC(size_t, wxStrcspn, wxCRT_StrcspnA, wxCRT_StrcspnW, wxStrcspn_String)
  458. #undef WX_STR_DECL
  459. #undef WX_STR_CALL
  460. #define WX_STR_DECL(name, T1, T2) name(T1 s1, T2 s2, size_t n)
  461. #define WX_STR_CALL(func, a1, a2) func(a1, a2, n)
  462. template<typename T>
  463. inline int wxStrncmp_String(const wxString& s1, const T& s2, size_t n)
  464. { return s1.compare(0, n, s2, 0, n); }
  465. WX_STRCMP_FUNC(wxStrncmp, wxCRT_StrncmpA, wxCRT_StrncmpW, wxStrncmp_String)
  466. template<typename T>
  467. inline int wxStrnicmp_String(const wxString& s1, const T& s2, size_t n)
  468. { return s1.substr(0, n).CmpNoCase(wxString(s2).substr(0, n)); }
  469. WX_STRCMP_FUNC(wxStrnicmp, wxCRT_StrnicmpA, wxCRT_StrnicmpW, wxStrnicmp_String)
  470. #undef WX_STR_DECL
  471. #undef WX_STR_CALL
  472. #undef WX_STRCMP_FUNC
  473. #undef WX_STR_FUNC
  474. #undef WX_STR_FUNC_NO_INVERT
  475. #if defined(wxCRT_StrxfrmA) && defined(wxCRT_StrxfrmW)
  476. inline size_t wxStrxfrm(char *dest, const char *src, size_t n)
  477. { return wxCRT_StrxfrmA(dest, src, n); }
  478. inline size_t wxStrxfrm(wchar_t *dest, const wchar_t *src, size_t n)
  479. { return wxCRT_StrxfrmW(dest, src, n); }
  480. template<typename T>
  481. inline size_t wxStrxfrm(T *dest, const wxScopedCharTypeBuffer<T>& src, size_t n)
  482. { return wxStrxfrm(dest, src.data(), n); }
  483. inline size_t wxStrxfrm(char *dest, const wxString& src, size_t n)
  484. { return wxCRT_StrxfrmA(dest, src.mb_str(), n); }
  485. inline size_t wxStrxfrm(wchar_t *dest, const wxString& src, size_t n)
  486. { return wxCRT_StrxfrmW(dest, src.wc_str(), n); }
  487. inline size_t wxStrxfrm(char *dest, const wxCStrData& src, size_t n)
  488. { return wxCRT_StrxfrmA(dest, src.AsCharBuf(), n); }
  489. inline size_t wxStrxfrm(wchar_t *dest, const wxCStrData& src, size_t n)
  490. { return wxCRT_StrxfrmW(dest, src.AsWCharBuf(), n); }
  491. #endif // defined(wxCRT_Strxfrm[AW])
  492. inline char *wxStrtok(char *str, const char *delim, char **saveptr)
  493. { return wxCRT_StrtokA(str, delim, saveptr); }
  494. inline wchar_t *wxStrtok(wchar_t *str, const wchar_t *delim, wchar_t **saveptr)
  495. { return wxCRT_StrtokW(str, delim, saveptr); }
  496. template<typename T>
  497. inline T *wxStrtok(T *str, const wxScopedCharTypeBuffer<T>& delim, T **saveptr)
  498. { return wxStrtok(str, delim.data(), saveptr); }
  499. inline char *wxStrtok(char *str, const wxCStrData& delim, char **saveptr)
  500. { return wxCRT_StrtokA(str, delim.AsCharBuf(), saveptr); }
  501. inline wchar_t *wxStrtok(wchar_t *str, const wxCStrData& delim, wchar_t **saveptr)
  502. { return wxCRT_StrtokW(str, delim.AsWCharBuf(), saveptr); }
  503. inline char *wxStrtok(char *str, const wxString& delim, char **saveptr)
  504. { return wxCRT_StrtokA(str, delim.mb_str(), saveptr); }
  505. inline wchar_t *wxStrtok(wchar_t *str, const wxString& delim, wchar_t **saveptr)
  506. { return wxCRT_StrtokW(str, delim.wc_str(), saveptr); }
  507. inline const char *wxStrstr(const char *haystack, const char *needle)
  508. { return wxCRT_StrstrA(haystack, needle); }
  509. inline const wchar_t *wxStrstr(const wchar_t *haystack, const wchar_t *needle)
  510. { return wxCRT_StrstrW(haystack, needle); }
  511. inline const char *wxStrstr(const char *haystack, const wxString& needle)
  512. { return wxCRT_StrstrA(haystack, needle.mb_str()); }
  513. inline const wchar_t *wxStrstr(const wchar_t *haystack, const wxString& needle)
  514. { return wxCRT_StrstrW(haystack, needle.wc_str()); }
  515. // these functions return char* pointer into the non-temporary conversion buffer
  516. // used by c_str()'s implicit conversion to char*, for ANSI build compatibility
  517. inline const char *wxStrstr(const wxString& haystack, const wxString& needle)
  518. { return wxCRT_StrstrA(haystack.c_str(), needle.mb_str()); }
  519. inline const char *wxStrstr(const wxCStrData& haystack, const wxString& needle)
  520. { return wxCRT_StrstrA(haystack, needle.mb_str()); }
  521. inline const char *wxStrstr(const wxCStrData& haystack, const wxCStrData& needle)
  522. { return wxCRT_StrstrA(haystack, needle.AsCharBuf()); }
  523. // if 'needle' is char/wchar_t, then the same is probably wanted as return value
  524. inline const char *wxStrstr(const wxString& haystack, const char *needle)
  525. { return wxCRT_StrstrA(haystack.c_str(), needle); }
  526. inline const char *wxStrstr(const wxCStrData& haystack, const char *needle)
  527. { return wxCRT_StrstrA(haystack, needle); }
  528. inline const wchar_t *wxStrstr(const wxString& haystack, const wchar_t *needle)
  529. { return wxCRT_StrstrW(haystack.c_str(), needle); }
  530. inline const wchar_t *wxStrstr(const wxCStrData& haystack, const wchar_t *needle)
  531. { return wxCRT_StrstrW(haystack, needle); }
  532. inline const char *wxStrchr(const char *s, char c)
  533. { return wxCRT_StrchrA(s, c); }
  534. inline const wchar_t *wxStrchr(const wchar_t *s, wchar_t c)
  535. { return wxCRT_StrchrW(s, c); }
  536. inline const char *wxStrrchr(const char *s, char c)
  537. { return wxCRT_StrrchrA(s, c); }
  538. inline const wchar_t *wxStrrchr(const wchar_t *s, wchar_t c)
  539. { return wxCRT_StrrchrW(s, c); }
  540. inline const char *wxStrchr(const char *s, const wxUniChar& uc)
  541. { char c; return uc.GetAsChar(&c) ? wxCRT_StrchrA(s, c) : NULL; }
  542. inline const wchar_t *wxStrchr(const wchar_t *s, const wxUniChar& c)
  543. { return wxCRT_StrchrW(s, (wchar_t)c); }
  544. inline const char *wxStrrchr(const char *s, const wxUniChar& uc)
  545. { char c; return uc.GetAsChar(&c) ? wxCRT_StrrchrA(s, c) : NULL; }
  546. inline const wchar_t *wxStrrchr(const wchar_t *s, const wxUniChar& c)
  547. { return wxCRT_StrrchrW(s, (wchar_t)c); }
  548. inline const char *wxStrchr(const char *s, const wxUniCharRef& uc)
  549. { char c; return uc.GetAsChar(&c) ? wxCRT_StrchrA(s, c) : NULL; }
  550. inline const wchar_t *wxStrchr(const wchar_t *s, const wxUniCharRef& c)
  551. { return wxCRT_StrchrW(s, (wchar_t)c); }
  552. inline const char *wxStrrchr(const char *s, const wxUniCharRef& uc)
  553. { char c; return uc.GetAsChar(&c) ? wxCRT_StrrchrA(s, c) : NULL; }
  554. inline const wchar_t *wxStrrchr(const wchar_t *s, const wxUniCharRef& c)
  555. { return wxCRT_StrrchrW(s, (wchar_t)c); }
  556. template<typename T>
  557. inline const T* wxStrchr(const wxScopedCharTypeBuffer<T>& s, T c)
  558. { return wxStrchr(s.data(), c); }
  559. template<typename T>
  560. inline const T* wxStrrchr(const wxScopedCharTypeBuffer<T>& s, T c)
  561. { return wxStrrchr(s.data(), c); }
  562. template<typename T>
  563. inline const T* wxStrchr(const wxScopedCharTypeBuffer<T>& s, const wxUniChar& c)
  564. { return wxStrchr(s.data(), (T)c); }
  565. template<typename T>
  566. inline const T* wxStrrchr(const wxScopedCharTypeBuffer<T>& s, const wxUniChar& c)
  567. { return wxStrrchr(s.data(), (T)c); }
  568. template<typename T>
  569. inline const T* wxStrchr(const wxScopedCharTypeBuffer<T>& s, const wxUniCharRef& c)
  570. { return wxStrchr(s.data(), (T)c); }
  571. template<typename T>
  572. inline const T* wxStrrchr(const wxScopedCharTypeBuffer<T>& s, const wxUniCharRef& c)
  573. { return wxStrrchr(s.data(), (T)c); }
  574. // these functions return char* pointer into the non-temporary conversion buffer
  575. // used by c_str()'s implicit conversion to char*, for ANSI build compatibility
  576. inline const char* wxStrchr(const wxString& s, char c)
  577. { return wxCRT_StrchrA((const char*)s.c_str(), c); }
  578. inline const char* wxStrrchr(const wxString& s, char c)
  579. { return wxCRT_StrrchrA((const char*)s.c_str(), c); }
  580. inline const char* wxStrchr(const wxString& s, int c)
  581. { return wxCRT_StrchrA((const char*)s.c_str(), c); }
  582. inline const char* wxStrrchr(const wxString& s, int c)
  583. { return wxCRT_StrrchrA((const char*)s.c_str(), c); }
  584. inline const char* wxStrchr(const wxString& s, const wxUniChar& uc)
  585. { char c; return uc.GetAsChar(&c) ? wxCRT_StrchrA(s.c_str(), c) : NULL; }
  586. inline const char* wxStrrchr(const wxString& s, const wxUniChar& uc)
  587. { char c; return uc.GetAsChar(&c) ? wxCRT_StrrchrA(s.c_str(), c) : NULL; }
  588. inline const char* wxStrchr(const wxString& s, const wxUniCharRef& uc)
  589. { char c; return uc.GetAsChar(&c) ? wxCRT_StrchrA(s.c_str(), c) : NULL; }
  590. inline const char* wxStrrchr(const wxString& s, const wxUniCharRef& uc)
  591. { char c; return uc.GetAsChar(&c) ? wxCRT_StrrchrA(s.c_str(), c) : NULL; }
  592. inline const wchar_t* wxStrchr(const wxString& s, wchar_t c)
  593. { return wxCRT_StrchrW((const wchar_t*)s.c_str(), c); }
  594. inline const wchar_t* wxStrrchr(const wxString& s, wchar_t c)
  595. { return wxCRT_StrrchrW((const wchar_t*)s.c_str(), c); }
  596. inline const char* wxStrchr(const wxCStrData& s, char c)
  597. { return wxCRT_StrchrA(s.AsChar(), c); }
  598. inline const char* wxStrrchr(const wxCStrData& s, char c)
  599. { return wxCRT_StrrchrA(s.AsChar(), c); }
  600. inline const char* wxStrchr(const wxCStrData& s, int c)
  601. { return wxCRT_StrchrA(s.AsChar(), c); }
  602. inline const char* wxStrrchr(const wxCStrData& s, int c)
  603. { return wxCRT_StrrchrA(s.AsChar(), c); }
  604. inline const char* wxStrchr(const wxCStrData& s, const wxUniChar& uc)
  605. { char c; return uc.GetAsChar(&c) ? wxCRT_StrchrA(s, c) : NULL; }
  606. inline const char* wxStrrchr(const wxCStrData& s, const wxUniChar& uc)
  607. { char c; return uc.GetAsChar(&c) ? wxCRT_StrrchrA(s, c) : NULL; }
  608. inline const char* wxStrchr(const wxCStrData& s, const wxUniCharRef& uc)
  609. { char c; return uc.GetAsChar(&c) ? wxCRT_StrchrA(s, c) : NULL; }
  610. inline const char* wxStrrchr(const wxCStrData& s, const wxUniCharRef& uc)
  611. { char c; return uc.GetAsChar(&c) ? wxCRT_StrrchrA(s, c) : NULL; }
  612. inline const wchar_t* wxStrchr(const wxCStrData& s, wchar_t c)
  613. { return wxCRT_StrchrW(s.AsWChar(), c); }
  614. inline const wchar_t* wxStrrchr(const wxCStrData& s, wchar_t c)
  615. { return wxCRT_StrrchrW(s.AsWChar(), c); }
  616. inline const char *wxStrpbrk(const char *s, const char *accept)
  617. { return wxCRT_StrpbrkA(s, accept); }
  618. inline const wchar_t *wxStrpbrk(const wchar_t *s, const wchar_t *accept)
  619. { return wxCRT_StrpbrkW(s, accept); }
  620. inline const char *wxStrpbrk(const char *s, const wxString& accept)
  621. { return wxCRT_StrpbrkA(s, accept.mb_str()); }
  622. inline const char *wxStrpbrk(const char *s, const wxCStrData& accept)
  623. { return wxCRT_StrpbrkA(s, accept.AsCharBuf()); }
  624. inline const wchar_t *wxStrpbrk(const wchar_t *s, const wxString& accept)
  625. { return wxCRT_StrpbrkW(s, accept.wc_str()); }
  626. inline const wchar_t *wxStrpbrk(const wchar_t *s, const wxCStrData& accept)
  627. { return wxCRT_StrpbrkW(s, accept.AsWCharBuf()); }
  628. inline const char *wxStrpbrk(const wxString& s, const wxString& accept)
  629. { return wxCRT_StrpbrkA(s.c_str(), accept.mb_str()); }
  630. inline const char *wxStrpbrk(const wxString& s, const char *accept)
  631. { return wxCRT_StrpbrkA(s.c_str(), accept); }
  632. inline const wchar_t *wxStrpbrk(const wxString& s, const wchar_t *accept)
  633. { return wxCRT_StrpbrkW(s.wc_str(), accept); }
  634. inline const char *wxStrpbrk(const wxString& s, const wxCStrData& accept)
  635. { return wxCRT_StrpbrkA(s.c_str(), accept.AsCharBuf()); }
  636. inline const char *wxStrpbrk(const wxCStrData& s, const wxString& accept)
  637. { return wxCRT_StrpbrkA(s.AsChar(), accept.mb_str()); }
  638. inline const char *wxStrpbrk(const wxCStrData& s, const char *accept)
  639. { return wxCRT_StrpbrkA(s.AsChar(), accept); }
  640. inline const wchar_t *wxStrpbrk(const wxCStrData& s, const wchar_t *accept)
  641. { return wxCRT_StrpbrkW(s.AsWChar(), accept); }
  642. inline const char *wxStrpbrk(const wxCStrData& s, const wxCStrData& accept)
  643. { return wxCRT_StrpbrkA(s.AsChar(), accept.AsCharBuf()); }
  644. template <typename S, typename T>
  645. inline const T *wxStrpbrk(const S& s, const wxScopedCharTypeBuffer<T>& accept)
  646. { return wxStrpbrk(s, accept.data()); }
  647. /* inlined non-const versions */
  648. template <typename T>
  649. inline char *wxStrstr(char *haystack, T needle)
  650. { return const_cast<char*>(wxStrstr(const_cast<const char*>(haystack), needle)); }
  651. template <typename T>
  652. inline wchar_t *wxStrstr(wchar_t *haystack, T needle)
  653. { return const_cast<wchar_t*>(wxStrstr(const_cast<const wchar_t*>(haystack), needle)); }
  654. template <typename T>
  655. inline char * wxStrchr(char *s, T c)
  656. { return const_cast<char*>(wxStrchr(const_cast<const char*>(s), c)); }
  657. template <typename T>
  658. inline wchar_t * wxStrchr(wchar_t *s, T c)
  659. { return (wchar_t *)wxStrchr((const wchar_t *)s, c); }
  660. template <typename T>
  661. inline char * wxStrrchr(char *s, T c)
  662. { return const_cast<char*>(wxStrrchr(const_cast<const char*>(s), c)); }
  663. template <typename T>
  664. inline wchar_t * wxStrrchr(wchar_t *s, T c)
  665. { return const_cast<wchar_t*>(wxStrrchr(const_cast<const wchar_t*>(s), c)); }
  666. template <typename T>
  667. inline char * wxStrpbrk(char *s, T accept)
  668. { return const_cast<char*>(wxStrpbrk(const_cast<const char*>(s), accept)); }
  669. template <typename T>
  670. inline wchar_t * wxStrpbrk(wchar_t *s, T accept)
  671. { return const_cast<wchar_t*>(wxStrpbrk(const_cast<const wchar_t*>(s), accept)); }
  672. // ----------------------------------------------------------------------------
  673. // stdio.h functions
  674. // ----------------------------------------------------------------------------
  675. // NB: using fn_str() for mode is a hack to get the same type (char*/wchar_t*)
  676. // as needed, the conversion itself doesn't matter, it's ASCII
  677. inline FILE *wxFopen(const wxString& path, const wxString& mode)
  678. { return wxCRT_Fopen(path.fn_str(), mode.fn_str()); }
  679. inline FILE *wxFreopen(const wxString& path, const wxString& mode, FILE *stream)
  680. { return wxCRT_Freopen(path.fn_str(), mode.fn_str(), stream); }
  681. inline int wxRemove(const wxString& path)
  682. { return wxCRT_Remove(path.fn_str()); }
  683. inline int wxRename(const wxString& oldpath, const wxString& newpath)
  684. { return wxCRT_Rename(oldpath.fn_str(), newpath.fn_str()); }
  685. extern WXDLLIMPEXP_BASE int wxPuts(const wxString& s);
  686. extern WXDLLIMPEXP_BASE int wxFputs(const wxString& s, FILE *stream);
  687. extern WXDLLIMPEXP_BASE void wxPerror(const wxString& s);
  688. extern WXDLLIMPEXP_BASE int wxFputc(const wxUniChar& c, FILE *stream);
  689. #define wxPutc(c, stream) wxFputc(c, stream)
  690. #define wxPutchar(c) wxFputc(c, stdout)
  691. #define wxFputchar(c) wxPutchar(c)
  692. // NB: We only provide ANSI version of fgets() because fgetws() interprets the
  693. // stream according to current locale, which is rarely what is desired.
  694. inline char *wxFgets(char *s, int size, FILE *stream)
  695. { return wxCRT_FgetsA(s, size, stream); }
  696. // This version calls ANSI version and converts the string using wxConvLibc
  697. extern WXDLLIMPEXP_BASE wchar_t *wxFgets(wchar_t *s, int size, FILE *stream);
  698. #define wxGets(s) wxGets_is_insecure_and_dangerous_use_wxFgets_instead
  699. // NB: We only provide ANSI versions of this for the same reasons as in the
  700. // case of wxFgets() above
  701. inline int wxFgetc(FILE *stream) { return wxCRT_FgetcA(stream); }
  702. inline int wxUngetc(int c, FILE *stream) { return wxCRT_UngetcA(c, stream); }
  703. #define wxGetc(stream) wxFgetc(stream)
  704. #define wxGetchar() wxFgetc(stdin)
  705. #define wxFgetchar() wxGetchar()
  706. // ----------------------------------------------------------------------------
  707. // stdlib.h functions
  708. // ----------------------------------------------------------------------------
  709. #ifdef wxCRT_AtoiW
  710. inline int wxAtoi(const wxString& str) { return wxCRT_AtoiW(str.wc_str()); }
  711. #else
  712. inline int wxAtoi(const wxString& str) { return wxCRT_AtoiA(str.mb_str()); }
  713. #endif
  714. #ifdef wxCRT_AtolW
  715. inline long wxAtol(const wxString& str) { return wxCRT_AtolW(str.wc_str()); }
  716. #else
  717. inline long wxAtol(const wxString& str) { return wxCRT_AtolA(str.mb_str()); }
  718. #endif
  719. #ifdef wxCRT_AtofW
  720. inline double wxAtof(const wxString& str) { return wxCRT_AtofW(str.wc_str()); }
  721. #else
  722. inline double wxAtof(const wxString& str) { return wxCRT_AtofA(str.mb_str()); }
  723. #endif
  724. inline double wxStrtod(const char *nptr, char **endptr)
  725. { return wxCRT_StrtodA(nptr, endptr); }
  726. inline double wxStrtod(const wchar_t *nptr, wchar_t **endptr)
  727. { return wxCRT_StrtodW(nptr, endptr); }
  728. template<typename T>
  729. inline double wxStrtod(const wxScopedCharTypeBuffer<T>& nptr, T **endptr)
  730. { return wxStrtod(nptr.data(), endptr); }
  731. // We implement wxStrto*() like this so that the code compiles when NULL is
  732. // passed in - - if we had just char** and wchar_t** overloads for 'endptr', it
  733. // would be ambiguous. The solution is to use a template so that endptr can be
  734. // any type: when NULL constant is used, the type will be int and we can handle
  735. // that case specially. Otherwise, we infer the type that 'nptr' should be
  736. // converted to from the type of 'endptr'. We need wxStrtoxCharType<T> template
  737. // to make the code compile even for T=int (that's the case when it's not going
  738. // to be ever used, but it still has to compile).
  739. template<typename T> struct wxStrtoxCharType {};
  740. template<> struct wxStrtoxCharType<char**>
  741. {
  742. typedef const char* Type;
  743. static char** AsPointer(char **p) { return p; }
  744. };
  745. template<> struct wxStrtoxCharType<wchar_t**>
  746. {
  747. typedef const wchar_t* Type;
  748. static wchar_t** AsPointer(wchar_t **p) { return p; }
  749. };
  750. template<> struct wxStrtoxCharType<int>
  751. {
  752. typedef const char* Type; /* this one is never used */
  753. static char** AsPointer(int WXUNUSED_UNLESS_DEBUG(p))
  754. {
  755. wxASSERT_MSG( p == 0, "passing non-NULL int is invalid" );
  756. return NULL;
  757. }
  758. };
  759. template<typename T>
  760. inline double wxStrtod(const wxString& nptr, T endptr)
  761. {
  762. if ( endptr == 0 )
  763. {
  764. // when we don't care about endptr, use the string representation that
  765. // doesn't require any conversion (it doesn't matter for this function
  766. // even if its UTF-8):
  767. return wxStrtod(nptr.wx_str(), (wxStringCharType**)NULL);
  768. }
  769. else
  770. {
  771. // note that it is important to use c_str() here and not mb_str() or
  772. // wc_str(), because we store the pointer into (possibly converted)
  773. // buffer in endptr and so it must be valid even when wxStrtod() returns
  774. typedef typename wxStrtoxCharType<T>::Type CharType;
  775. return wxStrtod((CharType)nptr.c_str(),
  776. wxStrtoxCharType<T>::AsPointer(endptr));
  777. }
  778. }
  779. template<typename T>
  780. inline double wxStrtod(const wxCStrData& nptr, T endptr)
  781. { return wxStrtod(nptr.AsString(), endptr); }
  782. #define WX_STRTOX_FUNC(rettype, name, implA, implW) \
  783. /* see wxStrtod() above for explanation of this code: */ \
  784. inline rettype name(const char *nptr, char **endptr, int base) \
  785. { return implA(nptr, endptr, base); } \
  786. inline rettype name(const wchar_t *nptr, wchar_t **endptr, int base) \
  787. { return implW(nptr, endptr, base); } \
  788. template<typename T> \
  789. inline rettype name(const wxScopedCharTypeBuffer<T>& nptr, T **endptr, int)\
  790. { return name(nptr.data(), endptr); } \
  791. template<typename T> \
  792. inline rettype name(const wxString& nptr, T endptr, int base) \
  793. { \
  794. if ( endptr == 0 ) \
  795. return name(nptr.wx_str(), (wxStringCharType**)NULL, base); \
  796. else \
  797. { \
  798. typedef typename wxStrtoxCharType<T>::Type CharType; \
  799. return name((CharType)nptr.c_str(), \
  800. wxStrtoxCharType<T>::AsPointer(endptr), \
  801. base); \
  802. } \
  803. } \
  804. template<typename T> \
  805. inline rettype name(const wxCStrData& nptr, T endptr, int base) \
  806. { return name(nptr.AsString(), endptr, base); }
  807. WX_STRTOX_FUNC(long, wxStrtol, wxCRT_StrtolA, wxCRT_StrtolW)
  808. WX_STRTOX_FUNC(unsigned long, wxStrtoul, wxCRT_StrtoulA, wxCRT_StrtoulW)
  809. #ifdef wxLongLong_t
  810. WX_STRTOX_FUNC(wxLongLong_t, wxStrtoll, wxCRT_StrtollA, wxCRT_StrtollW)
  811. WX_STRTOX_FUNC(wxULongLong_t, wxStrtoull, wxCRT_StrtoullA, wxCRT_StrtoullW)
  812. #endif // wxLongLong_t
  813. #undef WX_STRTOX_FUNC
  814. // there is no command interpreter under CE, hence no system()
  815. #ifndef __WXWINCE__
  816. // mingw32 doesn't provide _tsystem() even though it provides other stdlib.h
  817. // functions in their wide versions
  818. #ifdef wxCRT_SystemW
  819. inline int wxSystem(const wxString& str) { return wxCRT_SystemW(str.wc_str()); }
  820. #else
  821. inline int wxSystem(const wxString& str) { return wxCRT_SystemA(str.mb_str()); }
  822. #endif
  823. #endif // !__WXWINCE__/__WXWINCE__
  824. inline char* wxGetenv(const char *name) { return wxCRT_GetenvA(name); }
  825. inline wchar_t* wxGetenv(const wchar_t *name) { return wxCRT_GetenvW(name); }
  826. inline char* wxGetenv(const wxString& name) { return wxCRT_GetenvA(name.mb_str()); }
  827. inline char* wxGetenv(const wxCStrData& name) { return wxCRT_GetenvA(name.AsCharBuf()); }
  828. inline char* wxGetenv(const wxScopedCharBuffer& name) { return wxCRT_GetenvA(name.data()); }
  829. inline wchar_t* wxGetenv(const wxScopedWCharBuffer& name) { return wxCRT_GetenvW(name.data()); }
  830. // ----------------------------------------------------------------------------
  831. // time.h functions
  832. // ----------------------------------------------------------------------------
  833. inline size_t wxStrftime(char *s, size_t max,
  834. const wxString& format, const struct tm *tm)
  835. { return wxCRT_StrftimeA(s, max, format.mb_str(), tm); }
  836. inline size_t wxStrftime(wchar_t *s, size_t max,
  837. const wxString& format, const struct tm *tm)
  838. { return wxCRT_StrftimeW(s, max, format.wc_str(), tm); }
  839. // NB: we can't provide both char* and wchar_t* versions for obvious reasons
  840. // and returning wxString wouldn't work either (it would be immediately
  841. // destroyed and if assigned to char*/wchar_t*, the pointer would be
  842. // invalid), so we only keep ASCII version, because the returned value
  843. // is always ASCII anyway
  844. #define wxAsctime asctime
  845. #define wxCtime ctime
  846. // ----------------------------------------------------------------------------
  847. // ctype.h functions
  848. // ----------------------------------------------------------------------------
  849. // FIXME-UTF8: we'd be better off implementing these ourselves, as the CRT
  850. // version is locale-dependent
  851. // FIXME-UTF8: these don't work when EOF is passed in because of wxUniChar,
  852. // is this OK or not?
  853. inline bool wxIsalnum(const wxUniChar& c) { return wxCRT_IsalnumW(c) != 0; }
  854. inline bool wxIsalpha(const wxUniChar& c) { return wxCRT_IsalphaW(c) != 0; }
  855. inline bool wxIscntrl(const wxUniChar& c) { return wxCRT_IscntrlW(c) != 0; }
  856. inline bool wxIsdigit(const wxUniChar& c) { return wxCRT_IsdigitW(c) != 0; }
  857. inline bool wxIsgraph(const wxUniChar& c) { return wxCRT_IsgraphW(c) != 0; }
  858. inline bool wxIslower(const wxUniChar& c) { return wxCRT_IslowerW(c) != 0; }
  859. inline bool wxIsprint(const wxUniChar& c) { return wxCRT_IsprintW(c) != 0; }
  860. inline bool wxIspunct(const wxUniChar& c) { return wxCRT_IspunctW(c) != 0; }
  861. inline bool wxIsspace(const wxUniChar& c) { return wxCRT_IsspaceW(c) != 0; }
  862. inline bool wxIsupper(const wxUniChar& c) { return wxCRT_IsupperW(c) != 0; }
  863. inline bool wxIsxdigit(const wxUniChar& c) { return wxCRT_IsxdigitW(c) != 0; }
  864. inline wxUniChar wxTolower(const wxUniChar& c) { return wxCRT_TolowerW(c); }
  865. inline wxUniChar wxToupper(const wxUniChar& c) { return wxCRT_ToupperW(c); }
  866. #if WXWIN_COMPATIBILITY_2_8
  867. // we had goofed and defined wxIsctrl() instead of (correct) wxIscntrl() in the
  868. // initial versions of this header -- now it is too late to remove it so
  869. // although we fixed the function/macro name above, still provide the
  870. // backwards-compatible synonym.
  871. wxDEPRECATED( inline int wxIsctrl(const wxUniChar& c) );
  872. inline int wxIsctrl(const wxUniChar& c) { return wxIscntrl(c); }
  873. #endif // WXWIN_COMPATIBILITY_2_8
  874. inline bool wxIsascii(const wxUniChar& c) { return c.IsAscii(); }
  875. #endif /* _WX_WXCRT_H_ */