-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbyte_stream_fixed.h
executable file
·68 lines (56 loc) · 1.4 KB
/
byte_stream_fixed.h
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
// Copyright Mark Frohnmayer and GarageGames. See /license/info.txt in this distribution for licensing terms.
class byte_stream_fixed
{
public:
uint32 _position;
uint32 _buffer_size;
byte *_buffer;
byte_stream_fixed(byte *buffer = 0, uint32 buffer_size = 0)
{
set_buffer(buffer, buffer_size);
}
void set_position(uint32 byte_pos)
{
_position = byte_pos;
if(_position > _buffer_size)
_position = _buffer_size;
}
const uint32 get_position() const
{
return _position;
}
const uint32 get_buffer_size() const
{
return _buffer_size;
}
byte *get_buffer()
{
return _buffer;
}
const byte *get_buffer() const
{
return _buffer;
}
void set_buffer(byte *buffer, uint32 buffer_size)
{
_buffer = buffer;
_buffer_size = buffer_size;
_position = 0;
}
uint32 read_bytes(byte *dest_ptr, uint32 read_byte_count)
{
if(read_byte_count + _position > _buffer_size)
read_byte_count = _buffer_size - _position;
memcpy(dest_ptr, _buffer + _position, read_byte_count);
_position += read_byte_count;
return read_byte_count;
}
uint32 write_bytes(const byte *src_ptr, uint32 write_byte_count)
{
if(write_byte_count + _position > _buffer_size)
write_byte_count = _buffer_size - _position;
memcpy(_buffer + _position, src_ptr, write_byte_count);
_position += write_byte_count;
return write_byte_count;
}
};