vrshoot

view libs/assimp/ObjTools.h @ 2:334d17aed7de

visual studio project files
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 18:36:38 +0200
parents
children
line source
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
5 Copyright (c) 2006-2012, assimp team
6 All rights reserved.
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
12 * Redistributions of source code must retain the above
13 copyright notice, this list of conditions and the
14 following disclaimer.
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
21 * Neither the name of the assimp team, nor the names of its
22 contributors may be used to endorse or promote products
23 derived from this software without specific prior
24 written permission of the assimp team.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 ----------------------------------------------------------------------
39 */
41 /** @file ObjTools.h
42 * @brief Some helpful templates for text parsing
43 */
44 #ifndef OBJ_TOOLS_H_INC
45 #define OBJ_TOOLS_H_INC
47 #include "fast_atof.h"
49 namespace Assimp
50 {
52 /** @brief Returns true, if the last entry of the buffer is reached.
53 * @param it Iterator of current position.
54 * @param end Iterator with end of buffer.
55 * @return true, if the end of the buffer is reached.
56 */
57 template<class char_t>
58 inline bool isEndOfBuffer( char_t it, char_t end )
59 {
60 if ( it == end )
61 {
62 return true;
63 }
64 else
65 {
66 end--;
67 }
68 return ( it == end );
69 }
71 /** @brief Returns true, if token is a space on any supported platform
72 * @param token Token to search in
73 * @return true, if token is a space
74 */
75 inline bool isSeparator( char token )
76 {
77 return ( token == ' ' ||
78 token == '\n' ||
79 token == '\f' ||
80 token == '\r' ||
81 token == '\t' );
82 }
84 /** @brief Returns true, fi token id a new line marking token.
85 * @param token Token to search in
86 * @return true, if token is a newline token.
87 */
88 inline bool isNewLine( char token )
89 {
90 return ( token == '\n' || token == '\f' || token == '\r' );
91 }
93 /** @brief Returns next word separated by a space
94 * @param pBuffer Pointer to data buffer
95 * @param pEnd Pointer to end of buffer
96 * @return Pointer to next space
97 */
98 template<class Char_T>
99 inline Char_T getNextWord( Char_T pBuffer, Char_T pEnd )
100 {
101 while ( !isEndOfBuffer( pBuffer, pEnd ) )
102 {
103 if ( !isSeparator( *pBuffer ) || isNewLine( *pBuffer ) )
104 break;
105 pBuffer++;
106 }
107 return pBuffer;
108 }
110 /** @brief Returns ponter a next token
111 * @param pBuffer Pointer to data buffer
112 * @param pEnd Pointer to end of buffer
113 * @return Pointer to next token
114 */
115 template<class Char_T>
116 inline Char_T getNextToken( Char_T pBuffer, Char_T pEnd )
117 {
118 while ( !isEndOfBuffer( pBuffer, pEnd ) )
119 {
120 if ( isSeparator( *pBuffer ) )
121 break;
122 pBuffer++;
123 }
124 return getNextWord( pBuffer, pEnd );
125 }
127 /** @brief Skips a line
128 * @param it Iterator set to current position
129 * @param end Iterator set to end of scratch buffer for readout
130 * @param uiLine Current linenumber in format
131 * @return Current-iterator with new position
132 */
133 template<class char_t>
134 inline char_t skipLine( char_t it, char_t end, unsigned int &uiLine )
135 {
136 while ( !isEndOfBuffer( it, end ) && !isNewLine( *it ) )
137 ++it;
138 if ( it != end )
139 {
140 ++it;
141 ++uiLine;
142 }
143 // fix .. from time to time there are spaces at the beginning of a material line
144 while ( it != end && (*it == '\t' || *it == ' ') )
145 ++it;
146 return it;
147 }
149 /** @brief Get a name from the current line. Preserve space in the middle,
150 * but trim it at the end.
151 * @param it set to current position
152 * @param end set to end of scratch buffer for readout
153 * @param name Separated name
154 * @return Current-iterator with new position
155 */
156 template<class char_t>
157 inline char_t getName( char_t it, char_t end, std::string &name )
158 {
159 name = "";
160 it = getNextToken<char_t>( it, end );
161 if ( isEndOfBuffer( it, end ) )
162 return end;
164 char *pStart = &( *it );
165 while ( !isEndOfBuffer( it, end ) && !isNewLine( *it ) ) {
166 ++it;
167 }
169 while(isEndOfBuffer( it, end ) || isNewLine( *it ) || isSeparator(*it)) {
170 --it;
171 }
172 ++it;
174 // Get name
175 // if there is no name, and the previous char is a separator, come back to start
176 while (&(*it) < pStart) {
177 ++it;
178 }
179 std::string strName( pStart, &(*it) );
180 if ( strName.empty() )
181 return it;
182 else
183 name = strName;
185 return it;
186 }
188 /** @brief Get next word from given line
189 * @param it set to current position
190 * @param end set to end of scratch buffer for readout
191 * @param pBuffer Buffer for next word
192 * @param length Buffer length
193 * @return Current-iterator with new position
194 */
195 template<class char_t>
196 inline char_t CopyNextWord( char_t it, char_t end, char *pBuffer, size_t length )
197 {
198 size_t index = 0;
199 it = getNextWord<char_t>( it, end );
200 while ( !isSeparator( *it ) && !isEndOfBuffer( it, end ) )
201 {
202 pBuffer[index] = *it ;
203 index++;
204 if (index == length-1)
205 break;
206 ++it;
207 }
208 pBuffer[ index ] = '\0';
209 return it;
210 }
212 /** @brief Get next float from given line
213 * @param it set to current position
214 * @param end set to end of scratch buffer for readout
215 * @param value Separated float value.
216 * @return Current-iterator with new position
217 */
218 template<class char_t>
219 inline char_t getFloat( char_t it, char_t end, float &value )
220 {
221 static const size_t BUFFERSIZE = 1024;
222 char buffer[ BUFFERSIZE ];
223 it = CopyNextWord<char_t>( it, end, buffer, BUFFERSIZE );
224 value = (float) fast_atof( buffer );
226 return it;
227 }
229 /** @brief Will perform a simple tokenize.
230 * @param str String to tokenize.
231 * @param tokens Array with tokens, will be empty if no token was found.
232 * @param delimiters Delimiter for tokenize.
233 * @return Number of found token.
234 */
235 template<class string_type>
236 unsigned int tokenize( const string_type& str, std::vector<string_type>& tokens,
237 const string_type& delimiters )
238 {
239 // Skip delimiters at beginning.
240 typename string_type::size_type lastPos = str.find_first_not_of( delimiters, 0 );
242 // Find first "non-delimiter".
243 typename string_type::size_type pos = str.find_first_of( delimiters, lastPos );
244 while ( string_type::npos != pos || string_type::npos != lastPos )
245 {
246 // Found a token, add it to the vector.
247 string_type tmp = str.substr(lastPos, pos - lastPos);
248 if ( !tmp.empty() && ' ' != tmp[ 0 ] )
249 tokens.push_back( tmp );
251 // Skip delimiters. Note the "not_of"
252 lastPos = str.find_first_not_of( delimiters, pos );
254 // Find next "non-delimiter"
255 pos = str.find_first_of( delimiters, lastPos );
256 }
258 return static_cast<unsigned int>( tokens.size() );
259 }
261 } // Namespace Assimp
263 #endif