diff options
Diffstat (limited to 'src/substream.cpp')
-rw-r--r-- | src/substream.cpp | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/src/substream.cpp b/src/substream.cpp new file mode 100644 index 0000000..ddb31b4 --- /dev/null +++ b/src/substream.cpp | |||
@@ -0,0 +1,109 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2008 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include "bu/substream.h" | ||
9 | |||
10 | Bu::SubStream::SubStream( Bu::Stream &rNext, long iSize ) : | ||
11 | Bu::Filter( rNext ), | ||
12 | iStart( 0 ), | ||
13 | iPos( 0 ), | ||
14 | iSize( iSize ) | ||
15 | { | ||
16 | iStart = rNext.tell(); | ||
17 | } | ||
18 | |||
19 | Bu::SubStream::~SubStream() | ||
20 | { | ||
21 | } | ||
22 | |||
23 | size_t Bu::SubStream::read( void *pBuf, size_t nBytes ) | ||
24 | { | ||
25 | if( (long)nBytes > iSize-iPos ) | ||
26 | nBytes = iSize-iPos; | ||
27 | nBytes = rNext.read( pBuf, nBytes ); | ||
28 | iPos += nBytes; | ||
29 | return nBytes; | ||
30 | } | ||
31 | |||
32 | size_t Bu::SubStream::write( const void *pBuf, size_t nBytes ) | ||
33 | { | ||
34 | if( (long)nBytes > iSize-iPos ) | ||
35 | nBytes = iSize-iPos; | ||
36 | nBytes = rNext.write( pBuf, nBytes ); | ||
37 | iPos += nBytes; | ||
38 | return nBytes; | ||
39 | } | ||
40 | |||
41 | void Bu::SubStream::start() | ||
42 | { | ||
43 | // doesn't mean anything... | ||
44 | } | ||
45 | |||
46 | size_t Bu::SubStream::stop() | ||
47 | { | ||
48 | // doesn't mean anything... | ||
49 | return 0; | ||
50 | } | ||
51 | |||
52 | void Bu::SubStream::close() | ||
53 | { | ||
54 | // don't do anything? maybe... | ||
55 | } | ||
56 | |||
57 | long Bu::SubStream::tell() | ||
58 | { | ||
59 | return iPos; | ||
60 | } | ||
61 | |||
62 | void Bu::SubStream::seek( long offset ) | ||
63 | { | ||
64 | if( iPos+offset < 0 ) | ||
65 | offset = -iPos; | ||
66 | else if( iPos+offset > iSize ) | ||
67 | offset = iSize-iPos; | ||
68 | rNext.seek( offset ); | ||
69 | iPos += offset; | ||
70 | } | ||
71 | |||
72 | void Bu::SubStream::setPos( long pos ) | ||
73 | { | ||
74 | if( pos < 0 ) | ||
75 | pos = 0; | ||
76 | else if( pos > iSize ) | ||
77 | pos = iSize; | ||
78 | iPos = pos; | ||
79 | pos += iStart; | ||
80 | rNext.setPos( pos ); | ||
81 | } | ||
82 | |||
83 | void Bu::SubStream::setPosEnd( long pos ) | ||
84 | { | ||
85 | if( iSize-pos < 0 ) | ||
86 | pos = 0; | ||
87 | else if( iSize-pos > iSize ) | ||
88 | pos = iSize; | ||
89 | else | ||
90 | pos = iSize-pos; | ||
91 | iPos = pos; | ||
92 | rNext.setPos( iStart+pos ); | ||
93 | } | ||
94 | |||
95 | bool Bu::SubStream::isEos() | ||
96 | { | ||
97 | return rNext.isEos() || iPos == iSize; | ||
98 | } | ||
99 | |||
100 | bool Bu::SubStream::canRead() | ||
101 | { | ||
102 | return rNext.canRead() && (iPos < iSize); | ||
103 | } | ||
104 | |||
105 | bool Bu::SubStream::canWrite() | ||
106 | { | ||
107 | return rNext.canWrite() && (iPos < iSize); | ||
108 | } | ||
109 | |||