aboutsummaryrefslogtreecommitdiff
path: root/src/stable/myriadstream.cpp
blob: 9ea2e17462742534f10b1cb9e77ab1910e737626 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "bu/myriadstream.h"

#include "bu/mutexlocker.h"

Bu::MyriadStream::MyriadStream( Bu::Myriad &rMyriad,
        Bu::Myriad::Stream *pStream, Bu::Myriad::Mode eMode ) :
    rMyriad( rMyriad ),
    pStream( pStream ),
    eMode( eMode ),
    iPos( 0 )
{
    if( (eMode&Bu::Myriad::ReadWrite) == 0 )
    {
        throw Bu::MyriadException( Bu::MyriadException::invalidParameter,
                "MyriadStream must be opened Read or Write or both.");
    }
    Bu::MutexLocker l( mAccess );
    pStream->open();

    if( (eMode&Bu::Myriad::Write) != 0 )
    {
        // Writing mode, what other options do we deal with?
        if( (eMode&Bu::Myriad::Truncate) != 0 )
        {
            // Truncate, set size to zero before starting.
            pStream->setSize( 0 );
        }
        else if( (eMode&Bu::Myriad::Append) != 0 )
        {
            iPos = pStream->getSize();
        }
    }
}

Bu::MyriadStream::~MyriadStream()
{
    close();
}

void Bu::MyriadStream::close()
{
    Bu::MutexLocker l( mAccess );
    if( eMode )
    {
        pStream->close();
        eMode = Bu::Myriad::None;
    }
}

Bu::size Bu::MyriadStream::read( void *pBuf, size iBytes )
{
    Bu::MutexLocker l( mAccess );
    int32_t iRead = pStream->read( iPos, pBuf, iBytes );
    iPos += iRead;
    return iRead;
}

Bu::size Bu::MyriadStream::write( const void *pBuf, size iBytes )
{
    Bu::MutexLocker l( mAccess );
    int32_t iWrite = pStream->write( iPos, pBuf, iBytes );
    iPos += iWrite;
    return iWrite;
}

Bu::size Bu::MyriadStream::tell()
{
    Bu::MutexLocker l( mAccess );
    return iPos;
}

void Bu::MyriadStream::seek( size offset )
{
    Bu::MutexLocker l( mAccess );
    iPos += offset;
}

void Bu::MyriadStream::setPos( size pos )
{
    Bu::MutexLocker l( mAccess );
    iPos = pos;
}

void Bu::MyriadStream::setPosEnd( size pos )
{
    Bu::MutexLocker l( mAccess );
    iPos = pStream->getSize()-pos;
}

bool Bu::MyriadStream::isEos()
{
    Bu::MutexLocker l( mAccess );
    return iPos == pStream->getSize();
}

bool Bu::MyriadStream::isOpen()
{
    return true;
}

void Bu::MyriadStream::flush()
{
    // Does this make sense?
}

bool Bu::MyriadStream::canRead()
{
    return true;
}

bool Bu::MyriadStream::canWrite()
{
    return true;
}

bool Bu::MyriadStream::isReadable()
{
    Bu::MutexLocker l( mAccess );
    return (eMode&Bu::Myriad::Read) != 0;
}

bool Bu::MyriadStream::isWritable()
{
    Bu::MutexLocker l( mAccess );
    return (eMode&Bu::Myriad::Write) != 0;
}

bool Bu::MyriadStream::isSeekable()
{
    return true;
}

bool Bu::MyriadStream::isBlocking()
{
    return true;
}

void Bu::MyriadStream::setBlocking( bool /*bBlocking*/ )
{
    // Dunno what this would even mean here.
}

void Bu::MyriadStream::setSize( size iSize )
{
    Bu::MutexLocker l( mAccess );
    pStream->setSize( iSize );
    if( iPos > iSize )
        iPos = iSize;
}

Bu::size Bu::MyriadStream::getSize() const
{
    Bu::MutexLocker l( mAccess );
    return pStream->getSize();
}

Bu::size Bu::MyriadStream::getBlockSize() const
{
    Bu::MutexLocker l( mAccess );
    return pStream->getBlockSize();
}

Bu::String Bu::MyriadStream::getLocation() const
{
    Bu::MutexLocker l( mAccess );
    return pStream->getLocation();
}