1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: strbuf.c,v $
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>
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
,
53 rtl_string_new_WithLength( newStr
, 16 );
57 rtl_string_new_WithLength( newStr
, count
+ 16 );
58 (*newStr
)->length
= count
;
59 rtl_copyMemory( (*newStr
)->buffer
, value
, count
);
63 /*************************************************************************
64 * rtl_stringbuffer_newFromStringBuffer
66 sal_Int32 SAL_CALL
rtl_stringbuffer_newFromStringBuffer( rtl_String
** newStr
,
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
);
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
;
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
,
113 const sal_Char
* str
,
121 if (*capacity
< (*This
)->length
+ len
)
122 rtl_stringbuffer_ensureCapacity( This
, capacity
, (*This
)->length
+ len
);
128 nOldLen
= (*This
)->length
;
129 pBuf
= (*This
)->buffer
;
132 n
= (nOldLen
- offset
);
134 /* optimized for 1 character */
135 pBuf
[offset
+ len
] = pBuf
[offset
];
137 rtl_moveMemory( pBuf
+ offset
+ len
, pBuf
+ offset
, n
* sizeof(sal_Char
) );
139 /* insert the new characters */
142 /* optimized for 1 character */
145 rtl_copyMemory( pBuf
+ offset
, str
, len
* sizeof(sal_Char
) );
146 (*This
)->length
= nOldLen
+ len
;
147 pBuf
[ nOldLen
+ len
] = 0;