Coverage Report

Created: 2026-04-01 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/assimp/code/Common/DefaultIOSystem.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
/** @file Default implementation of IOSystem using the standard C file functions */
42
43
#include <assimp/StringComparison.h>
44
45
#include <assimp/DefaultIOStream.h>
46
#include <assimp/DefaultIOSystem.h>
47
#include <assimp/ai_assert.h>
48
#include <stdlib.h>
49
#include <assimp/DefaultLogger.hpp>
50
51
#ifdef __unix__
52
#    include <stdlib.h>
53
#    include <sys/param.h>
54
#endif
55
56
#ifdef _WIN32
57
#    include <windows.h>
58
#endif
59
60
using namespace Assimp;
61
62
#ifdef _WIN32
63
64
const std::wstring wdummy;
65
66
static std::wstring Utf8ToWide(const char *in) {
67
    if (nullptr == in) {
68
        return wdummy;
69
    }
70
    int size = MultiByteToWideChar(CP_UTF8, 0, in, -1, nullptr, 0);
71
    // size includes terminating null; std::wstring adds null automatically
72
    std::wstring out(static_cast<size_t>(size) - 1, L'\0');
73
    MultiByteToWideChar(CP_UTF8, 0, in, -1, &out[0], size);
74
75
    return out;
76
}
77
78
const std::string dummy;
79
80
static std::string WideToUtf8(const wchar_t *in) {
81
    if (nullptr == in) {
82
        return dummy;
83
    }
84
    int size = WideCharToMultiByte(CP_UTF8, 0, in, -1, nullptr, 0, nullptr, nullptr);
85
    // size includes terminating null; std::string adds null automatically
86
    std::string out(static_cast<size_t>(size) - 1, '\0');
87
    WideCharToMultiByte(CP_UTF8, 0, in, -1, &out[0], size, nullptr, nullptr);
88
89
    return out;
90
}
91
#endif
92
93
// ------------------------------------------------------------------------------------------------
94
// Tests for the existence of a file at the given path.
95
426k
bool DefaultIOSystem::Exists(const char *pFile) const {
96
426k
    if (pFile == nullptr) {
97
0
        return false;
98
0
    }
99
100
#ifdef _WIN32
101
    struct __stat64 filestat;
102
    if (_wstat64(Utf8ToWide(pFile).c_str(), &filestat) != 0) {
103
        return false;
104
    }
105
#else
106
426k
  struct stat statbuf;
107
426k
    if (stat(pFile, &statbuf) != 0) {
108
423k
        return false;
109
423k
    }
110
    // test for a regular file
111
3.25k
    if (!S_ISREG(statbuf.st_mode)) {
112
3.25k
        return false;
113
3.25k
    }
114
0
#endif
115
116
0
    return true;
117
3.25k
}
118
119
// ------------------------------------------------------------------------------------------------
120
// Open a new file with a given path.
121
774
IOStream *DefaultIOSystem::Open(const char *strFile, const char *strMode) {
122
774
    ai_assert(strFile != nullptr);
123
774
    ai_assert(strMode != nullptr);
124
774
    FILE *file;
125
126
#ifdef _WIN32
127
    std::wstring name = Utf8ToWide(strFile);
128
    if (name.empty()) {
129
        return nullptr;
130
    }
131
132
    file = ::_wfopen(name.c_str(), Utf8ToWide(strMode).c_str());
133
#else
134
774
    file = ::fopen(strFile, strMode);
135
774
#endif
136
137
774
    if (!file) {
138
774
        return nullptr;
139
774
    }
140
141
0
    return new DefaultIOStream(file, strFile);
142
774
}
143
144
// ------------------------------------------------------------------------------------------------
145
// Closes the given file and releases all resources associated with it.
146
0
void DefaultIOSystem::Close(IOStream *pFile) {
147
0
    delete pFile;
148
0
}
149
150
// ------------------------------------------------------------------------------------------------
151
// Returns the operation specific directory separator
152
14.1k
char DefaultIOSystem::getOsSeparator() const {
153
14.1k
#ifndef _WIN32
154
14.1k
    return '/';
155
#else
156
    return '\\';
157
#endif
158
14.1k
}
159
160
// ------------------------------------------------------------------------------------------------
161
// IOSystem default implementation (ComparePaths isn't a pure virtual function)
162
0
bool IOSystem::ComparePaths(const char *one, const char *second) const {
163
0
    return !ASSIMP_stricmp(one, second);
164
0
}
165
166
// ------------------------------------------------------------------------------------------------
167
// Convert a relative path into an absolute path
168
0
inline static std::string MakeAbsolutePath(const char *in) {
169
0
    ai_assert(in);
170
0
    std::string out;
171
#ifdef _WIN32
172
    wchar_t *ret = ::_wfullpath(nullptr, Utf8ToWide(in).c_str(), 0);
173
    if (ret) {
174
        out = WideToUtf8(ret);
175
        free(ret);
176
    }
177
#else
178
0
    char *ret = realpath(in, nullptr);
179
0
    if (ret) {
180
0
        out = ret;
181
0
        free(ret);
182
0
    }
183
0
#endif
184
0
    else {
185
        // preserve the input path, maybe someone else is able to fix
186
        // the path before it is accessed (e.g. our file system filter)
187
0
        ASSIMP_LOG_WARN("Invalid path: ", std::string(in));
188
0
        out = in;
189
0
    }
190
0
    return out;
191
0
}
192
193
// ------------------------------------------------------------------------------------------------
194
// DefaultIOSystem's more specialized implementation
195
0
bool DefaultIOSystem::ComparePaths(const char *one, const char *second) const {
196
    // chances are quite good both paths are formatted identically,
197
    // so we can hopefully return here already
198
0
    if (!ASSIMP_stricmp(one, second))
199
0
        return true;
200
201
0
    std::string temp1 = MakeAbsolutePath(one);
202
0
    std::string temp2 = MakeAbsolutePath(second);
203
204
0
    return !ASSIMP_stricmp(temp1, temp2);
205
0
}
206
207
// ------------------------------------------------------------------------------------------------
208
0
std::string DefaultIOSystem::fileName(const std::string &path) {
209
0
    std::string ret = path;
210
0
    std::size_t last = ret.find_last_of("\\/");
211
0
    if (last != std::string::npos) ret = ret.substr(last + 1);
212
0
    return ret;
213
0
}
214
215
// ------------------------------------------------------------------------------------------------
216
0
std::string DefaultIOSystem::completeBaseName(const std::string &path) {
217
0
    std::string ret = fileName(path);
218
0
    std::size_t pos = ret.find_last_of('.');
219
0
    if (pos != std::string::npos) ret = ret.substr(0, pos);
220
0
    return ret;
221
0
}
222
223
// ------------------------------------------------------------------------------------------------
224
0
std::string DefaultIOSystem::absolutePath(const std::string &path) {
225
0
    std::string ret = path;
226
0
    std::size_t last = ret.find_last_of("\\/");
227
0
    if (last != std::string::npos) ret = ret.substr(0, last);
228
0
    return ret;
229
0
}
230
231
// ------------------------------------------------------------------------------------------------