/src/assimp/code/CApi/CInterfaceIOWrapper.cpp
Line | Count | Source |
1 | | /* |
2 | | --------------------------------------------------------------------------- |
3 | | Open Asset Import Library (assimp) |
4 | | --------------------------------------------------------------------------- |
5 | | |
6 | | Copyright (c) 2006-2026, assimp team |
7 | | |
8 | | All rights reserved. |
9 | | |
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: |
13 | | |
14 | | * Redistributions of source code must retain the above |
15 | | copyright notice, this list of conditions and the |
16 | | following disclaimer. |
17 | | |
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. |
22 | | |
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. |
27 | | |
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 | | |
42 | | /** @file aiFileIO -> IOSystem wrapper*/ |
43 | | |
44 | | #include "CInterfaceIOWrapper.h" |
45 | | |
46 | | namespace Assimp { |
47 | | |
48 | | // ------------------------------------------------------------------------------------------------ |
49 | 0 | CIOStreamWrapper::~CIOStreamWrapper() { |
50 | | // Various places depend on this destructor to close the file |
51 | 0 | if (mFile != nullptr) { |
52 | 0 | mIO->mFileSystem->CloseProc(mIO->mFileSystem, mFile); |
53 | 0 | } |
54 | 0 | } |
55 | | |
56 | | // ------------------------------------------------------------------------------------------------ |
57 | | size_t CIOStreamWrapper::Read(void *pvBuffer, |
58 | | size_t pSize, |
59 | 0 | size_t pCount) { |
60 | | // need to typecast here as C has no void* |
61 | 0 | return mFile->ReadProc(mFile, (char *)pvBuffer, pSize, pCount); |
62 | 0 | } |
63 | | |
64 | | // ------------------------------------------------------------------------------------------------ |
65 | | size_t CIOStreamWrapper::Write(const void *pvBuffer, |
66 | | size_t pSize, |
67 | 0 | size_t pCount) { |
68 | | // need to typecast here as C has no void* |
69 | 0 | return mFile->WriteProc(mFile, (const char *)pvBuffer, pSize, pCount); |
70 | 0 | } |
71 | | |
72 | | // ------------------------------------------------------------------------------------------------ |
73 | | aiReturn CIOStreamWrapper::Seek(size_t pOffset, |
74 | 0 | aiOrigin pOrigin) { |
75 | 0 | return mFile->SeekProc(mFile, pOffset, pOrigin); |
76 | 0 | } |
77 | | |
78 | | // ------------------------------------------------------------------------------------------------ |
79 | 0 | size_t CIOStreamWrapper::Tell() const { |
80 | 0 | return mFile->TellProc(mFile); |
81 | 0 | } |
82 | | |
83 | | // ------------------------------------------------------------------------------------------------ |
84 | 0 | size_t CIOStreamWrapper::FileSize() const { |
85 | 0 | return mFile->FileSizeProc(mFile); |
86 | 0 | } |
87 | | |
88 | | // ------------------------------------------------------------------------------------------------ |
89 | 0 | void CIOStreamWrapper::Flush() { |
90 | 0 | return mFile->FlushProc(mFile); |
91 | 0 | } |
92 | | |
93 | | // ------------------------------------------------------------------------------------------------ |
94 | | // Custom IOStream implementation for the C-API |
95 | 0 | bool CIOSystemWrapper::Exists(const char *pFile) const { |
96 | 0 | aiFile *p = mFileSystem->OpenProc(mFileSystem, pFile, "rb"); |
97 | 0 | if (p) { |
98 | 0 | mFileSystem->CloseProc(mFileSystem, p); |
99 | 0 | return true; |
100 | 0 | } |
101 | 0 | return false; |
102 | 0 | } |
103 | | |
104 | | // ................................................................... |
105 | 0 | char CIOSystemWrapper::getOsSeparator() const { |
106 | 0 | #ifndef _WIN32 |
107 | 0 | return '/'; |
108 | | #else |
109 | | return '\\'; |
110 | | #endif |
111 | 0 | } |
112 | | |
113 | | // ................................................................... |
114 | 0 | IOStream *CIOSystemWrapper::Open(const char *pFile, const char *pMode) { |
115 | 0 | aiFile *p = mFileSystem->OpenProc(mFileSystem, pFile, pMode); |
116 | 0 | if (!p) { |
117 | 0 | return nullptr; |
118 | 0 | } |
119 | 0 | return new CIOStreamWrapper(p, this); |
120 | 0 | } |
121 | | |
122 | | // ................................................................... |
123 | 0 | void CIOSystemWrapper::Close(IOStream *pFile) { |
124 | 0 | if (!pFile) { |
125 | 0 | return; |
126 | 0 | } |
127 | 0 | delete pFile; |
128 | 0 | } |
129 | | |
130 | | } // namespace Assimp |