Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "rechead.hxx"
21 : #include "scerrors.hxx"
22 :
23 : #include <osl/diagnose.h>
24 :
25 : // STATIC DATA
26 :
27 0 : ScMultipleReadHeader::ScMultipleReadHeader(SvStream& rNewStream) :
28 0 : rStream( rNewStream )
29 : {
30 : sal_uInt32 nDataSize;
31 0 : rStream.ReadUInt32( nDataSize );
32 0 : sal_uLong nDataPos = rStream.Tell();
33 0 : nTotalEnd = nDataPos + nDataSize;
34 0 : nEntryEnd = nTotalEnd;
35 :
36 0 : rStream.SeekRel(nDataSize);
37 : sal_uInt16 nID;
38 0 : rStream.ReadUInt16( nID );
39 0 : if (nID != SCID_SIZES)
40 : {
41 : OSL_FAIL("SCID_SIZES not found");
42 0 : if ( rStream.GetError() == SVSTREAM_OK )
43 0 : rStream.SetError( SVSTREAM_FILEFORMAT_ERROR );
44 :
45 : // everything to 0, so BytesLeft() aborts at least
46 0 : pBuf = NULL; pMemStream = NULL;
47 0 : nEntryEnd = nDataPos;
48 : }
49 : else
50 : {
51 : sal_uInt32 nSizeTableLen;
52 0 : rStream.ReadUInt32( nSizeTableLen );
53 0 : pBuf = new sal_uInt8[nSizeTableLen];
54 0 : rStream.Read( pBuf, nSizeTableLen );
55 0 : pMemStream = new SvMemoryStream( pBuf, nSizeTableLen, StreamMode::READ );
56 : }
57 :
58 0 : nEndPos = rStream.Tell();
59 0 : rStream.Seek( nDataPos );
60 0 : }
61 :
62 0 : ScMultipleReadHeader::~ScMultipleReadHeader()
63 : {
64 0 : if ( pMemStream && pMemStream->Tell() != pMemStream->GetEndOfData() )
65 : {
66 : OSL_FAIL( "Sizes not fully read" );
67 0 : if ( rStream.GetError() == SVSTREAM_OK )
68 0 : rStream.SetError( SCWARN_IMPORT_INFOLOST );
69 : }
70 0 : delete pMemStream;
71 0 : delete[] pBuf;
72 :
73 0 : rStream.Seek(nEndPos);
74 0 : }
75 :
76 0 : void ScMultipleReadHeader::EndEntry()
77 : {
78 0 : sal_uLong nPos = rStream.Tell();
79 : OSL_ENSURE( nPos <= nEntryEnd, "read too much" );
80 0 : if ( nPos != nEntryEnd )
81 : {
82 0 : if ( rStream.GetError() == SVSTREAM_OK )
83 0 : rStream.SetError( SCWARN_IMPORT_INFOLOST );
84 0 : rStream.Seek( nEntryEnd ); // ignore the rest
85 : }
86 :
87 0 : nEntryEnd = nTotalEnd; // all remaining, if no StartEntry follows
88 0 : }
89 :
90 0 : void ScMultipleReadHeader::StartEntry()
91 : {
92 0 : sal_uLong nPos = rStream.Tell();
93 : sal_uInt32 nEntrySize;
94 0 : (*pMemStream).ReadUInt32( nEntrySize );
95 :
96 0 : nEntryEnd = nPos + nEntrySize;
97 : OSL_ENSURE( nEntryEnd <= nTotalEnd, "read too many entries" );
98 0 : }
99 :
100 0 : sal_uLong ScMultipleReadHeader::BytesLeft() const
101 : {
102 0 : sal_uLong nReadEnd = rStream.Tell();
103 0 : if (nReadEnd <= nEntryEnd)
104 0 : return nEntryEnd-nReadEnd;
105 :
106 : OSL_FAIL("ScMultipleReadHeader::BytesLeft: Error");
107 0 : return 0;
108 : }
109 :
110 0 : ScMultipleWriteHeader::ScMultipleWriteHeader(SvStream& rNewStream, sal_uInt32 nDefault) :
111 : rStream( rNewStream ),
112 0 : aMemStream( 4096, 4096 )
113 : {
114 0 : nDataSize = nDefault;
115 0 : rStream.WriteUInt32( nDataSize );
116 :
117 0 : nDataPos = rStream.Tell();
118 0 : nEntryStart = nDataPos;
119 0 : }
120 :
121 0 : ScMultipleWriteHeader::~ScMultipleWriteHeader()
122 : {
123 0 : sal_uLong nDataEnd = rStream.Tell();
124 :
125 0 : rStream.WriteUInt16( SCID_SIZES );
126 0 : rStream.WriteUInt32( aMemStream.Tell() );
127 0 : rStream.Write( aMemStream.GetData(), aMemStream.Tell() );
128 :
129 0 : if ( nDataEnd - nDataPos != nDataSize ) // matched default ?
130 : {
131 0 : nDataSize = nDataEnd - nDataPos;
132 0 : sal_uLong nPos = rStream.Tell();
133 0 : rStream.Seek(nDataPos-sizeof(sal_uInt32));
134 0 : rStream.WriteUInt32( nDataSize ); // record size at the beginning
135 0 : rStream.Seek(nPos);
136 : }
137 0 : }
138 :
139 0 : void ScMultipleWriteHeader::EndEntry()
140 : {
141 0 : sal_uLong nPos = rStream.Tell();
142 0 : aMemStream.WriteUInt32( nPos - nEntryStart );
143 0 : }
144 :
145 0 : void ScMultipleWriteHeader::StartEntry()
146 : {
147 0 : sal_uLong nPos = rStream.Tell();
148 0 : nEntryStart = nPos;
149 0 : }
150 :
151 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|