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 : // STATIC DATA -----------------------------------------------------------
24 :
25 : // =======================================================================
26 :
27 0 : ScMultipleReadHeader::ScMultipleReadHeader(SvStream& rNewStream) :
28 0 : rStream( rNewStream )
29 : {
30 : sal_uInt32 nDataSize;
31 0 : rStream >> 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 >> nID;
39 0 : if (nID != SCID_SIZES)
40 : {
41 : OSL_FAIL("SCID_SIZES nicht gefunden");
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 >> nSizeTableLen;
53 0 : pBuf = new sal_uInt8[nSizeTableLen];
54 0 : rStream.Read( pBuf, nSizeTableLen );
55 0 : pMemStream = new SvMemoryStream( (char*)pBuf, nSizeTableLen, STREAM_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 nicht vollstaendig gelesen" );
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, "zuviel gelesen" );
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) >> nEntrySize;
95 :
96 0 : nEntryEnd = nPos + nEntrySize;
97 : OSL_ENSURE( nEntryEnd <= nTotalEnd, "zuviele Eintraege gelesen" );
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("Fehler bei ScMultipleReadHeader::BytesLeft");
107 0 : return 0;
108 : }
109 :
110 : // -----------------------------------------------------------------------
111 :
112 0 : ScMultipleWriteHeader::ScMultipleWriteHeader(SvStream& rNewStream, sal_uInt32 nDefault) :
113 : rStream( rNewStream ),
114 0 : aMemStream( 4096, 4096 )
115 : {
116 0 : nDataSize = nDefault;
117 0 : rStream << nDataSize;
118 :
119 0 : nDataPos = rStream.Tell();
120 0 : nEntryStart = nDataPos;
121 0 : }
122 :
123 0 : ScMultipleWriteHeader::~ScMultipleWriteHeader()
124 : {
125 0 : sal_uLong nDataEnd = rStream.Tell();
126 :
127 0 : rStream << (sal_uInt16) SCID_SIZES;
128 0 : rStream << static_cast<sal_uInt32>(aMemStream.Tell());
129 0 : rStream.Write( aMemStream.GetData(), aMemStream.Tell() );
130 :
131 0 : if ( nDataEnd - nDataPos != nDataSize ) // matched default ?
132 : {
133 0 : nDataSize = nDataEnd - nDataPos;
134 0 : sal_uLong nPos = rStream.Tell();
135 0 : rStream.Seek(nDataPos-sizeof(sal_uInt32));
136 0 : rStream << nDataSize; // record size at the beginning
137 0 : rStream.Seek(nPos);
138 : }
139 0 : }
140 :
141 0 : void ScMultipleWriteHeader::EndEntry()
142 : {
143 0 : sal_uLong nPos = rStream.Tell();
144 0 : aMemStream << static_cast<sal_uInt32>(nPos - nEntryStart);
145 0 : }
146 :
147 0 : void ScMultipleWriteHeader::StartEntry()
148 : {
149 0 : sal_uLong nPos = rStream.Tell();
150 0 : nEntryStart = nPos;
151 0 : }
152 :
153 :
154 :
155 :
156 :
157 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|