-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteBuffer.cc
More file actions
79 lines (64 loc) · 1.09 KB
/
ByteBuffer.cc
File metadata and controls
79 lines (64 loc) · 1.09 KB
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
#include "ByteBuffer.h"
#define BYTE_BUFFER_DEBUG true
ByteBuffer::ByteBuffer() :
data_(),
size_(0),
apparent_size_(0)
{
}
ByteBuffer::~ByteBuffer()
{
}
int ByteBuffer::alloc(size_t size)
{
int ret = ERR_NONE;
if (size > size_)
{
ret = AllocateMemory(size);
}
else
{
apparent_size_ = size;
ClearMemory();
}
return ret;
}
int ByteBuffer::OpenFile(const char * path)
{
FILE* fp;
size_t filesz, filepos;
if ((fp = fopen(path, "rb")) == NULL)
{
return ERR_FAILOPEN;
}
fseek(fp, 0, SEEK_END);
filesz = ftell(fp);
rewind(fp);
if (alloc(filesz) != ERR_NONE)
{
fclose(fp);
return ERR_FAILMALLOC;
}
for (filepos = 0; filesz > kBlockSize; filesz -= kBlockSize, filepos += kBlockSize)
{
fread(data_.data() + filepos, 1, kBlockSize, fp);
}
if (filesz)
{
fread(data_.data() + filepos, 1, filesz, fp);
}
fclose(fp);
return ERR_NONE;
}
int ByteBuffer::AllocateMemory(size_t size)
{
size_ = (size_t)align(size, 0x1000);
apparent_size_ = size;
data_.resize(size_);
ClearMemory();
return ERR_NONE;
}
void ByteBuffer::ClearMemory()
{
memset(data_.data(), 0, size_);
}