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.

267 lines
7.5 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/std/cstdlib.hpp
  3. *
  4. * Purpose: Mappings to stdlib string functions
  5. *
  6. * Created: 2nd December 2004
  7. * Updated: 31st March 2010
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2004-2010, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file stlsoft/std/cstdlib.hpp
  40. *
  41. * \brief [C++ only] Mappings of stdlib string functions that use
  42. * \ref group__concept__shim__string_access string
  43. * (\ref group__library__utility "Utility" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_STD_HPP_CSTDLIB
  46. #define STLSOFT_INCL_STLSOFT_STD_HPP_CSTDLIB
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_STD_HPP_CSTDLIB_MAJOR 2
  49. # define STLSOFT_VER_STLSOFT_STD_HPP_CSTDLIB_MINOR 0
  50. # define STLSOFT_VER_STLSOFT_STD_HPP_CSTDLIB_REVISION 1
  51. # define STLSOFT_VER_STLSOFT_STD_HPP_CSTDLIB_EDIT 25
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Includes
  55. */
  56. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  57. # include <stlsoft/stlsoft.h>
  58. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  59. #ifndef STLSOFT_INCL_STLSOFT_STD_HPP_CBASE_
  60. # include <stlsoft/std/cbase_.hpp>
  61. #endif /* !STLSOFT_INCL_STLSOFT_STD_HPP_CBASE_ */
  62. #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
  63. # include <stlsoft/shims/access/string.hpp>
  64. #endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING */
  65. #ifndef STLSOFT_INCL_H_STDLIB
  66. # define STLSOFT_INCL_H_STDLIB
  67. # include <stdlib.h>
  68. #endif /* !STLSOFT_INCL_H_STDLIB */
  69. /* /////////////////////////////////////////////////////////////////////////
  70. * Compiler / library feature discrimination
  71. */
  72. #define STLSOFT_STD_CSTDLIB_ATOI_SUPPORTED
  73. #define STLSOFT_STD_CSTDLIB_ATOL_SUPPORTED
  74. #define STLSOFT_STD_CSTDLIB_ATOF_SUPPORTED
  75. #if defined(STLSOFT_COMPILER_IS_BORLAND) || \
  76. defined(STLSOFT_COMPILER_IS_DMC) || \
  77. ( defined(STLSOFT_COMPILER_IS_GCC) && \
  78. defined(WIN32)) || \
  79. ( defined(STLSOFT_COMPILER_IS_INTEL) && \
  80. defined(WIN32)) || \
  81. defined(STLSOFT_COMPILER_IS_MSVC) || \
  82. defined(STLSOFT_COMPILER_IS_MWERKS) || \
  83. defined(STLSOFT_COMPILER_IS_WATCOM)
  84. # define STLSOFT_STD_CSTDLIB_WTOI_SUPPORTED
  85. #endif /* compiler */
  86. #if defined(STLSOFT_COMPILER_IS_BORLAND) || \
  87. defined(STLSOFT_COMPILER_IS_DMC) || \
  88. ( defined(STLSOFT_COMPILER_IS_GCC) && \
  89. defined(WIN32)) || \
  90. ( defined(STLSOFT_COMPILER_IS_INTEL) && \
  91. defined(WIN32)) || \
  92. defined(STLSOFT_COMPILER_IS_MSVC) || \
  93. defined(STLSOFT_COMPILER_IS_WATCOM)
  94. # define STLSOFT_STD_CSTDLIB_WTOL_SUPPORTED
  95. #endif /* compiler */
  96. #if defined(STLSOFT_COMPILER_IS_BORLAND) || \
  97. ( defined(STLSOFT_COMPILER_IS_MSVC) && \
  98. _MSC_VER >= 1300) || \
  99. defined(STLSOFT_COMPILER_IS_WATCOM)
  100. # define STLSOFT_STD_CSTDLIB_WTOF_SUPPORTED
  101. #endif /* compiler */
  102. /* /////////////////////////////////////////////////////////////////////////
  103. * Namespace
  104. */
  105. #ifndef _STLSOFT_NO_NAMESPACE
  106. namespace stlsoft
  107. {
  108. #endif /* _STLSOFT_NO_NAMESPACE */
  109. /* /////////////////////////////////////////////////////////////////////////
  110. * Helper classes
  111. */
  112. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  113. struct atoi_helper
  114. {
  115. static int call(char const* s)
  116. {
  117. return ::atoi(s);
  118. }
  119. #ifdef STLSOFT_STD_CSTDLIB_WTOI_SUPPORTED
  120. static int call(wchar_t const* s)
  121. {
  122. return ::_wtoi(s);
  123. }
  124. #endif /* STLSOFT_STD_CSTDLIB_WTOI_SUPPORTED */
  125. };
  126. struct atol_helper
  127. {
  128. static long call(char const* s)
  129. {
  130. return ::atol(s);
  131. }
  132. #ifdef STLSOFT_STD_CSTDLIB_WTOL_SUPPORTED
  133. static long call(wchar_t const* s)
  134. {
  135. return ::_wtol(s);
  136. }
  137. #endif /* STLSOFT_STD_CSTDLIB_WTOL_SUPPORTED */
  138. };
  139. struct atof_helper
  140. {
  141. static double call(char const* s)
  142. {
  143. return ::atof(s);
  144. }
  145. #ifdef STLSOFT_STD_CSTDLIB_WTOF_SUPPORTED
  146. static double call(wchar_t const* s)
  147. {
  148. return ::_wtof(s);
  149. }
  150. #endif /* STLSOFT_STD_CSTDLIB_WTOF_SUPPORTED */
  151. };
  152. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  153. /* /////////////////////////////////////////////////////////////////////////
  154. * Functions
  155. */
  156. /// \name atoi family
  157. /// @{
  158. template <ss_typename_param_k S>
  159. inline int atoi(S const& s)
  160. {
  161. return atoi_helper::call(stlsoft_ns_qual(c_str_ptr)(s));
  162. }
  163. template <ss_typename_param_k S>
  164. inline long atol(S const& s)
  165. {
  166. return atol_helper::call(stlsoft_ns_qual(c_str_ptr)(s));
  167. }
  168. template <ss_typename_param_k S>
  169. inline double atof(S const& s)
  170. {
  171. return atof_helper::call(stlsoft_ns_qual(c_str_ptr)(s));
  172. }
  173. /// @}
  174. /// \name strtol family
  175. /// @{
  176. inline long strtol(ss_char_a_t const* s, ss_char_a_t** endptr, int radix)
  177. {
  178. return ::strtol(s, endptr, radix);
  179. }
  180. inline long strtol(ss_char_w_t const* s, ss_char_w_t** endptr, int radix)
  181. {
  182. return ::wcstol(s, endptr, radix);
  183. }
  184. inline unsigned long strtoul(ss_char_a_t const* s, ss_char_a_t** endptr, int radix)
  185. {
  186. return ::strtoul(s, endptr, radix);
  187. }
  188. inline unsigned long strtoul(ss_char_w_t const* s, ss_char_w_t** endptr, int radix)
  189. {
  190. return ::wcstoul(s, endptr, radix);
  191. }
  192. inline double strtod(ss_char_a_t const* s, ss_char_a_t** endptr)
  193. {
  194. return ::strtod(s, endptr);
  195. }
  196. inline double strtod(ss_char_w_t const* s, ss_char_w_t** endptr)
  197. {
  198. return ::wcstod(s, endptr);
  199. }
  200. /// @}
  201. /** \brief system()
  202. *
  203. * \ingroup group__library__utility
  204. */
  205. template <ss_typename_param_k S>
  206. inline int system(S const& s)
  207. {
  208. return ::system(stlsoft_ns_qual(c_str_ptr)(s));
  209. }
  210. /* /////////////////////////////////////////////////////////////////////////
  211. * Unit-testing
  212. */
  213. #ifdef STLSOFT_UNITTEST
  214. # include "./unittest/cstdlib_unittest_.h"
  215. #endif /* STLSOFT_UNITTEST */
  216. /* ////////////////////////////////////////////////////////////////////// */
  217. #ifndef _STLSOFT_NO_NAMESPACE
  218. } // namespace stlsoft
  219. #endif /* _STLSOFT_NO_NAMESPACE */
  220. /* ////////////////////////////////////////////////////////////////////// */
  221. #endif /* !STLSOFT_INCL_STLSOFT_STD_HPP_CSTDLIB */
  222. /* ///////////////////////////// end of file //////////////////////////// */