2008-05-09 17:24:28 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
2008-05-22 10:20:31 +00:00
|
|
|
Open Asset Import Library (ASSIMP)
|
2008-05-09 17:24:28 +00:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2010-04-10 15:30:22 +00:00
|
|
|
Copyright (c) 2006-2010, ASSIMP Development Team
|
2008-05-09 17:24:28 +00:00
|
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the following
|
|
|
|
conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the ASSIMP team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the ASSIMP Development Team.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
2009-02-13 22:03:57 +00:00
|
|
|
/** @file DefaultIOStream.cpp
|
|
|
|
* @brief Default File I/O implementation for #Importer
|
|
|
|
*/
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2008-10-13 16:45:48 +00:00
|
|
|
#include "AssimpPCH.h"
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
#include "DefaultIOStream.h"
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
2009-01-12 22:06:54 +00:00
|
|
|
// ----------------------------------------------------------------------------------
|
2008-05-05 12:36:31 +00:00
|
|
|
DefaultIOStream::~DefaultIOStream()
|
|
|
|
{
|
2009-06-21 19:44:48 +00:00
|
|
|
if (mFile) {
|
2009-01-12 22:06:54 +00:00
|
|
|
::fclose(mFile);
|
2009-06-21 19:44:48 +00:00
|
|
|
}
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
2009-01-12 22:06:54 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------
|
2008-05-05 12:36:31 +00:00
|
|
|
size_t DefaultIOStream::Read(void* pvBuffer,
|
2009-01-12 22:06:54 +00:00
|
|
|
size_t pSize,
|
|
|
|
size_t pCount)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2008-06-22 10:09:26 +00:00
|
|
|
ai_assert(NULL != pvBuffer && 0 != pSize && 0 != pCount);
|
2009-01-12 22:06:54 +00:00
|
|
|
return (mFile ? ::fread(pvBuffer, pSize, pCount, mFile) : 0);
|
|
|
|
}
|
2008-06-22 10:09:26 +00:00
|
|
|
|
2009-01-12 22:06:54 +00:00
|
|
|
// ----------------------------------------------------------------------------------
|
2008-05-05 12:36:31 +00:00
|
|
|
size_t DefaultIOStream::Write(const void* pvBuffer,
|
2009-01-12 22:06:54 +00:00
|
|
|
size_t pSize,
|
|
|
|
size_t pCount)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2008-06-22 10:09:26 +00:00
|
|
|
ai_assert(NULL != pvBuffer && 0 != pSize && 0 != pCount);
|
2009-01-12 22:06:54 +00:00
|
|
|
return (mFile ? ::fwrite(pvBuffer, pSize, pCount, mFile) : 0);
|
|
|
|
}
|
2008-06-22 10:09:26 +00:00
|
|
|
|
2009-01-12 22:06:54 +00:00
|
|
|
// ----------------------------------------------------------------------------------
|
2008-05-05 12:36:31 +00:00
|
|
|
aiReturn DefaultIOStream::Seek(size_t pOffset,
|
2009-01-12 22:06:54 +00:00
|
|
|
aiOrigin pOrigin)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2009-06-21 19:44:48 +00:00
|
|
|
if (!mFile) {
|
2009-02-13 22:03:57 +00:00
|
|
|
return AI_FAILURE;
|
2009-06-21 19:44:48 +00:00
|
|
|
}
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2009-01-12 22:06:54 +00:00
|
|
|
// Just to check whether our enum maps one to one with the CRT constants
|
2009-02-13 22:03:57 +00:00
|
|
|
BOOST_STATIC_ASSERT(aiOrigin_CUR == SEEK_CUR &&
|
|
|
|
aiOrigin_END == SEEK_END && aiOrigin_SET == SEEK_SET);
|
2009-01-12 22:06:54 +00:00
|
|
|
|
|
|
|
// do the seek
|
|
|
|
return (0 == ::fseek(mFile, (long)pOffset,(int)pOrigin) ? AI_SUCCESS : AI_FAILURE);
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
2009-01-12 22:06:54 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------
|
2008-05-05 12:36:31 +00:00
|
|
|
size_t DefaultIOStream::Tell() const
|
|
|
|
{
|
2009-06-21 19:44:48 +00:00
|
|
|
if (!mFile) {
|
|
|
|
return 0;
|
|
|
|
}
|
2009-01-12 22:06:54 +00:00
|
|
|
return ::ftell(mFile);
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
2009-01-12 22:06:54 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------
|
2008-05-05 12:36:31 +00:00
|
|
|
size_t DefaultIOStream::FileSize() const
|
|
|
|
{
|
2009-06-21 19:44:48 +00:00
|
|
|
if (! mFile || mFilename.empty()) {
|
2008-05-05 12:36:31 +00:00
|
|
|
return 0;
|
2009-06-21 19:44:48 +00:00
|
|
|
}
|
2009-02-13 22:03:57 +00:00
|
|
|
|
2011-04-22 21:29:18 +00:00
|
|
|
if (SIZE_MAX == cachedSize) {
|
2009-01-12 22:06:54 +00:00
|
|
|
|
2009-02-13 22:03:57 +00:00
|
|
|
// TODO: Is that really faster if we're already owning a handle to the file?
|
2008-05-05 12:36:31 +00:00
|
|
|
#if defined _WIN32 && !defined __GNUC__
|
2009-02-13 22:03:57 +00:00
|
|
|
struct __stat64 fileStat;
|
|
|
|
int err = _stat64( mFilename.c_str(), &fileStat );
|
|
|
|
if (0 != err)
|
|
|
|
return 0;
|
|
|
|
cachedSize = (size_t) (fileStat.st_size);
|
2008-05-05 12:36:31 +00:00
|
|
|
#else
|
2009-02-13 22:03:57 +00:00
|
|
|
struct stat fileStat;
|
|
|
|
int err = stat(mFilename.c_str(), &fileStat );
|
|
|
|
if (0 != err)
|
|
|
|
return 0;
|
|
|
|
cachedSize = (size_t) (fileStat.st_size);
|
2008-05-05 12:36:31 +00:00
|
|
|
#endif
|
2009-02-13 22:03:57 +00:00
|
|
|
}
|
|
|
|
return cachedSize;
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
2009-01-12 22:06:54 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------
|
|
|
|
void DefaultIOStream::Flush()
|
|
|
|
{
|
2009-06-21 19:44:48 +00:00
|
|
|
if (mFile) {
|
2009-01-12 22:06:54 +00:00
|
|
|
::fflush(mFile);
|
2009-06-21 19:44:48 +00:00
|
|
|
}
|
2009-01-12 22:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------
|