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,
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 ************************************************************************/
57 namespace OpenStormBento
61 CBenValue::GetValueSize()
63 unsigned long Size
= 0;
64 pCBenValueSegment pCurr
= NULL
;
65 while ((pCurr
= GetNextValueSegment(pCurr
)) != NULL
)
66 Size
+= pCurr
->GetSize();
71 CBenValue::ReadValueData(BenDataPtr pReadBuffer
, unsigned long Offset
,
72 unsigned long Amt
, unsigned long * pAmtRead
)
75 unsigned long SegOffset
= 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
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
;
110 if ((Err
= pContainer
->SeekToPosition(pCurrSeg
->GetPosition() +
111 OffsetIntoSeg
)) != BenErr_OK
)
114 if ((Err
= pContainer
->Read(pBuffer
, AmtThisSeg
,
115 &AmtReadThisSeg
)) != BenErr_OK
)
119 *pAmtRead
+= AmtReadThisSeg
;
121 if (AmtThisSeg
!= AmtReadThisSeg
)
122 return BenErr_UnexpectedEndOfFile
;
124 pBuffer
+= AmtReadThisSeg
;
125 Offset
+= AmtReadThisSeg
;
126 Amt
-= AmtReadThisSeg
;
129 SegOffset
+= pCurrSeg
->GetSize();
135 CBenValue::ReadValueDataKnownSize(BenDataPtr pBuffer
, unsigned long Offset
,
138 unsigned long AmtRead
;
139 BenError Err
= ReadValueData(pBuffer
, Offset
, Amt
, &AmtRead
);
141 if (Err
== UtErr_OK
&& AmtRead
!= Amt
)
142 Err
= (BenError
) UtErr_Fail
;
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
;
161 unsigned long SegOffset
= 0;
162 pCBenValueSegment pCurrSeg
= NULL
;
164 pLtcBenContainer pContainer
= GetContainer();
165 BenConstByteDataPtr pBuffer
= (BenConstByteDataPtr
) pWriteBuffer
;
167 while ((pCurrSeg
= GetNextValueSegment(pCurrSeg
)) != NULL
)
172 if (SegOffset
<= Offset
&& Offset
< SegOffset
+ pCurrSeg
->GetSize())
174 unsigned long OffsetIntoSeg
= Offset
- SegOffset
;
176 unsigned long AmtThisSeg
= UtMin(Amt
, pCurrSeg
->GetSize() -
179 if (pCurrSeg
->IsImmediate())
181 UtHugeMemcpy(pCurrSeg
->GetImmediateData() + OffsetIntoSeg
,
182 pBuffer
, AmtThisSeg
);
183 *pAmtWritten
+= AmtThisSeg
;
187 if ((Err
= pContainer
->SeekToPosition(pCurrSeg
->GetPosition()
188 + OffsetIntoSeg
)) != BenErr_OK
)
191 unsigned long AmtWrittenThisWrite
;
192 Err
= pContainer
->Write(pBuffer
, AmtThisSeg
,
193 &AmtWrittenThisWrite
);
194 *pAmtWritten
+= AmtWrittenThisWrite
;
195 if (Err
!= BenErr_OK
)
199 pBuffer
+= AmtThisSeg
;
200 Offset
+= AmtThisSeg
;
204 SegOffset
+= pCurrSeg
->GetSize();
207 if (Offset
> SegOffset
)
209 assert(! "Write past end of value");
210 return BenErr_InvalidWriteOffset
;
216 if ((Err
= pContainer
->SeekFromEnd(0)) != BenErr_OK
)
219 BenContainerPos EndFilePos
;
220 if ((Err
= pContainer
->GetPosition(&EndFilePos
)) != BenErr_OK
)
223 unsigned long AmtWrittenThisWrite
;
224 Err
= pContainer
->Write(pBuffer
, Amt
, &AmtWrittenThisWrite
);
225 *pAmtWritten
+= AmtWrittenThisWrite
;
226 if (Err
!= BenErr_OK
)
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
);
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
-
255 else pCurrSeg
->SetSize(SegSize
);
257 SegOffset
+= SegSize
;
265 CBenValue::WriteValueData(BenConstDataPtr pWriteBuffer
, unsigned long Offset
,
268 unsigned long AmtWritten
;
269 return WriteValueData(pWriteBuffer
, Offset
, Amt
, &AmtWritten
);
273 CBenValue::WriteImmediateValueData(BenConstDataPtr pBuffer
,
276 // Only one write can be an immediate value in current implementation
277 if (cValueSegments
.GetFirst() != cValueSegments
.GetTerminating() ||
279 return BenErr_InvalidImmediateWrite
;
284 new CBenValueSegment(this, pBuffer
, Size
);
289 CBenValue::NewReference(BenObjectID ReferencedObjectID
, pCBenReference
294 if (cpReferencedList
== NULL
)
296 pLtcBenContainer pContainer
= cpProperty
->GetContainer();
298 pCBenObject pNewObject
;
299 if ((Err
= pContainer
->NewObject(&pNewObject
)) != BenErr_OK
)
302 pCBenValue pNewValue
;
303 if ((Err
= pNewObject
->NewValue(BEN_PROPID_OBJ_REFERENCES
,
304 BEN_TYPEID_OBJ_REFERENCES_DATA
, &pNewValue
)) != BenErr_OK
)
310 cpReferencedList
= pNewValue
;
311 cReferencedObjectsSize
= 0;
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
)
324 cReferencedObjectsSize
+= 8;
329 CBenValue::GetReferencedObject(pCBenReference pReference
)
331 return UtGetIntelDWord(pReference
->GetData());
335 CBenValue::GetReferencedListID()
337 if (cpReferencedList
!= NULL
)
338 return cpReferencedList
->GetProperty()->GetBenObject()->GetID();
341 #ifdef BENUTIL_SUPPORT
342 return cReferencedListID
;
348 }//end namespace OpenStormBento