vrshoot

view libs/assimp/DefaultIOSystem.cpp @ 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 ---------------------------------------------------------------------------
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 Default implementation of IOSystem using the standard C file functions */
43 #include "AssimpPCH.h"
45 #include <stdlib.h>
46 #include "DefaultIOSystem.h"
47 #include "DefaultIOStream.h"
49 #ifdef __unix__
50 #include <sys/param.h>
51 #include <stdlib.h>
52 #endif
54 using namespace Assimp;
56 // ------------------------------------------------------------------------------------------------
57 // Constructor.
58 DefaultIOSystem::DefaultIOSystem()
59 {
60 // nothing to do here
61 }
63 // ------------------------------------------------------------------------------------------------
64 // Destructor.
65 DefaultIOSystem::~DefaultIOSystem()
66 {
67 // nothing to do here
68 }
70 // ------------------------------------------------------------------------------------------------
71 // Tests for the existence of a file at the given path.
72 bool DefaultIOSystem::Exists( const char* pFile) const
73 {
74 FILE* file = ::fopen( pFile, "rb");
75 if( !file)
76 return false;
78 ::fclose( file);
79 return true;
80 }
82 // ------------------------------------------------------------------------------------------------
83 // Open a new file with a given path.
84 IOStream* DefaultIOSystem::Open( const char* strFile, const char* strMode)
85 {
86 ai_assert(NULL != strFile);
87 ai_assert(NULL != strMode);
89 FILE* file = ::fopen( strFile, strMode);
90 if( NULL == file)
91 return NULL;
93 return new DefaultIOStream(file, (std::string) strFile);
94 }
96 // ------------------------------------------------------------------------------------------------
97 // Closes the given file and releases all resources associated with it.
98 void DefaultIOSystem::Close( IOStream* pFile)
99 {
100 delete pFile;
101 }
103 // ------------------------------------------------------------------------------------------------
104 // Returns the operation specific directory separator
105 char DefaultIOSystem::getOsSeparator() const
106 {
107 #ifndef _WIN32
108 return '/';
109 #else
110 return '\\';
111 #endif
112 }
114 // ------------------------------------------------------------------------------------------------
115 // IOSystem default implementation (ComparePaths isn't a pure virtual function)
116 bool IOSystem::ComparePaths (const char* one, const char* second) const
117 {
118 return !ASSIMP_stricmp(one,second);
119 }
121 // maximum path length
122 // XXX http://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html
123 #ifdef PATH_MAX
124 # define PATHLIMIT PATH_MAX
125 #else
126 # define PATHLIMIT 4096
127 #endif
129 // ------------------------------------------------------------------------------------------------
130 // Convert a relative path into an absolute path
131 inline void MakeAbsolutePath (const char* in, char* _out)
132 {
133 ai_assert(in && _out);
134 char* ret;
135 #ifdef _WIN32
136 ret = ::_fullpath(_out, in,PATHLIMIT);
137 #else
138 // use realpath
139 ret = realpath(in, _out);
140 #endif
141 if(!ret) {
142 // preserve the input path, maybe someone else is able to fix
143 // the path before it is accessed (e.g. our file system filter)
144 DefaultLogger::get()->warn("Invalid path: "+std::string(in));
145 strcpy(_out,in);
146 }
147 }
149 // ------------------------------------------------------------------------------------------------
150 // DefaultIOSystem's more specialized implementation
151 bool DefaultIOSystem::ComparePaths (const char* one, const char* second) const
152 {
153 // chances are quite good both paths are formatted identically,
154 // so we can hopefully return here already
155 if( !ASSIMP_stricmp(one,second) )
156 return true;
158 char temp1[PATHLIMIT];
159 char temp2[PATHLIMIT];
161 MakeAbsolutePath (one, temp1);
162 MakeAbsolutePath (second, temp2);
164 return !ASSIMP_stricmp(temp1,temp2);
165 }
167 #undef PATHLIMIT