1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 
5 Copyright (c) 2006-2021, 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>
isEndOfBuffer(char_t it,char_t end)61 inline bool isEndOfBuffer(char_t it, char_t end) {
62     if (it == end) {
63         return true;
64     }
65     --end;
66 
67     return (it == end);
68 }
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>
getNextWord(Char_T pBuffer,Char_T pEnd)77 inline Char_T getNextWord(Char_T pBuffer, Char_T pEnd) {
78     while (!isEndOfBuffer(pBuffer, pEnd)) {
79         if (!IsSpaceOrNewLine(*pBuffer) || IsLineEnd(*pBuffer)) {
80             break;
81         }
82         ++pBuffer;
83     }
84 
85     return pBuffer;
86 }
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>
getNextToken(Char_T pBuffer,Char_T pEnd)95 inline Char_T getNextToken(Char_T pBuffer, Char_T pEnd) {
96     while (!isEndOfBuffer(pBuffer, pEnd)) {
97         if (IsSpaceOrNewLine(*pBuffer)) {
98             break;
99         }
100         ++pBuffer;
101     }
102     return getNextWord(pBuffer, pEnd);
103 }
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>
skipLine(char_t it,char_t end,unsigned int & uiLine)113 inline char_t skipLine(char_t it, char_t end, unsigned int &uiLine) {
114     while (!isEndOfBuffer(it, end) && !IsLineEnd(*it)) {
115         ++it;
116     }
117 
118     if (it != end) {
119         ++it;
120         ++uiLine;
121     }
122     // fix .. from time to time there are spaces at the beginning of a material line
123     while (it != end && (*it == '\t' || *it == ' ')) {
124         ++it;
125     }
126 
127     return it;
128 }
129 
130 /**
131  *  @brief  Get a name from the current line. Preserve space in the middle,
132  *    but trim it at the end.
133  *  @param[in]  it      set to current position
134  *  @param[in]  end     set to end of scratch buffer for readout
135  *  @param[out] name    Separated name
136  *  @return Current-iterator with new position
137  */
138 template <class char_t>
getName(char_t it,char_t end,std::string & name)139 inline char_t getName(char_t it, char_t end, std::string &name) {
140     name = "";
141     if (isEndOfBuffer(it, end)) {
142         return end;
143     }
144 
145     char *pStart = &(*it);
146     while (!isEndOfBuffer(it, end) && !IsLineEnd(*it)) {
147         ++it;
148     }
149 
150     while (IsSpace(*it)) {
151         --it;
152     }
153     // Get name
154     // if there is no name, and the previous char is a separator, come back to start
155     while (&(*it) < pStart) {
156         ++it;
157     }
158     std::string strName(pStart, &(*it));
159     if (!strName.empty()) {
160         name = strName;
161     }
162 
163 
164     return it;
165 }
166 
167 /**
168  *  @brief  Get a name from the current line. Do not preserve space
169  *    in the middle, but trim it at the end.
170  *  @param  it      set to current position
171  *  @param  end     set to end of scratch buffer for readout
172  *  @param  name    Separated name
173  *  @return Current-iterator with new position
174  */
175 template <class char_t>
getNameNoSpace(char_t it,char_t end,std::string & name)176 inline char_t getNameNoSpace(char_t it, char_t end, std::string &name) {
177     name = "";
178     if (isEndOfBuffer(it, end)) {
179         return end;
180     }
181 
182     char *pStart = &(*it);
183     while (!isEndOfBuffer(it, end) && !IsLineEnd(*it) && !IsSpaceOrNewLine(*it)) {
184         ++it;
185     }
186 
187     while (isEndOfBuffer(it, end) || IsLineEnd(*it) || IsSpaceOrNewLine(*it)) {
188         --it;
189     }
190     ++it;
191 
192     // Get name
193     // if there is no name, and the previous char is a separator, come back to start
194     while (&(*it) < pStart) {
195         ++it;
196     }
197     std::string strName(pStart, &(*it));
198     if (!strName.empty()) {
199         name = strName;
200     }
201 
202     return it;
203 }
204 
205 /**
206  *  @brief  Get next word from given line
207  *  @param[in] it      set to current position
208  *  @param[in] end     set to end of scratch buffer for readout
209  *  @param[in] pBuffer Buffer for next word
210  *  @param[in] length  Buffer length
211  *  @return Current-iterator with new position
212  */
213 template <class char_t>
CopyNextWord(char_t it,char_t end,char * pBuffer,size_t length)214 inline char_t CopyNextWord(char_t it, char_t end, char *pBuffer, size_t length) {
215     size_t index = 0;
216     it = getNextWord<char_t>(it, end);
217     while (!IsSpaceOrNewLine(*it) && !isEndOfBuffer(it, end)) {
218         pBuffer[index] = *it;
219         ++index;
220         if (index == length - 1) {
221             break;
222         }
223         ++it;
224     }
225     pBuffer[index] = '\0';
226     return it;
227 }
228 
229 /**
230  *  @brief  Get next float from given line
231  *  @param[in]  it      set to current position
232  *  @param[in]  end     set to end of scratch buffer for readout
233  *  @param[out] value   Separated float value.
234  *  @return Current-iterator with new position
235  */
236 template <class char_t>
getFloat(char_t it,char_t end,ai_real & value)237 inline char_t getFloat(char_t it, char_t end, ai_real &value) {
238     static const size_t BUFFERSIZE = 1024;
239     char buffer[BUFFERSIZE];
240     it = CopyNextWord<char_t>(it, end, buffer, BUFFERSIZE);
241     value = (ai_real)fast_atof(buffer);
242 
243     return it;
244 }
245 
246 /**
247  *  @brief  Will remove white-spaces for a string.
248  *  @param[in] str  The string to clean
249  *  @return The trimmed string.
250  */
251 template <class string_type>
trim_whitespaces(string_type str)252 inline string_type trim_whitespaces(string_type str) {
253     while (!str.empty() && IsSpace(str[0])) {
254         str.erase(0);
255     }
256     while (!str.empty() && IsSpace(str[str.length() - 1])) {
257         str.erase(str.length() - 1);
258     }
259     return str;
260 }
261 
262 /**
263  *  @brief  Checks for a line-end.
264  *  @param[in] it   Current iterator in string.
265  *  @param[in] end  End of the string.
266  *  @return The trimmed string.
267  */
268 template <class T>
hasLineEnd(T it,T end)269 bool hasLineEnd(T it, T end) {
270     bool hasLineEnd = false;
271     while (!isEndOfBuffer(it, end)) {
272         ++it;
273         if (IsLineEnd(it)) {
274             hasLineEnd = true;
275             break;
276         }
277     }
278 
279     return hasLineEnd;
280 }
281 
282 } // Namespace Assimp
283 
284 #endif // OBJ_TOOLS_H_INC
285