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