update dev300-m58
[ooovba.git] / basic / source / comp / buffer.cxx
blob9b26c9747f64cc559e3ee063db7e2f69471c78a5
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: buffer.cxx,v $
10 * $Revision: 1.13 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basic.hxx"
34 #include "sbcomp.hxx"
35 #include "buffer.hxx"
36 #include <string.h>
38 const static UINT32 UP_LIMIT=0xFFFFFF00L;
40 // Der SbiBuffer wird in Inkrements von mindestens 16 Bytes erweitert.
41 // Dies ist notwendig, da viele Klassen von einer Pufferlaenge
42 // von x*16 Bytes ausgehen.
44 SbiBuffer::SbiBuffer( SbiParser* p, short n )
46 pParser = p;
47 n = ( (n + 15 ) / 16 ) * 16;
48 if( !n ) n = 16;
49 pBuf = NULL;
50 pCur = NULL;
51 nInc = n;
52 nSize =
53 nOff = 0;
56 SbiBuffer::~SbiBuffer()
58 delete[] pBuf;
61 // Rausreichen des Puffers
62 // Dies fuehrt zur Loeschung des Puffers!
64 char* SbiBuffer::GetBuffer()
66 char* p = pBuf;
67 pBuf = NULL;
68 pCur = NULL;
69 return p;
72 // Test, ob der Puffer n Bytes aufnehmen kann.
73 // Im Zweifelsfall wird er vergroessert
75 BOOL SbiBuffer::Check( USHORT n )
77 if( !n ) return TRUE;
78 if( ( static_cast<UINT32>( nOff )+ n ) > static_cast<UINT32>( nSize ) )
80 if( nInc == 0 )
81 return FALSE;
82 USHORT nn = 0;
83 while( nn < n ) nn = nn + nInc;
84 char* p;
85 if( ( static_cast<UINT32>( nSize ) + nn ) > UP_LIMIT ) p = NULL;
86 else p = new char [nSize + nn];
87 if( !p )
89 pParser->Error( SbERR_PROG_TOO_LARGE );
90 nInc = 0;
91 delete[] pBuf; pBuf = NULL;
92 return FALSE;
94 else
96 if( nSize ) memcpy( p, pBuf, nSize );
97 delete[] pBuf;
98 pBuf = p;
99 pCur = pBuf + nOff;
100 nSize = nSize + nn;
103 return TRUE;
106 // Angleich des Puffers auf die uebergebene Byte-Grenze
108 void SbiBuffer::Align( INT32 n )
110 if( nOff % n ) {
111 UINT32 nn =( ( nOff + n ) / n ) * n;
112 if( nn <= UP_LIMIT )
114 nn = nn - nOff;
115 if( Check( static_cast<USHORT>(nn) ) )
117 memset( pCur, 0, nn );
118 pCur += nn;
119 nOff = nOff + nn;
125 // Patch einer Location
127 void SbiBuffer::Patch( UINT32 off, UINT32 val )
129 if( ( off + sizeof( UINT32 ) ) < nOff )
131 UINT16 val1 = static_cast<UINT16>( val & 0xFFFF );
132 UINT16 val2 = static_cast<UINT16>( val >> 16 );
133 BYTE* p = (BYTE*) pBuf + off;
134 *p++ = (char) ( val1 & 0xFF );
135 *p++ = (char) ( val1 >> 8 );
136 *p++ = (char) ( val2 & 0xFF );
137 *p = (char) ( val2 >> 8 );
141 // Forward References auf Labels und Prozeduren
142 // bauen eine Kette auf. Der Anfang der Kette ist beim uebergebenen
143 // Parameter, das Ende der Kette ist 0.
145 void SbiBuffer::Chain( UINT32 off )
147 if( off && pBuf )
149 BYTE *ip;
150 UINT32 i = off;
151 UINT32 val1 = (nOff & 0xFFFF);
152 UINT32 val2 = (nOff >> 16);
155 ip = (BYTE*) pBuf + i;
156 BYTE* pTmp = ip;
157 i = *pTmp++; i |= *pTmp++ << 8; i |= *pTmp++ << 16; i |= *pTmp++ << 24;
159 if( i >= nOff )
161 pParser->Error( SbERR_INTERNAL_ERROR, "BACKCHAIN" );
162 break;
164 *ip++ = (char) ( val1 & 0xFF );
165 *ip++ = (char) ( val1 >> 8 );
166 *ip++ = (char) ( val2 & 0xFF );
167 *ip = (char) ( val2 >> 8 );
168 } while( i );
172 BOOL SbiBuffer::operator +=( INT8 n )
174 if( Check( 1 ) )
176 *pCur++ = (char) n; nOff++; return TRUE;
177 } else return FALSE;
180 BOOL SbiBuffer::operator +=( UINT8 n )
182 if( Check( 1 ) )
184 *pCur++ = (char) n; nOff++; return TRUE;
185 } else return FALSE;
188 BOOL SbiBuffer::operator +=( INT16 n )
190 if( Check( 2 ) )
192 *pCur++ = (char) ( n & 0xFF );
193 *pCur++ = (char) ( n >> 8 );
194 nOff += 2; return TRUE;
195 } else return FALSE;
198 BOOL SbiBuffer::operator +=( UINT16 n )
200 if( Check( 2 ) )
202 *pCur++ = (char) ( n & 0xFF );
203 *pCur++ = (char) ( n >> 8 );
204 nOff += 2; return TRUE;
205 } else return FALSE;
208 BOOL SbiBuffer::operator +=( UINT32 n )
210 if( Check( 4 ) )
212 UINT16 n1 = static_cast<UINT16>( n & 0xFFFF );
213 UINT16 n2 = static_cast<UINT16>( n >> 16 );
214 if ( operator +=( n1 ) && operator +=( n2 ) )
215 return TRUE;
216 return TRUE;
218 return FALSE;
221 BOOL SbiBuffer::operator +=( INT32 n )
223 return operator +=( (UINT32) n );
227 BOOL SbiBuffer::operator +=( const String& n )
229 USHORT l = n.Len() + 1;
230 if( Check( l ) )
232 ByteString aByteStr( n, gsl_getSystemTextEncoding() );
233 memcpy( pCur, aByteStr.GetBuffer(), l );
234 pCur += l;
235 nOff = nOff + l;
236 return TRUE;
238 else return FALSE;
241 BOOL SbiBuffer::Add( const void* p, USHORT len )
243 if( Check( len ) )
245 memcpy( pCur, p, len );
246 pCur += len;
247 nOff = nOff + len;
248 return TRUE;
249 } else return FALSE;