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 <tools/debug.hxx>
21 
22 #include <sdiocmpt.hxx>
23 
old_SdrDownCompat(SvStream & rNewStream,StreamMode nNewMode)24 old_SdrDownCompat::old_SdrDownCompat(SvStream& rNewStream, StreamMode nNewMode)
25 :   rStream(rNewStream),
26     nSubRecSiz(0),
27     nSubRecPos(0),
28     nMode(nNewMode),
29     bOpen(false)
30 {
31     OpenSubRecord();
32 }
33 
~old_SdrDownCompat()34 old_SdrDownCompat::~old_SdrDownCompat()
35 {
36     if(bOpen)
37         CloseSubRecord();
38 }
39 
Write()40 void old_SdrDownCompat::Write()
41 {
42     rStream.WriteUInt32( nSubRecSiz );
43 }
44 
OpenSubRecord()45 void old_SdrDownCompat::OpenSubRecord()
46 {
47     if(rStream.GetError())
48         return;
49 
50     nSubRecPos = rStream.Tell();
51 
52     if(nMode == StreamMode::READ)
53     {
54         rStream.ReadUInt32( nSubRecSiz );
55     }
56     else if(nMode == StreamMode::WRITE)
57     {
58         Write();
59     }
60 
61     bOpen = true;
62 }
63 
CloseSubRecord()64 void old_SdrDownCompat::CloseSubRecord()
65 {
66     if(rStream.GetError())
67         return;
68 
69     sal_uInt32 nCurrentPos(rStream.Tell());
70 
71     if(nMode == StreamMode::READ)
72     {
73         sal_uInt32 nReadCnt(nCurrentPos - nSubRecPos);
74         if(nReadCnt != nSubRecSiz)
75         {
76             rStream.Seek(nSubRecPos + nSubRecSiz);
77         }
78     }
79     else if(nMode == StreamMode::WRITE)
80     {
81         nSubRecSiz = nCurrentPos - nSubRecPos;
82         rStream.Seek(nSubRecPos);
83         Write();
84         rStream.Seek(nCurrentPos);
85     }
86 
87     bOpen = false;
88 }
89 
90 /*************************************************************************
91 |*
92 |* Constructor, writes and reads version number
93 |*
94 \************************************************************************/
95 
SdIOCompat(SvStream & rNewStream,StreamMode nNewMode,sal_uInt16 nVersion)96 SdIOCompat::SdIOCompat(SvStream& rNewStream, StreamMode nNewMode, sal_uInt16 nVersion)
97 :   old_SdrDownCompat(rNewStream, nNewMode)
98 {
99     if (nNewMode == StreamMode::WRITE)
100     {
101         DBG_ASSERT(nVersion != SDIOCOMPAT_VERSIONDONTKNOW,
102                    "can't write unknown version");
103         rNewStream.WriteUInt16( nVersion );
104     }
105     else if (nNewMode == StreamMode::READ)
106     {
107         DBG_ASSERT(nVersion == SDIOCOMPAT_VERSIONDONTKNOW,
108                    "referring to the version while reading is silly!");
109         rNewStream.ReadUInt16( nVersion );
110     }
111 }
112 
~SdIOCompat()113 SdIOCompat::~SdIOCompat()
114 {
115 }
116 
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
118