00001 /* ///////////////////////////////////////////////////////////////////////// 00002 * File: stlsoft/string/cstring_functions.hpp 00003 * 00004 * Purpose: String duplication functions. 00005 * 00006 * Created: 26th May 2005 00007 * Updated: 31st March 2010 00008 * 00009 * Home: http://stlsoft.org/ 00010 * 00011 * Copyright (c) 2005-2010, Matthew Wilson and Synesis Software 00012 * All rights reserved. 00013 * 00014 * Redistribution and use in source and binary forms, with or without 00015 * modification, are permitted provided that the following conditions are met: 00016 * 00017 * - Redistributions of source code must retain the above copyright notice, this 00018 * list of conditions and the following disclaimer. 00019 * - Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of 00023 * any contributors may be used to endorse or promote products derived from 00024 * this software without specific prior written permission. 00025 * 00026 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00027 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00028 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00029 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00030 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00031 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00032 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00033 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00034 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00035 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00036 * POSSIBILITY OF SUCH DAMAGE. 00037 * 00038 * ////////////////////////////////////////////////////////////////////// */ 00039 00040 00047 #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_CSTRING_FUNCTIONS 00048 #define STLSOFT_INCL_STLSOFT_STRING_HPP_CSTRING_FUNCTIONS 00049 00050 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION 00051 # define STLSOFT_VER_STLSOFT_STRING_HPP_CSTRING_FUNCTIONS_MAJOR 2 00052 # define STLSOFT_VER_STLSOFT_STRING_HPP_CSTRING_FUNCTIONS_MINOR 2 00053 # define STLSOFT_VER_STLSOFT_STRING_HPP_CSTRING_FUNCTIONS_REVISION 1 00054 # define STLSOFT_VER_STLSOFT_STRING_HPP_CSTRING_FUNCTIONS_EDIT 29 00055 #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */ 00056 00057 /* ///////////////////////////////////////////////////////////////////////// 00058 * Compatibility 00059 */ 00060 00061 /* 00062 [Incompatibilies-start] 00063 STLSOFT_COMPILER_IS_WATCOM: 00064 [Incompatibilies-end] 00065 */ 00066 00067 /* ///////////////////////////////////////////////////////////////////////// 00068 * Includes 00069 */ 00070 00071 #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT 00072 # include <stlsoft/stlsoft.h> 00073 #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */ 00074 #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING 00075 # include <stlsoft/shims/access/string.hpp> 00076 #endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING */ 00077 #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS 00078 # include <stlsoft/string/char_traits.hpp> 00079 #endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS */ 00080 #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_BASE 00081 # include <stlsoft/memory/allocator_base.hpp> // for STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT 00082 #endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_BASE */ 00083 00084 #ifdef STLSOFT_UNITTEST 00085 # include <stlsoft/memory/malloc_allocator.hpp> 00086 # include <stlsoft/memory/new_allocator.hpp> 00087 # include <string.h> 00088 #endif /* STLSOFT_UNITTEST */ 00089 00090 /* ///////////////////////////////////////////////////////////////////////// 00091 * Namespace 00092 */ 00093 00094 #ifndef _STLSOFT_NO_NAMESPACE 00095 namespace stlsoft 00096 { 00097 #endif /* _STLSOFT_NO_NAMESPACE */ 00098 00099 /* ///////////////////////////////////////////////////////////////////////// 00100 * Classes 00101 */ 00102 00103 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION 00104 00117 template< ss_typename_param_k C 00118 , ss_typename_param_k A 00119 > 00120 inline C* string_dup_impl(C const* str, ss_size_t cch, A& ator) 00121 { 00122 C* r = ator.allocate(1 + cch, NULL); 00123 00124 char_traits<C>::copy(r, str, cch); 00125 r[cch] = '\0'; 00126 00127 return r; 00128 } 00129 #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */ 00130 00144 template< ss_typename_param_k C 00145 , ss_typename_param_k A 00146 > 00147 inline C* string_dup(C const* str, ss_size_t cch, A& ator) 00148 { 00149 #ifdef STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT 00150 typedef C char_t; 00151 typedef ss_typename_type_k A::ss_template_qual_k rebind<C>::other ator_t; 00152 00153 ator_t ator_; 00154 00155 STLSOFT_SUPPRESS_UNUSED(ator); 00156 00157 return string_dup_impl(str, cch, ator_); 00158 #else /* ? STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT */ 00159 return string_dup_impl(str, cch, ator); 00160 #endif /* STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT */ 00161 } 00162 00176 template< ss_typename_param_k C 00177 , ss_typename_param_k A 00178 > 00179 inline C* string_dup(C const* str, A& ator, ss_size_t* psize = NULL) 00180 { 00181 ss_size_t dummy; 00182 00183 if(NULL == psize) 00184 { 00185 psize = &dummy; 00186 } 00187 00188 return string_dup(str, *psize = c_str_len(str), ator); 00189 } 00190 00192 // Unit-testing 00193 00194 #ifdef STLSOFT_UNITTEST 00195 # include "./unittest/cstring_functions_unittest_.h" 00196 #endif /* STLSOFT_UNITTEST */ 00197 00198 /* ////////////////////////////////////////////////////////////////////// */ 00199 00200 #ifndef _STLSOFT_NO_NAMESPACE 00201 } // namespace stlsoft 00202 #endif /* _STLSOFT_NO_NAMESPACE */ 00203 00204 /* ////////////////////////////////////////////////////////////////////// */ 00205 00206 #endif /* !STLSOFT_INCL_STLSOFT_HPP_CSTRING_FUNCTIONS */ 00207 00208 /* ///////////////////////////// end of file //////////////////////////// */