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.

305 lines
9.3 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: mfcstl/shims/access/string/cstring.hpp
  3. *
  4. * Purpose: Contains classes and functions for dealing with MFC strings.
  5. *
  6. * Created: 24th May 2002
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2002-2009, 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 mfcstl/shims/access/string/cstring.hpp
  40. *
  41. * \brief [C++] Definition of the string access shims for
  42. * <code>CString</code>
  43. * (\ref group__concept__shim__string_access "String Access Shims" Concept).
  44. */
  45. #ifndef MFCSTL_INCL_MFCSTL_SHIMS_ACCESS_STRING_HPP_CSTRING
  46. #define MFCSTL_INCL_MFCSTL_SHIMS_ACCESS_STRING_HPP_CSTRING
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define MFCSTL_VER_MFCSTL_SHIMS_ACCESS_STRING_HPP_CSTRING_MAJOR 4
  49. # define MFCSTL_VER_MFCSTL_SHIMS_ACCESS_STRING_HPP_CSTRING_MINOR 0
  50. # define MFCSTL_VER_MFCSTL_SHIMS_ACCESS_STRING_HPP_CSTRING_REVISION 1
  51. # define MFCSTL_VER_MFCSTL_SHIMS_ACCESS_STRING_HPP_CSTRING_EDIT 89
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Includes
  55. */
  56. #ifndef MFCSTL_INCL_MFCSTL_HPP_MFCSTL
  57. # include <mfcstl/mfcstl.hpp>
  58. #endif /* !MFCSTL_INCL_MFCSTL_HPP_MFCSTL */
  59. #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
  60. # include <stlsoft/shims/access/string.hpp>
  61. #endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING */
  62. /* /////////////////////////////////////////////////////////////////////////
  63. * Namespace
  64. */
  65. #ifndef _MFCSTL_NO_NAMESPACE
  66. # if defined(_STLSOFT_NO_NAMESPACE) || \
  67. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  68. /* There is no stlsoft namespace, so must define ::mfcstl */
  69. namespace mfcstl
  70. {
  71. # else
  72. /* Define stlsoft::mfcstl_project */
  73. namespace stlsoft
  74. {
  75. namespace mfcstl_project
  76. {
  77. # endif /* _STLSOFT_NO_NAMESPACE */
  78. #endif /* !_MFCSTL_NO_NAMESPACE */
  79. /* /////////////////////////////////////////////////////////////////////////
  80. * c_str_data
  81. *
  82. * This can be applied to an expression, and the return value is either a
  83. * pointer to the character string or to an empty string.
  84. */
  85. /* CString */
  86. /** \brief \ref group__concept__shim__string_access__c_str_data for CString
  87. *
  88. * \ingroup group__concept__shim__string_access
  89. *
  90. */
  91. inline LPCTSTR c_str_data(CString const& s)
  92. {
  93. /* CString always points to valid memory, whether its own
  94. * CStringData or afxEmptyString.m_pchData
  95. */
  96. return s;
  97. }
  98. #if defined(UNICODE)
  99. inline LPCTSTR c_str_data_w(CString const& s)
  100. #else /* ? UNICODE */
  101. inline LPCTSTR c_str_data_a(CString const& s)
  102. #endif /* UNICODE */
  103. {
  104. return c_str_data(s);
  105. }
  106. /* /////////////////////////////////////////////////////////////////////////
  107. * c_str_len
  108. *
  109. * This can be applied to an expression, and the return value is the number of
  110. * characters in the character string in the expression.
  111. */
  112. /* CString */
  113. /** \brief \ref group__concept__shim__string_access__c_str_len for CString
  114. *
  115. * \ingroup group__concept__shim__string_access
  116. *
  117. */
  118. inline ms_size_t c_str_len(CString const& s)
  119. {
  120. return s.GetLength();
  121. }
  122. #if defined(UNICODE)
  123. inline ms_size_t c_str_len_w(CString const& s)
  124. #else /* ? UNICODE */
  125. inline ms_size_t c_str_len_a(CString const& s)
  126. #endif /* UNICODE */
  127. {
  128. return c_str_len(s);
  129. }
  130. /* /////////////////////////////////////////////////////////////////////////
  131. * c_str_ptr
  132. *
  133. * This can be applied to an expression, and the return value is either a
  134. * pointer to the character string or to an empty string.
  135. */
  136. /* CString */
  137. /** \brief \ref group__concept__shim__string_access__c_str_ptr for CString
  138. *
  139. * \ingroup group__concept__shim__string_access
  140. *
  141. */
  142. inline LPCTSTR c_str_ptr(CString const& s)
  143. {
  144. /* CString always points to valid memory, whether its own
  145. * CStringData or afxEmptyString.m_pchData
  146. */
  147. return s;
  148. }
  149. #if defined(UNICODE)
  150. inline LPCTSTR c_str_ptr_w(CString const& s)
  151. #else /* ? UNICODE */
  152. inline LPCTSTR c_str_ptr_a(CString const& s)
  153. #endif /* UNICODE */
  154. {
  155. return c_str_ptr(s);
  156. }
  157. /* /////////////////////////////////////////////////////////////////////////
  158. * c_str_ptr_null
  159. *
  160. * This can be applied to an expression, and the return value is either a
  161. * pointer to the character string or NULL.
  162. */
  163. /* CString */
  164. /** \brief \ref group__concept__shim__string_access__c_str_ptr_null for CString
  165. *
  166. * \ingroup group__concept__shim__string_access
  167. *
  168. */
  169. inline LPCTSTR c_str_ptr_null(CString const& s)
  170. {
  171. /* CString always points to valid memory, whether its own
  172. * CStringData or afxEmptyString.m_pchData
  173. */
  174. return s.IsEmpty() ? NULL : (LPCTSTR)s;
  175. }
  176. #if defined(UNICODE)
  177. inline LPCTSTR c_str_ptr_null_w(CString const& s)
  178. #else /* ? UNICODE */
  179. inline LPCTSTR c_str_ptr_null_a(CString const& s)
  180. #endif /* UNICODE */
  181. {
  182. return c_str_ptr_null(s);
  183. }
  184. ////////////////////////////////////////////////////////////////////////////
  185. // Unit-testing
  186. #ifdef STLSOFT_UNITTEST
  187. # include "./unittest/cstring_unittest_.h"
  188. #endif /* STLSOFT_UNITTEST */
  189. /* ////////////////////////////////////////////////////////////////////// */
  190. #ifndef _MFCSTL_NO_NAMESPACE
  191. # if defined(_STLSOFT_NO_NAMESPACE) || \
  192. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  193. } // namespace mfcstl
  194. # else
  195. } // namespace stlsoft::mfcstl_project
  196. } // namespace stlsoft
  197. # endif /* _STLSOFT_NO_NAMESPACE */
  198. #endif /* !_MFCSTL_NO_NAMESPACE */
  199. /* /////////////////////////////////////////////////////////////////////////
  200. * Namespace
  201. *
  202. * The string access shims exist either in the stlsoft namespace, or in the
  203. * global namespace. This is required by the lookup rules.
  204. *
  205. */
  206. #ifndef _MFCSTL_NO_NAMESPACE
  207. # if !defined(_STLSOFT_NO_NAMESPACE) && \
  208. !defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  209. namespace stlsoft
  210. {
  211. # else /* ? _STLSOFT_NO_NAMESPACE */
  212. /* There is no stlsoft namespace, so must define in the global namespace */
  213. # endif /* !_STLSOFT_NO_NAMESPACE */
  214. using ::mfcstl::c_str_data;
  215. #if defined(UNICODE)
  216. using ::mfcstl::c_str_data_w;
  217. #else /* ? UNICODE */
  218. using ::mfcstl::c_str_data_a;
  219. #endif /* UNICODE */
  220. using ::mfcstl::c_str_len;
  221. #if defined(UNICODE)
  222. using ::mfcstl::c_str_len_w;
  223. #else /* ? UNICODE */
  224. using ::mfcstl::c_str_len_a;
  225. #endif /* UNICODE */
  226. using ::mfcstl::c_str_ptr;
  227. #if defined(UNICODE)
  228. using ::mfcstl::c_str_ptr_w;
  229. #else /* ? UNICODE */
  230. using ::mfcstl::c_str_ptr_a;
  231. #endif /* UNICODE */
  232. using ::mfcstl::c_str_ptr_null;
  233. #if defined(UNICODE)
  234. using ::mfcstl::c_str_ptr_null_w;
  235. #else /* ? UNICODE */
  236. using ::mfcstl::c_str_ptr_null_a;
  237. #endif /* UNICODE */
  238. # if !defined(_STLSOFT_NO_NAMESPACE) && \
  239. !defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  240. } // namespace stlsoft
  241. # else /* ? _STLSOFT_NO_NAMESPACE */
  242. /* There is no stlsoft namespace, so must define in the global namespace */
  243. # endif /* !_STLSOFT_NO_NAMESPACE */
  244. #endif /* !_MFCSTL_NO_NAMESPACE */
  245. /* /////////////////////////////////////////////////////////////////////////
  246. * Global namespace shims
  247. */
  248. /* This defines a stream inserter shim function template for CString for use
  249. * with the Visual C++ <7.1 standard library.
  250. *
  251. * It cannot be defined as a template because that conflicts with operator << (CArchive &, CString const&)
  252. */
  253. #if defined(STLSOFT_CF_STD_LIBRARY_IS_DINKUMWARE_VC) && \
  254. STLSOFT_CF_STD_LIBRARY_DINKUMWARE_VC_VERSION < STLSOFT_CF_DINKUMWARE_VC_VERSION_7_1
  255. # include <iosfwd>
  256. /* Has to be like this otherwise it conflicts with operator <<(class CArchive &,const class CString &) (at least with VC++ <7.1). */
  257. inline mfcstl_ns_qual_std(basic_ostream)<TCHAR>& operator <<(mfcstl_ns_qual_std(basic_ostream)<TCHAR> &stm, CString const& shim)
  258. {
  259. return stm << static_cast<LPCTSTR>(shim);
  260. }
  261. #endif /* library */
  262. /* ////////////////////////////////////////////////////////////////////// */
  263. #endif /* !MFCSTL_INCL_MFCSTL_SHIMS_ACCESS_STRING_HPP_CSTRING */
  264. /* ///////////////////////////// end of file //////////////////////////// */