KeosString.cpp

Go to the documentation of this file.
00001 /*
00002  * This source file is part of KEOS (Free 3D Engine)
00003  * For the latest info, see http://www.keosengine.org/
00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
00005  *
00006  * This program is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU Lesser General Public License as published by the Free Software
00008  * Foundation; either version 2 of the License, or (at your option) any later
00009  * version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00018  * http://www.gnu.org/copyleft/lesser.txt.
00019  *
00020  */
00021 
00022 #include "KeosString.h"
00023 
00024 namespace Keos
00025 {
00026   //=======================================================================
00027   // CStringUtil implementation
00028   //=======================================================================
00029 
00030   const String CStringUtil::BLANK = String("");
00031 
00032   //-----------------------------------------------------------------------
00033   void CStringUtil::Trim(String& str, bool bLeft, bool bRight)
00034   {
00035     static const String strDelims = " \t\r";
00036     if (bRight)
00037       str.erase(str.find_last_not_of(strDelims) + 1); // trim right
00038     if (bLeft)
00039       str.erase(0, str.find_first_not_of(strDelims)); // trim left
00040   }
00041 
00042   //-----------------------------------------------------------------------
00043   void CStringUtil::Split(const String& strString, std::vector<String>& Tokens, const String& strDelim)
00044   {
00045     Tokens.clear();
00046 
00047     for (String::size_type p1 = 0, p2 = 0; p1 != String::npos; )
00048     {
00049       p1 = strString.find_first_not_of(strDelim, p1);
00050       if (p1 != String::npos)
00051       {
00052         p2 = strString.find_first_of(strDelim , p1);
00053         Tokens.push_back(strString.substr(p1, p2 - p1));
00054         p1 = p2;
00055       }
00056     }
00057   }
00058 
00059   //-----------------------------------------------------------------------
00060   void CStringUtil::ToLowerCase(String& str)
00061   {
00062     std::transform(
00063       str.begin(),
00064       str.end(),
00065       str.begin(),
00066       tolower);
00067   }
00068 
00069   //-----------------------------------------------------------------------
00070   void CStringUtil::ToUpperCase(String& str)
00071   {
00072     std::transform(
00073       str.begin(),
00074       str.end(),
00075       str.begin(),
00076       toupper);
00077   }
00078 
00079   //-----------------------------------------------------------------------
00080   String CStringUtil::ToLower(const String& strText)
00081   {
00082     String strRet(strText.size(), ' ');
00083 
00084     std::transform(strText.begin(), strText.end(), strRet.begin(), std::tolower);
00085 
00086     return strRet;
00087   }
00088 
00089   //-----------------------------------------------------------------------
00090   String CStringUtil::ToUpper(const String& strText)
00091   {
00092     String strRet(strText.size(), ' ');
00093 
00094     std::transform(strText.begin(), strText.end(), strRet.begin(), std::toupper);
00095 
00096     return strRet;
00097   }
00098 
00099 
00100   //=======================================================================
00101   // CStringConverter implementation
00102   //=======================================================================
00103 
00104   //-----------------------------------------------------------------------
00105   String CStringConverter::ToString(float fVal, ushort nPrecision,
00106                                     ushort nWidth, char fill, std::ios::fmtflags flags)
00107   {
00108     CStringUtil::StrStreamType stream;
00109     stream.precision(nPrecision);
00110     stream.width(nWidth);
00111     stream.fill(fill);
00112     if (flags)
00113       stream.setf(flags);
00114     stream << fVal;
00115     return stream.str();
00116   }
00117 
00118   //-----------------------------------------------------------------------
00119   String CStringConverter::ToString(int nVal,
00120                                     unsigned short nWidth, char fill, std::ios::fmtflags flags)
00121   {
00122     CStringUtil::StrStreamType stream;
00123     stream.width(nWidth);
00124     stream.fill(fill);
00125     if (flags)
00126       stream.setf(flags);
00127     stream << nVal;
00128     return stream.str();
00129   }
00130 
00131   //-----------------------------------------------------------------------
00132   String CStringConverter::ToString(bool bVal, bool bYesNo)
00133   {
00134     if (bVal)
00135     {
00136       if (bYesNo)
00137         return "yes";
00138       else
00139         return "true";
00140     }
00141     else
00142     {
00143       if (bYesNo)
00144         return "no";
00145       else
00146         return "false";
00147     }
00148   }
00149 
00150   //-----------------------------------------------------------------------
00151   Real CStringConverter::ParseReal(const String& strVal)
00152   {
00153     return atof(strVal.c_str());
00154   }
00155 
00156   //-----------------------------------------------------------------------
00157   int CStringConverter::ParseInt(const String& strVal)
00158   {
00159     return atoi(strVal.c_str());
00160   }
00161 
00162   //-----------------------------------------------------------------------
00163   uint CStringConverter::ParseUnsignedInt(const String& strVal)
00164   {
00165     return static_cast<uint>(strtoul(strVal.c_str(), 0, 10));
00166   }
00167 
00168   //-----------------------------------------------------------------------
00169   long CStringConverter::ParseLong(const String& strVal)
00170   {
00171     return strtol(strVal.c_str(), 0, 10);
00172   }
00173 
00174   //-----------------------------------------------------------------------
00175   ulong CStringConverter::ParseUnsignedLong(const String& strVal)
00176   {
00177     return strtoul(strVal.c_str(), 0, 10);
00178   }
00179 
00180   //-----------------------------------------------------------------------
00181   bool CStringConverter::ParseBool(const String& strVal)
00182   {
00183     return (strVal == "true" || strVal == "yes");
00184   }
00185 }
00186 
00187 

Generated on Fri Mar 9 14:29:03 2007 for Keos by  doxygen 1.5.1-p1