xref: /AOO41X/main/sal/rtl/source/strbuf.c (revision 647f063d49501903f1667b75f5634541fc603283)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <osl/interlck.h>
25 
26 #ifndef _RTL_STRING_HXX_
27 #include <rtl/strbuf.hxx>
28 #endif
29 #include <rtl/memory.h>
30 
31 /*
32 #include <rtl/alloc.h>
33 */
34 
35 
36 
37 /*************************************************************************
38  *  rtl_stringbuffer_newFromStr_WithLength
39  */
rtl_stringbuffer_newFromStr_WithLength(rtl_String ** newStr,const sal_Char * value,sal_Int32 count)40 void SAL_CALL rtl_stringbuffer_newFromStr_WithLength( rtl_String ** newStr,
41                                                       const sal_Char * value,
42                                                       sal_Int32 count )
43 {
44     if (!value)
45     {
46         rtl_string_new_WithLength( newStr, 16 );
47         return;
48     }
49 
50     rtl_string_new_WithLength( newStr, count + 16 );
51     (*newStr)->length = count;
52     rtl_copyMemory( (*newStr)->buffer, value, count );
53     return;
54 }
55 
56 /*************************************************************************
57  *  rtl_stringbuffer_newFromStringBuffer
58  */
rtl_stringbuffer_newFromStringBuffer(rtl_String ** newStr,sal_Int32 capacity,rtl_String * oldStr)59 sal_Int32 SAL_CALL rtl_stringbuffer_newFromStringBuffer( rtl_String ** newStr,
60                                                          sal_Int32 capacity,
61                                                          rtl_String * oldStr )
62 {
63     sal_Int32 newCapacity = capacity;
64 
65     if (newCapacity < oldStr->length)
66         newCapacity = oldStr->length;
67 
68     rtl_string_new_WithLength( newStr, newCapacity );
69     if (oldStr->length > 0) {
70         (*newStr)->length = oldStr->length;
71         rtl_copyMemory( (*newStr)->buffer, oldStr->buffer, oldStr->length );
72     }
73     return newCapacity;
74 }
75 
76 /*************************************************************************
77  *  rtl_stringbuffer_ensureCapacity
78  */
rtl_stringbuffer_ensureCapacity(rtl_String ** This,sal_Int32 * capacity,sal_Int32 minimumCapacity)79 void SAL_CALL rtl_stringbuffer_ensureCapacity
80     (rtl_String ** This, sal_Int32* capacity, sal_Int32 minimumCapacity)
81 {
82     if (minimumCapacity > *capacity)
83     {
84         rtl_String * pTmp = *This;
85         rtl_String * pNew = NULL;
86         *capacity = ((*This)->length + 1) * 2;
87         if (minimumCapacity > *capacity)
88             /* still lower, set to the minimum capacity */
89             *capacity = minimumCapacity;
90 
91         rtl_string_new_WithLength(&pNew, *capacity);
92         pNew->length = (*This)->length;
93         *This = pNew;
94 
95         rtl_copyMemory( (*This)->buffer, pTmp->buffer, pTmp->length );
96         rtl_string_release( pTmp );
97     }
98 }
99 
100 /*************************************************************************
101  *  rtl_stringbuffer_insert
102  */
rtl_stringbuffer_insert(rtl_String ** This,sal_Int32 * capacity,sal_Int32 offset,const sal_Char * str,sal_Int32 len)103 void SAL_CALL rtl_stringbuffer_insert( rtl_String ** This,
104                                        sal_Int32 * capacity,
105                                        sal_Int32 offset,
106                                        const sal_Char * str,
107                                        sal_Int32 len )
108 {
109     sal_Int32 nOldLen;
110     sal_Char * pBuf;
111     sal_Int32 n;
112     if( len != 0 )
113     {
114         if (*capacity < (*This)->length + len)
115             rtl_stringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
116 
117         /*
118         if( len == 1 )
119             This->buffer
120         */
121         nOldLen = (*This)->length;
122         pBuf = (*This)->buffer;
123 
124         /* copy the tail */
125         n = (nOldLen - offset);
126         if( n == 1 )
127             /* optimized for 1 character */
128             pBuf[offset + len] = pBuf[offset];
129         else if( n > 1 )
130             rtl_moveMemory( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Char) );
131 
132         /* insert the new characters */
133         n = len;
134         if( len == 1 )
135                             /* optimized for 1 character */
136             pBuf[offset] = *str;
137         else if( n > 1 )
138             rtl_copyMemory( pBuf + offset, str, len * sizeof(sal_Char) );
139         (*This)->length = nOldLen + len;
140         pBuf[ nOldLen + len ] = 0;
141     }
142 }
143 
144