merge the formfield patch from ooo-build
[ooovba.git] / sal / rtl / source / strbuf.c
bloba5335280d46b3619e1d114ca1a3beea09b01ddbe
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: strbuf.c,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <osl/interlck.h>
33 #ifndef _RTL_STRING_HXX_
34 #include <rtl/strbuf.hxx>
35 #endif
36 #include <rtl/memory.h>
39 #include <rtl/alloc.h>
44 /*************************************************************************
45 * rtl_stringbuffer_newFromStr_WithLength
47 void SAL_CALL rtl_stringbuffer_newFromStr_WithLength( rtl_String ** newStr,
48 const sal_Char * value,
49 sal_Int32 count )
51 if (!value)
53 rtl_string_new_WithLength( newStr, 16 );
54 return;
57 rtl_string_new_WithLength( newStr, count + 16 );
58 (*newStr)->length = count;
59 rtl_copyMemory( (*newStr)->buffer, value, count );
60 return;
63 /*************************************************************************
64 * rtl_stringbuffer_newFromStringBuffer
66 sal_Int32 SAL_CALL rtl_stringbuffer_newFromStringBuffer( rtl_String ** newStr,
67 sal_Int32 capacity,
68 rtl_String * oldStr )
70 sal_Int32 newCapacity = capacity;
72 if (newCapacity < oldStr->length)
73 newCapacity = oldStr->length;
75 rtl_string_new_WithLength( newStr, newCapacity );
76 if (oldStr->length > 0) {
77 (*newStr)->length = oldStr->length;
78 rtl_copyMemory( (*newStr)->buffer, oldStr->buffer, oldStr->length );
80 return newCapacity;
83 /*************************************************************************
84 * rtl_stringbuffer_ensureCapacity
86 void SAL_CALL rtl_stringbuffer_ensureCapacity
87 (rtl_String ** This, sal_Int32* capacity, sal_Int32 minimumCapacity)
89 if (minimumCapacity > *capacity)
91 rtl_String * pTmp = *This;
92 rtl_String * pNew = NULL;
93 *capacity = ((*This)->length + 1) * 2;
94 if (minimumCapacity > *capacity)
95 /* still lower, set to the minimum capacity */
96 *capacity = minimumCapacity;
98 rtl_string_new_WithLength(&pNew, *capacity);
99 pNew->length = (*This)->length;
100 *This = pNew;
102 rtl_copyMemory( (*This)->buffer, pTmp->buffer, pTmp->length );
103 rtl_string_release( pTmp );
107 /*************************************************************************
108 * rtl_stringbuffer_insert
110 void SAL_CALL rtl_stringbuffer_insert( rtl_String ** This,
111 sal_Int32 * capacity,
112 sal_Int32 offset,
113 const sal_Char * str,
114 sal_Int32 len )
116 sal_Int32 nOldLen;
117 sal_Char * pBuf;
118 sal_Int32 n;
119 if( len != 0 )
121 if (*capacity < (*This)->length + len)
122 rtl_stringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
125 if( len == 1 )
126 This->buffer
128 nOldLen = (*This)->length;
129 pBuf = (*This)->buffer;
131 /* copy the tail */
132 n = (nOldLen - offset);
133 if( n == 1 )
134 /* optimized for 1 character */
135 pBuf[offset + len] = pBuf[offset];
136 else if( n > 1 )
137 rtl_moveMemory( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Char) );
139 /* insert the new characters */
140 n = len;
141 if( len == 1 )
142 /* optimized for 1 character */
143 pBuf[offset] = *str;
144 else if( n > 1 )
145 rtl_copyMemory( pBuf + offset, str, len * sizeof(sal_Char) );
146 (*This)->length = nOldLen + len;
147 pBuf[ nOldLen + len ] = 0;