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.

141 lines
5.0 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/string/copy_functions.hpp
  3. *
  4. * Purpose: String utility functions for copying.
  5. *
  6. * Created: 13th June 2006
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2006-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 stlsoft/string/copy_functions.hpp
  40. *
  41. * \brief [C++ only] String utility functions for copying
  42. * (\ref group__library__string "String" Library).
  43. */
  44. #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_COPY_FUNCTIONS
  45. #define STLSOFT_INCL_STLSOFT_STRING_HPP_COPY_FUNCTIONS
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define STLSOFT_VER_INCL_STLSOFT_STRING_HPP_COPY_FUNCTIONS_MAJOR 1
  48. # define STLSOFT_VER_INCL_STLSOFT_STRING_HPP_COPY_FUNCTIONS_MINOR 0
  49. # define STLSOFT_VER_INCL_STLSOFT_STRING_HPP_COPY_FUNCTIONS_REVISION 2
  50. # define STLSOFT_VER_INCL_STLSOFT_STRING_HPP_COPY_FUNCTIONS_EDIT 7
  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. #ifdef STLSOFT_UNITTEST
  62. # include <string.h>
  63. #endif /* STLSOFT_UNITTEST */
  64. /* /////////////////////////////////////////////////////////////////////////
  65. * Namespace
  66. */
  67. #ifndef _STLSOFT_NO_NAMESPACE
  68. namespace stlsoft
  69. {
  70. #endif /* _STLSOFT_NO_NAMESPACE */
  71. /* ////////////////////////////////////////////////////////////////////// */
  72. /** \brief Utility function for copying C-string contents into a caller
  73. * supplied buffer, which may be NULL to measure the required extent.
  74. *
  75. * \ingroup group__library__string
  76. *
  77. * \param dest Pointer to a character buffer that will receive the
  78. * contents. May be NULL, in which case the function returns \c cchSource.
  79. * \param cchDest The maximum number of characters to be written into
  80. * \c dest.
  81. * \param src Pointer to character buffer whose contents will be copied
  82. * into \c dest. May not be NULL.
  83. * \param cchSource The number of characters in \c src.
  84. *
  85. */
  86. template <ss_typename_param_k C>
  87. inline ss_size_t copy_contents(C *dest, ss_size_t cchDest, C const* src, ss_size_t cchSource)
  88. {
  89. STLSOFT_ASSERT(NULL != src);
  90. if(NULL == dest)
  91. {
  92. return cchSource;
  93. }
  94. else
  95. {
  96. typedef C char_t;
  97. typedef stlsoft_char_traits<C> traits_t;
  98. const ss_size_t cchContent = (cchSource < cchDest) ? cchSource : cchDest;
  99. traits_t::copy(dest, src, cchContent);
  100. if(cchContent < cchDest)
  101. {
  102. traits_t::assign(&dest[cchContent], cchDest - cchContent, 0);
  103. }
  104. return cchContent;
  105. }
  106. }
  107. ////////////////////////////////////////////////////////////////////////////
  108. // Unit-testing
  109. #ifdef STLSOFT_UNITTEST
  110. # include "./unittest/copy_functions_unittest_.h"
  111. #endif /* STLSOFT_UNITTEST */
  112. /* ////////////////////////////////////////////////////////////////////// */
  113. #ifndef _STLSOFT_NO_NAMESPACE
  114. } // namespace stlsoft
  115. #endif /* _STLSOFT_NO_NAMESPACE */
  116. /* ////////////////////////////////////////////////////////////////////// */
  117. #endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_COPY_FUNCTIONS */
  118. /* ///////////////////////////// end of file //////////////////////////// */