diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/irrlicht-1.8/source/Irrlicht/CLimitReadFile.cpp | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/CLimitReadFile.cpp b/libraries/irrlicht-1.8/source/Irrlicht/CLimitReadFile.cpp new file mode 100644 index 0000000..51afaad --- /dev/null +++ b/libraries/irrlicht-1.8/source/Irrlicht/CLimitReadFile.cpp | |||
@@ -0,0 +1,127 @@ | |||
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 "CLimitReadFile.h" | ||
6 | #include "irrString.h" | ||
7 | |||
8 | namespace irr | ||
9 | { | ||
10 | namespace io | ||
11 | { | ||
12 | |||
13 | |||
14 | CLimitReadFile::CLimitReadFile(IReadFile* alreadyOpenedFile, long pos, | ||
15 | long areaSize, const io::path& name) | ||
16 | : Filename(name), AreaStart(0), AreaEnd(0), Pos(0), | ||
17 | File(alreadyOpenedFile) | ||
18 | { | ||
19 | #ifdef _DEBUG | ||
20 | setDebugName("CLimitReadFile"); | ||
21 | #endif | ||
22 | |||
23 | if (File) | ||
24 | { | ||
25 | File->grab(); | ||
26 | AreaStart = pos; | ||
27 | AreaEnd = AreaStart + areaSize; | ||
28 | } | ||
29 | } | ||
30 | |||
31 | |||
32 | CLimitReadFile::~CLimitReadFile() | ||
33 | { | ||
34 | if (File) | ||
35 | File->drop(); | ||
36 | } | ||
37 | |||
38 | |||
39 | //! returns how much was read | ||
40 | s32 CLimitReadFile::read(void* buffer, u32 sizeToRead) | ||
41 | { | ||
42 | #if 1 | ||
43 | if (0 == File) | ||
44 | return 0; | ||
45 | |||
46 | s32 r = AreaStart + Pos; | ||
47 | s32 toRead = core::s32_min(AreaEnd, r + sizeToRead) - core::s32_max(AreaStart, r); | ||
48 | if (toRead < 0) | ||
49 | return 0; | ||
50 | File->seek(r); | ||
51 | r = File->read(buffer, toRead); | ||
52 | Pos += r; | ||
53 | return r; | ||
54 | #else | ||
55 | const long pos = File->getPos(); | ||
56 | |||
57 | if (pos >= AreaEnd) | ||
58 | return 0; | ||
59 | |||
60 | if (pos + (long)sizeToRead >= AreaEnd) | ||
61 | sizeToRead = AreaEnd - pos; | ||
62 | |||
63 | return File->read(buffer, sizeToRead); | ||
64 | #endif | ||
65 | } | ||
66 | |||
67 | |||
68 | //! changes position in file, returns true if successful | ||
69 | bool CLimitReadFile::seek(long finalPos, bool relativeMovement) | ||
70 | { | ||
71 | #if 1 | ||
72 | Pos = core::s32_clamp(finalPos + (relativeMovement ? Pos : 0 ), 0, AreaEnd - AreaStart); | ||
73 | return true; | ||
74 | #else | ||
75 | const long pos = File->getPos(); | ||
76 | |||
77 | if (relativeMovement) | ||
78 | { | ||
79 | if (pos + finalPos > AreaEnd) | ||
80 | finalPos = AreaEnd - pos; | ||
81 | } | ||
82 | else | ||
83 | { | ||
84 | finalPos += AreaStart; | ||
85 | if (finalPos > AreaEnd) | ||
86 | return false; | ||
87 | } | ||
88 | |||
89 | return File->seek(finalPos, relativeMovement); | ||
90 | #endif | ||
91 | } | ||
92 | |||
93 | |||
94 | //! returns size of file | ||
95 | long CLimitReadFile::getSize() const | ||
96 | { | ||
97 | return AreaEnd - AreaStart; | ||
98 | } | ||
99 | |||
100 | |||
101 | //! returns where in the file we are. | ||
102 | long CLimitReadFile::getPos() const | ||
103 | { | ||
104 | #if 1 | ||
105 | return Pos; | ||
106 | #else | ||
107 | return File->getPos() - AreaStart; | ||
108 | #endif | ||
109 | } | ||
110 | |||
111 | |||
112 | //! returns name of file | ||
113 | const io::path& CLimitReadFile::getFileName() const | ||
114 | { | ||
115 | return Filename; | ||
116 | } | ||
117 | |||
118 | |||
119 | IReadFile* createLimitReadFile(const io::path& fileName, IReadFile* alreadyOpenedFile, long pos, long areaSize) | ||
120 | { | ||
121 | return new CLimitReadFile(alreadyOpenedFile, pos, areaSize, fileName); | ||
122 | } | ||
123 | |||
124 | |||
125 | } // end namespace io | ||
126 | } // end namespace irr | ||
127 | |||