bump product version to 5.0.4.1
[LibreOffice.git] / basic / source / comp / buffer.cxx
bloba21ad03358a67eca499b376efa94f936c53c1bf6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <osl/thread.h>
21 #include "buffer.hxx"
22 #include "parser.hxx"
24 const static sal_uInt32 UP_LIMIT=0xFFFFFF00L;
26 // The SbiBuffer will be expanded in increments of at least 16 Bytes.
27 // This is necessary, because many classes emanate from a buffer length
28 // of x*16 Bytes.
30 SbiBuffer::SbiBuffer( SbiParser* p, short n )
32 pParser = p;
33 n = ( (n + 15 ) / 16 ) * 16;
34 if( !n ) n = 16;
35 pBuf = NULL;
36 pCur = NULL;
37 nInc = n;
38 nSize =
39 nOff = 0;
42 SbiBuffer::~SbiBuffer()
44 delete[] pBuf;
47 // Reach out the buffer
48 // This lead to the deletion of the buffer!
50 char* SbiBuffer::GetBuffer()
52 char* p = pBuf;
53 pBuf = NULL;
54 pCur = NULL;
55 return p;
58 // Test, if the buffer can contain n Bytes.
59 // In case of doubt it will be enlarged
61 bool SbiBuffer::Check( sal_Int32 n )
63 if( !n )
65 return true;
67 if( nOff + n > nSize )
69 if( nInc == 0 )
71 return false;
74 sal_Int32 nn = 0;
75 while( nn < n )
77 nn = nn + nInc;
79 char* p;
80 if( ( nSize + nn ) > UP_LIMIT )
82 p = NULL;
84 else
86 p = new char [nSize + nn];
88 if( !p )
90 pParser->Error( SbERR_PROG_TOO_LARGE );
91 nInc = 0;
92 delete[] pBuf; pBuf = NULL;
93 return false;
95 else
97 if( nSize ) memcpy( p, pBuf, nSize );
98 delete[] pBuf;
99 pBuf = p;
100 pCur = pBuf + nOff;
101 nSize = nSize + nn;
104 return true;
107 // Patch of a Location
109 void SbiBuffer::Patch( sal_uInt32 off, sal_uInt32 val )
111 if( ( off + sizeof( sal_uInt32 ) ) < nOff )
113 sal_uInt16 val1 = static_cast<sal_uInt16>( val & 0xFFFF );
114 sal_uInt16 val2 = static_cast<sal_uInt16>( val >> 16 );
115 sal_uInt8* p = reinterpret_cast<sal_uInt8*>(pBuf) + off;
116 *p++ = (char) ( val1 & 0xFF );
117 *p++ = (char) ( val1 >> 8 );
118 *p++ = (char) ( val2 & 0xFF );
119 *p = (char) ( val2 >> 8 );
123 // Forward References upon label und procedures
124 // establish a linkage. The beginning of the linkage is at the passed parameter,
125 // the end of the linkage is 0.
127 void SbiBuffer::Chain( sal_uInt32 off )
129 if( off && pBuf )
131 sal_uInt8 *ip;
132 sal_uInt32 i = off;
133 sal_uInt32 val1 = (nOff & 0xFFFF);
134 sal_uInt32 val2 = (nOff >> 16);
137 ip = reinterpret_cast<sal_uInt8*>(pBuf) + i;
138 sal_uInt8* pTmp = ip;
139 i = *pTmp++; i |= *pTmp++ << 8; i |= *pTmp++ << 16; i |= *pTmp++ << 24;
141 if( i >= nOff )
143 pParser->Error( SbERR_INTERNAL_ERROR, "BACKCHAIN" );
144 break;
146 *ip++ = (char) ( val1 & 0xFF );
147 *ip++ = (char) ( val1 >> 8 );
148 *ip++ = (char) ( val2 & 0xFF );
149 *ip = (char) ( val2 >> 8 );
150 } while( i );
154 bool SbiBuffer::operator +=( sal_Int8 n )
156 if( Check( 1 ) )
158 *pCur++ = (char) n;
159 nOff += 1;
160 return true;
162 else
164 return false;
168 bool SbiBuffer::operator +=( sal_uInt8 n )
170 if( Check( 1 ) )
172 *pCur++ = (char) n;
173 nOff += 1;
174 return true;
176 else
178 return false;
182 bool SbiBuffer::operator +=( sal_Int16 n )
184 if( Check( 2 ) )
186 *pCur++ = (char) ( n & 0xFF );
187 *pCur++ = (char) ( n >> 8 );
188 nOff += 2;
189 return true;
191 else
193 return false;
197 bool SbiBuffer::operator +=( sal_uInt16 n )
199 if( Check( 2 ) )
201 *pCur++ = (char) ( n & 0xFF );
202 *pCur++ = (char) ( n >> 8 );
203 nOff += 2;
204 return true;
206 else
208 return false;
212 bool SbiBuffer::operator +=( sal_uInt32 n )
214 if( Check( 4 ) )
216 sal_uInt16 n1 = static_cast<sal_uInt16>( n & 0xFFFF );
217 sal_uInt16 n2 = static_cast<sal_uInt16>( n >> 16 );
218 operator +=(n1) && operator +=(n2);
219 return true;
221 else
223 return false;
227 bool SbiBuffer::operator +=( sal_Int32 n )
229 return operator +=( (sal_uInt32) n );
233 bool SbiBuffer::operator +=( const OUString& n )
235 sal_uInt32 len = n.getLength() + 1;
236 if( Check( len ) )
238 OString aByteStr(OUStringToOString(n, osl_getThreadTextEncoding()));
239 memcpy( pCur, aByteStr.getStr(), len );
240 pCur += len;
241 nOff += len;
242 return true;
244 else
246 return false;
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */