vrshoot

view libs/assimp/MemoryIOWrapper.h @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line source
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
5 Copyright (c) 2006-2012, assimp team
6 All rights reserved.
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
12 * Redistributions of source code must retain the above
13 copyright notice, this list of conditions and the
14 following disclaimer.
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
21 * Neither the name of the assimp team, nor the names of its
22 contributors may be used to endorse or promote products
23 derived from this software without specific prior
24 written permission of the assimp team.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 ----------------------------------------------------------------------
39 */
41 /** @file MemoryIOWrapper.h
42 * Handy IOStream/IOSystem implemetation to read directly from a memory buffer */
43 #ifndef AI_MEMORYIOSTREAM_H_INC
44 #define AI_MEMORYIOSTREAM_H_INC
45 namespace Assimp {
46 #define AI_MEMORYIO_MAGIC_FILENAME "$$$___magic___$$$"
47 #define AI_MEMORYIO_MAGIC_FILENAME_LENGTH 17
49 // ----------------------------------------------------------------------------------
50 /** Implementation of IOStream to read directly from a memory buffer */
51 // ----------------------------------------------------------------------------------
52 class MemoryIOStream : public IOStream
53 {
54 //friend class MemoryIOSystem;
55 public:
56 MemoryIOStream (const uint8_t* buff, size_t len, bool own = false)
57 : buffer (buff)
58 , length(len)
59 , pos((size_t)0)
60 , own(own)
61 {
62 }
64 public:
66 ~MemoryIOStream () {
67 if(own) {
68 delete[] buffer;
69 }
70 }
72 // -------------------------------------------------------------------
73 // Read from stream
74 size_t Read(void* pvBuffer, size_t pSize, size_t pCount) {
75 const size_t cnt = std::min(pCount,(length-pos)/pSize),ofs = pSize*cnt;
77 memcpy(pvBuffer,buffer+pos,ofs);
78 pos += ofs;
80 return cnt;
81 }
83 // -------------------------------------------------------------------
84 // Write to stream
85 size_t Write(const void* /*pvBuffer*/, size_t /*pSize*/,size_t /*pCount*/) {
86 ai_assert(false); // won't be needed
87 return 0;
88 }
90 // -------------------------------------------------------------------
91 // Seek specific position
92 aiReturn Seek(size_t pOffset, aiOrigin pOrigin) {
93 if (aiOrigin_SET == pOrigin) {
94 if (pOffset >= length) {
95 return AI_FAILURE;
96 }
97 pos = pOffset;
98 }
99 else if (aiOrigin_END == pOrigin) {
100 if (pOffset >= length) {
101 return AI_FAILURE;
102 }
103 pos = length-pOffset;
104 }
105 else {
106 if (pOffset+pos >= length) {
107 return AI_FAILURE;
108 }
109 pos += pOffset;
110 }
111 return AI_SUCCESS;
112 }
114 // -------------------------------------------------------------------
115 // Get current seek position
116 size_t Tell() const {
117 return pos;
118 }
120 // -------------------------------------------------------------------
121 // Get size of file
122 size_t FileSize() const {
123 return length;
124 }
126 // -------------------------------------------------------------------
127 // Flush file contents
128 void Flush() {
129 ai_assert(false); // won't be needed
130 }
132 private:
133 const uint8_t* buffer;
134 size_t length,pos;
135 bool own;
136 };
138 // ---------------------------------------------------------------------------
139 /** Dummy IO system to read from a memory buffer */
140 class MemoryIOSystem : public IOSystem
141 {
142 public:
143 /** Constructor. */
144 MemoryIOSystem (const uint8_t* buff, size_t len)
145 : buffer (buff), length(len) {
146 }
148 /** Destructor. */
149 ~MemoryIOSystem() {
150 }
152 // -------------------------------------------------------------------
153 /** Tests for the existence of a file at the given path. */
154 bool Exists( const char* pFile) const {
155 return !strncmp(pFile,AI_MEMORYIO_MAGIC_FILENAME,AI_MEMORYIO_MAGIC_FILENAME_LENGTH);
156 }
158 // -------------------------------------------------------------------
159 /** Returns the directory separator. */
160 char getOsSeparator() const {
161 return '/'; // why not? it doesn't care
162 }
164 // -------------------------------------------------------------------
165 /** Open a new file with a given path. */
166 IOStream* Open( const char* pFile, const char* /*pMode*/ = "rb") {
167 if (strncmp(pFile,AI_MEMORYIO_MAGIC_FILENAME,AI_MEMORYIO_MAGIC_FILENAME_LENGTH)) {
168 return NULL;
169 }
170 return new MemoryIOStream(buffer,length);
171 }
173 // -------------------------------------------------------------------
174 /** Closes the given file and releases all resources associated with it. */
175 void Close( IOStream* /*pFile*/) {
176 }
178 // -------------------------------------------------------------------
179 /** Compare two paths */
180 bool ComparePaths (const char* /*one*/, const char* /*second*/) const {
181 return false;
182 }
184 private:
185 const uint8_t* buffer;
186 size_t length;
187 };
188 } // end namespace Assimp
190 #endif