vrshoot

view libs/assimp/DefaultIOStream.cpp @ 2:334d17aed7de

visual studio project files
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 18:36:38 +0200
parents
children
line source
1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
6 Copyright (c) 2006-2012, assimp team
8 All rights reserved.
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the following
12 conditions are met:
14 * Redistributions of source code must retain the above
15 copyright notice, this list of conditions and the
16 following disclaimer.
18 * Redistributions in binary form must reproduce the above
19 copyright notice, this list of conditions and the
20 following disclaimer in the documentation and/or other
21 materials provided with the distribution.
23 * Neither the name of the assimp team, nor the names of its
24 contributors may be used to endorse or promote products
25 derived from this software without specific prior
26 written permission of the assimp team.
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 ---------------------------------------------------------------------------
40 */
41 /** @file DefaultIOStream.cpp
42 * @brief Default File I/O implementation for #Importer
43 */
45 #include "AssimpPCH.h"
47 #include "DefaultIOStream.h"
48 #include <sys/types.h>
49 #include <sys/stat.h>
51 using namespace Assimp;
53 // ----------------------------------------------------------------------------------
54 DefaultIOStream::~DefaultIOStream()
55 {
56 if (mFile) {
57 ::fclose(mFile);
58 }
59 }
61 // ----------------------------------------------------------------------------------
62 size_t DefaultIOStream::Read(void* pvBuffer,
63 size_t pSize,
64 size_t pCount)
65 {
66 ai_assert(NULL != pvBuffer && 0 != pSize && 0 != pCount);
67 return (mFile ? ::fread(pvBuffer, pSize, pCount, mFile) : 0);
68 }
70 // ----------------------------------------------------------------------------------
71 size_t DefaultIOStream::Write(const void* pvBuffer,
72 size_t pSize,
73 size_t pCount)
74 {
75 ai_assert(NULL != pvBuffer && 0 != pSize && 0 != pCount);
76 return (mFile ? ::fwrite(pvBuffer, pSize, pCount, mFile) : 0);
77 }
79 // ----------------------------------------------------------------------------------
80 aiReturn DefaultIOStream::Seek(size_t pOffset,
81 aiOrigin pOrigin)
82 {
83 if (!mFile) {
84 return AI_FAILURE;
85 }
87 // Just to check whether our enum maps one to one with the CRT constants
88 BOOST_STATIC_ASSERT(aiOrigin_CUR == SEEK_CUR &&
89 aiOrigin_END == SEEK_END && aiOrigin_SET == SEEK_SET);
91 // do the seek
92 return (0 == ::fseek(mFile, (long)pOffset,(int)pOrigin) ? AI_SUCCESS : AI_FAILURE);
93 }
95 // ----------------------------------------------------------------------------------
96 size_t DefaultIOStream::Tell() const
97 {
98 if (!mFile) {
99 return 0;
100 }
101 return ::ftell(mFile);
102 }
104 // ----------------------------------------------------------------------------------
105 size_t DefaultIOStream::FileSize() const
106 {
107 if (! mFile || mFilename.empty()) {
108 return 0;
109 }
111 if (SIZE_MAX == cachedSize) {
113 // TODO: Is that really faster if we're already owning a handle to the file?
114 #if defined _WIN32 && !defined __GNUC__
115 struct __stat64 fileStat;
116 int err = _stat64( mFilename.c_str(), &fileStat );
117 if (0 != err)
118 return 0;
119 cachedSize = (size_t) (fileStat.st_size);
120 #else
121 struct stat fileStat;
122 int err = stat(mFilename.c_str(), &fileStat );
123 if (0 != err)
124 return 0;
125 cachedSize = (size_t) (fileStat.st_size);
126 #endif
127 }
128 return cachedSize;
129 }
131 // ----------------------------------------------------------------------------------
132 void DefaultIOStream::Flush()
133 {
134 if (mFile) {
135 ::fflush(mFile);
136 }
137 }
139 // ----------------------------------------------------------------------------------