1 | /* |
2 | Copyright (C) 2002 Andrea Mazzoleni ( http://advancemame.sf.net ) |
3 | Copyright (C) 2001-4 Igor Pavlov ( http://www.7-zip.org ) |
4 | |
5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License version 2.1 as published by the Free Software Foundation. |
8 | |
9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with this library; if not, write to the Free Software |
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 | */ |
18 | |
19 | #include "winout.h" |
20 | |
21 | namespace NStream { |
22 | namespace NWindow { |
23 | |
24 | void 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 | |
39 | COut::~COut() |
40 | { |
41 | delete []m_Buffer; |
42 | } |
43 | |
44 | void COut::SetWindowSize(UINT32 aWindowSize) |
45 | { |
46 | m_WindowSize = aWindowSize; |
47 | m_MoveFrom = m_KeepSizeReserv + m_KeepSizeBefore - aWindowSize; |
48 | } |
49 | |
50 | void 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 | |
64 | HRESULT 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 | |
79 | void 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 | |