xref: /AOO41X/main/sc/source/core/tool/rechead.cxx (revision b3f79822e811ac3493b185030a72c3c5a51f32d8)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 
28 
29 // INCLUDE ---------------------------------------------------------------
30 
31 #include <tools/debug.hxx>
32 
33 #include "rechead.hxx"
34 #include "scerrors.hxx"
35 
36 // STATIC DATA -----------------------------------------------------------
37 
38 // =======================================================================
39 
ScMultipleReadHeader(SvStream & rNewStream)40 ScMultipleReadHeader::ScMultipleReadHeader(SvStream& rNewStream) :
41     rStream( rNewStream )
42 {
43     sal_uInt32 nDataSize;
44     rStream >> nDataSize;
45     sal_uLong nDataPos = rStream.Tell();
46     nTotalEnd = nDataPos + nDataSize;
47     nEntryEnd = nTotalEnd;
48 
49     rStream.SeekRel(nDataSize);
50     sal_uInt16 nID;
51     rStream >> nID;
52     if (nID != SCID_SIZES)
53     {
54         DBG_ERROR("SCID_SIZES nicht gefunden");
55         if ( rStream.GetError() == SVSTREAM_OK )
56             rStream.SetError( SVSTREAM_FILEFORMAT_ERROR );
57 
58         //  alles auf 0, damit BytesLeft() wenigstens abbricht
59         pBuf = NULL; pMemStream = NULL;
60         nEntryEnd = nDataPos;
61     }
62     else
63     {
64         sal_uInt32 nSizeTableLen;
65         rStream >> nSizeTableLen;
66         pBuf = new sal_uInt8[nSizeTableLen];
67         rStream.Read( pBuf, nSizeTableLen );
68         pMemStream = new SvMemoryStream( (char*)pBuf, nSizeTableLen, STREAM_READ );
69     }
70 
71     nEndPos = rStream.Tell();
72     rStream.Seek( nDataPos );
73 }
74 
~ScMultipleReadHeader()75 ScMultipleReadHeader::~ScMultipleReadHeader()
76 {
77     if ( pMemStream && pMemStream->Tell() != pMemStream->GetEndOfData() )
78     {
79         DBG_ERRORFILE( "Sizes nicht vollstaendig gelesen" );
80         if ( rStream.GetError() == SVSTREAM_OK )
81             rStream.SetError( SCWARN_IMPORT_INFOLOST );
82     }
83     delete pMemStream;
84     delete[] pBuf;
85 
86     rStream.Seek(nEndPos);
87 }
88 
EndEntry()89 void ScMultipleReadHeader::EndEntry()
90 {
91     sal_uLong nPos = rStream.Tell();
92     DBG_ASSERT( nPos <= nEntryEnd, "zuviel gelesen" );
93     if ( nPos != nEntryEnd )
94     {
95         if ( rStream.GetError() == SVSTREAM_OK )
96             rStream.SetError( SCWARN_IMPORT_INFOLOST );
97         rStream.Seek( nEntryEnd );          // Rest ueberspringen
98     }
99 
100     nEntryEnd = nTotalEnd;          // den ganzen Rest, wenn kein StartEntry kommt
101 }
102 
StartEntry()103 void ScMultipleReadHeader::StartEntry()
104 {
105     sal_uLong nPos = rStream.Tell();
106     sal_uInt32 nEntrySize;
107     (*pMemStream) >> nEntrySize;
108 
109     nEntryEnd = nPos + nEntrySize;
110     DBG_ASSERT( nEntryEnd <= nTotalEnd, "zuviele Eintraege gelesen" );
111 }
112 
BytesLeft() const113 sal_uLong ScMultipleReadHeader::BytesLeft() const
114 {
115     sal_uLong nReadEnd = rStream.Tell();
116     if (nReadEnd <= nEntryEnd)
117         return nEntryEnd-nReadEnd;
118 
119     DBG_ERROR("Fehler bei ScMultipleReadHeader::BytesLeft");
120     return 0;
121 }
122 
123 // -----------------------------------------------------------------------
124 
ScMultipleWriteHeader(SvStream & rNewStream,sal_uInt32 nDefault)125 ScMultipleWriteHeader::ScMultipleWriteHeader(SvStream& rNewStream, sal_uInt32 nDefault) :
126     rStream( rNewStream ),
127     aMemStream( 4096, 4096 )
128 {
129     nDataSize = nDefault;
130     rStream << nDataSize;
131 
132     nDataPos = rStream.Tell();
133     nEntryStart = nDataPos;
134 }
135 
~ScMultipleWriteHeader()136 ScMultipleWriteHeader::~ScMultipleWriteHeader()
137 {
138     sal_uLong nDataEnd = rStream.Tell();
139 
140     rStream << (sal_uInt16) SCID_SIZES;
141     rStream << static_cast<sal_uInt32>(aMemStream.Tell());
142     rStream.Write( aMemStream.GetData(), aMemStream.Tell() );
143 
144     if ( nDataEnd - nDataPos != nDataSize )                 // Default getroffen?
145     {
146         nDataSize = nDataEnd - nDataPos;
147         sal_uLong nPos = rStream.Tell();
148         rStream.Seek(nDataPos-sizeof(sal_uInt32));
149         rStream << nDataSize;                               // Groesse am Anfang eintragen
150         rStream.Seek(nPos);
151     }
152 }
153 
EndEntry()154 void ScMultipleWriteHeader::EndEntry()
155 {
156     sal_uLong nPos = rStream.Tell();
157     aMemStream << static_cast<sal_uInt32>(nPos - nEntryStart);
158 }
159 
StartEntry()160 void ScMultipleWriteHeader::StartEntry()
161 {
162     sal_uLong nPos = rStream.Tell();
163     nEntryStart = nPos;
164 }
165 
166 
167 
168 
169 
170