[692] | 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-2005 The OGRE Team
|
---|
| 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 | */
|
---|
| 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 |
|
---|
| 33 | namespace 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 | #if OGRE_COMPILER == OGRE_COMPILER_MSVC
|
---|
| 86 | //-----------------------------------------------------------------------
|
---|
| 87 | String StringConverter::toString(unsigned long 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 | #endif
|
---|
| 100 | //-----------------------------------------------------------------------
|
---|
| 101 | #else
|
---|
| 102 | String StringConverter::toString(size_t val,
|
---|
| 103 | unsigned short width, char fill, std::ios::fmtflags flags)
|
---|
| 104 | {
|
---|
| 105 | StringUtil::StrStreamType stream;
|
---|
| 106 | stream.width(width);
|
---|
| 107 | stream.fill(fill);
|
---|
| 108 | if (flags)
|
---|
| 109 | stream.setf(flags);
|
---|
| 110 | stream << val;
|
---|
| 111 | return stream.str();
|
---|
| 112 | }
|
---|
| 113 | //-----------------------------------------------------------------------
|
---|
| 114 | String StringConverter::toString(unsigned long val,
|
---|
| 115 | unsigned short width, char fill, std::ios::fmtflags flags)
|
---|
| 116 | {
|
---|
| 117 | StringUtil::StrStreamType stream;
|
---|
| 118 | stream.width(width);
|
---|
| 119 | stream.fill(fill);
|
---|
| 120 | if (flags)
|
---|
| 121 | stream.setf(flags);
|
---|
| 122 | stream << val;
|
---|
| 123 | return stream.str();
|
---|
| 124 | }
|
---|
| 125 | //-----------------------------------------------------------------------
|
---|
| 126 | #endif
|
---|
| 127 | String StringConverter::toString(long val,
|
---|
| 128 | unsigned short width, char fill, std::ios::fmtflags flags)
|
---|
| 129 | {
|
---|
| 130 | StringUtil::StrStreamType stream;
|
---|
| 131 | stream.width(width);
|
---|
| 132 | stream.fill(fill);
|
---|
| 133 | if (flags)
|
---|
| 134 | stream.setf(flags);
|
---|
| 135 | stream << val;
|
---|
| 136 | return stream.str();
|
---|
| 137 | }
|
---|
| 138 | //-----------------------------------------------------------------------
|
---|
| 139 | String StringConverter::toString(const Vector3& val)
|
---|
| 140 | {
|
---|
| 141 | StringUtil::StrStreamType stream;
|
---|
| 142 | stream << val.x << " " << val.y << " " << val.z;
|
---|
| 143 | return stream.str();
|
---|
| 144 | }
|
---|
| 145 | //-----------------------------------------------------------------------
|
---|
| 146 | String StringConverter::toString(const Matrix3& val)
|
---|
| 147 | {
|
---|
| 148 | StringUtil::StrStreamType stream;
|
---|
| 149 | stream << val[0][0] << " "
|
---|
| 150 | << val[0][1] << " "
|
---|
| 151 | << val[0][2] << " "
|
---|
| 152 | << val[1][0] << " "
|
---|
| 153 | << val[1][1] << " "
|
---|
| 154 | << val[1][2] << " "
|
---|
| 155 | << val[2][0] << " "
|
---|
| 156 | << val[2][1] << " "
|
---|
| 157 | << val[2][2];
|
---|
| 158 | return stream.str();
|
---|
| 159 | }
|
---|
| 160 | //-----------------------------------------------------------------------
|
---|
| 161 | String StringConverter::toString(bool val, bool yesNo)
|
---|
| 162 | {
|
---|
| 163 | if (val)
|
---|
| 164 | {
|
---|
| 165 | if (yesNo)
|
---|
| 166 | {
|
---|
| 167 | return "yes";
|
---|
| 168 | }
|
---|
| 169 | else
|
---|
| 170 | {
|
---|
| 171 | return "true";
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 | else
|
---|
| 175 | if (yesNo)
|
---|
| 176 | {
|
---|
| 177 | return "no";
|
---|
| 178 | }
|
---|
| 179 | else
|
---|
| 180 | {
|
---|
| 181 | return "false";
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 | //-----------------------------------------------------------------------
|
---|
| 185 | String StringConverter::toString(const Matrix4& val)
|
---|
| 186 | {
|
---|
| 187 | StringUtil::StrStreamType stream;
|
---|
| 188 | stream << val[0][0] << " "
|
---|
| 189 | << val[0][1] << " "
|
---|
| 190 | << val[0][2] << " "
|
---|
| 191 | << val[0][3] << " "
|
---|
| 192 | << val[1][0] << " "
|
---|
| 193 | << val[1][1] << " "
|
---|
| 194 | << val[1][2] << " "
|
---|
| 195 | << val[1][3] << " "
|
---|
| 196 | << val[2][0] << " "
|
---|
| 197 | << val[2][1] << " "
|
---|
| 198 | << val[2][2] << " "
|
---|
| 199 | << val[2][3] << " "
|
---|
| 200 | << val[3][0] << " "
|
---|
| 201 | << val[3][1] << " "
|
---|
| 202 | << val[3][2] << " "
|
---|
| 203 | << val[3][3];
|
---|
| 204 | return stream.str();
|
---|
| 205 | }
|
---|
| 206 | //-----------------------------------------------------------------------
|
---|
| 207 | String StringConverter::toString(const Quaternion& val)
|
---|
| 208 | {
|
---|
| 209 | StringUtil::StrStreamType stream;
|
---|
| 210 | stream << val.w << " " << val.x << " " << val.y << " " << val.z;
|
---|
| 211 | return stream.str();
|
---|
| 212 | }
|
---|
| 213 | //-----------------------------------------------------------------------
|
---|
| 214 | String StringConverter::toString(const ColourValue& val)
|
---|
| 215 | {
|
---|
| 216 | StringUtil::StrStreamType stream;
|
---|
| 217 | stream << val.r << " " << val.g << " " << val.b << " " << val.a;
|
---|
| 218 | return stream.str();
|
---|
| 219 | }
|
---|
| 220 | //-----------------------------------------------------------------------
|
---|
| 221 | String StringConverter::toString(const StringVector& val)
|
---|
| 222 | {
|
---|
| 223 | StringUtil::StrStreamType stream;
|
---|
| 224 | StringVector::const_iterator i, iend, ibegin;
|
---|
| 225 | ibegin = val.begin();
|
---|
| 226 | iend = val.end();
|
---|
| 227 | for (i = ibegin; i != iend; ++i)
|
---|
| 228 | {
|
---|
| 229 | if (i != ibegin)
|
---|
| 230 | stream << " ";
|
---|
| 231 |
|
---|
| 232 | stream << *i;
|
---|
| 233 | }
|
---|
| 234 | return stream.str();
|
---|
| 235 | }
|
---|
| 236 | //-----------------------------------------------------------------------
|
---|
| 237 | Real StringConverter::parseReal(const String& val)
|
---|
| 238 | {
|
---|
| 239 | return atof(val.c_str());
|
---|
| 240 | }
|
---|
| 241 | //-----------------------------------------------------------------------
|
---|
| 242 | int StringConverter::parseInt(const String& val)
|
---|
| 243 | {
|
---|
| 244 | return atoi(val.c_str());
|
---|
| 245 | }
|
---|
| 246 | //-----------------------------------------------------------------------
|
---|
| 247 | unsigned int StringConverter::parseUnsignedInt(const String& val)
|
---|
| 248 | {
|
---|
| 249 | return static_cast<unsigned int>(strtoul(val.c_str(), 0, 10));
|
---|
| 250 | }
|
---|
| 251 | //-----------------------------------------------------------------------
|
---|
| 252 | long StringConverter::parseLong(const String& val)
|
---|
| 253 | {
|
---|
| 254 | return strtol(val.c_str(), 0, 10);
|
---|
| 255 | }
|
---|
| 256 | //-----------------------------------------------------------------------
|
---|
| 257 | unsigned long StringConverter::parseUnsignedLong(const String& val)
|
---|
| 258 | {
|
---|
| 259 | return strtoul(val.c_str(), 0, 10);
|
---|
| 260 | }
|
---|
| 261 | //-----------------------------------------------------------------------
|
---|
| 262 | bool StringConverter::parseBool(const String& val)
|
---|
| 263 | {
|
---|
| 264 | return (val == "true" || val == "yes");
|
---|
| 265 | }
|
---|
| 266 | //-----------------------------------------------------------------------
|
---|
| 267 | Vector3 StringConverter::parseVector3(const String& val)
|
---|
| 268 | {
|
---|
| 269 | // Split on space
|
---|
| 270 | std::vector<String> vec = StringUtil::split(val);
|
---|
| 271 |
|
---|
| 272 | if (vec.size() != 3)
|
---|
| 273 | {
|
---|
| 274 | return Vector3::ZERO;
|
---|
| 275 | }
|
---|
| 276 | else
|
---|
| 277 | {
|
---|
| 278 | return Vector3(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]));
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | }
|
---|
| 282 | //-----------------------------------------------------------------------
|
---|
| 283 | Matrix3 StringConverter::parseMatrix3(const String& val)
|
---|
| 284 | {
|
---|
| 285 | // Split on space
|
---|
| 286 | std::vector<String> vec = StringUtil::split(val);
|
---|
| 287 |
|
---|
| 288 | if (vec.size() != 9)
|
---|
| 289 | {
|
---|
| 290 | return Matrix3::IDENTITY;
|
---|
| 291 | }
|
---|
| 292 | else
|
---|
| 293 | {
|
---|
| 294 | return Matrix3(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]),
|
---|
| 295 | parseReal(vec[3]),parseReal(vec[4]),parseReal(vec[5]),
|
---|
| 296 | parseReal(vec[6]),parseReal(vec[7]),parseReal(vec[8]));
|
---|
| 297 | }
|
---|
| 298 | }
|
---|
| 299 | //-----------------------------------------------------------------------
|
---|
| 300 | Matrix4 StringConverter::parseMatrix4(const String& val)
|
---|
| 301 | {
|
---|
| 302 | // Split on space
|
---|
| 303 | std::vector<String> vec = StringUtil::split(val);
|
---|
| 304 |
|
---|
| 305 | if (vec.size() != 16)
|
---|
| 306 | {
|
---|
| 307 | return Matrix4::IDENTITY;
|
---|
| 308 | }
|
---|
| 309 | else
|
---|
| 310 | {
|
---|
| 311 | return Matrix4(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), parseReal(vec[3]),
|
---|
| 312 | parseReal(vec[4]),parseReal(vec[5]), parseReal(vec[6]), parseReal(vec[7]),
|
---|
| 313 | parseReal(vec[8]),parseReal(vec[9]), parseReal(vec[10]), parseReal(vec[11]),
|
---|
| 314 | parseReal(vec[12]),parseReal(vec[13]), parseReal(vec[14]), parseReal(vec[15]));
|
---|
| 315 | }
|
---|
| 316 | }
|
---|
| 317 | //-----------------------------------------------------------------------
|
---|
| 318 | Quaternion StringConverter::parseQuaternion(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 Quaternion::IDENTITY;
|
---|
| 326 | }
|
---|
| 327 | else
|
---|
| 328 | {
|
---|
| 329 | return Quaternion(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), parseReal(vec[3]));
|
---|
| 330 | }
|
---|
| 331 | }
|
---|
| 332 | //-----------------------------------------------------------------------
|
---|
| 333 | ColourValue StringConverter::parseColourValue(const String& val)
|
---|
| 334 | {
|
---|
| 335 | // Split on space
|
---|
| 336 | std::vector<String> vec = StringUtil::split(val);
|
---|
| 337 |
|
---|
| 338 | if (vec.size() == 4)
|
---|
| 339 | {
|
---|
| 340 | return ColourValue(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), parseReal(vec[3]));
|
---|
| 341 | }
|
---|
| 342 | else if (vec.size() == 3)
|
---|
| 343 | {
|
---|
| 344 | return ColourValue(parseReal(vec[0]),parseReal(vec[1]),parseReal(vec[2]), 1.0f);
|
---|
| 345 | }
|
---|
| 346 | else
|
---|
| 347 | {
|
---|
| 348 | return ColourValue::Black;
|
---|
| 349 | }
|
---|
| 350 | }
|
---|
| 351 | //-----------------------------------------------------------------------
|
---|
| 352 | StringVector StringConverter::parseStringVector(const String& val)
|
---|
| 353 | {
|
---|
| 354 | return StringUtil::split(val);
|
---|
| 355 | }
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 |
|
---|