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.

311 lines
9.0 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: comstl/string/olestring_functions.h (originally MOOleStr.h, ::SynesisCom)
  3. *
  4. * Purpose: Contains classes and functions for dealing with OLE/COM strings.
  5. *
  6. * Created: 2nd March 1996
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 1996-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 comstl/string/olestring_functions.h
  40. *
  41. * \brief [C, C++] Functions for manipulating OLE/COM strings
  42. * (\ref group__library__string "String" Library).
  43. */
  44. #ifndef COMSTL_INCL_COMSTL_STRING_H_OLESTRING_FUNCTIONS
  45. #define COMSTL_INCL_COMSTL_STRING_H_OLESTRING_FUNCTIONS
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define COMSTL_VER_COMSTL_STRING_H_OLESTRING_FUNCTIONS_MAJOR 5
  48. # define COMSTL_VER_COMSTL_STRING_H_OLESTRING_FUNCTIONS_MINOR 1
  49. # define COMSTL_VER_COMSTL_STRING_H_OLESTRING_FUNCTIONS_REVISION 4
  50. # define COMSTL_VER_COMSTL_STRING_H_OLESTRING_FUNCTIONS_EDIT 158
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef COMSTL_INCL_COMSTL_H_COMSTL
  56. # include <comstl/comstl.h>
  57. #endif /* !COMSTL_INCL_COMSTL_H_COMSTL */
  58. #ifndef COMSTL_INCL_COMSTL_MEMORY_H_MEMORY_FUNCTIONS
  59. # include <comstl/memory/functions.h>
  60. #endif /* !COMSTL_INCL_COMSTL_MEMORY_H_MEMORY_FUNCTIONS */
  61. #ifndef STLSOFT_INCL_STLSOFT_INTERNAL_H_SAFESTR
  62. # include <stlsoft/internal/safestr.h>
  63. #endif /* !STLSOFT_INCL_STLSOFT_INTERNAL_H_SAFESTR */
  64. #ifndef STLSOFT_INCL_H_WCHAR
  65. # define STLSOFT_INCL_H_WCHAR
  66. # include <wchar.h>
  67. #endif /* !STLSOFT_INCL_H_WCHAR */
  68. /* /////////////////////////////////////////////////////////////////////////
  69. * Namespace
  70. */
  71. #if !defined(_COMSTL_NO_NAMESPACE) && \
  72. !defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  73. # if defined(_STLSOFT_NO_NAMESPACE)
  74. /* There is no stlsoft namespace, so must define ::comstl */
  75. namespace comstl
  76. {
  77. # else
  78. /* Define stlsoft::comstl_project */
  79. namespace stlsoft
  80. {
  81. namespace comstl_project
  82. {
  83. # endif /* _STLSOFT_NO_NAMESPACE */
  84. #endif /* !_COMSTL_NO_NAMESPACE */
  85. /* /////////////////////////////////////////////////////////////////////////
  86. * C functions
  87. */
  88. /** \brief [C only] Creates an OLE string from an ANSI string
  89. *
  90. * \ingroup group__library__string
  91. *
  92. * \param s The string from which to create the OLE string
  93. * \result The created OLE string
  94. */
  95. STLSOFT_INLINE LPOLESTR comstl__olestring_create_a(cs_char_a_t const* s)
  96. {
  97. LPOLESTR posz;
  98. if(NULL == s)
  99. {
  100. posz = NULL;
  101. }
  102. else
  103. {
  104. /* NOTE: All this posz2 hoop jumping is needed because BC++ gets confused. Ahh, bless it ... */
  105. cs_size_t cch = stlsoft_static_cast(cs_size_t, STLSOFT_NS_GLOBAL(MultiByteToWideChar)(0, 0, s, -1, NULL, 0));
  106. const LPOLESTR posz2 = stlsoft_static_cast(LPOLESTR, STLSOFT_NS_GLOBAL(CoTaskMemAlloc)(sizeof(OLECHAR) * (cch + 1)));
  107. posz = posz2;
  108. if(NULL != posz)
  109. {
  110. cs_size_t n = stlsoft_static_cast(cs_size_t, STLSOFT_NS_GLOBAL(MultiByteToWideChar)(0, 0, s, -1, posz2, stlsoft_static_cast(int, cch + 1)));
  111. if(n < cch)
  112. {
  113. posz[n] = L'\0';
  114. }
  115. }
  116. }
  117. return posz;
  118. }
  119. /** \brief [C only] Creates an OLE string from a Unicode string
  120. *
  121. * \ingroup group__library__string
  122. *
  123. * \param s The string from which to create the OLE string
  124. * \result The created OLE string
  125. */
  126. STLSOFT_INLINE LPOLESTR comstl__olestring_create_w(cs_char_w_t const* s)
  127. {
  128. LPOLESTR posz;
  129. if(NULL == s)
  130. {
  131. posz = NULL;
  132. }
  133. else
  134. {
  135. cs_size_t cch = STLSOFT_NS_GLOBAL(wcslen)(s);
  136. posz = stlsoft_static_cast(LPOLESTR, STLSOFT_NS_GLOBAL(CoTaskMemAlloc)(sizeof(OLECHAR) * (cch + 1)));
  137. if(NULL != posz)
  138. {
  139. #ifdef STLSOFT_USING_SAFE_STR_FUNCTIONS
  140. STLSOFT_NS_GLOBAL(wcscpy_s)(posz, 1 + cch, s);
  141. #else /* ? STLSOFT_USING_SAFE_STR_FUNCTIONS */
  142. STLSOFT_NS_GLOBAL(wcscpy)(posz, s);
  143. #endif /* STLSOFT_USING_SAFE_STR_FUNCTIONS */
  144. }
  145. }
  146. return posz;
  147. }
  148. /** \brief [C only] Destroys an OLE string
  149. *
  150. * \ingroup group__library__string
  151. *
  152. * \param posz The OLE string to destroy
  153. */
  154. STLSOFT_INLINE void comstl__olestring_destroy(LPOLESTR posz)
  155. {
  156. COMSTL_MESSAGE_ASSERT("Invalid request to destroy non-COM string", 0 != comstl__CoTaskMemDidAlloc(posz));
  157. STLSOFT_NS_GLOBAL(CoTaskMemFree)(stlsoft_static_cast(LPVOID, posz));
  158. }
  159. /** \brief [C only] Duplicates an OLE string
  160. *
  161. * \ingroup group__library__string
  162. *
  163. * \param posz The OLE string to duplicate
  164. * \return The copied OLE string
  165. */
  166. STLSOFT_INLINE LPOLESTR comstl__olestring_dup(LPCOLESTR posz)
  167. {
  168. return comstl__olestring_create_w(posz);
  169. }
  170. /* /////////////////////////////////////////////////////////////////////////
  171. * Namespace
  172. */
  173. #ifdef STLSOFT_DOCUMENTATION_SKIP_SECTION
  174. namespace comstl
  175. {
  176. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  177. /* /////////////////////////////////////////////////////////////////////////
  178. * C++ functions
  179. */
  180. #ifdef __cplusplus
  181. /** \brief Creates an OLE string from an ANSI string
  182. *
  183. * \ingroup group__library__string
  184. *
  185. * \param s The string from which to create the OLE string
  186. * \result The created OLE string
  187. */
  188. inline LPOLESTR olestring_create_a(cs_char_a_t const* s)
  189. {
  190. return comstl__olestring_create_a(s);
  191. }
  192. /** \brief Creates an OLE string from a Unicode string
  193. *
  194. * \ingroup group__library__string
  195. *
  196. * \param s The string from which to create the OLE string
  197. * \result The created OLE string
  198. */
  199. inline LPOLESTR olestring_create_w(cs_char_w_t const* s)
  200. {
  201. return comstl__olestring_create_w(s);
  202. }
  203. /** \brief Creates an OLE string from an ANSI string
  204. *
  205. * \ingroup group__library__string
  206. *
  207. * \param s The string from which to create the OLE string
  208. * \result The created OLE string
  209. */
  210. inline LPOLESTR olestring_create(cs_char_a_t const* s)
  211. {
  212. return olestring_create_a(s);
  213. }
  214. /** \brief Creates an OLE string from a Unicode string
  215. *
  216. * \ingroup group__library__string
  217. *
  218. * \param s The string from which to create the OLE string
  219. * \result The created OLE string
  220. */
  221. inline LPOLESTR olestring_create(cs_char_w_t const* s)
  222. {
  223. return olestring_create_w(s);
  224. }
  225. /** \brief Destroys an OLE string
  226. *
  227. * \ingroup group__library__string
  228. *
  229. * \param posz The OLE string to destroy
  230. */
  231. inline void olestring_destroy(LPOLESTR posz)
  232. {
  233. comstl__olestring_destroy(posz);
  234. }
  235. /** \brief Duplicates an OLE string
  236. *
  237. * \ingroup group__library__string
  238. *
  239. * \param posz The OLE string to duplicate
  240. * \return The copied OLE string
  241. */
  242. inline LPOLESTR olestring_dup(LPCOLESTR posz)
  243. {
  244. return olestring_create(posz);
  245. }
  246. #endif /* __cplusplus */
  247. /* /////////////////////////////////////////////////////////////////////////
  248. * Unit-testing
  249. */
  250. #ifdef STLSOFT_UNITTEST
  251. # include "./unittest/olestring_functions_unittest_.h"
  252. #endif /* STLSOFT_UNITTEST */
  253. /* ////////////////////////////////////////////////////////////////////// */
  254. #ifndef _COMSTL_NO_NAMESPACE
  255. # if defined(_STLSOFT_NO_NAMESPACE) || \
  256. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  257. } /* namespace comstl */
  258. # else
  259. } /* namespace comstl_project */
  260. } /* namespace stlsoft */
  261. # endif /* _STLSOFT_NO_NAMESPACE */
  262. #endif /* !_COMSTL_NO_NAMESPACE */
  263. /* ////////////////////////////////////////////////////////////////////// */
  264. #endif /* !COMSTL_INCL_COMSTL_STRING_H_OLESTRING_FUNCTIONS */
  265. /* ///////////////////////////// end of file //////////////////////////// */