source: OGRE/trunk/ogrenew/OgreMain/src/OgreStringConverter.cpp @ 657

Revision 657, 11.9 KB checked in by mattausch, 18 years ago (diff)

added ogre dependencies and patched ogre sources

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