Coverage Report

Created: 2025-11-11 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/assimp/code/AssetLib/Obj/ObjTools.h
Line
Count
Source
1
/*
2
Open Asset Import Library (assimp)
3
----------------------------------------------------------------------
4
5
Copyright (c) 2006-2025, assimp team
6
7
All rights reserved.
8
9
Redistribution and use of this software in source and binary forms,
10
with or without modification, are permitted provided that the
11
following conditions are met:
12
13
* Redistributions of source code must retain the above
14
  copyright notice, this list of conditions and the
15
  following disclaimer.
16
17
* Redistributions in binary form must reproduce the above
18
  copyright notice, this list of conditions and the
19
  following disclaimer in the documentation and/or other
20
  materials provided with the distribution.
21
22
* Neither the name of the assimp team, nor the names of its
23
  contributors may be used to endorse or promote products
24
  derived from this software without specific prior
25
  written permission of the assimp team.
26
27
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39
----------------------------------------------------------------------
40
*/
41
42
/** @file   ObjTools.h
43
 *  @brief  Some helpful templates for text parsing
44
 */
45
#ifndef OBJ_TOOLS_H_INC
46
#define OBJ_TOOLS_H_INC
47
48
#include <assimp/ParsingUtils.h>
49
#include <assimp/fast_atof.h>
50
#include <vector>
51
52
namespace Assimp {
53
54
/**
55
 *  @brief  Returns true, if the last entry of the buffer is reached.
56
 *  @param[in] it   Iterator of current position.
57
 *  @param[in] end  Iterator with end of buffer.
58
 *  @return true, if the end of the buffer is reached.
59
 */
60
template <class char_t>
61
350M
inline bool isEndOfBuffer(char_t it, char_t end) {
62
350M
    if (it == end) {
63
0
        return true;
64
0
    }
65
350M
    --end;
66
67
350M
    return (it == end);
68
350M
}
69
70
/**
71
 *  @brief  Returns next word separated by a space
72
 *  @param[in] pBuffer  Pointer to data buffer
73
 *  @param[in] pEnd     Pointer to end of buffer
74
 *  @return Pointer to next space
75
 */
76
template <class Char_T>
77
10.8M
inline Char_T getNextWord(Char_T pBuffer, Char_T pEnd) {
78
26.9M
    while (!isEndOfBuffer(pBuffer, pEnd)) {
79
26.9M
        if (!IsSpaceOrNewLine(*pBuffer) || IsLineEnd(*pBuffer)) {
80
10.8M
            break;
81
10.8M
        }
82
16.1M
        ++pBuffer;
83
16.1M
    }
84
85
10.8M
    return pBuffer;
86
10.8M
}
87
88
/**
89
 *  @brief  Returns pointer a next token
90
 *  @param[in] pBuffer  Pointer to data buffer
91
 *  @param[in] pEnd     Pointer to end of buffer
92
 *  @return Pointer to next token
93
 */
94
template <class Char_T>
95
9.68M
inline Char_T getNextToken(Char_T pBuffer, Char_T pEnd) {
96
97.1M
    while (!isEndOfBuffer(pBuffer, pEnd)) {
97
97.1M
        if (IsSpaceOrNewLine(*pBuffer)) {
98
9.68M
            break;
99
9.68M
        }
100
87.4M
        ++pBuffer;
101
87.4M
    }
102
9.68M
    return getNextWord(pBuffer, pEnd);
103
9.68M
}
104
105
/**
106
 *  @brief  Skips a line
107
 *  @param[in]  it      Iterator set to current position
108
 *  @param[in]  end     Iterator set to end of scratch buffer for readout
109
 *  @param[out] uiLine  Current line number in format
110
 *  @return Current-iterator with new position
111
 */
112
template <class char_t>
113
38.2M
inline char_t skipLine(char_t it, char_t end, unsigned int &uiLine) {
114
38.2M
    if (it >= end) {
115
519
        return it;
116
519
    }
117
118
218M
    while (!isEndOfBuffer(it, end) && !IsLineEnd(*it)) {
119
179M
        ++it;
120
179M
    }
121
122
38.2M
    if (it != end) {
123
38.2M
        ++it;
124
38.2M
        ++uiLine;
125
38.2M
    }
126
    // fix .. from time to time there are spaces at the beginning of a material line
127
39.9M
    while (it != end && (*it == '\t' || *it == ' ')) {
128
1.70M
        ++it;
129
1.70M
    }
130
131
38.2M
    return it;
132
38.2M
}
133
134
/**
135
 *  @brief  Get a name from the current line. Preserve space in the middle,
136
 *          but trim it at the end.
137
 *  @param[in]  it      set to current position
138
 *  @param[in]  end     set to end of scratch buffer for readout
139
 *  @param[out] name    Separated name
140
 *  @return Current-iterator with new position
141
 */
142
template <class char_t>
143
958k
inline char_t getName(char_t it, char_t end, std::string &name) {
144
958k
    name = "";
145
958k
    if (isEndOfBuffer(it, end)) {
146
519
        return end;
147
519
    }
148
149
957k
    char *pStart = &(*it);
150
2.30M
    while (!isEndOfBuffer(it, end) && !IsLineEnd(*it)) {
151
1.35M
        ++it;
152
1.35M
    }
153
154
957k
    while (IsSpace(*it)) {
155
0
        --it;
156
0
    }
157
    // Get name
158
    // if there is no name, and the previous char is a separator, come back to start
159
957k
    while (&(*it) < pStart) {
160
0
        ++it;
161
0
    }
162
957k
    std::string strName(pStart, &(*it));
163
957k
    if (!strName.empty()) {
164
38.7k
        name = strName;
165
38.7k
    }
166
167
168
957k
    return it;
169
958k
}
170
171
/**
172
 *  @brief  Get a name from the current line. Do not preserve space
173
 *    in the middle, but trim it at the end.
174
 *  @param  it      set to current position
175
 *  @param  end     set to end of scratch buffer for readout
176
 *  @param  name    Separated name
177
 *  @return Current-iterator with new position
178
 */
179
template <class char_t>
180
58.7k
inline char_t getNameNoSpace(char_t it, char_t end, std::string &name) {
181
58.7k
    name = "";
182
58.7k
    if (isEndOfBuffer(it, end)) {
183
0
        return end;
184
0
    }
185
186
58.7k
    char *pStart = &(*it);
187
578k
    while (!isEndOfBuffer(it, end) && !IsLineEnd(*it) && !IsSpaceOrNewLine(*it)) {
188
519k
        ++it;
189
519k
    }
190
191
117k
    while (isEndOfBuffer(it, end) || IsLineEnd(*it) || IsSpaceOrNewLine(*it)) {
192
58.7k
        --it;
193
58.7k
    }
194
58.7k
    ++it;
195
196
    // Get name
197
    // if there is no name, and the previous char is a separator, come back to start
198
58.7k
    while (&(*it) < pStart) {
199
0
        ++it;
200
0
    }
201
58.7k
    std::string strName(pStart, &(*it));
202
58.7k
    if (!strName.empty()) {
203
58.7k
        name = strName;
204
58.7k
    }
205
206
58.7k
    return it;
207
58.7k
}
208
209
/**
210
 *  @brief  Get next word from given line
211
 *  @param[in] it      set to current position
212
 *  @param[in] end     set to end of scratch buffer for readout
213
 *  @param[in] pBuffer Buffer for next word
214
 *  @param[in] length  Buffer length
215
 *  @return Current-iterator with new position
216
 */
217
template <class char_t>
218
1.08M
inline char_t CopyNextWord(char_t it, char_t end, char *pBuffer, size_t length) {
219
1.08M
    size_t index = 0;
220
1.08M
    it = getNextWord<char_t>(it, end);
221
3.84M
    while (!IsSpaceOrNewLine(*it) && !isEndOfBuffer(it, end)) {
222
2.77M
        pBuffer[index] = *it;
223
2.77M
        ++index;
224
2.77M
        if (index == length - 1) {
225
21.3k
            break;
226
21.3k
        }
227
2.75M
        ++it;
228
2.75M
    }
229
1.08M
    pBuffer[index] = '\0';
230
1.08M
    return it;
231
1.08M
}
232
233
/**
234
 *  @brief  Get next float from given line
235
 *  @param[in]  it      set to current position
236
 *  @param[in]  end     set to end of scratch buffer for readout
237
 *  @param[out] value   Separated float value.
238
 *  @return Current-iterator with new position
239
 */
240
template <class char_t>
241
1.47k
inline char_t getFloat(char_t it, char_t end, ai_real &value) {
242
1.47k
    static const size_t BUFFERSIZE = 1024;
243
1.47k
    char buffer[BUFFERSIZE] = {};
244
1.47k
    it = CopyNextWord<char_t>(it, end, buffer, BUFFERSIZE);
245
1.47k
    value = (ai_real)fast_atof(buffer);
246
247
1.47k
    return it;
248
1.47k
}
249
250
/**
251
 *  @brief  Checks for a line-end.
252
 *  @param[in] it   Current iterator in string.
253
 *  @param[in] end  End of the string.
254
 *  @return The trimmed string.
255
 */
256
template <class T>
257
bool hasLineEnd(T it, T end) {
258
    bool hasLineEnd = false;
259
    while (!isEndOfBuffer(it, end)) {
260
        ++it;
261
        if (IsLineEnd(it)) {
262
            hasLineEnd = true;
263
            break;
264
        }
265
    }
266
267
    return hasLineEnd;
268
}
269
270
} // Namespace Assimp
271
272
#endif // OBJ_TOOLS_H_INC