Back to index

supertuxkart  0.5+dfsg1
string_utils.cpp
Go to the documentation of this file.
00001 //  $Id: string_utils.cpp 2111 2008-05-31 07:04:30Z cosmosninja $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2004 Steve Baker <sjbaker1@airmail.net>,
00005 //                     Ingo Ruhnke <grumbel@gmx.de>
00006 //
00007 //  This program is free software; you can redistribute it and/or
00008 //  modify it under the terms of the GNU General Public License
00009 //  as published by the Free Software Foundation; either version 2
00010 //  of the License, or (at your option) any later version.
00011 //
00012 //  This program is distributed in the hope that it will be useful,
00013 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //  GNU General Public License for more details.
00016 //
00017 //  You should have received a copy of the GNU General Public License
00018 //  along with this program; if not, write to the Free Software
00019 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 
00021 #include <algorithm>
00022 #include <cstring>
00023 #include "string_utils.hpp"
00024 
00025 namespace StringUtils
00026 {
00027     bool has_suffix(const std::string& lhs, const std::string rhs)
00028     {
00029         if (lhs.length() < rhs.length())
00030             return false;
00031         else
00032             // While this is basically correct, it fails with older
00033             // g++ versions (at least 2.95.3), which have a wrong template. To
00034             // avoid this issue, a more C-traditional way is used.
00035             return strcmp(lhs.c_str()+(lhs.length()-rhs.length()), rhs.c_str())==0;
00036     }
00037 
00038 //--------------------------i---------------------------------------------------
00039     std::string path(const std::string& filename)
00040     {
00041         for(int i = int(filename.size()) - 1; i >= 0; --i)
00042         {
00043             if (filename[i]=='/' || filename[i]=='\\')
00044             {
00045                 return filename.substr(0,i);
00046             }
00047         }
00048         return "";
00049     }
00050 
00051 //-----------------------------------------------------------------------------
00052     std::string basename(const std::string& filename)
00053     {
00054         for(int i = int(filename.size()) - 1; i >= 0; --i)
00055         {
00056             if (filename[i]=='/' || filename[i]=='\\')
00057             {
00058                 return filename.substr(i+1);
00059             }
00060         }
00061         return filename;
00062     }
00063 
00064 //-----------------------------------------------------------------------------
00065     std::string without_extension(const std::string& filename)
00066     {
00067         for(int i = int(filename.size()) - 1; i >= 0; --i)
00068         {
00069             if (filename[i] == '.')
00070             {
00071                 return filename.substr(0, i);
00072             }
00073         }
00074         return filename;
00075     }
00076 
00077 //-----------------------------------------------------------------------------
00078     std::string extension(const std::string& filename)
00079     {
00080         for(int i = int(filename.size()) - 1; i >= 0; --i)
00081         {
00082             if (filename[i] == '.')
00083             {
00084                 return filename.substr(i+1);
00085             }
00086         }
00087         return filename;
00088     }
00089 
00090 //-----------------------------------------------------------------------------
00091     std::string upcase (const std::string& str)
00092     {
00093         std::string name = str;
00094         std::transform(name.begin(), name.end(), name.begin(), ::toupper);
00095         return name;
00096     }
00097 
00098 //-----------------------------------------------------------------------------
00099     std::string downcase (const std::string& str)
00100     {
00101         std::string name = str;
00102         std::transform(name.begin(), name.end(), name.begin(), ::tolower);
00103         return name;
00104     }
00105 //-----------------------------------------------------------------------------
00106 // Splits a string into substrings separated by a certain character, and 
00107 // returns a std::vector of all those substring. E.g.:
00108 //  split("a b=c d=e",' ')  --> ["a", "b=c", "d=e"]
00109     std::vector<std::string> split(const std::string& s, char c)
00110     {
00111         std::vector<std::string> result;
00112     
00113         std::string::size_type start=0;
00114         while(start!=std::string::npos)
00115         {
00116             std::string::size_type i=s.find(c, start);
00117             if(i!=std::string::npos)
00118             {
00119                 result.push_back(std::string(s,start, i-start));
00120                 start=i+1;
00121             } 
00122             else
00123             {
00124                 result.push_back(std::string(s,start));
00125                 start = i;
00126             }
00127         }
00128         return result;
00129     }
00130 
00131 } // namespace StringUtils
00132 
00133 /* EOF */