forked from riicchhaarrd/d3dbsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_reader.h
91 lines (81 loc) · 1.34 KB
/
buffer_reader.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#pragma once
#include <string>
#include <string.h>
class BufferReader
{
unsigned char* m_buffer;
size_t m_readpos;
size_t m_readpos_saved;
size_t m_size;
public:
BufferReader(void* buffer, size_t size)
:
m_buffer((unsigned char*)buffer), m_size(size), m_readpos(0)
{
}
void save()
{
m_readpos_saved = m_readpos;
}
//careful, better would be to push it to a std::stack<>
void restore()
{
m_readpos = m_readpos_saved;
}
size_t tell() const
{
return m_readpos;
}
void advance(int n)
{
m_readpos += n;
}
int peek(int dist)
{
if (m_readpos + dist < 0 || m_readpos + dist >= m_size)
return -1;
return m_buffer[m_readpos + dist];
}
void set_cursor(int n)
{
m_readpos = n;
}
std::string read_string(int eof_char = 0)
{
std::string s;
while (1)
{
char c;
size_t pos = m_readpos;
if(!read(&c, 1, sizeof(c)))
break;
if (c == eof_char)
{
m_readpos = pos;
break;
}
s.push_back(c);
}
return s;
}
void seek(int ch)
{
while (1)
{
if (m_readpos >= m_size)
break;
int tmp = m_buffer[m_readpos++];
if (tmp == ch)
break;
}
}
bool read(void *out, size_t element_size/*unused*/, size_t n)
{
size_t total = element_size * n;
if (m_readpos + total > m_size)
return false;
memcpy(out, &m_buffer[m_readpos], total);
m_readpos += total;
return true;
}
};