| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693 |
- /*
- -----------------------------------------------------------------------------
- This source file is part of OGRE
- (Object-oriented Graphics Rendering Engine)
- For the latest info, see http://www.ogre3d.org/
- Copyright (c) 2000-2011 Torus Knot Software Ltd
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- -----------------------------------------------------------------------------
- */
- #include "CmString.h"
- #include "CmColor.h"
- #include "CmMath.h"
- #include "CmMatrix3.h"
- #include "CmMatrix4.h"
- #include "CmQuaternion.h"
- #include "CmVector2.h"
- #include "CmVector3.h"
- #include "CmVector4.h"
- #include "CmException.h"
- namespace CamelotFramework {
- //-----------------------------------------------------------------------
- const String StringUtil::BLANK;
- //-----------------------------------------------------------------------
- void StringUtil::trim(String& str, bool left, bool right)
- {
- /*
- size_t lspaces, rspaces, len = length(), i;
- lspaces = rspaces = 0;
- if( left )
- {
- // Find spaces / tabs on the left
- for( i = 0;
- i < len && ( at(i) == ' ' || at(i) == '\t' || at(i) == '\r');
- ++lspaces, ++i );
- }
-
- if( right && lspaces < len )
- {
- // Find spaces / tabs on the right
- for( i = len - 1;
- i >= 0 && ( at(i) == ' ' || at(i) == '\t' || at(i) == '\r');
- rspaces++, i-- );
- }
- *this = substr(lspaces, len-lspaces-rspaces);
- */
- static const String delims = " \t\r";
- if(right)
- str.erase(str.find_last_not_of(delims)+1); // trim right
- if(left)
- str.erase(0, str.find_first_not_of(delims)); // trim left
- }
- //-----------------------------------------------------------------------
- vector<CamelotFramework::String>::type StringUtil::split( const String& str, const String& delims, unsigned int maxSplits)
- {
- vector<CamelotFramework::String>::type ret;
- // Pre-allocate some space for performance
- ret.reserve(maxSplits ? maxSplits+1 : 10); // 10 is guessed capacity for most case
- unsigned int numSplits = 0;
- // Use STL methods
- size_t start, pos;
- start = 0;
- do
- {
- pos = str.find_first_of(delims, start);
- if (pos == start)
- {
- // Do nothing
- start = pos + 1;
- }
- else if (pos == String::npos || (maxSplits && numSplits == maxSplits))
- {
- // Copy the rest of the string
- ret.push_back( str.substr(start) );
- break;
- }
- else
- {
- // Copy up to delimiter
- ret.push_back( str.substr(start, pos - start) );
- start = pos + 1;
- }
- // parse up to next real data
- start = str.find_first_not_of(delims, start);
- ++numSplits;
- } while (pos != String::npos);
- return ret;
- }
- //-----------------------------------------------------------------------
- vector<CamelotFramework::String>::type StringUtil::tokenise( const String& str, const String& singleDelims, const String& doubleDelims, unsigned int maxSplits)
- {
- vector<CamelotFramework::String>::type ret;
- // Pre-allocate some space for performance
- ret.reserve(maxSplits ? maxSplits+1 : 10); // 10 is guessed capacity for most case
- unsigned int numSplits = 0;
- String delims = singleDelims + doubleDelims;
- // Use STL methods
- size_t start, pos;
- char curDoubleDelim = 0;
- start = 0;
- do
- {
- if (curDoubleDelim != 0)
- {
- pos = str.find(curDoubleDelim, start);
- }
- else
- {
- pos = str.find_first_of(delims, start);
- }
- if (pos == start)
- {
- char curDelim = str.at(pos);
- if (doubleDelims.find_first_of(curDelim) != String::npos)
- {
- curDoubleDelim = curDelim;
- }
- // Do nothing
- start = pos + 1;
- }
- else if (pos == String::npos || (maxSplits && numSplits == maxSplits))
- {
- if (curDoubleDelim != 0)
- {
- //Missing closer. Warn or throw exception?
- }
- // Copy the rest of the string
- ret.push_back( str.substr(start) );
- break;
- }
- else
- {
- if (curDoubleDelim != 0)
- {
- curDoubleDelim = 0;
- }
- // Copy up to delimiter
- ret.push_back( str.substr(start, pos - start) );
- start = pos + 1;
- }
- if (curDoubleDelim == 0)
- {
- // parse up to next real data
- start = str.find_first_not_of(singleDelims, start);
- }
-
- ++numSplits;
- } while (pos != String::npos);
- return ret;
- }
- //-----------------------------------------------------------------------
- void StringUtil::toLowerCase(String& str)
- {
- std::transform(
- str.begin(),
- str.end(),
- str.begin(),
- tolower);
- }
- //-----------------------------------------------------------------------
- void StringUtil::toUpperCase(String& str)
- {
- std::transform(
- str.begin(),
- str.end(),
- str.begin(),
- toupper);
- }
- //-----------------------------------------------------------------------
- bool StringUtil::startsWith(const String& str, const String& pattern, bool lowerCase)
- {
- size_t thisLen = str.length();
- size_t patternLen = pattern.length();
- if (thisLen < patternLen || patternLen == 0)
- return false;
- String startOfThis = str.substr(0, patternLen);
- if (lowerCase)
- StringUtil::toLowerCase(startOfThis);
- return (startOfThis == pattern);
- }
- //-----------------------------------------------------------------------
- bool StringUtil::endsWith(const String& str, const String& pattern, bool lowerCase)
- {
- size_t thisLen = str.length();
- size_t patternLen = pattern.length();
- if (thisLen < patternLen || patternLen == 0)
- return false;
- String endOfThis = str.substr(thisLen - patternLen, patternLen);
- if (lowerCase)
- StringUtil::toLowerCase(endOfThis);
- return (endOfThis == pattern);
- }
- //-----------------------------------------------------------------------
- String StringUtil::standardisePath(const String& init)
- {
- String path = init;
- std::replace( path.begin(), path.end(), '\\', '/' );
- if( path[path.length() - 1] != '/' )
- path += '/';
- return path;
- }
- //-----------------------------------------------------------------------
- void StringUtil::splitFilename(const String& qualifiedName,
- String& outBasename, String& outPath)
- {
- String path = qualifiedName;
- // Replace \ with / first
- std::replace( path.begin(), path.end(), '\\', '/' );
- // split based on final /
- size_t i = path.find_last_of('/');
- if (i == String::npos)
- {
- outPath.clear();
- outBasename = qualifiedName;
- }
- else
- {
- outBasename = path.substr(i+1, path.size() - i - 1);
- outPath = path.substr(0, i+1);
- }
- }
- //-----------------------------------------------------------------------
- void StringUtil::splitBaseFilename(const CamelotFramework::String& fullName,
- CamelotFramework::String& outBasename, CamelotFramework::String& outExtention)
- {
- size_t i = fullName.find_last_of(".");
- if (i == CamelotFramework::String::npos)
- {
- outExtention.clear();
- outBasename = fullName;
- }
- else
- {
- outExtention = fullName.substr(i+1);
- outBasename = fullName.substr(0, i);
- }
- }
- // ----------------------------------------------------------------------------------------------------------------------------------------------
- void StringUtil::splitFullFilename( const CamelotFramework::String& qualifiedName,
- CamelotFramework::String& outBasename, CamelotFramework::String& outExtention, CamelotFramework::String& outPath )
- {
- CamelotFramework::String fullName;
- splitFilename( qualifiedName, fullName, outPath );
- splitBaseFilename( fullName, outBasename, outExtention );
- }
- //-----------------------------------------------------------------------
- bool StringUtil::match(const String& str, const String& pattern, bool caseSensitive)
- {
- String tmpStr = str;
- String tmpPattern = pattern;
- if (!caseSensitive)
- {
- StringUtil::toLowerCase(tmpStr);
- StringUtil::toLowerCase(tmpPattern);
- }
- String::const_iterator strIt = tmpStr.begin();
- String::const_iterator patIt = tmpPattern.begin();
- String::const_iterator lastWildCardIt = tmpPattern.end();
- while (strIt != tmpStr.end() && patIt != tmpPattern.end())
- {
- if (*patIt == '*')
- {
- lastWildCardIt = patIt;
- // Skip over looking for next character
- ++patIt;
- if (patIt == tmpPattern.end())
- {
- // Skip right to the end since * matches the entire rest of the string
- strIt = tmpStr.end();
- }
- else
- {
- // scan until we find next pattern character
- while(strIt != tmpStr.end() && *strIt != *patIt)
- ++strIt;
- }
- }
- else
- {
- if (*patIt != *strIt)
- {
- if (lastWildCardIt != tmpPattern.end())
- {
- // The last wildcard can match this incorrect sequence
- // rewind pattern to wildcard and keep searching
- patIt = lastWildCardIt;
- lastWildCardIt = tmpPattern.end();
- }
- else
- {
- // no wildwards left
- return false;
- }
- }
- else
- {
- ++patIt;
- ++strIt;
- }
- }
- }
- // If we reached the end of both the pattern and the string, we succeeded
- if (patIt == tmpPattern.end() && strIt == tmpStr.end())
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //-----------------------------------------------------------------------
- const String StringUtil::replaceAll(const String& source, const String& replaceWhat, const String& replaceWithWhat)
- {
- String result = source;
- String::size_type pos = 0;
- while(1)
- {
- pos = result.find(replaceWhat,pos);
- if (pos == String::npos) break;
- result.replace(pos,replaceWhat.size(),replaceWithWhat);
- pos += replaceWithWhat.size();
- }
- return result;
- }
- //-----------------------------------------------------------------------
- String toString(float val, unsigned short precision,
- unsigned short width, char fill, std::ios::fmtflags flags)
- {
- stringstream stream;
- stream.precision(precision);
- stream.width(width);
- stream.fill(fill);
- if (flags)
- stream.setf(flags);
- stream << val;
- return stream.str();
- }
- //-----------------------------------------------------------------------
- String toString(Radian val, unsigned short precision,
- unsigned short width, char fill, std::ios::fmtflags flags)
- {
- return toString(val.valueAngleUnits(), precision, width, fill, flags);
- }
- //-----------------------------------------------------------------------
- String toString(Degree val, unsigned short precision,
- unsigned short width, char fill, std::ios::fmtflags flags)
- {
- return toString(val.valueAngleUnits(), precision, width, fill, flags);
- }
- //-----------------------------------------------------------------------
- String toString(int val,
- unsigned short width, char fill, std::ios::fmtflags flags)
- {
- stringstream stream;
- stream.width(width);
- stream.fill(fill);
- if (flags)
- stream.setf(flags);
- stream << val;
- return stream.str();
- }
- //-----------------------------------------------------------------------
- #if CM_ARCH_TYPE == CM_ARCHITECTURE_64 || CM_PLATFORM == CM_PLATFORM_APPLE
- String toString(unsigned int val,
- unsigned short width, char fill, std::ios::fmtflags flags)
- {
- stringstream stream;
- stream.width(width);
- stream.fill(fill);
- if (flags)
- stream.setf(flags);
- stream << val;
- return stream.str();
- }
- //-----------------------------------------------------------------------
- String toString(size_t val,
- unsigned short width, char fill, std::ios::fmtflags flags)
- {
- stringstream stream;
- stream.width(width);
- stream.fill(fill);
- if (flags)
- stream.setf(flags);
- stream << val;
- return stream.str();
- }
- #if CM_COMPILER == CM_COMPILER_MSVC
- //-----------------------------------------------------------------------
- String toString(unsigned long val,
- unsigned short width, char fill, std::ios::fmtflags flags)
- {
- stringstream stream;
- stream.width(width);
- stream.fill(fill);
- if (flags)
- stream.setf(flags);
- stream << val;
- return stream.str();
- }
- #endif
- //-----------------------------------------------------------------------
- #else
- String toString(size_t val,
- unsigned short width, char fill, std::ios::fmtflags flags)
- {
- stringstream stream;
- stream.width(width);
- stream.fill(fill);
- if (flags)
- stream.setf(flags);
- stream << val;
- return stream.str();
- }
- //-----------------------------------------------------------------------
- String toString(unsigned long val,
- unsigned short width, char fill, std::ios::fmtflags flags)
- {
- stringstream stream;
- stream.width(width);
- stream.fill(fill);
- if (flags)
- stream.setf(flags);
- stream << val;
- return stream.str();
- }
- //-----------------------------------------------------------------------
- String toString(unsigned long long int val,
- unsigned short width, char fill, std::ios::fmtflags flags)
- {
- stringstream stream;
- stream.width(width);
- stream.fill(fill);
- if (flags)
- stream.setf(flags);
- stream << val;
- return stream.str();
- }
- //-----------------------------------------------------------------------
- #endif
- String toString(long val,
- unsigned short width, char fill, std::ios::fmtflags flags)
- {
- stringstream stream;
- stream.width(width);
- stream.fill(fill);
- if (flags)
- stream.setf(flags);
- stream << val;
- return stream.str();
- }
- //-----------------------------------------------------------------------
- String toString(const Vector2& val)
- {
- stringstream stream;
- stream << val.x << " " << val.y;
- return stream.str();
- }
- //-----------------------------------------------------------------------
- String toString(const Vector3& val)
- {
- stringstream stream;
- stream << val.x << " " << val.y << " " << val.z;
- return stream.str();
- }
- //-----------------------------------------------------------------------
- String toString(const Vector4& val)
- {
- stringstream stream;
- stream << val.x << " " << val.y << " " << val.z << " " << val.w;
- return stream.str();
- }
- //-----------------------------------------------------------------------
- String toString(const Matrix3& val)
- {
- stringstream stream;
- stream << val[0][0] << " "
- << val[0][1] << " "
- << val[0][2] << " "
- << val[1][0] << " "
- << val[1][1] << " "
- << val[1][2] << " "
- << val[2][0] << " "
- << val[2][1] << " "
- << val[2][2];
- return stream.str();
- }
- //-----------------------------------------------------------------------
- String toString(bool val, bool yesNo)
- {
- if (val)
- {
- if (yesNo)
- {
- return "yes";
- }
- else
- {
- return "true";
- }
- }
- else
- if (yesNo)
- {
- return "no";
- }
- else
- {
- return "false";
- }
- }
- //-----------------------------------------------------------------------
- String toString(const Matrix4& val)
- {
- stringstream stream;
- stream << val[0][0] << " "
- << val[0][1] << " "
- << val[0][2] << " "
- << val[0][3] << " "
- << val[1][0] << " "
- << val[1][1] << " "
- << val[1][2] << " "
- << val[1][3] << " "
- << val[2][0] << " "
- << val[2][1] << " "
- << val[2][2] << " "
- << val[2][3] << " "
- << val[3][0] << " "
- << val[3][1] << " "
- << val[3][2] << " "
- << val[3][3];
- return stream.str();
- }
- //-----------------------------------------------------------------------
- String toString(const Quaternion& val)
- {
- stringstream stream;
- stream << val.w << " " << val.x << " " << val.y << " " << val.z;
- return stream.str();
- }
- //-----------------------------------------------------------------------
- String toString(const Color& val)
- {
- stringstream stream;
- stream << val.r << " " << val.g << " " << val.b << " " << val.a;
- return stream.str();
- }
- //-----------------------------------------------------------------------
- String toString(const std::vector<CamelotFramework::String>& val)
- {
- stringstream stream;
- std::vector<CamelotFramework::String>::const_iterator i, iend, ibegin;
- ibegin = val.begin();
- iend = val.end();
- for (i = ibegin; i != iend; ++i)
- {
- if (i != ibegin)
- stream << " ";
- stream << *i;
- }
- return stream.str();
- }
- //-----------------------------------------------------------------------
- float parseFloat(const String& val, float defaultValue)
- {
- // Use istringstream for direct correspondence with toString
- StringStream str(val);
- float ret = defaultValue;
- str >> ret;
- return ret;
- }
- //-----------------------------------------------------------------------
- int parseInt(const String& val, int defaultValue)
- {
- // Use istringstream for direct correspondence with toString
- StringStream str(val);
- int ret = defaultValue;
- str >> ret;
- return ret;
- }
- //-----------------------------------------------------------------------
- unsigned int parseUnsignedInt(const String& val, unsigned int defaultValue)
- {
- // Use istringstream for direct correspondence with toString
- StringStream str(val);
- unsigned int ret = defaultValue;
- str >> ret;
- return ret;
- }
- //-----------------------------------------------------------------------
- long parseLong(const String& val, long defaultValue)
- {
- // Use istringstream for direct correspondence with toString
- StringStream str(val);
- long ret = defaultValue;
- str >> ret;
- return ret;
- }
- //-----------------------------------------------------------------------
- unsigned long parseUnsignedLong(const String& val, unsigned long defaultValue)
- {
- // Use istringstream for direct correspondence with toString
- StringStream str(val);
- unsigned long ret = defaultValue;
- str >> ret;
- return ret;
- }
- //-----------------------------------------------------------------------
- bool parseBool(const String& val, bool defaultValue)
- {
- if ((StringUtil::startsWith(val, "true") || StringUtil::startsWith(val, "yes")
- || StringUtil::startsWith(val, "1")))
- return true;
- else if ((StringUtil::startsWith(val, "false") || StringUtil::startsWith(val, "no")
- || StringUtil::startsWith(val, "0")))
- return false;
- else
- return defaultValue;
- }
- //-----------------------------------------------------------------------
- vector<CamelotFramework::String>::type parseStringVector(const String& val)
- {
- return StringUtil::split(val);
- }
- //-----------------------------------------------------------------------
- bool isNumber(const String& val)
- {
- StringStream str(val);
- float tst;
- str >> tst;
- return !str.fail() && str.eof();
- }
- //----------------------------------------------------------------------
- void __string_throwDataOverflowException()
- {
- CM_EXCEPT(InternalErrorException, "Data overflow! Size doesn't fit into 32 bits.");
- }
- }
|