xref: /aoo41x/main/sc/source/core/tool/rechead.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sc.hxx"
30 
31 
32 
33 // INCLUDE ---------------------------------------------------------------
34 
35 #include <tools/debug.hxx>
36 
37 #include "rechead.hxx"
38 #include "scerrors.hxx"
39 
40 // STATIC DATA -----------------------------------------------------------
41 
42 // =======================================================================
43 
44 ScMultipleReadHeader::ScMultipleReadHeader(SvStream& rNewStream) :
45 	rStream( rNewStream )
46 {
47 	sal_uInt32 nDataSize;
48 	rStream >> nDataSize;
49 	sal_uLong nDataPos = rStream.Tell();
50 	nTotalEnd = nDataPos + nDataSize;
51 	nEntryEnd = nTotalEnd;
52 
53 	rStream.SeekRel(nDataSize);
54 	sal_uInt16 nID;
55 	rStream >> nID;
56 	if (nID != SCID_SIZES)
57 	{
58 		DBG_ERROR("SCID_SIZES nicht gefunden");
59 		if ( rStream.GetError() == SVSTREAM_OK )
60 			rStream.SetError( SVSTREAM_FILEFORMAT_ERROR );
61 
62 		//	alles auf 0, damit BytesLeft() wenigstens abbricht
63 		pBuf = NULL; pMemStream = NULL;
64 		nEntryEnd = nDataPos;
65 	}
66 	else
67 	{
68 		sal_uInt32 nSizeTableLen;
69 		rStream >> nSizeTableLen;
70 		pBuf = new sal_uInt8[nSizeTableLen];
71 		rStream.Read( pBuf, nSizeTableLen );
72 		pMemStream = new SvMemoryStream( (char*)pBuf, nSizeTableLen, STREAM_READ );
73 	}
74 
75 	nEndPos = rStream.Tell();
76 	rStream.Seek( nDataPos );
77 }
78 
79 ScMultipleReadHeader::~ScMultipleReadHeader()
80 {
81 	if ( pMemStream && pMemStream->Tell() != pMemStream->GetEndOfData() )
82 	{
83 		DBG_ERRORFILE( "Sizes nicht vollstaendig gelesen" );
84 		if ( rStream.GetError() == SVSTREAM_OK )
85 			rStream.SetError( SCWARN_IMPORT_INFOLOST );
86 	}
87 	delete pMemStream;
88 	delete[] pBuf;
89 
90 	rStream.Seek(nEndPos);
91 }
92 
93 void ScMultipleReadHeader::EndEntry()
94 {
95 	sal_uLong nPos = rStream.Tell();
96 	DBG_ASSERT( nPos <= nEntryEnd, "zuviel gelesen" );
97 	if ( nPos != nEntryEnd )
98 	{
99 		if ( rStream.GetError() == SVSTREAM_OK )
100 			rStream.SetError( SCWARN_IMPORT_INFOLOST );
101 		rStream.Seek( nEntryEnd );			// Rest ueberspringen
102 	}
103 
104 	nEntryEnd = nTotalEnd;			// den ganzen Rest, wenn kein StartEntry kommt
105 }
106 
107 void ScMultipleReadHeader::StartEntry()
108 {
109 	sal_uLong nPos = rStream.Tell();
110 	sal_uInt32 nEntrySize;
111 	(*pMemStream) >> nEntrySize;
112 
113 	nEntryEnd = nPos + nEntrySize;
114 	DBG_ASSERT( nEntryEnd <= nTotalEnd, "zuviele Eintraege gelesen" );
115 }
116 
117 sal_uLong ScMultipleReadHeader::BytesLeft() const
118 {
119 	sal_uLong nReadEnd = rStream.Tell();
120 	if (nReadEnd <= nEntryEnd)
121 		return nEntryEnd-nReadEnd;
122 
123 	DBG_ERROR("Fehler bei ScMultipleReadHeader::BytesLeft");
124 	return 0;
125 }
126 
127 // -----------------------------------------------------------------------
128 
129 ScMultipleWriteHeader::ScMultipleWriteHeader(SvStream& rNewStream, sal_uInt32 nDefault) :
130 	rStream( rNewStream ),
131 	aMemStream( 4096, 4096 )
132 {
133 	nDataSize = nDefault;
134 	rStream << nDataSize;
135 
136 	nDataPos = rStream.Tell();
137 	nEntryStart = nDataPos;
138 }
139 
140 ScMultipleWriteHeader::~ScMultipleWriteHeader()
141 {
142 	sal_uLong nDataEnd = rStream.Tell();
143 
144 	rStream << (sal_uInt16) SCID_SIZES;
145 	rStream << static_cast<sal_uInt32>(aMemStream.Tell());
146 	rStream.Write( aMemStream.GetData(), aMemStream.Tell() );
147 
148 	if ( nDataEnd - nDataPos != nDataSize )					// Default getroffen?
149 	{
150 		nDataSize = nDataEnd - nDataPos;
151 		sal_uLong nPos = rStream.Tell();
152 		rStream.Seek(nDataPos-sizeof(sal_uInt32));
153 		rStream << nDataSize;								// Groesse am Anfang eintragen
154 		rStream.Seek(nPos);
155 	}
156 }
157 
158 void ScMultipleWriteHeader::EndEntry()
159 {
160 	sal_uLong nPos = rStream.Tell();
161 	aMemStream << static_cast<sal_uInt32>(nPos - nEntryStart);
162 }
163 
164 void ScMultipleWriteHeader::StartEntry()
165 {
166 	sal_uLong nPos = rStream.Tell();
167 	nEntryStart = nPos;
168 }
169 
170 
171 
172 
173 
174