You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

527 lines
15 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/std/cstring.hpp
  3. *
  4. * Purpose: Mappings to string string functions
  5. *
  6. * Created: 2nd December 2004
  7. * Updated: 31st March 2010
  8. *
  9. * Thanks: To Anton Sekeris for providing good advice on the naming scheme
  10. * for the stlsoft/std headers
  11. *
  12. * Home: http://stlsoft.org/
  13. *
  14. * Copyright (c) 2004-2010, Matthew Wilson and Synesis Software
  15. * All rights reserved.
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions are met:
  19. *
  20. * - Redistributions of source code must retain the above copyright notice, this
  21. * list of conditions and the following disclaimer.
  22. * - Redistributions in binary form must reproduce the above copyright notice,
  23. * this list of conditions and the following disclaimer in the documentation
  24. * and/or other materials provided with the distribution.
  25. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  26. * any contributors may be used to endorse or promote products derived from
  27. * this software without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  30. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  33. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  34. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  35. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  36. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  37. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  38. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. * POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. * ////////////////////////////////////////////////////////////////////// */
  42. /** \file stlsoft/std/cstring.hpp
  43. *
  44. * \brief [C++ only] Mappings of <cstring> string functions that use
  45. * \ref group__concept__shim__string_access string
  46. * (\ref group__library__utility "Utility" Library).
  47. */
  48. #ifndef STLSOFT_INCL_STLSOFT_STD_HPP_CSTRING
  49. #define STLSOFT_INCL_STLSOFT_STD_HPP_CSTRING
  50. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  51. # define STLSOFT_VER_STLSOFT_STD_HPP_CSTRING_MAJOR 1
  52. # define STLSOFT_VER_STLSOFT_STD_HPP_CSTRING_MINOR 5
  53. # define STLSOFT_VER_STLSOFT_STD_HPP_CSTRING_REVISION 4
  54. # define STLSOFT_VER_STLSOFT_STD_HPP_CSTRING_EDIT 33
  55. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  56. /* /////////////////////////////////////////////////////////////////////////
  57. * Includes
  58. */
  59. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  60. # include <stlsoft/stlsoft.h>
  61. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  62. #ifndef STLSOFT_INCL_STLSOFT_STD_HPP_CBASE_
  63. # include <stlsoft/std/cbase_.hpp>
  64. #endif /* !STLSOFT_INCL_STLSOFT_STD_HPP_CBASE_ */
  65. #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
  66. # include <stlsoft/shims/access/string.hpp>
  67. #endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING */
  68. #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_TRAITS
  69. # include <stlsoft/string/string_traits.hpp>
  70. #endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_TRAITS */
  71. #ifndef STLSOFT_INCL_H_STRING
  72. # define STLSOFT_INCL_H_STRING
  73. # include <string.h>
  74. #endif /* !STLSOFT_INCL_H_STRING */
  75. #ifndef STLSOFT_INCL_H_WCHAR
  76. # define STLSOFT_INCL_H_WCHAR
  77. # include <wchar.h>
  78. #endif /* !STLSOFT_INCL_H_WCHAR */
  79. #if defined(STLSOFT_COMPILER_IS_BORLAND)
  80. # include <malloc.h>
  81. #endif /* compiler */
  82. /* /////////////////////////////////////////////////////////////////////////
  83. * Namespace
  84. */
  85. #ifndef _STLSOFT_NO_NAMESPACE
  86. namespace stlsoft
  87. {
  88. #endif /* _STLSOFT_NO_NAMESPACE */
  89. /* /////////////////////////////////////////////////////////////////////////
  90. * Functions
  91. */
  92. /// \name Copying and concatenation family
  93. /// @{
  94. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  95. #ifndef _STLSOFT_NO_NAMESPACE
  96. namespace std_impl
  97. {
  98. #endif /* _STLSOFT_NO_NAMESPACE */
  99. inline ss_char_a_t* strcpy_a(ss_char_a_t* dest, ss_char_a_t const* src)
  100. {
  101. return ::strcpy(dest, src);
  102. }
  103. inline ss_char_w_t* strcpy_w(ss_char_w_t* dest, ss_char_w_t const* src)
  104. {
  105. return ::wcscpy(dest, src);
  106. }
  107. inline ss_char_a_t* strcpy_(ss_char_a_t* dest, ss_char_a_t const* src)
  108. {
  109. return strcpy_a(dest, src);
  110. }
  111. inline ss_char_w_t* strcpy_(ss_char_w_t* dest, ss_char_w_t const* src)
  112. {
  113. return strcpy_w(dest, src);
  114. }
  115. inline ss_char_a_t* strcat_a(ss_char_a_t* dest, ss_char_a_t const* src)
  116. {
  117. return ::strcat(dest, src);
  118. }
  119. inline ss_char_w_t* strcat_w(ss_char_w_t* dest, ss_char_w_t const* src)
  120. {
  121. return ::wcscat(dest, src);
  122. }
  123. inline ss_char_a_t* strcat_(ss_char_a_t* dest, ss_char_a_t const* src)
  124. {
  125. return strcat_a(dest, src);
  126. }
  127. inline ss_char_w_t* strcat_(ss_char_w_t* dest, ss_char_w_t const* src)
  128. {
  129. return strcat_w(dest, src);
  130. }
  131. inline ss_char_a_t* strncpy_a(ss_char_a_t* dest, ss_char_a_t const* src, ss_size_t n)
  132. {
  133. return ::strncpy(dest, src, n);
  134. }
  135. inline ss_char_w_t* strncpy_w(ss_char_w_t* dest, ss_char_w_t const* src, ss_size_t n)
  136. {
  137. return ::wcsncpy(dest, src, n);
  138. }
  139. inline ss_char_a_t* strncpy_(ss_char_a_t* dest, ss_char_a_t const* src, ss_size_t n)
  140. {
  141. return strncpy_a(dest, src, n);
  142. }
  143. inline ss_char_w_t* strncpy_(ss_char_w_t* dest, ss_char_w_t const* src, ss_size_t n)
  144. {
  145. return strncpy_w(dest, src, n);
  146. }
  147. inline ss_char_a_t* strncat_a(ss_char_a_t* dest, ss_char_a_t const* src, ss_size_t n)
  148. {
  149. return ::strncat(dest, src, n);
  150. }
  151. inline ss_char_w_t* strncat_w(ss_char_w_t* dest, ss_char_w_t const* src, ss_size_t n)
  152. {
  153. return ::wcsncat(dest, src, n);
  154. }
  155. inline ss_char_a_t* strncat_(ss_char_a_t* dest, ss_char_a_t const* src, ss_size_t n)
  156. {
  157. return strncat_a(dest, src, n);
  158. }
  159. inline ss_char_w_t* strncat_(ss_char_w_t* dest, ss_char_w_t const* src, ss_size_t n)
  160. {
  161. return strncat_w(dest, src, n);
  162. }
  163. #ifndef _STLSOFT_NO_NAMESPACE
  164. } // namespace std_impl
  165. #endif /* _STLSOFT_NO_NAMESPACE */
  166. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  167. #ifdef STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT
  168. template <ss_typename_param_k S>
  169. inline ss_typename_type_ret_k string_traits<S>::char_type* strcpy(ss_typename_type_k string_traits<S>::char_type* dest, S const& src)
  170. {
  171. return stlsoft_std_ns_qual(strcpy_)(dest, stlsoft_ns_qual(c_str_ptr)(src));
  172. }
  173. #endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
  174. inline ss_char_a_t* strcpy(ss_char_a_t* dest, ss_char_a_t const* src)
  175. {
  176. return stlsoft_std_ns_qual(strcpy_)(dest, src);
  177. }
  178. inline ss_char_w_t* strcpy(ss_char_w_t* dest, ss_char_w_t const* src)
  179. {
  180. return stlsoft_std_ns_qual(strcpy_)(dest, src);
  181. }
  182. #ifdef STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT
  183. template <ss_typename_param_k S>
  184. inline ss_typename_type_ret_k string_traits<S>::char_type* strcat(ss_typename_type_k string_traits<S>::char_type* dest, S const& src)
  185. {
  186. return stlsoft_std_ns_qual(strcat_)(dest, stlsoft_ns_qual(c_str_ptr)(src));
  187. }
  188. #endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
  189. inline ss_char_a_t* strcat(ss_char_a_t* dest, ss_char_a_t const* src)
  190. {
  191. return stlsoft_std_ns_qual(strcat_)(dest, src);
  192. }
  193. inline ss_char_w_t* strcat(ss_char_w_t* dest, ss_char_w_t const* src)
  194. {
  195. return stlsoft_std_ns_qual(strcat_)(dest, src);
  196. }
  197. #ifdef STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT
  198. template <ss_typename_param_k S>
  199. inline ss_typename_type_ret_k string_traits<S>::char_type* strncpy(ss_typename_type_k string_traits<S>::char_type* dest, S const& src)
  200. {
  201. return stlsoft_std_ns_qual(strncpy_)(dest, stlsoft_ns_qual(c_str_data)(src), stlsoft_ns_qual(c_str_len)(src));
  202. }
  203. #endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
  204. #ifdef STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT
  205. template <ss_typename_param_k S>
  206. inline ss_typename_type_ret_k string_traits<S>::char_type* strncat(ss_typename_type_k string_traits<S>::char_type* dest, S const& src)
  207. {
  208. return stlsoft_std_ns_qual(strncat_)(dest, stlsoft_ns_qual(c_str_data)(src), stlsoft_ns_qual(c_str_len)(src));
  209. }
  210. #endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
  211. #ifdef STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT
  212. template <ss_typename_param_k S>
  213. inline ss_typename_type_ret_k string_traits<S>::char_type* strncpy(ss_typename_type_k string_traits<S>::char_type* dest, S const& src, ss_size_t n)
  214. {
  215. return stlsoft_std_ns_qual(strncpy_)(dest, stlsoft_ns_qual(c_str_data)(src), n);
  216. }
  217. #endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
  218. inline ss_char_a_t* strncpy(ss_char_a_t* dest, ss_char_a_t const* src, ss_size_t n)
  219. {
  220. return stlsoft_std_ns_qual(strncpy_)(dest, src, n);
  221. }
  222. inline ss_char_w_t* strncpy(ss_char_w_t* dest, ss_char_w_t const* src, ss_size_t n)
  223. {
  224. return stlsoft_std_ns_qual(strncpy_)(dest, src, n);
  225. }
  226. #ifdef STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT
  227. template <ss_typename_param_k S>
  228. inline ss_typename_type_ret_k string_traits<S>::char_type* strncat(ss_typename_type_k string_traits<S>::char_type* dest, S const& src, ss_size_t n)
  229. {
  230. return stlsoft_std_ns_qual(strncat_)(dest, stlsoft_ns_qual(c_str_data)(src), n);
  231. }
  232. #endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
  233. inline ss_char_a_t* strncat(ss_char_a_t* dest, ss_char_a_t const* src, ss_size_t n)
  234. {
  235. return stlsoft_std_ns_qual(strncat_)(dest, src, n);
  236. }
  237. inline ss_char_w_t* strncat(ss_char_w_t* dest, ss_char_w_t const* src, ss_size_t n)
  238. {
  239. return stlsoft_std_ns_qual(strncat_)(dest, src, n);
  240. }
  241. /// @}
  242. /// \name Length and comparison family
  243. /// @{
  244. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  245. #ifndef _STLSOFT_NO_NAMESPACE
  246. namespace std_impl
  247. {
  248. #endif /* _STLSOFT_NO_NAMESPACE */
  249. inline int strcmp_a(ss_char_a_t const* s1, ss_char_a_t const* s2)
  250. {
  251. return ::strcmp(s1, s2);
  252. }
  253. inline int strcmp_w(ss_char_w_t const* s1, ss_char_w_t const* s2)
  254. {
  255. return ::wcscmp(s1, s2);
  256. }
  257. inline int strcmp_(ss_char_a_t const* s1, ss_char_a_t const* s2)
  258. {
  259. return strcmp_a(s1, s2);
  260. }
  261. inline int strcmp_(ss_char_w_t const* s1, ss_char_w_t const* s2)
  262. {
  263. return strcmp_w(s1, s2);
  264. }
  265. inline int strncmp_a(ss_char_a_t const* s1, ss_char_a_t const* s2, ss_size_t n)
  266. {
  267. return ::strncmp(s1, s2, n);
  268. }
  269. inline int strncmp_w(ss_char_w_t const* s1, ss_char_w_t const* s2, ss_size_t n)
  270. {
  271. return ::wcsncmp(s1, s2, n);
  272. }
  273. inline int strncmp_(ss_char_a_t const* s1, ss_char_a_t const* s2, ss_size_t n)
  274. {
  275. return strncmp_a(s1, s2, n);
  276. }
  277. inline int strncmp_(ss_char_w_t const* s1, ss_char_w_t const* s2, ss_size_t n)
  278. {
  279. return strncmp_w(s1, s2, n);
  280. }
  281. #ifndef _STLSOFT_NO_NAMESPACE
  282. } // namespace std_impl
  283. #endif /* _STLSOFT_NO_NAMESPACE */
  284. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  285. template <ss_typename_param_k S>
  286. inline ss_size_t strlen(S const& s)
  287. {
  288. return stlsoft_ns_qual(c_str_len)(s);
  289. }
  290. inline ss_size_t strlen(ss_char_a_t const* s)
  291. {
  292. return ::strlen(s);
  293. }
  294. inline ss_size_t strlen(ss_char_w_t const* s)
  295. {
  296. return ::wcslen(s);
  297. }
  298. template <ss_typename_param_k S>
  299. inline int strcmp(S const& s1, S const& s2)
  300. {
  301. return stlsoft_std_ns_qual(strcmp_)(stlsoft_ns_qual(c_str_ptr)(s1), stlsoft_ns_qual(c_str_ptr)(s2));
  302. }
  303. inline int strcmp(ss_char_a_t const* s1, ss_char_a_t const* s2)
  304. {
  305. return ::strcmp(s1, s2);
  306. }
  307. inline int strcmp(ss_char_w_t const* s1, ss_char_w_t const* s2)
  308. {
  309. return ::wcscmp(s1, s2);
  310. }
  311. template <ss_typename_param_k S>
  312. inline int strncmp(S const& s1, S const& s2, ss_size_t n)
  313. {
  314. return stlsoft_std_ns_qual(strncmp_)(stlsoft_ns_qual(c_str_ptr)(s1), stlsoft_ns_qual(c_str_ptr)(s2), n);
  315. }
  316. inline int strncmp(ss_char_a_t const* s1, ss_char_a_t const* s2, ss_size_t n)
  317. {
  318. return ::strncmp(s1, s2, n);
  319. }
  320. inline int strncmp(ss_char_w_t const* s1, ss_char_w_t const* s2, ss_size_t n)
  321. {
  322. return ::wcsncmp(s1, s2, n);
  323. }
  324. /// @}
  325. /// \name Searching family
  326. /// @{
  327. inline ss_char_a_t const* strchr(ss_char_a_t const* s, ss_char_a_t ch)
  328. {
  329. return ::strchr(s, ch);
  330. }
  331. inline ss_char_w_t const* strchr(ss_char_w_t const* s, ss_char_w_t ch)
  332. {
  333. return ::wcschr(s, ch);
  334. }
  335. inline ss_char_a_t const* strrchr(ss_char_a_t const* s, ss_char_a_t ch)
  336. {
  337. return ::strrchr(s, ch);
  338. }
  339. inline ss_char_w_t const* strrchr(ss_char_w_t const* s, ss_char_w_t ch)
  340. {
  341. return ::wcsrchr(s, ch);
  342. }
  343. inline ss_char_a_t const* strstr(ss_char_a_t const* s, ss_char_a_t const* charSet)
  344. {
  345. return ::strstr(s, charSet);
  346. }
  347. inline ss_char_w_t const* strstr(ss_char_w_t const* s, ss_char_w_t const* charSet)
  348. {
  349. return ::wcsstr(s, charSet);
  350. }
  351. #if 0
  352. // NOTE: Can't do the ones that return pointers, since the shims may return temporaries. That would *not be good
  353. ss_size_t __cdecl strcspn(char const*, char const*);
  354. ss_size_t __cdecl strspn(char const*, char const*);
  355. char * __cdecl strpbrk(char const*, char const*);
  356. #endif /* 0 */
  357. /// @}
  358. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  359. #ifndef _STLSOFT_NO_NAMESPACE
  360. namespace std_impl
  361. {
  362. #endif /* _STLSOFT_NO_NAMESPACE */
  363. inline ss_char_a_t* strdup_a(ss_char_a_t const* s)
  364. {
  365. return ::strdup(s);
  366. }
  367. inline ss_char_w_t* strdup_w(ss_char_w_t const* s)
  368. {
  369. # if defined(STLSOFT_COMPILER_IS_BORLAND) || \
  370. ( defined(STLSOFT_COMPILER_IS_GCC) && \
  371. !defined(WIN32)) || \
  372. defined(STLSOFT_COMPILER_IS_WATCOM)
  373. const ss_size_t len = ::wcslen(s);
  374. ss_char_w_t *sz = static_cast<ss_char_w_t*>(::malloc(sizeof(ss_char_w_t) * (1 + len)));
  375. if(NULL != sz)
  376. {
  377. ::wcscpy(sz, s);
  378. }
  379. return sz;
  380. # else /* ? compiler */
  381. return ::wcsdup(const_cast<ss_char_w_t*>(s));
  382. # endif /* compiler */
  383. }
  384. inline ss_char_a_t* strdup_(ss_char_a_t const* s)
  385. {
  386. return strdup_a(s);
  387. }
  388. inline ss_char_w_t* strdup_(ss_char_w_t const* s)
  389. {
  390. return strdup_w(s);
  391. }
  392. #ifndef _STLSOFT_NO_NAMESPACE
  393. } // namespace std_impl
  394. #endif /* _STLSOFT_NO_NAMESPACE */
  395. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  396. /** \brief Duplicates the given string
  397. *
  398. * \ingroup group__library__utility
  399. *
  400. * \note The returned string is allocated by the standard strdup() function,
  401. * which uses malloc() to allocate the memory, and so must be freed using free()
  402. */
  403. #ifdef STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT
  404. template <ss_typename_param_k S>
  405. inline ss_typename_type_ret_k string_traits<S>::char_type* strdup(S const& s)
  406. {
  407. return stlsoft_std_ns_qual(strdup_)(stlsoft_ns_qual(c_str_ptr)(s));
  408. }
  409. #endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */
  410. /** \brief Duplicates the multibyte C-string
  411. *
  412. * \ingroup group__library__utility
  413. */
  414. inline ss_char_a_t* strdup(ss_char_a_t const* s)
  415. {
  416. return stlsoft_std_ns_qual(strdup_)(s);
  417. }
  418. /** \brief Duplicates the wide C-string
  419. *
  420. * \ingroup group__library__utility
  421. */
  422. inline ss_char_w_t* strdup(ss_char_w_t const* s)
  423. {
  424. return stlsoft_std_ns_qual(strdup_)(s);
  425. }
  426. /* ////////////////////////////////////////////////////////////////////// */
  427. #ifndef _STLSOFT_NO_NAMESPACE
  428. } // namespace stlsoft
  429. #endif /* _STLSOFT_NO_NAMESPACE */
  430. /* ////////////////////////////////////////////////////////////////////// */
  431. #endif /* !STLSOFT_INCL_STLSOFT_STD_HPP_CSTRING */
  432. /* ///////////////////////////// end of file //////////////////////////// */