1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 
5 Copyright (c) 2006-2016, assimp team
6 All rights reserved.
7 
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:
11 
12 * Redistributions of source code must retain the above
13   copyright notice, this list of conditions and the
14   following disclaimer.
15 
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.
20 
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.
25 
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.
37 
38 ----------------------------------------------------------------------
39 */
40 
41 /** @file Default file I/O using fXXX()-family of functions */
42 #ifndef AI_DEFAULTIOSTREAM_H_INC
43 #define AI_DEFAULTIOSTREAM_H_INC
44 
45 #include <stdio.h>
46 #include <assimp/IOStream.hpp>
47 #include <assimp/importerdesc.h>
48 #include "Defines.h"
49 
50 namespace Assimp    {
51 
52 // ----------------------------------------------------------------------------------
53 //! @class  DefaultIOStream
54 //! @brief  Default IO implementation, use standard IO operations
55 //! @note   An instance of this class can exist without a valid file handle
56 //!         attached to it. All calls fail, but the instance can nevertheless be
57 //!         used with no restrictions.
58 class DefaultIOStream : public IOStream
59 {
60     friend class DefaultIOSystem;
61 #if __ANDROID__
62 #if __ANDROID_API__ > 9
63 #if defined(AI_CONFIG_ANDROID_JNI_ASSIMP_MANAGER_SUPPORT)
64     friend class AndroidJNIIOSystem;
65 #endif // defined(AI_CONFIG_ANDROID_JNI_ASSIMP_MANAGER_SUPPORT)
66 #endif // __ANDROID_API__ > 9
67 #endif // __ANDROID__
68 
69 protected:
70     DefaultIOStream();
71     DefaultIOStream(FILE* pFile, const std::string &strFilename);
72 
73 public:
74     /** Destructor public to allow simple deletion to close the file. */
75     ~DefaultIOStream ();
76 
77     // -------------------------------------------------------------------
78     /// Read from stream
79     size_t Read(void* pvBuffer,
80         size_t pSize,
81         size_t pCount);
82 
83 
84     // -------------------------------------------------------------------
85     /// Write to stream
86     size_t Write(const void* pvBuffer,
87         size_t pSize,
88         size_t pCount);
89 
90     // -------------------------------------------------------------------
91     /// Seek specific position
92     aiReturn Seek(size_t pOffset,
93         aiOrigin pOrigin);
94 
95     // -------------------------------------------------------------------
96     /// Get current seek position
97     size_t Tell() const;
98 
99     // -------------------------------------------------------------------
100     /// Get size of file
101     size_t FileSize() const;
102 
103     // -------------------------------------------------------------------
104     /// Flush file contents
105     void Flush();
106 
107 private:
108     //  File datastructure, using clib
109     FILE* mFile;
110     //  Filename
111     std::string mFilename;
112 
113     // Cached file size
114     mutable size_t cachedSize;
115 };
116 
117 
118 // ----------------------------------------------------------------------------------
DefaultIOStream()119 inline DefaultIOStream::DefaultIOStream () :
120     mFile       (NULL),
121     mFilename   (""),
122     cachedSize  (SIZE_MAX)
123 {
124     // empty
125 }
126 
127 
128 // ----------------------------------------------------------------------------------
DefaultIOStream(FILE * pFile,const std::string & strFilename)129 inline DefaultIOStream::DefaultIOStream (FILE* pFile,
130         const std::string &strFilename) :
131     mFile(pFile),
132     mFilename(strFilename),
133     cachedSize  (SIZE_MAX)
134 {
135     // empty
136 }
137 // ----------------------------------------------------------------------------------
138 
139 } // ns assimp
140 
141 #endif //!!AI_DEFAULTIOSTREAM_H_INC
142 
143