1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #include <osl/interlck.h> 29 30 #ifndef _RTL_STRING_HXX_ 31 #include <rtl/strbuf.hxx> 32 #endif 33 #include <rtl/memory.h> 34 35 /* 36 #include <rtl/alloc.h> 37 */ 38 39 40 41 /************************************************************************* 42 * rtl_stringbuffer_newFromStr_WithLength 43 */ 44 void SAL_CALL rtl_stringbuffer_newFromStr_WithLength( rtl_String ** newStr, 45 const sal_Char * value, 46 sal_Int32 count ) 47 { 48 if (!value) 49 { 50 rtl_string_new_WithLength( newStr, 16 ); 51 return; 52 } 53 54 rtl_string_new_WithLength( newStr, count + 16 ); 55 (*newStr)->length = count; 56 rtl_copyMemory( (*newStr)->buffer, value, count ); 57 return; 58 } 59 60 /************************************************************************* 61 * rtl_stringbuffer_newFromStringBuffer 62 */ 63 sal_Int32 SAL_CALL rtl_stringbuffer_newFromStringBuffer( rtl_String ** newStr, 64 sal_Int32 capacity, 65 rtl_String * oldStr ) 66 { 67 sal_Int32 newCapacity = capacity; 68 69 if (newCapacity < oldStr->length) 70 newCapacity = oldStr->length; 71 72 rtl_string_new_WithLength( newStr, newCapacity ); 73 if (oldStr->length > 0) { 74 (*newStr)->length = oldStr->length; 75 rtl_copyMemory( (*newStr)->buffer, oldStr->buffer, oldStr->length ); 76 } 77 return newCapacity; 78 } 79 80 /************************************************************************* 81 * rtl_stringbuffer_ensureCapacity 82 */ 83 void SAL_CALL rtl_stringbuffer_ensureCapacity 84 (rtl_String ** This, sal_Int32* capacity, sal_Int32 minimumCapacity) 85 { 86 if (minimumCapacity > *capacity) 87 { 88 rtl_String * pTmp = *This; 89 rtl_String * pNew = NULL; 90 *capacity = ((*This)->length + 1) * 2; 91 if (minimumCapacity > *capacity) 92 /* still lower, set to the minimum capacity */ 93 *capacity = minimumCapacity; 94 95 rtl_string_new_WithLength(&pNew, *capacity); 96 pNew->length = (*This)->length; 97 *This = pNew; 98 99 rtl_copyMemory( (*This)->buffer, pTmp->buffer, pTmp->length ); 100 rtl_string_release( pTmp ); 101 } 102 } 103 104 /************************************************************************* 105 * rtl_stringbuffer_insert 106 */ 107 void SAL_CALL rtl_stringbuffer_insert( rtl_String ** This, 108 sal_Int32 * capacity, 109 sal_Int32 offset, 110 const sal_Char * str, 111 sal_Int32 len ) 112 { 113 sal_Int32 nOldLen; 114 sal_Char * pBuf; 115 sal_Int32 n; 116 if( len != 0 ) 117 { 118 if (*capacity < (*This)->length + len) 119 rtl_stringbuffer_ensureCapacity( This, capacity, (*This)->length + len ); 120 121 /* 122 if( len == 1 ) 123 This->buffer 124 */ 125 nOldLen = (*This)->length; 126 pBuf = (*This)->buffer; 127 128 /* copy the tail */ 129 n = (nOldLen - offset); 130 if( n == 1 ) 131 /* optimized for 1 character */ 132 pBuf[offset + len] = pBuf[offset]; 133 else if( n > 1 ) 134 rtl_moveMemory( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Char) ); 135 136 /* insert the new characters */ 137 n = len; 138 if( len == 1 ) 139 /* optimized for 1 character */ 140 pBuf[offset] = *str; 141 else if( n > 1 ) 142 rtl_copyMemory( pBuf + offset, str, len * sizeof(sal_Char) ); 143 (*This)->length = nOldLen + len; 144 pBuf[ nOldLen + len ] = 0; 145 } 146 } 147 148