summaryrefslogtreecommitdiff
path: root/src/membuf.cpp
blob: 1a6bf9a93742fdc042b3f8bebcb38e42c32e3433 (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
/*
 * 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/membuf.h"

using namespace Bu;

Bu::MemBuf::MemBuf() :
	nPos( 0 )
{
}

Bu::MemBuf::MemBuf( const Bu::FString &str ) :
	sBuf( str ),
	nPos( 0 )
{
}

Bu::MemBuf::~MemBuf()
{
}

void Bu::MemBuf::close()
{
}

size Bu::MemBuf::read( void *pBuf, size nBytes )
{
	if( (size)sBuf.getSize()-(size)nPos < nBytes )
		nBytes = sBuf.getSize()-nPos;

	memcpy( pBuf, sBuf.getStr()+nPos, nBytes );
	nPos += nBytes;

	return nBytes;
}
	
size Bu::MemBuf::write( const void *pBuf, size nBytes )
{
	if( nPos == sBuf.getSize() )
	{
		// Easiest, just append the data.
		sBuf.append( (const char *)pBuf, nBytes );
		nPos += nBytes;
		return nBytes;
	}
	else
	{
		// Trickier, we must do this in two parts, overwrite, then append
		// Frist, overwrite.
		size iOver = sBuf.getSize() - nPos;
		if( iOver > nBytes )
			iOver = nBytes;
		memcpy( sBuf.getStr()+nPos, pBuf, iOver );
		// Then append
		if( iOver < nBytes )
		{
			sBuf.append( ((const char *)pBuf)+iOver, nBytes-iOver );
		}
		nPos += nBytes;
		return nBytes;
	}
}

size Bu::MemBuf::tell()
{
	return nPos;
}

void Bu::MemBuf::seek( size offset )
{
	nPos += offset;
	if( nPos < 0 ) nPos = 0;
	else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize();
}

void Bu::MemBuf::setPos( size pos )
{
	nPos = pos;
	if( nPos < 0 ) nPos = 0;
	else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize();
}

void Bu::MemBuf::setPosEnd( size pos )
{
	nPos = sBuf.getSize()-pos;
	if( nPos < 0 ) nPos = 0;
	else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize();
}

bool Bu::MemBuf::isEos()
{
	return (nPos == sBuf.getSize());
}

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

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

bool Bu::MemBuf::canRead()
{
	return !isEos();
}

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

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

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

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

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

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

void Bu::MemBuf::setSize( size iSize )
{
	if( iSize < 0 )
		iSize = 0;
	sBuf.setSize( iSize );
	if( nPos > iSize )
		nPos = iSize;
}

Bu::FString &Bu::MemBuf::getString()
{
	return sBuf;
}

void Bu::MemBuf::setString( const Bu::FString &sNewData )
{
	sBuf = sNewData;
	nPos = 0;
}