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
43/** @file aiFileIO -> IOSystem wrapper*/
44
45#include "CInterfaceIOWrapper.h"
46
47namespace Assimp {
48
49CIOStreamWrapper::~CIOStreamWrapper(void)
50{
51 /* Various places depend on this destructor to close the file */
52 if (mFile) {
53 mIO->mFileSystem->CloseProc(mIO->mFileSystem, mFile);
54 mFile = nullptr;
55 }
56}
57
58// ...................................................................
59size_t CIOStreamWrapper::Read(void* pvBuffer,
60 size_t pSize,
61 size_t pCount
62){
63 // need to typecast here as C has no void*
64 return mFile->ReadProc(mFile,(char*)pvBuffer,pSize,pCount);
65}
66
67// ...................................................................
68size_t CIOStreamWrapper::Write(const void* pvBuffer,
69 size_t pSize,
70 size_t pCount
71){
72 // need to typecast here as C has no void*
73 return mFile->WriteProc(mFile,(const char*)pvBuffer,pSize,pCount);
74}
75
76// ...................................................................
77aiReturn CIOStreamWrapper::Seek(size_t pOffset,
78 aiOrigin pOrigin
79){
80 return mFile->SeekProc(mFile,pOffset,pOrigin);
81}
82
83// ...................................................................
84size_t CIOStreamWrapper::Tell(void) const {
85 return mFile->TellProc(mFile);
86}
87
88// ...................................................................
89size_t CIOStreamWrapper::FileSize() const {
90 return mFile->FileSizeProc(mFile);
91}
92
93// ...................................................................
94void CIOStreamWrapper::Flush () {
95 return mFile->FlushProc(mFile);
96}
97
98// ------------------------------------------------------------------------------------------------
99// Custom IOStream implementation for the C-API
100bool CIOSystemWrapper::Exists( const char* pFile) const {
101 aiFile* p = mFileSystem->OpenProc(mFileSystem,pFile,"rb");
102 if (p){
103 mFileSystem->CloseProc(mFileSystem,p);
104 return true;
105 }
106 return false;
107}
108
109// ...................................................................
110char CIOSystemWrapper::getOsSeparator() const {
111#ifndef _WIN32
112 return '/';
113#else
114 return '\\';
115#endif
116}
117
118// ...................................................................
119IOStream* CIOSystemWrapper::Open(const char* pFile,const char* pMode) {
120 aiFile* p = mFileSystem->OpenProc(mFileSystem,pFile,pMode);
121 if (!p) {
122 return NULL;
123 }
124 return new CIOStreamWrapper(p, this);
125}
126
127// ...................................................................
128void CIOSystemWrapper::Close( IOStream* pFile) {
129 if (!pFile) {
130 return;
131 }
132 delete pFile;
133}
134
135}
136