vrshoot

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/assimp/DefaultIOSystem.cpp	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,167 @@
     1.4 +/*
     1.5 +---------------------------------------------------------------------------
     1.6 +Open Asset Import Library (assimp)
     1.7 +---------------------------------------------------------------------------
     1.8 +
     1.9 +Copyright (c) 2006-2012, assimp team
    1.10 +
    1.11 +All rights reserved.
    1.12 +
    1.13 +Redistribution and use of this software in source and binary forms, 
    1.14 +with or without modification, are permitted provided that the following 
    1.15 +conditions are met:
    1.16 +
    1.17 +* Redistributions of source code must retain the above
    1.18 +  copyright notice, this list of conditions and the
    1.19 +  following disclaimer.
    1.20 +
    1.21 +* Redistributions in binary form must reproduce the above
    1.22 +  copyright notice, this list of conditions and the
    1.23 +  following disclaimer in the documentation and/or other
    1.24 +  materials provided with the distribution.
    1.25 +
    1.26 +* Neither the name of the assimp team, nor the names of its
    1.27 +  contributors may be used to endorse or promote products
    1.28 +  derived from this software without specific prior
    1.29 +  written permission of the assimp team.
    1.30 +
    1.31 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
    1.32 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
    1.33 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.34 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
    1.35 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.36 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
    1.37 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.38 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
    1.39 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    1.40 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.41 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.42 +---------------------------------------------------------------------------
    1.43 +*/
    1.44 +/** @file Default implementation of IOSystem using the standard C file functions */
    1.45 +
    1.46 +#include "AssimpPCH.h"
    1.47 +
    1.48 +#include <stdlib.h>
    1.49 +#include "DefaultIOSystem.h"
    1.50 +#include "DefaultIOStream.h"
    1.51 +
    1.52 +#ifdef __unix__
    1.53 +#include <sys/param.h>
    1.54 +#include <stdlib.h>
    1.55 +#endif
    1.56 +
    1.57 +using namespace Assimp;
    1.58 +
    1.59 +// ------------------------------------------------------------------------------------------------
    1.60 +// Constructor. 
    1.61 +DefaultIOSystem::DefaultIOSystem()
    1.62 +{
    1.63 +	// nothing to do here
    1.64 +}
    1.65 +
    1.66 +// ------------------------------------------------------------------------------------------------
    1.67 +// Destructor. 
    1.68 +DefaultIOSystem::~DefaultIOSystem()
    1.69 +{
    1.70 +	// nothing to do here
    1.71 +}
    1.72 +
    1.73 +// ------------------------------------------------------------------------------------------------
    1.74 +// Tests for the existence of a file at the given path.
    1.75 +bool DefaultIOSystem::Exists( const char* pFile) const
    1.76 +{
    1.77 +	FILE* file = ::fopen( pFile, "rb");
    1.78 +	if( !file)
    1.79 +		return false;
    1.80 +
    1.81 +	::fclose( file);
    1.82 +	return true;
    1.83 +}
    1.84 +
    1.85 +// ------------------------------------------------------------------------------------------------
    1.86 +// Open a new file with a given path.
    1.87 +IOStream* DefaultIOSystem::Open( const char* strFile, const char* strMode)
    1.88 +{
    1.89 +	ai_assert(NULL != strFile);
    1.90 +	ai_assert(NULL != strMode);
    1.91 +
    1.92 +	FILE* file = ::fopen( strFile, strMode);
    1.93 +	if( NULL == file) 
    1.94 +		return NULL;
    1.95 +
    1.96 +	return new DefaultIOStream(file, (std::string) strFile);
    1.97 +}
    1.98 +
    1.99 +// ------------------------------------------------------------------------------------------------
   1.100 +// Closes the given file and releases all resources associated with it.
   1.101 +void DefaultIOSystem::Close( IOStream* pFile)
   1.102 +{
   1.103 +	delete pFile;
   1.104 +}
   1.105 +
   1.106 +// ------------------------------------------------------------------------------------------------
   1.107 +// Returns the operation specific directory separator
   1.108 +char DefaultIOSystem::getOsSeparator() const
   1.109 +{
   1.110 +#ifndef _WIN32
   1.111 +	return '/';
   1.112 +#else
   1.113 +	return '\\';
   1.114 +#endif
   1.115 +}
   1.116 +
   1.117 +// ------------------------------------------------------------------------------------------------
   1.118 +// IOSystem default implementation (ComparePaths isn't a pure virtual function)
   1.119 +bool IOSystem::ComparePaths (const char* one, const char* second) const
   1.120 +{
   1.121 +	return !ASSIMP_stricmp(one,second);
   1.122 +}
   1.123 +
   1.124 +// maximum path length
   1.125 +// XXX http://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html 
   1.126 +#ifdef PATH_MAX
   1.127 +#	define PATHLIMIT PATH_MAX
   1.128 +#else
   1.129 +#	define PATHLIMIT 4096
   1.130 +#endif
   1.131 +
   1.132 +// ------------------------------------------------------------------------------------------------
   1.133 +// Convert a relative path into an absolute path
   1.134 +inline void MakeAbsolutePath (const char* in, char* _out)
   1.135 +{
   1.136 +	ai_assert(in && _out);
   1.137 +	char* ret;
   1.138 +#ifdef _WIN32
   1.139 +	ret = ::_fullpath(_out, in,PATHLIMIT);
   1.140 +#else
   1.141 +    	// use realpath
   1.142 +    	ret = realpath(in, _out);
   1.143 +#endif  
   1.144 +	if(!ret) {
   1.145 +		// preserve the input path, maybe someone else is able to fix
   1.146 +		// the path before it is accessed (e.g. our file system filter)
   1.147 +		DefaultLogger::get()->warn("Invalid path: "+std::string(in));
   1.148 +		strcpy(_out,in);
   1.149 +	}  
   1.150 +}
   1.151 +
   1.152 +// ------------------------------------------------------------------------------------------------
   1.153 +// DefaultIOSystem's more specialized implementation
   1.154 +bool DefaultIOSystem::ComparePaths (const char* one, const char* second) const
   1.155 +{
   1.156 +	// chances are quite good both paths are formatted identically,
   1.157 +	// so we can hopefully return here already
   1.158 +	if( !ASSIMP_stricmp(one,second) )
   1.159 +		return true;
   1.160 +
   1.161 +	char temp1[PATHLIMIT];
   1.162 +	char temp2[PATHLIMIT];
   1.163 +	
   1.164 +	MakeAbsolutePath (one, temp1);
   1.165 +	MakeAbsolutePath (second, temp2);
   1.166 +
   1.167 +	return !ASSIMP_stricmp(temp1,temp2);
   1.168 +}
   1.169 +
   1.170 +#undef PATHLIMIT