1/*
2---------------------------------------------------------------------------
3Open Asset Import Library (assimp)
4---------------------------------------------------------------------------
5
6Copyright (c) 2006-2017, assimp team
7
8
9All rights reserved.
10
11Redistribution and use of this software in source and binary forms,
12with or without modification, are permitted provided that the following
13conditions are met:
14
15* Redistributions of source code must retain the above
16 copyright notice, this list of conditions and the
17 following disclaimer.
18
19* Redistributions in binary form must reproduce the above
20 copyright notice, this list of conditions and the
21 following disclaimer in the documentation and/or other
22 materials provided with the distribution.
23
24* Neither the name of the assimp team, nor the names of its
25 contributors may be used to endorse or promote products
26 derived from this software without specific prior
27 written permission of the assimp team.
28
29THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40---------------------------------------------------------------------------
41*/
42/** @file Default implementation of IOSystem using the standard C file functions */
43
44#include "StringComparison.h"
45
46#include <assimp/DefaultIOSystem.h>
47#include <assimp/DefaultIOStream.h>
48#include <assimp/DefaultLogger.hpp>
49#include <assimp/ai_assert.h>
50#include <stdlib.h>
51
52#ifdef __unix__
53#include <sys/param.h>
54#include <stdlib.h>
55#endif
56
57using namespace Assimp;
58
59// ------------------------------------------------------------------------------------------------
60// Constructor.
61DefaultIOSystem::DefaultIOSystem()
62{
63 // nothing to do here
64}
65
66// ------------------------------------------------------------------------------------------------
67// Destructor.
68DefaultIOSystem::~DefaultIOSystem()
69{
70 // nothing to do here
71}
72
73// ------------------------------------------------------------------------------------------------
74// Tests for the existence of a file at the given path.
75bool DefaultIOSystem::Exists( const char* pFile) const
76{
77 FILE* file = ::fopen( pFile, "rb");
78 if( !file)
79 return false;
80
81 ::fclose( file);
82 return true;
83}
84
85// ------------------------------------------------------------------------------------------------
86// Open a new file with a given path.
87IOStream* DefaultIOSystem::Open( const char* strFile, const char* strMode)
88{
89 ai_assert(NULL != strFile);
90 ai_assert(NULL != strMode);
91
92 FILE* file = ::fopen( strFile, strMode);
93 if( NULL == file)
94 return NULL;
95
96 return new DefaultIOStream(file, (std::string) strFile);
97}
98
99// ------------------------------------------------------------------------------------------------
100// Closes the given file and releases all resources associated with it.
101void DefaultIOSystem::Close( IOStream* pFile)
102{
103 delete pFile;
104}
105
106// ------------------------------------------------------------------------------------------------
107// Returns the operation specific directory separator
108char DefaultIOSystem::getOsSeparator() const
109{
110#ifndef _WIN32
111 return '/';
112#else
113 return '\\';
114#endif
115}
116
117// ------------------------------------------------------------------------------------------------
118// IOSystem default implementation (ComparePaths isn't a pure virtual function)
119bool IOSystem::ComparePaths (const char* one, const char* second) const
120{
121 return !ASSIMP_stricmp(one,second);
122}
123
124// maximum path length
125// XXX http://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html
126#ifdef PATH_MAX
127# define PATHLIMIT PATH_MAX
128#else
129# define PATHLIMIT 4096
130#endif
131
132// ------------------------------------------------------------------------------------------------
133// Convert a relative path into an absolute path
134inline void MakeAbsolutePath (const char* in, char* _out)
135{
136 ai_assert(in && _out);
137 char* ret;
138#if defined( _MSC_VER ) || defined( __MINGW32__ )
139 ret = ::_fullpath( _out, in, PATHLIMIT );
140#else
141 // use realpath
142 ret = realpath(in, _out);
143#endif
144 if(!ret) {
145 // preserve the input path, maybe someone else is able to fix
146 // the path before it is accessed (e.g. our file system filter)
147 DefaultLogger::get()->warn("Invalid path: "+std::string(in));
148 strcpy(_out,in);
149 }
150}
151
152// ------------------------------------------------------------------------------------------------
153// DefaultIOSystem's more specialized implementation
154bool DefaultIOSystem::ComparePaths (const char* one, const char* second) const
155{
156 // chances are quite good both paths are formatted identically,
157 // so we can hopefully return here already
158 if( !ASSIMP_stricmp(one,second) )
159 return true;
160
161 char temp1[PATHLIMIT];
162 char temp2[PATHLIMIT];
163
164 MakeAbsolutePath (one, temp1);
165 MakeAbsolutePath (second, temp2);
166
167 return !ASSIMP_stricmp(temp1,temp2);
168}
169
170// ------------------------------------------------------------------------------------------------
171std::string DefaultIOSystem::fileName( const std::string &path )
172{
173 std::string ret = path;
174 std::size_t last = ret.find_last_of("\\/");
175 if (last != std::string::npos) ret = ret.substr(last + 1);
176 return ret;
177}
178
179// ------------------------------------------------------------------------------------------------
180std::string DefaultIOSystem::completeBaseName( const std::string &path )
181{
182 std::string ret = fileName(path);
183 std::size_t pos = ret.find_last_of('.');
184 if(pos != ret.npos) ret = ret.substr(0, pos);
185 return ret;
186}
187
188// ------------------------------------------------------------------------------------------------
189std::string DefaultIOSystem::absolutePath( const std::string &path )
190{
191 std::string ret = path;
192 std::size_t last = ret.find_last_of("\\/");
193 if (last != std::string::npos) ret = ret.substr(0, last);
194 return ret;
195}
196
197// ------------------------------------------------------------------------------------------------
198
199#undef PATHLIMIT
200