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

stringutils.inl

Go to the documentation of this file.
00001 /*
00002     Sheep - A Rigid Body Dynamics Engine
00003     Copyright (C) 2001-2004 Francois Beaune
00004     Contact: http://toxicengine.sourceforge.net/
00005 
00006     This file is part of Sheep.
00007 
00008     Sheep is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     Sheep is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with Sheep; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 inline
00024 std::string StringUtils::ToLower(const std::string &s) {
00025     std::string t;
00026 
00027     t.reserve(s.size());
00028 
00029     for(std::string::const_iterator i = s.begin(), e = s.end(); i != e; ++i)
00030         t.push_back(tolower(*i));
00031 
00032     return t;
00033 }
00034 
00035 inline
00036 std::string StringUtils::ToUpper(const std::string &s) {
00037     std::string t;
00038 
00039     t.reserve(s.size());
00040 
00041     for(std::string::const_iterator i = s.begin(), e = s.end(); i != e; ++i)
00042         t.push_back(toupper(*i));
00043 
00044     return t;
00045 }
00046 
00047 inline
00048 int StringUtils::CompareNoCase(const std::string &s1, const std::string &s2) {
00049     std::string::const_iterator p1 = s1.begin();
00050     std::string::const_iterator p2 = s2.begin();
00051 
00052     while(p1 != s1.end() && p2 != s2.end()) {
00053         if(toupper(*p1) != toupper(*p2))
00054             return (toupper(*p1) < toupper(*p2)) ? -1 : 1;
00055 
00056         ++p1;
00057         ++p2;
00058     }
00059 
00060     return (s1.size() == s2.size()) ? 0
00061         : (s1.size() < s2.size()) ? -1 : 1; // size is unsigned
00062 }
00063 
00064 template<typename T> inline
00065 T StringUtils::FromString(const std::string &s) {
00066     stringstream ss;
00067     T temp;
00068 
00069     ss << s;
00070     ss >> temp;
00071 
00072     if(ss.fail())
00073         throw ConversionErrorException();
00074 
00075     return temp;
00076 }
00077 
00078 template<typename T> inline
00079 std::string StringUtils::ToString(const T &t) {
00080     stringstream ss;
00081 
00082     ss << t;
00083 
00084     return ss.str();
00085 }
00086 
00087 inline
00088 char *StringUtils::Replicate(const char *s) {
00089     if(!s)
00090         return 0;
00091 
00092     char *t = new char[strlen(s) + 1];
00093 
00094     return t ? strcpy(t, s) : 0;
00095 }
00096 
00097 inline
00098 std::string StringUtils::GetPath(const std::string &s) {
00099     const std::string::size_type i = s.find_last_of("\\/");
00100 
00101     return (i == std::string::npos) ? "./" :
00102         s.substr(0, i + 1);
00103 }
00104 
00105 inline
00106 std::string StringUtils::GetFilename(const std::string &s) {
00107     const std::string::size_type i = s.find_last_of("\\/");
00108 
00109     return (i == std::string::npos) ? s :
00110         s.substr(i + 1, s.length() - i - 1);
00111 }
00112 
00113 inline
00114 std::string StringUtils::GetExtension(const std::string &s) {
00115     const std::string::size_type i = s.find_last_of("\\/.");
00116 
00117     if(i == std::string::npos)
00118         return std::string();   // return empty string
00119 
00120     return (s[i] == '.') ? s.substr(i + 1, s.length() - i - 1) : "";
00121 }
00122 
00123 inline
00124 std::string StringUtils::RemoveExtension(const std::string &s) {
00125     const std::string::size_type i = s.find_last_of("\\/.");
00126 
00127     if(i == std::string::npos || s[i] != '.')
00128         return s;
00129 
00130     return s.substr(0, i);
00131 }

Generated on Tue May 11 01:31:52 2004 for toxic by doxygen 1.3.6