1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "rechead.hxx"
21 #include "scerrors.hxx"
23 #include <osl/diagnose.h>
27 ScMultipleReadHeader::ScMultipleReadHeader(SvStream
& rNewStream
) :
31 rStream
.ReadUInt32( nDataSize
);
32 sal_uLong nDataPos
= rStream
.Tell();
33 nTotalEnd
= nDataPos
+ nDataSize
;
34 nEntryEnd
= nTotalEnd
;
36 rStream
.SeekRel(nDataSize
);
38 rStream
.ReadUInt16( nID
);
39 if (nID
!= SCID_SIZES
)
41 OSL_FAIL("SCID_SIZES not found");
42 if ( rStream
.GetError() == SVSTREAM_OK
)
43 rStream
.SetError( SVSTREAM_FILEFORMAT_ERROR
);
45 // everything to 0, so BytesLeft() aborts at least
46 pBuf
= NULL
; pMemStream
= NULL
;
51 sal_uInt32 nSizeTableLen
;
52 rStream
.ReadUInt32( nSizeTableLen
);
53 pBuf
= new sal_uInt8
[nSizeTableLen
];
54 rStream
.Read( pBuf
, nSizeTableLen
);
55 pMemStream
= new SvMemoryStream( pBuf
, nSizeTableLen
, StreamMode::READ
);
58 nEndPos
= rStream
.Tell();
59 rStream
.Seek( nDataPos
);
62 ScMultipleReadHeader::~ScMultipleReadHeader()
64 if ( pMemStream
&& pMemStream
->Tell() != pMemStream
->GetEndOfData() )
66 OSL_FAIL( "Sizes not fully read" );
67 if ( rStream
.GetError() == SVSTREAM_OK
)
68 rStream
.SetError( SCWARN_IMPORT_INFOLOST
);
73 rStream
.Seek(nEndPos
);
76 void ScMultipleReadHeader::EndEntry()
78 sal_uLong nPos
= rStream
.Tell();
79 OSL_ENSURE( nPos
<= nEntryEnd
, "read too much" );
80 if ( nPos
!= nEntryEnd
)
82 if ( rStream
.GetError() == SVSTREAM_OK
)
83 rStream
.SetError( SCWARN_IMPORT_INFOLOST
);
84 rStream
.Seek( nEntryEnd
); // ignore the rest
87 nEntryEnd
= nTotalEnd
; // all remaining, if no StartEntry follows
90 void ScMultipleReadHeader::StartEntry()
92 sal_uLong nPos
= rStream
.Tell();
93 sal_uInt32 nEntrySize
;
94 (*pMemStream
).ReadUInt32( nEntrySize
);
96 nEntryEnd
= nPos
+ nEntrySize
;
97 OSL_ENSURE( nEntryEnd
<= nTotalEnd
, "read too many entries" );
100 sal_uLong
ScMultipleReadHeader::BytesLeft() const
102 sal_uLong nReadEnd
= rStream
.Tell();
103 if (nReadEnd
<= nEntryEnd
)
104 return nEntryEnd
-nReadEnd
;
106 OSL_FAIL("ScMultipleReadHeader::BytesLeft: Error");
110 ScMultipleWriteHeader::ScMultipleWriteHeader(SvStream
& rNewStream
, sal_uInt32 nDefault
) :
111 rStream( rNewStream
),
112 aMemStream( 4096, 4096 )
114 nDataSize
= nDefault
;
115 rStream
.WriteUInt32( nDataSize
);
117 nDataPos
= rStream
.Tell();
118 nEntryStart
= nDataPos
;
121 ScMultipleWriteHeader::~ScMultipleWriteHeader()
123 sal_uLong nDataEnd
= rStream
.Tell();
125 rStream
.WriteUInt16( SCID_SIZES
);
126 rStream
.WriteUInt32( aMemStream
.Tell() );
127 rStream
.Write( aMemStream
.GetData(), aMemStream
.Tell() );
129 if ( nDataEnd
- nDataPos
!= nDataSize
) // matched default ?
131 nDataSize
= nDataEnd
- nDataPos
;
132 sal_uLong nPos
= rStream
.Tell();
133 rStream
.Seek(nDataPos
-sizeof(sal_uInt32
));
134 rStream
.WriteUInt32( nDataSize
); // record size at the beginning
139 void ScMultipleWriteHeader::EndEntry()
141 sal_uLong nPos
= rStream
.Tell();
142 aMemStream
.WriteUInt32( nPos
- nEntryStart
);
145 void ScMultipleWriteHeader::StartEntry()
147 sal_uLong nPos
= rStream
.Tell();
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */