/*
 * This source file is part of KEOS (Free 3D Engine)
 * For the latest info, see http://www.keosengine.org/
 * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
 * http://www.gnu.org/copyleft/lesser.txt.
 *
 */

#include "KeosString.h"

namespace Keos 
{
    //=======================================================================
    // CStringUtil implementation
    //=======================================================================
    
	const String CStringUtil::BLANK = String("");
	
    //-----------------------------------------------------------------------
    void CStringUtil::Trim(String& str, bool bLeft, bool bRight)
    {
        static const String strDelims = " \t\r";
        if(bRight)
            str.erase(str.find_last_not_of(strDelims)+1); // trim right
        if(bLeft)
            str.erase(0, str.find_first_not_of(strDelims)); // trim left
    }

    //-----------------------------------------------------------------------
    void CStringUtil::Split(const String& strString, std::vector<String>& Tokens, const String& strDelim)
    {
        Tokens.clear();

        for (String::size_type p1 = 0, p2 = 0; p1 != String::npos; )
        {
            p1 = strString.find_first_not_of(strDelim, p1);
            if (p1 != String::npos)
            {
                p2 = strString.find_first_of(strDelim , p1);
                Tokens.push_back(strString.substr(p1, p2 - p1));
                p1 = p2;
            }
        }
    }

    //-----------------------------------------------------------------------
    void CStringUtil::ToLowerCase(String& str)
    {
        std::transform(
            str.begin(),
            str.end(),
            str.begin(),
			tolower);
    }

    //-----------------------------------------------------------------------
    void CStringUtil::ToUpperCase(String& str) 
    {
        std::transform(
            str.begin(),
            str.end(),
            str.begin(),
			toupper);
    }

    //-----------------------------------------------------------------------
    String CStringUtil::ToLower(const String& strText)
    {
        String strRet(strText.size(), ' ');

        std::transform(strText.begin(), strText.end(), strRet.begin(), std::tolower);

        return strRet;
    }

    //-----------------------------------------------------------------------
    String CStringUtil::ToUpper(const String& strText)
    {
        String strRet(strText.size(), ' ');

        std::transform(strText.begin(), strText.end(), strRet.begin(), std::toupper);

        return strRet;
    }
    

    //=======================================================================
    // CStringConverter implementation
    //=======================================================================
    
    //-----------------------------------------------------------------------
    String CStringConverter::ToString(float fVal, ushort nPrecision, 
        ushort nWidth, char fill, std::ios::fmtflags flags)
    {
        CStringUtil::StrStreamType stream;
        stream.precision(nPrecision);
        stream.width(nWidth);
        stream.fill(fill);
        if (flags)
            stream.setf(flags);
        stream << fVal;
        return stream.str();
    }

	//-----------------------------------------------------------------------
    String CStringConverter::ToString(int nVal, 
        unsigned short nWidth, char fill, std::ios::fmtflags flags)
    {
        CStringUtil::StrStreamType stream;
        stream.width(nWidth);
        stream.fill(fill);
        if (flags)
            stream.setf(flags);
        stream << nVal;
        return stream.str();
    }

    //-----------------------------------------------------------------------
    String CStringConverter::ToString(bool bVal, bool bYesNo)
    {
        if (bVal)
        {
            if (bYesNo)
                return "yes";
            else
                return "true";
        }
        else {
            if (bYesNo)
                return "no";
            else
                return "false";
        }
    }

    //-----------------------------------------------------------------------
    Real CStringConverter::ParseReal(const String& strVal)
    {
        return atof(strVal.c_str());
    }

    //-----------------------------------------------------------------------
    int CStringConverter::ParseInt(const String& strVal)
    {
        return atoi(strVal.c_str());
    }

    //-----------------------------------------------------------------------
    uint CStringConverter::ParseUnsignedInt(const String& strVal)
    {
        return static_cast<uint>(strtoul(strVal.c_str(), 0, 10));
    }

    //-----------------------------------------------------------------------
    long CStringConverter::ParseLong(const String& strVal)
    {
        return strtol(strVal.c_str(), 0, 10);
    }

    //-----------------------------------------------------------------------
    ulong CStringConverter::ParseUnsignedLong(const String& strVal)
    {
        return strtoul(strVal.c_str(), 0, 10);
    }

    //-----------------------------------------------------------------------
    bool CStringConverter::ParseBool(const String& strVal)
    {
        return (strVal == "true" || strVal == "yes");
    }
}


