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, unsigned short precision, 
00037         unsigned short width, char fill, std::ios::fmtflags flags)
00038     {
00039         StringUtil::StrStreamType stream;
00040         stream.precision(precision);
00041         stream.width(width);
00042         stream.fill(fill);
00043         if (flags)
00044             stream.setf(flags);
00045         stream << val;
00046         return stream.str();
00047     }
00048     //-----------------------------------------------------------------------
00049     String StringConverter::toString(int val, 
00050         unsigned short width, char fill, std::ios::fmtflags flags)
00051     {
00052         StringUtil::StrStreamType stream;
00053         stream.width(width);
00054         stream.fill(fill);
00055         if (flags)
00056             stream.setf(flags);
00057         stream << val;
00058         return stream.str();
00059     }
00060     //-----------------------------------------------------------------------
00061     String StringConverter::toString(unsigned int val, 
00062         unsigned short width, char fill, std::ios::fmtflags flags)
00063     {
00064         StringUtil::StrStreamType stream;
00065         stream.width(width);
00066         stream.fill(fill);
00067         if (flags)
00068             stream.setf(flags);
00069         stream << val;
00070         return stream.str();
00071     }
00072     //-----------------------------------------------------------------------
00073     String StringConverter::toString(long val, 
00074         unsigned short width, char fill, std::ios::fmtflags flags)
00075     {
00076         StringUtil::StrStreamType stream;
00077         stream.width(width);
00078         stream.fill(fill);
00079         if (flags)
00080             stream.setf(flags);
00081         stream << val;
00082         return stream.str();
00083     }
00084     //-----------------------------------------------------------------------
00085     String StringConverter::toString(unsigned long val, 
00086         unsigned short width, char fill, std::ios::fmtflags flags)
00087     {
00088         StringUtil::StrStreamType stream;
00089         stream.width(width);
00090         stream.fill(fill);
00091         if (flags)
00092             stream.setf(flags);
00093         stream << val;
00094         return stream.str();
00095     }
00096     //-----------------------------------------------------------------------
00097     String StringConverter::toString(const Vector3& val)
00098     {
00099         StringUtil::StrStreamType stream;
00100         stream << val.x << " " << val.y << " " << val.z;
00101         return stream.str();
00102     }
00103     //-----------------------------------------------------------------------
00104     String StringConverter::toString(const Matrix3& val)
00105     {
00106         StringUtil::StrStreamType stream;
00107         stream << val[0][0] << " " 
00108             << val[0][1] << " "             
00109             << val[0][2] << " "             
00110             << val[1][0] << " "             
00111             << val[1][1] << " "             
00112             << val[1][2] << " "             
00113             << val[2][0] << " "             
00114             << val[2][1] << " "             
00115             << val[2][2];
00116         return stream.str();
00117     }
00118     //-----------------------------------------------------------------------
00119     String StringConverter::toString(bool val, bool yesNo)
00120     {
00121         if (val)
00122         {
00123             if (yesNo)
00124             {
00125                 return "yes";
00126             }
00127             else
00128             {
00129                 return "true";
00130             }
00131         }
00132         else
00133             if (yesNo)
00134             {
00135                 return "no";
00136             }
00137             else
00138             {
00139                 return "false";
00140             }
00141     }
00142     //-----------------------------------------------------------------------
00143     String StringConverter::toString(const Matrix4& val)
00144     {
00145         StringUtil::StrStreamType stream;
00146         stream << val[0][0] << " " 
00147             << val[0][1] << " "             
00148             << val[0][2] << " "             
00149             << val[0][3] << " "             
00150             << val[1][0] << " "             
00151             << val[1][1] << " "             
00152             << val[1][2] << " "             
00153             << val[1][3] << " "             
00154             << val[2][0] << " "             
00155             << val[2][1] << " "             
00156             << val[2][2] << " "             
00157             << val[2][3] << " "             
00158             << val[3][0] << " "             
00159             << val[3][1] << " "             
00160             << val[3][2] << " "             
00161             << val[3][3];
00162         return stream.str();
00163     }
00164     //-----------------------------------------------------------------------
00165     String StringConverter::toString(const Quaternion& val)
00166     {
00167         StringUtil::StrStreamType stream;
00168         stream  << val.w << " " << val.x << " " << val.y << " " << val.z;
00169         return stream.str();
00170     }
00171     //-----------------------------------------------------------------------
00172     String StringConverter::toString(const ColourValue& val)
00173     {
00174         StringUtil::StrStreamType stream;
00175         stream << val.r << " " << val.g << " " << val.b << " " << val.a;
00176         return stream.str();
00177     }
00178     //-----------------------------------------------------------------------
00179     String StringConverter::toString(const StringVector& val)
00180     {
00181         StringUtil::StrStreamType stream;
00182         StringVector::const_iterator i, iend, ibegin;
00183         ibegin = val.begin();
00184         iend = val.end();
00185         for (i = ibegin; i != iend; ++i)
00186         {
00187             if (i != ibegin)
00188                 stream << " ";
00189 
00190             stream << *i; 
00191         }
00192         return stream.str();
00193     }
00194     //-----------------------------------------------------------------------
00195     Real StringConverter::parseReal(const String& val)
00196     {
00197         return atof(val.c_str());
00198     }
00199     //-----------------------------------------------------------------------
00200     int StringConverter::parseInt(const String& val)
00201     {
00202         return atoi(val.c_str());
00203     }
00204     //-----------------------------------------------------------------------
00205     unsigned int StringConverter::parseUnsignedInt(const String& val)
00206     {
00207         return atoi(val.c_str());
00208     }
00209     //-----------------------------------------------------------------------
00210     long StringConverter::parseLong(const String& val)
00211     {
00212         return atol(val.c_str());
00213     }
00214     //-----------------------------------------------------------------------
00215     unsigned long StringConverter::parseUnsignedLong(const String& val)
00216     {
00217         return atol(val.c_str());
00218     }
00219     //-----------------------------------------------------------------------
00220     bool StringConverter::parseBool(const String& val)
00221     {
00222         if (val == "true")
00223             return true;
00224         else
00225             return false;
00226     }
00227     //-----------------------------------------------------------------------
00228     Vector3 StringConverter::parseVector3(const String& val)
00229     {
00230         // Split on space
00231         std::vector<String> vec = StringUtil::split(val);
00232 
00233         if (vec.size() != 3)
00234         {
00235             return Vector3::ZERO;
00236         }
00237         else
00238         {
00239             return Vector3(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]));
00240         }
00241 
00242     }
00243     //-----------------------------------------------------------------------
00244     Matrix3 StringConverter::parseMatrix3(const String& val)
00245     {
00246         // Split on space
00247         std::vector<String> vec = StringUtil::split(val);
00248 
00249         if (vec.size() != 9)
00250         {
00251             return Matrix3::IDENTITY;
00252         }
00253         else
00254         {
00255             return Matrix3(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]),
00256                 parseReal(vec[3]),parseReal(vec[4]),parseReal(vec[5]),
00257                 parseReal(vec[6]),parseReal(vec[7]),parseReal(vec[8]));
00258         }
00259     }
00260     //-----------------------------------------------------------------------
00261     Matrix4 StringConverter::parseMatrix4(const String& val)
00262     {
00263         // Split on space
00264         std::vector<String> vec = StringUtil::split(val);
00265 
00266         if (vec.size() != 16)
00267         {
00268             return Matrix4::IDENTITY;
00269         }
00270         else
00271         {
00272             return Matrix4(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), parseReal(vec[3]),
00273                 parseReal(vec[4]),parseReal(vec[5]), parseReal(vec[6]), parseReal(vec[7]),
00274                 parseReal(vec[8]),parseReal(vec[9]), parseReal(vec[10]), parseReal(vec[11]),
00275                 parseReal(vec[12]),parseReal(vec[13]), parseReal(vec[14]), parseReal(vec[15]));
00276         }
00277     }
00278     //-----------------------------------------------------------------------
00279     Quaternion StringConverter::parseQuaternion(const String& val)
00280     {
00281         // Split on space
00282         std::vector<String> vec = StringUtil::split(val);
00283 
00284         if (vec.size() != 4)
00285         {
00286             return Quaternion::IDENTITY;
00287         }
00288         else
00289         {
00290             return Quaternion(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), parseReal(vec[3]));
00291         }
00292     }
00293     //-----------------------------------------------------------------------
00294     ColourValue StringConverter::parseColourValue(const String& val)
00295     {
00296         // Split on space
00297         std::vector<String> vec = StringUtil::split(val);
00298 
00299         if (vec.size() == 4)
00300         {
00301             return ColourValue(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), parseReal(vec[3]));
00302         }
00303         else if (vec.size() == 3)
00304         {
00305             return ColourValue(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), 1.0f);
00306         }
00307         else
00308         {
00309             return ColourValue::Black;
00310         }
00311     }
00312     //-----------------------------------------------------------------------
00313     StringVector StringConverter::parseStringVector(const String& val)
00314     {
00315         return StringUtil::split(val);
00316     }
00317 }
00318 
00319 

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:47 2004