aboutsummaryrefslogtreecommitdiff
path: root/src/stable/substream.cpp
blob: c201752c80b380bb93a8716de8fd98433a67d49c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "bu/substream.h"

Bu::SubStream::SubStream( Bu::Stream &rNext, Bu::size iSize ) :
	Bu::Filter( rNext ),
	iStart( 0 ),
	iPos( 0 ),
	iSize( iSize )
{
	iStart = rNext.tell();
}

Bu::SubStream::~SubStream()
{
}

Bu::size Bu::SubStream::read( void *pBuf, Bu::size nBytes )
{
	if( (Bu::size)nBytes > iSize-iPos )
		nBytes = iSize-iPos;
	nBytes = rNext.read( pBuf, nBytes );
	iPos += nBytes;
	return nBytes;
}

Bu::size Bu::SubStream::write( const void *pBuf, Bu::size nBytes )
{
	if( (Bu::size)nBytes > iSize-iPos )
		nBytes = iSize-iPos;
	nBytes = rNext.write( pBuf, nBytes );
	iPos += nBytes;
	return nBytes;
}

void Bu::SubStream::start()
{
	// doesn't mean anything...
}

Bu::size Bu::SubStream::stop()
{
	// doesn't mean anything...
	return 0;
}

void Bu::SubStream::close()
{
	// don't do anything?  maybe...
}

Bu::size Bu::SubStream::tell()
{
	return iPos;
}

void Bu::SubStream::seek( Bu::size offset )
{
	if( iPos+offset < 0 )
		offset = -iPos;
	else if( iPos+offset > iSize )
		offset = iSize-iPos;
	rNext.seek( offset );
	iPos += offset;
}

void Bu::SubStream::setPos( Bu::size pos )
{
	if( pos < 0 )
		pos = 0;
	else if( pos > iSize )
		pos = iSize;
	iPos = pos;
	pos += iStart;
	rNext.setPos( pos );
}

void Bu::SubStream::setPosEnd( Bu::size pos )
{
	if( iSize-pos < 0 )
		pos = 0;
	else if( iSize-pos > iSize )
		pos = iSize;
	else
		pos = iSize-pos;
	iPos = pos;
	rNext.setPos( iStart+pos );
}

bool Bu::SubStream::isEos()
{
	return rNext.isEos() || iPos == iSize;
}

bool Bu::SubStream::canRead()
{
	return rNext.canRead() && (iPos < iSize);
}

bool Bu::SubStream::canWrite()
{
	return rNext.canWrite() && (iPos < iSize);
}