blob: 0a69587ed8f921af0ab621e9e0d6a8dfa4946c3b (
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
|
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#include "CMemoryReadWriteFile.h"
using namespace irr;
using namespace io;
CMemoryReadWriteFile::CMemoryReadWriteFile(const c8* filename)
: Data(), FileName(filename), Pos(0)
{
}
s32 CMemoryReadWriteFile::write(const void* buffer, u32 sizeToWrite)
{
// no point in writing 0 bytes
if (sizeToWrite < 1)
return 0;
// expand size
if (Pos + sizeToWrite > Data.size())
Data.set_used(Pos+sizeToWrite);
// copy data
memcpy( (void*) &Data[Pos], buffer, (size_t) sizeToWrite);
Pos += sizeToWrite;
return sizeToWrite;
}
bool CMemoryReadWriteFile::seek(long finalPos, bool relativeMovement)
{
if (relativeMovement)
{
if (finalPos + Pos < 0)
return 0;
else
Pos += finalPos;
}
else
{
Pos = finalPos;
}
if (Pos > (s32)Data.size())
Data.set_used(Pos+1);
return true;
}
const io::path& CMemoryReadWriteFile::getFileName() const
{
return FileName;
}
long CMemoryReadWriteFile::getPos() const
{
return Pos;
}
core::array<c8>& CMemoryReadWriteFile::getData()
{
return Data;
}
long CMemoryReadWriteFile::getSize() const
{
return Data.size();
}
s32 CMemoryReadWriteFile::read(void* buffer, u32 sizeToRead)
{
// cant read past the end
if (Pos + sizeToRead >= Data.size())
sizeToRead = Data.size() - Pos;
// cant read 0 bytes
if (!sizeToRead)
return 0;
// copy data
memcpy( buffer, (void*) &Data[Pos], (size_t) sizeToRead);
Pos += sizeToRead;
return sizeToRead;
}
|