-
Notifications
You must be signed in to change notification settings - Fork 1
/
arrayreader.hpp
67 lines (55 loc) · 1.39 KB
/
arrayreader.hpp
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
#ifndef ARRAY_READER_HPP_GUARD
#define ARRAY_READER_HPP_GUARD
//===----------------------------------------------------------------------===//
//
// The Descent map loader
//
// NAME : ArrayReader
// PURPOSE : Providing an interface for reading data out of an array.
// COPYRIGHT : (c) 2011 Sean Donnellan. All Rights Reserved.
// AUTHORS : Sean Donnellan ([email protected])
// DESCRIPTION : Provides a helper for extracting data out of an array.
//
//===----------------------------------------------------------------------===//
#include <stdint.h>
#include <stddef.h>
class ArrayReader
{
public:
ArrayReader(const uint8_t* const array, size_t size);
size_t Index() const
{
return myIndex;
};
void Seek(size_t index);
uint8_t ReadByte();
uint16_t ReadUInt16();
int16_t ReadInt16();
private:
size_t myIndex;
const size_t mySize;
const uint8_t* const myArray;
};
ArrayReader::ArrayReader(const uint8_t* const array, size_t size)
: myIndex(0), mySize(size), myArray(array)
{
}
void ArrayReader::Seek(size_t index)
{
myIndex = index;
}
uint8_t ArrayReader::ReadByte()
{
return myArray[myIndex++];
}
uint16_t ArrayReader::ReadUInt16()
{
myIndex += 2;
return (myArray[myIndex - 1] << 8) + myArray[myIndex - 2];
}
int16_t ArrayReader::ReadInt16()
{
myIndex += 2;
return (myArray[myIndex - 1] << 8) + myArray[myIndex - 2];
}
#endif