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.

331 lines
10 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/string/cstring_maker.hpp
  3. *
  4. * Purpose: Simple utility class for creating (constant) C-strings.
  5. *
  6. * Created: 14th May 2004
  7. * Updated: 17th April 2012
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2004-2012, 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/string/cstring_maker.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::string_maker class template
  42. * (\ref group__library__string "String" Library).
  43. */
  44. #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_CSTRING_MAKER
  45. #define STLSOFT_INCL_STLSOFT_STRING_HPP_CSTRING_MAKER
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define STLSOFT_VER_STLSOFT_STRING_HPP_CSTRING_MAKER_MAJOR 4
  48. # define STLSOFT_VER_STLSOFT_STRING_HPP_CSTRING_MAKER_MINOR 0
  49. # define STLSOFT_VER_STLSOFT_STRING_HPP_CSTRING_MAKER_REVISION 2
  50. # define STLSOFT_VER_STLSOFT_STRING_HPP_CSTRING_MAKER_EDIT 46
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  56. # include <stlsoft/stlsoft.h>
  57. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  58. #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS
  59. # include <stlsoft/string/char_traits.hpp>
  60. #endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS */
  61. #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_BASE
  62. # include <stlsoft/memory/allocator_base.hpp> // for STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT
  63. #endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_BASE */
  64. #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_SELECTOR
  65. # include <stlsoft/memory/allocator_selector.hpp>
  66. #endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_SELECTOR */
  67. /* /////////////////////////////////////////////////////////////////////////
  68. * Namespace
  69. */
  70. #ifndef _STLSOFT_NO_NAMESPACE
  71. namespace stlsoft
  72. {
  73. #endif /* _STLSOFT_NO_NAMESPACE */
  74. /* /////////////////////////////////////////////////////////////////////////
  75. * Classes
  76. */
  77. #if !defined(_STLSOFT_NO_NAMESPACE) && \
  78. ( !defined(STLSOFT_COMPILER_IS_MSVC) || \
  79. _MSC_VER > 1200)
  80. namespace
  81. {
  82. #endif /* _STLSOFT_NO_NAMESPACE */
  83. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  84. template <ss_typename_param_k C>
  85. struct char_to_byte__traits_
  86. {
  87. static ss_size_t byte_size_(ss_size_t cch)
  88. {
  89. return cch * sizeof(C);
  90. }
  91. };
  92. STLSOFT_TEMPLATE_SPECIALISATION
  93. struct char_to_byte__traits_<ss_char_a_t>
  94. {
  95. static ss_size_t byte_size_(ss_size_t cch)
  96. {
  97. return cch;
  98. }
  99. };
  100. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  101. #if !defined(_STLSOFT_NO_NAMESPACE) && \
  102. ( !defined(STLSOFT_COMPILER_IS_MSVC) || \
  103. _MSC_VER > 1200)
  104. } // anonymous namespace
  105. #endif /* _STLSOFT_NO_NAMESPACE */
  106. /** \brief Utility class that creates/destroys fixed-size C-strings for
  107. * other classes.
  108. *
  109. * \ingroup group__library__string
  110. */
  111. #if defined(STLSOFT_DOCUMENTATION_SKIP_SECTION) || \
  112. ( ( !defined(STLSOFT_COMPILER_IS_GCC) || \
  113. __GNUC__ >= 3) && \
  114. ( !defined(STLSOFT_COMPILER_IS_MSVC) || \
  115. _MSC_VER != 1100))
  116. template< ss_typename_param_k C
  117. , ss_typename_param_k A = ss_typename_type_def_k allocator_selector<C>::allocator_type
  118. , ss_typename_param_k T = stlsoft_char_traits<C>
  119. >
  120. struct cstring_maker
  121. {
  122. typedef C char_type;
  123. typedef A allocator_type;
  124. typedef T traits_type;
  125. typedef ss_size_t size_type;
  126. typedef cstring_maker<C, A, T> class_type;
  127. private:
  128. struct block
  129. {
  130. size_type n;
  131. char_type data[1];
  132. };
  133. public:
  134. /// Allocates uninitialised storage for writing into a string
  135. ///
  136. /// \note Includes space for a nul-terminator character
  137. ///
  138. /// \param cch The number of characters to allocate
  139. static char_type* alloc(size_type cch)
  140. {
  141. #if defined(WIN32) || \
  142. defined(_WIN32)
  143. cch = char_to_byte__traits_<char_type>::byte_size_(cch);
  144. #endif /* WIN32 || _WIN32 */
  145. size_type cb = offsetof(block, data) + sizeof(char_type) * (1 + cch);
  146. #ifdef STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT
  147. ss_typename_type_k allocator_type::ss_template_qual_k rebind<ss_byte_t>::other byte_ator;
  148. #else /* ? STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT */
  149. allocator_selector<ss_byte_t>::allocator_type byte_ator;
  150. #endif /* STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT */
  151. cb = (cb + 31) & ~31;
  152. block *pblock = static_cast<block*>(static_cast<void*>(byte_ator.allocate(cb, NULL)));
  153. if(NULL == pblock)
  154. {
  155. return NULL;
  156. }
  157. else
  158. {
  159. pblock->n = cch;
  160. pblock->data[cch] = '\0';
  161. return &pblock->data[0];
  162. }
  163. }
  164. /// Allocates a new copy of the given string
  165. ///
  166. /// \note Includes nul-terminator character
  167. ///
  168. /// \param s The string to copy. Cannot be null pointer
  169. static char_type* dup(char_type const* s)
  170. {
  171. STLSOFT_ASSERT(NULL != s);
  172. size_type len = traits_type::length(s);
  173. char_type *s_ = alloc(len);
  174. if(NULL != s_)
  175. {
  176. traits_type::copy(s_, s, 1 + len);
  177. }
  178. return s_;
  179. }
  180. /// Allocates a new copy of the given string, or returns NULL if given null string
  181. ///
  182. /// \note Includes nul-terminator character
  183. ///
  184. /// \param s The string to copy. Can be null pointer, in which case NULL is returned
  185. static char_type* dup_null(char_type const* s)
  186. {
  187. return (NULL == s) ? NULL : dup(s);
  188. }
  189. /// Releases the storage associated with the string
  190. ///
  191. /// \param s The string to free
  192. static void free(char_type* s)
  193. {
  194. #ifdef STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT
  195. ss_typename_type_k allocator_type::ss_template_qual_k rebind<ss_byte_t>::other byte_ator;
  196. #else /* ? STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT */
  197. allocator_selector<ss_byte_t>::allocator_type byte_ator;
  198. #endif /* STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT */
  199. if(NULL != s)
  200. {
  201. union
  202. {
  203. ss_byte_t *py;
  204. block *pblock;
  205. } u;
  206. u.py = static_cast<ss_byte_t*>(static_cast<void*>(s)) - offsetof(block, data);
  207. STLSOFT_ASSERT(u.pblock->data[u.pblock->n] == '\0');
  208. byte_ator.deallocate(u.py, u.pblock->n);
  209. }
  210. }
  211. };
  212. #else /* ? _MSC_VER */
  213. template< ss_typename_param_k C
  214. >
  215. struct cstring_maker;
  216. STLSOFT_TEMPLATE_SPECIALISATION
  217. struct cstring_maker<char>
  218. {
  219. public:
  220. typedef char char_type;
  221. typedef allocator_selector<char_type>::allocator_type allocator_type;
  222. typedef char_traits<char_type> traits_type;
  223. typedef ss_size_t size_type;
  224. typedef cstring_maker<char_type> class_type;
  225. public:
  226. static char_type* alloc(size_type cch)
  227. {
  228. return static_cast<char_type*>(malloc((1 + cch) * sizeof(char_type)));
  229. }
  230. static char_type* dup(char_type const* s)
  231. {
  232. return strdup(s);
  233. }
  234. static char_type* dup_null(char_type const* s)
  235. {
  236. return (NULL == s) ? NULL : dup(s);
  237. }
  238. static void free(char_type* s)
  239. {
  240. ::free(s);
  241. }
  242. };
  243. # if !defined(STLSOFT_COMPILER_IS_GCC)
  244. STLSOFT_TEMPLATE_SPECIALISATION
  245. struct cstring_maker<wchar_t>
  246. {
  247. public:
  248. typedef wchar_t char_type;
  249. typedef allocator_selector<char_type>::allocator_type allocator_type;
  250. typedef char_traits<char_type> traits_type;
  251. typedef ss_size_t size_type;
  252. typedef cstring_maker<char_type> class_type;
  253. public:
  254. static char_type* alloc(size_type cch)
  255. {
  256. return static_cast<char_type*>(malloc((1 + cch) * sizeof(char_type)));
  257. }
  258. static char_type* dup(char_type const* s)
  259. {
  260. return _wcsdup(s);
  261. }
  262. static char_type* dup_null(char_type const* s)
  263. {
  264. return (NULL == s) ? NULL : dup(s);
  265. }
  266. static void free(char_type* s)
  267. {
  268. ::free(s);
  269. }
  270. };
  271. # endif /* compiler */
  272. #endif /* _MSC_VER != 1100 */
  273. ////////////////////////////////////////////////////////////////////////////
  274. // Unit-testing
  275. #ifdef STLSOFT_UNITTEST
  276. # include "./unittest/cstring_maker_unittest_.h"
  277. #endif /* STLSOFT_UNITTEST */
  278. /* ////////////////////////////////////////////////////////////////////// */
  279. #ifndef _STLSOFT_NO_NAMESPACE
  280. } // namespace stlsoft
  281. #endif /* _STLSOFT_NO_NAMESPACE */
  282. /* ////////////////////////////////////////////////////////////////////// */
  283. #endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_CSTRING_MAKER */
  284. /* ///////////////////////////// end of file //////////////////////////// */