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#ifndef __STREAM_WINDOWOUT_H
20#define __STREAM_WINDOWOUT_H
21
22#include "iiostrm.h"
23
24namespace NStream {
25namespace NWindow {
26
27// m_KeepSizeBefore: how mach BYTEs must be in buffer before m_Pos;
28// m_KeepSizeAfter: how mach BYTEs must be in buffer after m_Pos;
29// m_KeepSizeReserv: how mach BYTEs must be in buffer for Moving Reserv;
30// must be >= aKeepSizeAfter; // test it
31
32class COut
33{
34 BYTE *m_Buffer;
35 UINT32 m_Pos;
36 UINT32 m_PosLimit;
37 UINT32 m_KeepSizeBefore;
38 UINT32 m_KeepSizeAfter;
39 UINT32 m_KeepSizeReserv;
40 UINT32 m_StreamPos;
41
42 UINT32 m_WindowSize;
43 UINT32 m_MoveFrom;
44
45 ISequentialOutStream *m_Stream;
46
47 virtual void MoveBlockBackward();
48public:
49 COut(): m_Buffer(0), m_Stream(0) {}
50 virtual ~COut();
51 void Create(UINT32 aKeepSizeBefore,
52 UINT32 aKeepSizeAfter, UINT32 aKeepSizeReserv = (1<<17));
53 void SetWindowSize(UINT32 aWindowSize);
54
55 void Init(ISequentialOutStream *aStream, bool aSolid = false);
56 HRESULT Flush();
57
58 UINT32 GetCurPos() const { return m_Pos; }
59 const BYTE *GetPointerToCurrentPos() const { return m_Buffer + m_Pos;};
60
61 void CopyBackBlock(UINT32 aDistance, UINT32 aLen)
62 {
63 if (m_Pos >= m_PosLimit)
64 MoveBlockBackward();
65 BYTE *p = m_Buffer + m_Pos;
66 aDistance++;
67 BYTE *p2 = p - aDistance;
68 for(UINT32 i = 0; i < aLen; i++)
69 p[i] = p2[i];
70 m_Pos += aLen;
71 }
72
73 void PutOneByte(BYTE aByte)
74 {
75 if (m_Pos >= m_PosLimit)
76 MoveBlockBackward();
77 m_Buffer[m_Pos++] = aByte;
78 }
79
80 BYTE GetOneByte(UINT32 anIndex) const
81 {
82 return m_Buffer[m_Pos + anIndex];
83 }
84
85 BYTE *GetBuffer() const { return m_Buffer; }
86};
87
88}}
89
90#endif
91