Back to index

supertuxkart  0.5+dfsg1
string_utils.hpp
Go to the documentation of this file.
00001 //  $Id: string_utils.hpp 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 #ifndef HEADER_STRING_UTILS_H
00022 #define HEADER_STRING_UTILS_H
00023 
00024 #include <string>
00025 #include <vector>
00026 #include <sstream>
00027 
00028 namespace StringUtils
00029 {
00030 
00031     bool has_suffix(const std::string& lhs, const std::string rhs);
00032 
00034     std::string basename(const std::string& filename);
00035 
00037     std::string path(const std::string& filename);
00038 
00039     std::string without_extension(const std::string& filename);
00040     std::string extension(const std::string& filename);
00041 
00042     template <class T>
00043     std::string to_string (const T& any)
00044     {
00045         std::ostringstream oss;
00046         oss << any ;
00047         return oss.str();
00048     }
00049 
00053     template <class T>
00054     bool from_string(const std::string& rep, T& x)
00055     {
00056         // this is necessary so that if "x" is not modified if the conversion fails
00057         T temp;
00058         std::istringstream iss(rep);
00059         iss >> temp;
00060 
00061         if (iss.fail())
00062         {
00063             return false;
00064         }
00065         else
00066         {
00067             x = temp;
00068             return true;
00069         }
00070     }
00071 
00072     std::string upcase (const std::string&);
00073     std::string downcase (const std::string&);
00074     std::vector<std::string> split(const std::string& s, char c);
00075 
00076 } // namespace StringUtils
00077 
00078 #endif
00079 
00080 /* EOF */