summaryrefslogtreecommitdiff
path: root/src/queuebuf.cpp
blob: 94041646541aef13e3d233b364645d370e614459 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * Copyright (C) 2007-2010 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/queuebuf.h"

#include "bu/sio.h"
using Bu::sio;

Bu::QueueBuf::QueueBuf( int iBlockSize /*=256*/ ) :
	iBlockSize( iBlockSize ),
	iReadOffset( 0 ),
	iWriteOffset( 0 ),
	iTotalSize( 0 )
{
}

Bu::QueueBuf::~QueueBuf()
{
	for( BlockList::iterator i = lBlocks.begin(); i; i++ )
		delete[] *i;
}

int Bu::QueueBuf::getSize()
{
	return iTotalSize;
}

void Bu::QueueBuf::close()
{
	for( BlockList::iterator i = lBlocks.begin(); i; i++ )
		delete[] *i;
	lBlocks.clear();
	iReadOffset = iWriteOffset = iTotalSize = 0;
}

size_t Bu::QueueBuf::read( void *pRawBuf, size_t nBytes )
{
	if( nBytes <= 0 )
		return 0;

	if( lBlocks.isEmpty() )
		return 0;

	size_t iLeft = nBytes;
	char *pBuf = (char *)pRawBuf;

	while( iLeft > 0 && iTotalSize > 0 )
	{
		if( iReadOffset == iBlockSize )
		{
			removeBlock();
			if( lBlocks.isEmpty() )
			{
				return nBytes-iLeft;
			}
			iReadOffset = 0;
		}
		char *pBlock = lBlocks.first();
		size_t iCopy = iBlockSize-iReadOffset;
		if( iLeft < iCopy )
			iCopy = iLeft;
		if( iTotalSize < iCopy )
			iCopy = iTotalSize;
		memcpy( pBuf, pBlock+iReadOffset, iCopy );
		iReadOffset += iCopy;
		iLeft -= iCopy;
		pBuf += iCopy;
		iTotalSize -= iCopy;
		sio << "Read " << iCopy << " bytes, new size: " << iTotalSize << sio.nl;
	}

	return nBytes - iLeft;
}

size_t QueueBuf::peek( void *pBuf, size_t nBytes )
{
	if( nBytes <= 0 )
		return 0;

	if( lBlocks.isEmpty() )
		return 0;

	size_t iLeft = nBytes;
	char *pBuf = (char *)pRawBuf;

	int iTmpReadOffset = iReadOffset;
	int iTmpRemSize = iTotalSize;
	while( iLeft > 0 && iTmpRemSize > 0 )
	{

		// Switching to use temp variables instead of iReadOffset and iTotalSize
		if( iReadOffset == iBlockSize )
		{
			if( lBlocks.isEmpty() )
			{
				return nBytes-iLeft;
			}
			iReadOffset = 0;
		}
		char *pBlock = lBlocks.first();
		size_t iCopy = iBlockSize-iReadOffset;
		if( iLeft < iCopy )
			iCopy = iLeft;
		if( iTotalSize < iCopy )
			iCopy = iTotalSize;
		memcpy( pBuf, pBlock+iReadOffset, iCopy );
		iReadOffset += iCopy;
		iLeft -= iCopy;
		pBuf += iCopy;
		iTotalSize -= iCopy;
		sio << "Read " << iCopy << " bytes, new size: " << iTotalSize << sio.nl;
	}

	return nBytes - iLeft;
}

size_t Bu::QueueBuf::write( const void *pRawBuf, size_t nBytes )
{
	if( nBytes <= 0 )
		return 0;

	if( lBlocks.isEmpty() )
	{
		addBlock();
		iWriteOffset = 0;
	}
	size_t iLeft = nBytes;
	const char *pBuf = (const char *)pRawBuf;

	while( iLeft > 0 )
	{
		if( iWriteOffset == iBlockSize )
		{
			addBlock();
			iWriteOffset = 0;
		}
		char *pBlock = lBlocks.last();
		size_t iCopy = iBlockSize-iWriteOffset;
		if( iLeft < iCopy )
			iCopy = iLeft;
		memcpy( pBlock+iWriteOffset, pBuf, iCopy );
		iWriteOffset += iCopy;
		iLeft -= iCopy;
		pBuf += iCopy;
		iTotalSize += iCopy;
		sio << "Wrote " << iCopy << " bytes, new size: " << iTotalSize << sio.nl;
	}

	return nBytes;
}

long Bu::QueueBuf::tell()
{
	return -1;
}

void Bu::QueueBuf::seek( long )
{
}

void Bu::QueueBuf::setPos( long )
{
}

void Bu::QueueBuf::setPosEnd( long )
{
}

bool Bu::QueueBuf::isEos()
{
	return iTotalSize == 0;
}

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

void Bu::QueueBuf::flush()
{
}

bool Bu::QueueBuf::canRead()
{
	return iTotalSize > 0;
}

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

bool Bu::QueueBuf::isReadable()
{
	return true;
}

bool Bu::QueueBuf::isWritable()
{
	return true;
}

bool Bu::QueueBuf::isSeekable()
{
	return false;
}

bool Bu::QueueBuf::isBlocking()
{
	return false;
}

void Bu::QueueBuf::setBlocking( bool )
{
}

void Bu::QueueBuf::addBlock()
{
	lBlocks.append( new char[iBlockSize] );
	sio << "Added new block." << sio.nl;
}

void Bu::QueueBuf::removeBlock()
{
	delete[] lBlocks.first();
	lBlocks.erase( lBlocks.begin() );
	sio << "Removed block." << sio.nl;
}