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 //------------------------------------------------------------------------
25 // includes
26 //------------------------------------------------------------------------
27 
28 #include "AutoBuffer.hxx"
29 #include <osl/diagnose.h>
30 
31 #include <windows.h>
32 
33 //------------------------------------------------------------------------
34 // namespace directives
35 //------------------------------------------------------------------------
36 
37 using rtl::OUString;
38 
39 //------------------------------------------------------------------------
40 //
41 //------------------------------------------------------------------------
42 
CAutoUnicodeBuffer(size_t size,sal_Bool bLazyCreation)43 CAutoUnicodeBuffer::CAutoUnicodeBuffer( size_t size, sal_Bool bLazyCreation ) :
44 	m_buffSize( size ),
45 	m_pBuff( NULL )
46 {
47 	if ( !bLazyCreation )
48 		init( );
49 }
50 
51 //------------------------------------------------------------------------
52 //
53 //------------------------------------------------------------------------
54 
~CAutoUnicodeBuffer()55 CAutoUnicodeBuffer::~CAutoUnicodeBuffer( )
56 {
57 	delete [] m_pBuff;
58 }
59 
60 //------------------------------------------------------------------------
61 //
62 //------------------------------------------------------------------------
63 
resize(size_t new_size)64 sal_Bool SAL_CALL CAutoUnicodeBuffer::resize( size_t new_size )
65 {
66 	OSL_ASSERT( new_size >= 0 );
67 
68 	if ( new_size != m_buffSize )
69 	{
70 		if ( new_size > m_buffSize )
71 		{
72 			delete [] m_pBuff;
73 			m_pBuff = NULL;
74 		}
75 
76 		m_buffSize = new_size;
77 	}
78 
79 	return sal_True;
80 }
81 
82 //------------------------------------------------------------------------
83 //
84 //------------------------------------------------------------------------
85 
empty()86 void SAL_CALL CAutoUnicodeBuffer::empty( )
87 {
88 	if ( m_pBuff )
89 		ZeroMemory( m_pBuff, m_buffSize * sizeof( sal_Unicode ) );
90 }
91 
92 //------------------------------------------------------------------------
93 //
94 //------------------------------------------------------------------------
95 
fill(const sal_Unicode * pContent,size_t nLen)96 sal_Bool SAL_CALL CAutoUnicodeBuffer::fill( const sal_Unicode* pContent, size_t nLen )
97 {
98 	OSL_ASSERT( pContent && m_buffSize && (m_buffSize >= nLen) );
99 
100 	init( );
101 
102 	sal_Bool bRet = sal_False;
103 
104 	if ( m_pBuff && pContent && nLen )
105 	{
106 		CopyMemory( m_pBuff, pContent, nLen * sizeof( sal_Unicode ) );
107 		bRet = sal_True;
108 	}
109 
110 	return bRet;
111 }
112 
113 //------------------------------------------------------------------------
114 //
115 //------------------------------------------------------------------------
116 
size() const117 size_t SAL_CALL CAutoUnicodeBuffer::size( ) const
118 {
119 	return m_buffSize;
120 }
121 
122 //------------------------------------------------------------------------
123 //
124 //------------------------------------------------------------------------
125 
operator sal_Unicode*()126 CAutoUnicodeBuffer::operator sal_Unicode*( )
127 {
128 	return m_pBuff;
129 }
130 
131 //------------------------------------------------------------------------
132 //
133 //------------------------------------------------------------------------
134 
operator &()135 sal_Unicode* CAutoUnicodeBuffer::operator&( )
136 {
137 	return m_pBuff;
138 }
139 
140 //------------------------------------------------------------------------
141 //
142 //------------------------------------------------------------------------
143 
operator &() const144 const sal_Unicode* CAutoUnicodeBuffer::operator&( ) const
145 {
146 	return m_pBuff;
147 }
148 
149 //------------------------------------------------------------------------
150 //
151 //------------------------------------------------------------------------
152 
init()153 void SAL_CALL CAutoUnicodeBuffer::init( )
154 {
155 	if ( !m_pBuff && (m_buffSize > 0) )
156 		m_pBuff = new sal_Unicode[ m_buffSize ];
157 
158 	empty( );
159 }
160