1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
8 | Also see acknowledgements in Readme.html |
---|
9 | |
---|
10 | This program is free software; you can redistribute it and/or modify it under |
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
13 | version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
23 | |
---|
24 | You may alternatively use this source under the terms of a specific version of |
---|
25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
26 | Torus Knot Software Ltd. |
---|
27 | ----------------------------------------------------------------------------- |
---|
28 | */ |
---|
29 | #include "OgreStableHeaders.h" |
---|
30 | #include "OgreStringConverter.h" |
---|
31 | #include "OgreVector2.h" |
---|
32 | #include "OgreVector3.h" |
---|
33 | #include "OgreVector4.h" |
---|
34 | #include "OgreMatrix3.h" |
---|
35 | #include "OgreMatrix4.h" |
---|
36 | #include "OgreQuaternion.h" |
---|
37 | #include "OgreColourValue.h" |
---|
38 | |
---|
39 | namespace Ogre { |
---|
40 | |
---|
41 | //----------------------------------------------------------------------- |
---|
42 | String StringConverter::toString(Real val, unsigned short precision, |
---|
43 | unsigned short width, char fill, std::ios::fmtflags flags) |
---|
44 | { |
---|
45 | StringUtil::StrStreamType stream; |
---|
46 | stream.precision(precision); |
---|
47 | stream.width(width); |
---|
48 | stream.fill(fill); |
---|
49 | if (flags) |
---|
50 | stream.setf(flags); |
---|
51 | stream << val; |
---|
52 | return stream.str(); |
---|
53 | } |
---|
54 | //----------------------------------------------------------------------- |
---|
55 | String StringConverter::toString(int val, |
---|
56 | unsigned short width, char fill, std::ios::fmtflags flags) |
---|
57 | { |
---|
58 | StringUtil::StrStreamType stream; |
---|
59 | stream.width(width); |
---|
60 | stream.fill(fill); |
---|
61 | if (flags) |
---|
62 | stream.setf(flags); |
---|
63 | stream << val; |
---|
64 | return stream.str(); |
---|
65 | } |
---|
66 | //----------------------------------------------------------------------- |
---|
67 | #if OGRE_ARCH_TYPE == OGRE_ARCHITECTURE_64 || OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
68 | String StringConverter::toString(unsigned int val, |
---|
69 | unsigned short width, char fill, std::ios::fmtflags flags) |
---|
70 | { |
---|
71 | StringUtil::StrStreamType stream; |
---|
72 | stream.width(width); |
---|
73 | stream.fill(fill); |
---|
74 | if (flags) |
---|
75 | stream.setf(flags); |
---|
76 | stream << val; |
---|
77 | return stream.str(); |
---|
78 | } |
---|
79 | //----------------------------------------------------------------------- |
---|
80 | String StringConverter::toString(size_t val, |
---|
81 | unsigned short width, char fill, std::ios::fmtflags flags) |
---|
82 | { |
---|
83 | StringUtil::StrStreamType stream; |
---|
84 | stream.width(width); |
---|
85 | stream.fill(fill); |
---|
86 | if (flags) |
---|
87 | stream.setf(flags); |
---|
88 | stream << val; |
---|
89 | return stream.str(); |
---|
90 | } |
---|
91 | #if OGRE_COMPILER == OGRE_COMPILER_MSVC |
---|
92 | //----------------------------------------------------------------------- |
---|
93 | String StringConverter::toString(unsigned long val, |
---|
94 | unsigned short width, char fill, std::ios::fmtflags flags) |
---|
95 | { |
---|
96 | StringUtil::StrStreamType stream; |
---|
97 | stream.width(width); |
---|
98 | stream.fill(fill); |
---|
99 | if (flags) |
---|
100 | stream.setf(flags); |
---|
101 | stream << val; |
---|
102 | return stream.str(); |
---|
103 | } |
---|
104 | |
---|
105 | #endif |
---|
106 | //----------------------------------------------------------------------- |
---|
107 | #else |
---|
108 | String StringConverter::toString(size_t val, |
---|
109 | unsigned short width, char fill, std::ios::fmtflags flags) |
---|
110 | { |
---|
111 | StringUtil::StrStreamType stream; |
---|
112 | stream.width(width); |
---|
113 | stream.fill(fill); |
---|
114 | if (flags) |
---|
115 | stream.setf(flags); |
---|
116 | stream << val; |
---|
117 | return stream.str(); |
---|
118 | } |
---|
119 | //----------------------------------------------------------------------- |
---|
120 | String StringConverter::toString(unsigned long val, |
---|
121 | unsigned short width, char fill, std::ios::fmtflags flags) |
---|
122 | { |
---|
123 | StringUtil::StrStreamType stream; |
---|
124 | stream.width(width); |
---|
125 | stream.fill(fill); |
---|
126 | if (flags) |
---|
127 | stream.setf(flags); |
---|
128 | stream << val; |
---|
129 | return stream.str(); |
---|
130 | } |
---|
131 | //----------------------------------------------------------------------- |
---|
132 | #endif |
---|
133 | String StringConverter::toString(long val, |
---|
134 | unsigned short width, char fill, std::ios::fmtflags flags) |
---|
135 | { |
---|
136 | StringUtil::StrStreamType stream; |
---|
137 | stream.width(width); |
---|
138 | stream.fill(fill); |
---|
139 | if (flags) |
---|
140 | stream.setf(flags); |
---|
141 | stream << val; |
---|
142 | return stream.str(); |
---|
143 | } |
---|
144 | //----------------------------------------------------------------------- |
---|
145 | String StringConverter::toString(const Vector2& val) |
---|
146 | { |
---|
147 | StringUtil::StrStreamType stream; |
---|
148 | stream << val.x << " " << val.y; |
---|
149 | return stream.str(); |
---|
150 | } |
---|
151 | //----------------------------------------------------------------------- |
---|
152 | String StringConverter::toString(const Vector3& val) |
---|
153 | { |
---|
154 | StringUtil::StrStreamType stream; |
---|
155 | stream << val.x << " " << val.y << " " << val.z; |
---|
156 | return stream.str(); |
---|
157 | } |
---|
158 | //----------------------------------------------------------------------- |
---|
159 | String StringConverter::toString(const Vector4& val) |
---|
160 | { |
---|
161 | StringUtil::StrStreamType stream; |
---|
162 | stream << val.x << " " << val.y << " " << val.z << " " << val.w; |
---|
163 | return stream.str(); |
---|
164 | } |
---|
165 | //----------------------------------------------------------------------- |
---|
166 | String StringConverter::toString(const Matrix3& val) |
---|
167 | { |
---|
168 | StringUtil::StrStreamType stream; |
---|
169 | stream << val[0][0] << " " |
---|
170 | << val[0][1] << " " |
---|
171 | << val[0][2] << " " |
---|
172 | << val[1][0] << " " |
---|
173 | << val[1][1] << " " |
---|
174 | << val[1][2] << " " |
---|
175 | << val[2][0] << " " |
---|
176 | << val[2][1] << " " |
---|
177 | << val[2][2]; |
---|
178 | return stream.str(); |
---|
179 | } |
---|
180 | //----------------------------------------------------------------------- |
---|
181 | String StringConverter::toString(bool val, bool yesNo) |
---|
182 | { |
---|
183 | if (val) |
---|
184 | { |
---|
185 | if (yesNo) |
---|
186 | { |
---|
187 | return "yes"; |
---|
188 | } |
---|
189 | else |
---|
190 | { |
---|
191 | return "true"; |
---|
192 | } |
---|
193 | } |
---|
194 | else |
---|
195 | if (yesNo) |
---|
196 | { |
---|
197 | return "no"; |
---|
198 | } |
---|
199 | else |
---|
200 | { |
---|
201 | return "false"; |
---|
202 | } |
---|
203 | } |
---|
204 | //----------------------------------------------------------------------- |
---|
205 | String StringConverter::toString(const Matrix4& val) |
---|
206 | { |
---|
207 | StringUtil::StrStreamType stream; |
---|
208 | stream << val[0][0] << " " |
---|
209 | << val[0][1] << " " |
---|
210 | << val[0][2] << " " |
---|
211 | << val[0][3] << " " |
---|
212 | << val[1][0] << " " |
---|
213 | << val[1][1] << " " |
---|
214 | << val[1][2] << " " |
---|
215 | << val[1][3] << " " |
---|
216 | << val[2][0] << " " |
---|
217 | << val[2][1] << " " |
---|
218 | << val[2][2] << " " |
---|
219 | << val[2][3] << " " |
---|
220 | << val[3][0] << " " |
---|
221 | << val[3][1] << " " |
---|
222 | << val[3][2] << " " |
---|
223 | << val[3][3]; |
---|
224 | return stream.str(); |
---|
225 | } |
---|
226 | //----------------------------------------------------------------------- |
---|
227 | String StringConverter::toString(const Quaternion& val) |
---|
228 | { |
---|
229 | StringUtil::StrStreamType stream; |
---|
230 | stream << val.w << " " << val.x << " " << val.y << " " << val.z; |
---|
231 | return stream.str(); |
---|
232 | } |
---|
233 | //----------------------------------------------------------------------- |
---|
234 | String StringConverter::toString(const ColourValue& val) |
---|
235 | { |
---|
236 | StringUtil::StrStreamType stream; |
---|
237 | stream << val.r << " " << val.g << " " << val.b << " " << val.a; |
---|
238 | return stream.str(); |
---|
239 | } |
---|
240 | //----------------------------------------------------------------------- |
---|
241 | String StringConverter::toString(const StringVector& val) |
---|
242 | { |
---|
243 | StringUtil::StrStreamType stream; |
---|
244 | StringVector::const_iterator i, iend, ibegin; |
---|
245 | ibegin = val.begin(); |
---|
246 | iend = val.end(); |
---|
247 | for (i = ibegin; i != iend; ++i) |
---|
248 | { |
---|
249 | if (i != ibegin) |
---|
250 | stream << " "; |
---|
251 | |
---|
252 | stream << *i; |
---|
253 | } |
---|
254 | return stream.str(); |
---|
255 | } |
---|
256 | //----------------------------------------------------------------------- |
---|
257 | Real StringConverter::parseReal(const String& val) |
---|
258 | { |
---|
259 | // Use istringstream for direct correspondence with toString |
---|
260 | std::istringstream str(val); |
---|
261 | Real ret = 0; |
---|
262 | str >> ret; |
---|
263 | |
---|
264 | return ret; |
---|
265 | } |
---|
266 | //----------------------------------------------------------------------- |
---|
267 | int StringConverter::parseInt(const String& val) |
---|
268 | { |
---|
269 | // Use istringstream for direct correspondence with toString |
---|
270 | std::istringstream str(val); |
---|
271 | int ret = 0; |
---|
272 | str >> ret; |
---|
273 | |
---|
274 | return ret; |
---|
275 | } |
---|
276 | //----------------------------------------------------------------------- |
---|
277 | unsigned int StringConverter::parseUnsignedInt(const String& val) |
---|
278 | { |
---|
279 | // Use istringstream for direct correspondence with toString |
---|
280 | std::istringstream str(val); |
---|
281 | unsigned int ret = 0; |
---|
282 | str >> ret; |
---|
283 | |
---|
284 | return ret; |
---|
285 | } |
---|
286 | //----------------------------------------------------------------------- |
---|
287 | long StringConverter::parseLong(const String& val) |
---|
288 | { |
---|
289 | // Use istringstream for direct correspondence with toString |
---|
290 | std::istringstream str(val); |
---|
291 | long ret = 0; |
---|
292 | str >> ret; |
---|
293 | |
---|
294 | return ret; |
---|
295 | } |
---|
296 | //----------------------------------------------------------------------- |
---|
297 | unsigned long StringConverter::parseUnsignedLong(const String& val) |
---|
298 | { |
---|
299 | // Use istringstream for direct correspondence with toString |
---|
300 | std::istringstream str(val); |
---|
301 | unsigned long ret = 0; |
---|
302 | str >> ret; |
---|
303 | |
---|
304 | return ret; |
---|
305 | } |
---|
306 | //----------------------------------------------------------------------- |
---|
307 | bool StringConverter::parseBool(const String& val) |
---|
308 | { |
---|
309 | return (StringUtil::startsWith(val, "true") || StringUtil::startsWith(val, "yes") |
---|
310 | || StringUtil::startsWith(val, "1")); |
---|
311 | } |
---|
312 | //----------------------------------------------------------------------- |
---|
313 | Vector3 StringConverter::parseVector3(const String& val) |
---|
314 | { |
---|
315 | // Split on space |
---|
316 | std::vector<String> vec = StringUtil::split(val); |
---|
317 | |
---|
318 | if (vec.size() != 3) |
---|
319 | { |
---|
320 | return Vector3::ZERO; |
---|
321 | } |
---|
322 | else |
---|
323 | { |
---|
324 | return Vector3(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2])); |
---|
325 | } |
---|
326 | |
---|
327 | } |
---|
328 | //----------------------------------------------------------------------- |
---|
329 | Matrix3 StringConverter::parseMatrix3(const String& val) |
---|
330 | { |
---|
331 | // Split on space |
---|
332 | std::vector<String> vec = StringUtil::split(val); |
---|
333 | |
---|
334 | if (vec.size() != 9) |
---|
335 | { |
---|
336 | return Matrix3::IDENTITY; |
---|
337 | } |
---|
338 | else |
---|
339 | { |
---|
340 | return Matrix3(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), |
---|
341 | parseReal(vec[3]),parseReal(vec[4]),parseReal(vec[5]), |
---|
342 | parseReal(vec[6]),parseReal(vec[7]),parseReal(vec[8])); |
---|
343 | } |
---|
344 | } |
---|
345 | //----------------------------------------------------------------------- |
---|
346 | Matrix4 StringConverter::parseMatrix4(const String& val) |
---|
347 | { |
---|
348 | // Split on space |
---|
349 | std::vector<String> vec = StringUtil::split(val); |
---|
350 | |
---|
351 | if (vec.size() != 16) |
---|
352 | { |
---|
353 | return Matrix4::IDENTITY; |
---|
354 | } |
---|
355 | else |
---|
356 | { |
---|
357 | return Matrix4(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), parseReal(vec[3]), |
---|
358 | parseReal(vec[4]),parseReal(vec[5]), parseReal(vec[6]), parseReal(vec[7]), |
---|
359 | parseReal(vec[8]),parseReal(vec[9]), parseReal(vec[10]), parseReal(vec[11]), |
---|
360 | parseReal(vec[12]),parseReal(vec[13]), parseReal(vec[14]), parseReal(vec[15])); |
---|
361 | } |
---|
362 | } |
---|
363 | //----------------------------------------------------------------------- |
---|
364 | Quaternion StringConverter::parseQuaternion(const String& val) |
---|
365 | { |
---|
366 | // Split on space |
---|
367 | std::vector<String> vec = StringUtil::split(val); |
---|
368 | |
---|
369 | if (vec.size() != 4) |
---|
370 | { |
---|
371 | return Quaternion::IDENTITY; |
---|
372 | } |
---|
373 | else |
---|
374 | { |
---|
375 | return Quaternion(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), parseReal(vec[3])); |
---|
376 | } |
---|
377 | } |
---|
378 | //----------------------------------------------------------------------- |
---|
379 | ColourValue StringConverter::parseColourValue(const String& val) |
---|
380 | { |
---|
381 | // Split on space |
---|
382 | std::vector<String> vec = StringUtil::split(val); |
---|
383 | |
---|
384 | if (vec.size() == 4) |
---|
385 | { |
---|
386 | return ColourValue(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), parseReal(vec[3])); |
---|
387 | } |
---|
388 | else if (vec.size() == 3) |
---|
389 | { |
---|
390 | return ColourValue(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), 1.0f); |
---|
391 | } |
---|
392 | else |
---|
393 | { |
---|
394 | return ColourValue::Black; |
---|
395 | } |
---|
396 | } |
---|
397 | //----------------------------------------------------------------------- |
---|
398 | StringVector StringConverter::parseStringVector(const String& val) |
---|
399 | { |
---|
400 | return StringUtil::split(val); |
---|
401 | } |
---|
402 | //----------------------------------------------------------------------- |
---|
403 | bool StringConverter::isNumber(const String& val) |
---|
404 | { |
---|
405 | std::istringstream str(val); |
---|
406 | float tst; |
---|
407 | str >> tst; |
---|
408 | return !str.fail() && str.eof(); |
---|
409 | } |
---|
410 | } |
---|
411 | |
---|
412 | |
---|