Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreStringConverter.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2002 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #include "OgreStableHeaders.h"
00026 #include "OgreStringConverter.h"
00027 #include "OgreVector3.h"
00028 #include "OgreMatrix3.h"
00029 #include "OgreMatrix4.h"
00030 #include "OgreQuaternion.h"
00031 #include "OgreColourValue.h"
00032 
00033 namespace Ogre {
00034 
00035     //-----------------------------------------------------------------------
00036     String StringConverter::toString(Real val)
00037     {
00038         String::StrStreamType stream;
00039         stream << val;
00040         return stream.str();
00041     }
00042     //-----------------------------------------------------------------------
00043     String StringConverter::toString(int val)
00044     {
00045         String::StrStreamType stream;
00046         stream << val;
00047         return stream.str();
00048     }
00049     //-----------------------------------------------------------------------
00050     String StringConverter::toString(unsigned int val)
00051     {
00052         String::StrStreamType stream;
00053         stream << val;
00054         return stream.str();
00055     }
00056     //-----------------------------------------------------------------------
00057     String StringConverter::toString(long val)
00058     {
00059         String::StrStreamType stream;
00060         stream << val;
00061         return stream.str();
00062     }
00063     //-----------------------------------------------------------------------
00064     String StringConverter::toString(unsigned long val)
00065     {
00066         String::StrStreamType stream;
00067         stream << val;
00068         return stream.str();
00069     }
00070     //-----------------------------------------------------------------------
00071     String StringConverter::toString(const Vector3& val)
00072     {
00073         String::StrStreamType stream;
00074         stream << val.x << " " << val.y << " " << val.z;
00075         return stream.str();
00076     }
00077     //-----------------------------------------------------------------------
00078     String StringConverter::toString(const Matrix3& val)
00079     {
00080         String::StrStreamType stream;
00081         stream << val[0][0] << " " 
00082             << val[0][1] << " "             
00083             << val[0][2] << " "             
00084             << val[1][0] << " "             
00085             << val[1][1] << " "             
00086             << val[1][2] << " "             
00087             << val[2][0] << " "             
00088             << val[2][1] << " "             
00089             << val[2][2];
00090         return stream.str();
00091     }
00092     //-----------------------------------------------------------------------
00093     String StringConverter::toString(bool val, bool yesNo)
00094     {
00095         if (val)
00096         {
00097             if (yesNo)
00098             {
00099                 return "yes";
00100             }
00101             else
00102             {
00103                 return "true";
00104             }
00105         }
00106         else
00107             if (yesNo)
00108             {
00109                 return "no";
00110             }
00111             else
00112             {
00113                 return "false";
00114             }
00115     }
00116     //-----------------------------------------------------------------------
00117     String StringConverter::toString(const Matrix4& val)
00118     {
00119         String::StrStreamType stream;
00120         stream << val[0][0] << " " 
00121             << val[0][1] << " "             
00122             << val[0][2] << " "             
00123             << val[0][3] << " "             
00124             << val[1][0] << " "             
00125             << val[1][1] << " "             
00126             << val[1][2] << " "             
00127             << val[1][3] << " "             
00128             << val[2][0] << " "             
00129             << val[2][1] << " "             
00130             << val[2][2] << " "             
00131             << val[2][3] << " "             
00132             << val[3][0] << " "             
00133             << val[3][1] << " "             
00134             << val[3][2] << " "             
00135             << val[3][3];
00136         return stream.str();
00137     }
00138     //-----------------------------------------------------------------------
00139     String StringConverter::toString(const Quaternion& val)
00140     {
00141         String::StrStreamType stream;
00142         stream  << val.w << " " << val.x << " " << val.y << " " << val.z;
00143         return stream.str();
00144     }
00145     //-----------------------------------------------------------------------
00146     String StringConverter::toString(const ColourValue& val)
00147     {
00148         String::StrStreamType stream;
00149         stream << val.r << " " << val.g << " " << val.b << " " << val.a;
00150         return stream.str();
00151     }
00152     //-----------------------------------------------------------------------
00153     String StringConverter::toString(const StringVector& val)
00154     {
00155         String::StrStreamType stream;
00156         StringVector::const_iterator i, iend, ibegin;
00157         ibegin = val.begin();
00158         iend = val.end();
00159         for (i = ibegin; i != iend; ++i)
00160         {
00161             if (i != ibegin)
00162                 stream << " ";
00163 
00164             stream << *i; 
00165         }
00166         return stream.str();
00167     }
00168     //-----------------------------------------------------------------------
00169     Real StringConverter::parseReal(const String& val)
00170     {
00171         return atof(val.c_str());
00172     }
00173     //-----------------------------------------------------------------------
00174     int StringConverter::parseInt(const String& val)
00175     {
00176         return atoi(val.c_str());
00177     }
00178     //-----------------------------------------------------------------------
00179     unsigned int StringConverter::parseUnsignedInt(const String& val)
00180     {
00181         return atoi(val.c_str());
00182     }
00183     //-----------------------------------------------------------------------
00184     long StringConverter::parseLong(const String& val)
00185     {
00186         return atol(val.c_str());
00187     }
00188     //-----------------------------------------------------------------------
00189     unsigned long StringConverter::parseUnsignedLong(const String& val)
00190     {
00191         return atol(val.c_str());
00192     }
00193     //-----------------------------------------------------------------------
00194     bool StringConverter::parseBool(const String& val)
00195     {
00196         if (val == "true")
00197             return true;
00198         else
00199             return false;
00200     }
00201     //-----------------------------------------------------------------------
00202     Vector3 StringConverter::parseVector3(const String& val)
00203     {
00204         // Split on space
00205         std::vector<String> vec = val.split();
00206 
00207         if (vec.size() != 3)
00208         {
00209             return Vector3::ZERO;
00210         }
00211         else
00212         {
00213             return Vector3(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]));
00214         }
00215 
00216     }
00217     //-----------------------------------------------------------------------
00218     Matrix3 StringConverter::parseMatrix3(const String& val)
00219     {
00220         // Split on space
00221         std::vector<String> vec = val.split();
00222 
00223         if (vec.size() != 9)
00224         {
00225             return Matrix3::IDENTITY;
00226         }
00227         else
00228         {
00229             return Matrix3(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]),
00230                 parseReal(vec[3]),parseReal(vec[4]),parseReal(vec[5]),
00231                 parseReal(vec[6]),parseReal(vec[7]),parseReal(vec[8]));
00232         }
00233     }
00234     //-----------------------------------------------------------------------
00235     Matrix4 StringConverter::parseMatrix4(const String& val)
00236     {
00237         // Split on space
00238         std::vector<String> vec = val.split();
00239 
00240         if (vec.size() != 16)
00241         {
00242             return Matrix4::IDENTITY;
00243         }
00244         else
00245         {
00246             return Matrix4(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), parseReal(vec[3]),
00247                 parseReal(vec[4]),parseReal(vec[5]), parseReal(vec[6]), parseReal(vec[7]),
00248                 parseReal(vec[8]),parseReal(vec[9]), parseReal(vec[10]), parseReal(vec[11]),
00249                 parseReal(vec[12]),parseReal(vec[13]), parseReal(vec[14]), parseReal(vec[15]));
00250         }
00251     }
00252     //-----------------------------------------------------------------------
00253     Quaternion StringConverter::parseQuaternion(const String& val)
00254     {
00255         // Split on space
00256         std::vector<String> vec = val.split();
00257 
00258         if (vec.size() != 4)
00259         {
00260             return Quaternion::IDENTITY;
00261         }
00262         else
00263         {
00264             return Quaternion(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), parseReal(vec[3]));
00265         }
00266     }
00267     //-----------------------------------------------------------------------
00268     ColourValue StringConverter::parseColourValue(const String& val)
00269     {
00270         // Split on space
00271         std::vector<String> vec = val.split();
00272 
00273         if (vec.size() == 4)
00274         {
00275             return ColourValue(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), parseReal(vec[3]));
00276         }
00277         else if (vec.size() == 3)
00278         {
00279             return ColourValue(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), 1.0f);
00280         }
00281         else
00282         {
00283             return ColourValue::Black;
00284         }
00285     }
00286     //-----------------------------------------------------------------------
00287     StringVector StringConverter::parseStringVector(const String& val)
00288     {
00289         return val.split();
00290     }
00291 }
00292 
00293 

Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:28 2004