merge the formfield patch from ooo-build
[ooovba.git] / lotuswordpro / source / filter / benval.cxx
blob55a3a79c0d8db63d30b5770d6af8b9a7f06b3606
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * either of the following licenses
6 * - GNU Lesser General Public License Version 2.1
7 * - Sun Industry Standards Source License Version 1.1
9 * Sun Microsystems Inc., October, 2000
11 * GNU Lesser General Public License Version 2.1
12 * =============================================
13 * Copyright 2000 by Sun Microsystems, Inc.
14 * 901 San Antonio Road, Palo Alto, CA 94303, USA
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License version 2.1, as published by the Free Software Foundation.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 * MA 02111-1307 USA
31 * Sun Industry Standards Source License Version 1.1
32 * =================================================
33 * The contents of this file are subject to the Sun Industry Standards
34 * Source License Version 1.1 (the "License"); You may not use this file
35 * except in compliance with the License. You may obtain a copy of the
36 * License at http://www.openoffice.org/license.html.
38 * Software provided under this License is provided on an "AS IS" basis,
39 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
40 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
41 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
42 * See the License for the specific provisions governing your rights and
43 * obligations concerning the Software.
45 * The Initial Developer of the Original Code is: IBM Corporation
47 * Copyright: 2008 by IBM Corporation
49 * All Rights Reserved.
51 * Contributor(s): _______________________________________
54 ************************************************************************/
55 #include "first.hxx"
56 #include "assert.h"
57 namespace OpenStormBento
60 unsigned long
61 CBenValue::GetValueSize()
63 unsigned long Size = 0;
64 pCBenValueSegment pCurr = NULL;
65 while ((pCurr = GetNextValueSegment(pCurr)) != NULL)
66 Size += pCurr->GetSize();
67 return Size;
70 BenError
71 CBenValue::ReadValueData(BenDataPtr pReadBuffer, unsigned long Offset,
72 unsigned long Amt, unsigned long * pAmtRead)
74 BenError Err;
75 unsigned long SegOffset = 0;
76 *pAmtRead = 0;
77 pCBenValueSegment pCurrSeg = NULL;
78 pLtcBenContainer pContainer = GetContainer();
79 BenByteDataPtr pBuffer = (BenByteDataPtr) pReadBuffer;
81 /// pReadBuffer -- pointer to buffer of read result, allocated outside this function
82 /// Offset -- read buffer's start offset address, relative value in the whole value stream
83 /// Amt -- read buffer's size
84 /// pAmtRead -- return the actual read size
86 /// SegOffset -- current segment's start address offset, relative value in the whole value stream
88 while ((pCurrSeg = GetNextValueSegment(pCurrSeg)) != NULL)
90 if (Amt == 0) /// whole buffer is full now, so return
91 return BenErr_OK;
93 if (SegOffset <= Offset && Offset < SegOffset + pCurrSeg->GetSize()) /// begin at current segment
95 unsigned long OffsetIntoSeg = Offset - SegOffset; /// relative value in this value segment stream
97 unsigned long AmtThisSeg = UtMin(Amt, pCurrSeg->GetSize() -
98 OffsetIntoSeg); /// size read in this segment, it's minimal value between Amt &
99 /// remain part from OffsetIntoSeg to the end of this segment
101 unsigned long AmtReadThisSeg; /// actual read size in this segment
102 if (pCurrSeg->IsImmediate())
104 UtHugeMemcpy(pBuffer, pCurrSeg->GetImmediateData() +
105 OffsetIntoSeg, AmtThisSeg);
106 AmtReadThisSeg = AmtThisSeg;
108 else
110 if ((Err = pContainer->SeekToPosition(pCurrSeg->GetPosition() +
111 OffsetIntoSeg)) != BenErr_OK)
112 return Err;
114 if ((Err = pContainer->Read(pBuffer, AmtThisSeg,
115 &AmtReadThisSeg)) != BenErr_OK)
116 return Err;
119 *pAmtRead += AmtReadThisSeg;
121 if (AmtThisSeg != AmtReadThisSeg)
122 return BenErr_UnexpectedEndOfFile;
124 pBuffer += AmtReadThisSeg;
125 Offset += AmtReadThisSeg;
126 Amt -= AmtReadThisSeg;
129 SegOffset += pCurrSeg->GetSize();
131 return BenErr_OK;
134 BenError
135 CBenValue::ReadValueDataKnownSize(BenDataPtr pBuffer, unsigned long Offset,
136 unsigned long Amt)
138 unsigned long AmtRead;
139 BenError Err = ReadValueData(pBuffer, Offset, Amt, &AmtRead);
141 if (Err == UtErr_OK && AmtRead != Amt)
142 Err = (BenError) UtErr_Fail;
144 return Err;
147 BenError
148 CBenValue::WriteValueData(BenConstDataPtr pWriteBuffer, unsigned long Offset,
149 unsigned long Amt, unsigned long * pAmtWritten)
151 #if 0// Deleted by 2004-06-16
152 unsigned long AmtWritten;
153 if (pAmtWritten == NULL)
154 pAmtWritten = &AmtWritten;
156 *pAmtWritten = 0;
158 if (Amt == 0)
159 return BenErr_OK;
161 unsigned long SegOffset = 0;
162 pCBenValueSegment pCurrSeg = NULL;
163 BenError Err;
164 pLtcBenContainer pContainer = GetContainer();
165 BenConstByteDataPtr pBuffer = (BenConstByteDataPtr) pWriteBuffer;
167 while ((pCurrSeg = GetNextValueSegment(pCurrSeg)) != NULL)
169 if (Amt == 0)
170 return BenErr_OK;
172 if (SegOffset <= Offset && Offset < SegOffset + pCurrSeg->GetSize())
174 unsigned long OffsetIntoSeg = Offset - SegOffset;
176 unsigned long AmtThisSeg = UtMin(Amt, pCurrSeg->GetSize() -
177 OffsetIntoSeg);
179 if (pCurrSeg->IsImmediate())
181 UtHugeMemcpy(pCurrSeg->GetImmediateData() + OffsetIntoSeg,
182 pBuffer, AmtThisSeg);
183 *pAmtWritten += AmtThisSeg;
185 else
187 if ((Err = pContainer->SeekToPosition(pCurrSeg->GetPosition()
188 + OffsetIntoSeg)) != BenErr_OK)
189 return Err;
191 unsigned long AmtWrittenThisWrite;
192 Err = pContainer->Write(pBuffer, AmtThisSeg,
193 &AmtWrittenThisWrite);
194 *pAmtWritten += AmtWrittenThisWrite;
195 if (Err != BenErr_OK)
196 return Err;
199 pBuffer += AmtThisSeg;
200 Offset += AmtThisSeg;
201 Amt -= AmtThisSeg;
204 SegOffset += pCurrSeg->GetSize();
207 if (Offset > SegOffset)
209 assert(! "Write past end of value");
210 return BenErr_InvalidWriteOffset;
213 if (Amt == 0)
214 return BenErr_OK;
216 if ((Err = pContainer->SeekFromEnd(0)) != BenErr_OK)
217 return Err;
219 BenContainerPos EndFilePos;
220 if ((Err = pContainer->GetPosition(&EndFilePos)) != BenErr_OK)
221 return Err;
223 unsigned long AmtWrittenThisWrite;
224 Err = pContainer->Write(pBuffer, Amt, &AmtWrittenThisWrite);
225 *pAmtWritten += AmtWrittenThisWrite;
226 if (Err != BenErr_OK)
227 return Err;
229 pCBenValueSegment pLastSeg = (pCBenValueSegment) (
230 cValueSegments.GetLast() == cValueSegments.GetTerminating() ?
231 NULL : cValueSegments.GetLast() );
232 if (pLastSeg != NULL && ! pLastSeg->IsImmediate() &&
233 pLastSeg->GetPosition() + pLastSeg->GetSize() == EndFilePos)
234 pLastSeg->SetSize(pLastSeg->GetSize() + Amt);
235 else new CBenValueSegment(this, EndFilePos, Amt);
236 #endif
237 return BenErr_OK;
240 BenError
241 CBenValue::TruncateValueSize(unsigned long NewSize)
243 unsigned long SegOffset = 0;
245 pCBenValueSegment pCurrSeg = GetNextValueSegment(NULL);
246 while (pCurrSeg != NULL)
248 pCBenValueSegment pNextSeg = GetNextValueSegment(pCurrSeg);
250 long SegSize = UtMin((long) pCurrSeg->GetSize(), (long) NewSize -
251 (long) SegOffset);
253 if (SegSize <= 0)
254 delete pCurrSeg;
255 else pCurrSeg->SetSize(SegSize);
257 SegOffset += SegSize;
258 pCurrSeg = pNextSeg;
261 return BenErr_OK;
264 BenError
265 CBenValue::WriteValueData(BenConstDataPtr pWriteBuffer, unsigned long Offset,
266 unsigned long Amt)
268 unsigned long AmtWritten;
269 return WriteValueData(pWriteBuffer, Offset, Amt, &AmtWritten);
272 BenError
273 CBenValue::WriteImmediateValueData(BenConstDataPtr pBuffer,
274 unsigned short Size)
276 // Only one write can be an immediate value in current implementation
277 if (cValueSegments.GetFirst() != cValueSegments.GetTerminating() ||
278 Size > 4)
279 return BenErr_InvalidImmediateWrite;
281 if (Size == 0)
282 return BenErr_OK;
284 new CBenValueSegment(this, pBuffer, Size);
285 return BenErr_OK;
288 BenError
289 CBenValue::NewReference(BenObjectID ReferencedObjectID, pCBenReference
290 pReference)
292 BenError Err;
294 if (cpReferencedList == NULL)
296 pLtcBenContainer pContainer = cpProperty->GetContainer();
298 pCBenObject pNewObject;
299 if ((Err = pContainer->NewObject(&pNewObject)) != BenErr_OK)
300 return Err;
302 pCBenValue pNewValue;
303 if ((Err = pNewObject->NewValue(BEN_PROPID_OBJ_REFERENCES,
304 BEN_TYPEID_OBJ_REFERENCES_DATA, &pNewValue)) != BenErr_OK)
306 delete pNewObject;
307 return Err;
310 cpReferencedList = pNewValue;
311 cReferencedObjectsSize = 0;
314 BenByte Buffer[8];
315 UtPutIntelDWord(Buffer, ReferencedObjectID);
316 UtPutIntelDWord(Buffer + 4, ReferencedObjectID);
318 UtPutIntelDWord(pReference->GetData(), ReferencedObjectID);
320 if ((Err = cpReferencedList->WriteValueData(Buffer,
321 cReferencedObjectsSize, 8)) != BenErr_OK)
322 return Err;
324 cReferencedObjectsSize += 8;
325 return BenErr_OK;
328 BenObjectID
329 CBenValue::GetReferencedObject(pCBenReference pReference)
331 return UtGetIntelDWord(pReference->GetData());
334 BenObjectID
335 CBenValue::GetReferencedListID()
337 if (cpReferencedList != NULL)
338 return cpReferencedList->GetProperty()->GetBenObject()->GetID();
339 else
341 #ifdef BENUTIL_SUPPORT
342 return cReferencedListID;
343 #else
344 return 0;
345 #endif
348 }//end namespace OpenStormBento