1/*
2Copyright (C) 2002 Andrea Mazzoleni ( http://advancemame.sf.net )
3Copyright (C) 2001-4 Igor Pavlov ( http://www.7-zip.org )
4
5This library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Lesser General Public
7License version 2.1 as published by the Free Software Foundation.
8
9This library is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12Lesser General Public License for more details.
13
14You should have received a copy of the GNU Lesser General Public
15License along with this library; if not, write to the Free Software
16Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18
19#include "winout.h"
20
21namespace NStream {
22namespace NWindow {
23
24void COut::Create(UINT32 aKeepSizeBefore, UINT32 aKeepSizeAfter, UINT32 aKeepSizeReserv)
25{
26 m_Pos = 0;
27 m_PosLimit = aKeepSizeReserv + aKeepSizeBefore;
28 m_KeepSizeBefore = aKeepSizeBefore;
29 m_KeepSizeAfter = aKeepSizeAfter;
30 m_KeepSizeReserv = aKeepSizeReserv;
31 m_StreamPos = 0;
32 m_MoveFrom = m_KeepSizeReserv;
33 m_WindowSize = aKeepSizeBefore;
34 UINT32 aBlockSize = m_KeepSizeBefore + m_KeepSizeAfter + m_KeepSizeReserv;
35 delete []m_Buffer;
36 m_Buffer = new BYTE[aBlockSize];
37}
38
39COut::~COut()
40{
41 delete []m_Buffer;
42}
43
44void COut::SetWindowSize(UINT32 aWindowSize)
45{
46 m_WindowSize = aWindowSize;
47 m_MoveFrom = m_KeepSizeReserv + m_KeepSizeBefore - aWindowSize;
48}
49
50void COut::Init(ISequentialOutStream *aStream, bool aSolid)
51{
52 m_Stream = aStream;
53
54 if(aSolid)
55 m_StreamPos = m_Pos;
56 else
57 {
58 m_Pos = 0;
59 m_PosLimit = m_KeepSizeReserv + m_KeepSizeBefore;
60 m_StreamPos = 0;
61 }
62}
63
64HRESULT COut::Flush()
65{
66 UINT32 aSize = m_Pos - m_StreamPos;
67 if(aSize == 0)
68 return S_OK;
69 UINT32 aProcessedSize;
70 HRESULT aResult = m_Stream->Write(m_Buffer + m_StreamPos, aSize, &aProcessedSize);
71 if (aResult != S_OK)
72 return aResult;
73 if (aSize != aProcessedSize)
74 return E_FAIL;
75 m_StreamPos = m_Pos;
76 return S_OK;
77}
78
79void COut::MoveBlockBackward()
80{
81 HRESULT aResult = Flush();
82 if (aResult != S_OK)
83 throw aResult;
84 memmove(m_Buffer, m_Buffer + m_MoveFrom, m_WindowSize + m_KeepSizeAfter);
85 m_Pos -= m_MoveFrom;
86 m_StreamPos -= m_MoveFrom;
87}
88
89}}
90