/***************************************************************************
                String.cpp
                -----------------------------------------
    begin      : 2011/01/07
    copyright  : (C) 2005 SolutionBox Inc.
    author     : Service 1 Team
    email      : svc1@solbox.com

    CopyRight(C) 2005 SolutionBox Inc. All Rights reserved.
    Redistribution and use in source and binary forms, with or with out
    modification, are not permitted in outside of SolutionBox Inc.
***************************************************************************/
#include "String.h"
#include "Logger.h"
#include <stdio.h>
#include <string.h>

using namespace std;

CString::CString()
{
}

CString::~CString()
{
}

string CString::Quotation( const string str )
{
	if( str.empty () )	
	{		
		return "''";	
	}	

	return "'" + str + "'";
}

string CString::IntToStr( int value )
{
	char tmp[32];
	memset( tmp, 0, 32 );

	sprintf( tmp, "%d", value );

	return string( tmp );
}

string CString::LongToStr( long long value )
{
	char tmp[32];
	memset( tmp, 0, 32 );

	sprintf( tmp, "%lld", value );

	return string( tmp );
}

std::string	CString::DoubleToStr( double value, int point )
{
	string format = "%." + IntToStr( point ) + "f";

    char tmp[32];
    memset( tmp, 0, 32 );
    sprintf( tmp, format.c_str(), value );
    
    return string( tmp );
}

string CString::Replace( const string srcStr, const string targetStr, const string replacedStr )
{
	size_t found;	
	size_t pos = 0;	

	string::iterator it;	

	string originSrcString = srcStr;

	for( it = originSrcString.begin(); it != originSrcString.end(); ++it)	
	{		
		found = originSrcString.find(targetStr, pos);

		if( found == string::npos )
		{
			break;
		}

		originSrcString.replace( found, targetStr.length (), replacedStr );

		pos = found + replacedStr.length();
	}	


	return originSrcString;
}

string CString::LTrim( const string srcString, char del )
{
	string originSrcString = srcString;

	string::iterator it = originSrcString.begin();

	while( it != originSrcString.end() )
	{
		if( del != *it  )
		{
			break;
		}   

		originSrcString.erase( it );

	}   

	return originSrcString;  
}   

string CString::RTrim( const string srcString, char del )
{
    string originSrcString = srcString;
    
    string::iterator it = --(originSrcString.end());
    
    while( it >= originSrcString.begin() )
    {
        if( del != *it  )
        {
            break;
        }   
        
        originSrcString.erase( it-- );
    }

    return originSrcString;
}

string CString::Trim( const string srcString )
{
	return LTrim( RTrim( srcString ) );
}

vector<string>  CString::Tokenize( string srcString, string token )
{
    size_t found;
    size_t pos = 0; 
    
    vector<string> vTokendStringList;
    string tokenizedString; 
	size_t tokendStringSize = 0;
    
    string::iterator it;    
    
    for( it = srcString.begin(); it != srcString.end(); ++it)
    {       
        found = srcString.find( token, pos );

        if( found == string::npos )
        {
            break;
        }
        
        tokendStringSize = found - pos;
        tokenizedString = Trim( srcString.substr( pos, tokendStringSize ) );
        vTokendStringList.push_back( tokenizedString );
        pos = found + token.length();
    }   

	if( pos != 0 )
	{
        tokendStringSize = srcString.length() - pos;
		tokenizedString = Trim( srcString.substr( pos, tokendStringSize ) );
        vTokendStringList.push_back( tokenizedString );
	}
	else
	{
        vTokendStringList.push_back( Trim( srcString ) );
	}
    
    return vTokendStringList;
}

