aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/irrlicht-1.8.1/source/Irrlicht/CMemoryFile.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/irrlicht-1.8.1/source/Irrlicht/CMemoryFile.cpp')
-rw-r--r--src/others/irrlicht-1.8.1/source/Irrlicht/CMemoryFile.cpp122
1 files changed, 122 insertions, 0 deletions
diff --git a/src/others/irrlicht-1.8.1/source/Irrlicht/CMemoryFile.cpp b/src/others/irrlicht-1.8.1/source/Irrlicht/CMemoryFile.cpp
new file mode 100644
index 0000000..d25c1f2
--- /dev/null
+++ b/src/others/irrlicht-1.8.1/source/Irrlicht/CMemoryFile.cpp
@@ -0,0 +1,122 @@
1// Copyright (C) 2002-2012 Nikolaus Gebhardt
2// This file is part of the "Irrlicht Engine".
3// For conditions of distribution and use, see copyright notice in irrlicht.h
4
5#include "CMemoryFile.h"
6#include "irrString.h"
7
8namespace irr
9{
10namespace io
11{
12
13
14CMemoryFile::CMemoryFile(void* memory, long len, const io::path& fileName, bool d)
15: Buffer(memory), Len(len), Pos(0), Filename(fileName), deleteMemoryWhenDropped(d)
16{
17 #ifdef _DEBUG
18 setDebugName("CMemoryFile");
19 #endif
20}
21
22
23CMemoryFile::~CMemoryFile()
24{
25 if (deleteMemoryWhenDropped)
26 delete [] (c8*)Buffer;
27}
28
29
30//! returns how much was read
31s32 CMemoryFile::read(void* buffer, u32 sizeToRead)
32{
33 s32 amount = static_cast<s32>(sizeToRead);
34 if (Pos + amount > Len)
35 amount -= Pos + amount - Len;
36
37 if (amount <= 0)
38 return 0;
39
40 c8* p = (c8*)Buffer;
41 memcpy(buffer, p + Pos, amount);
42
43 Pos += amount;
44
45 return amount;
46}
47
48//! returns how much was written
49s32 CMemoryFile::write(const void* buffer, u32 sizeToWrite)
50{
51 s32 amount = static_cast<s32>(sizeToWrite);
52 if (Pos + amount > Len)
53 amount -= Pos + amount - Len;
54
55 if (amount <= 0)
56 return 0;
57
58 c8* p = (c8*)Buffer;
59 memcpy(p + Pos, buffer, amount);
60
61 Pos += amount;
62
63 return amount;
64}
65
66
67
68//! changes position in file, returns true if successful
69//! if relativeMovement==true, the pos is changed relative to current pos,
70//! otherwise from begin of file
71bool CMemoryFile::seek(long finalPos, bool relativeMovement)
72{
73 if (relativeMovement)
74 {
75 if (Pos + finalPos > Len)
76 return false;
77
78 Pos += finalPos;
79 }
80 else
81 {
82 if (finalPos > Len)
83 return false;
84
85 Pos = finalPos;
86 }
87
88 return true;
89}
90
91
92//! returns size of file
93long CMemoryFile::getSize() const
94{
95 return Len;
96}
97
98
99//! returns where in the file we are.
100long CMemoryFile::getPos() const
101{
102 return Pos;
103}
104
105
106//! returns name of file
107const io::path& CMemoryFile::getFileName() const
108{
109 return Filename;
110}
111
112
113IReadFile* createMemoryReadFile(void* memory, long size, const io::path& fileName, bool deleteMemoryWhenDropped)
114{
115 CMemoryFile* file = new CMemoryFile(memory, size, fileName, deleteMemoryWhenDropped);
116 return file;
117}
118
119
120} // end namespace io
121} // end namespace irr
122