/* 
 * Copyright (C) 2012 Yee Young Han <websearch@naver.com> (http://blog.naver.com/websearch)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 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 General Public License for more details.
 *
 * You should have received a copy of the GNU 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 
 */

#include "SipPlatformDefine.h"
#include "SipUdp.h"
#include "MonitorString.h"
#include "TimeString.h"
#include "MemoryDebug.h"

CMonitorString::CMonitorString()
{
}

CMonitorString::~CMonitorString()
{
}

/**
 * @ingroup SipStack
 * @brief ÄÃ·³ ¹®ÀÚ¿­À» ÀúÀåÇÑ´Ù.
 * @param pszValue ¹®ÀÚ¿­
 */
void CMonitorString::AddCol( const char * pszValue )
{
	m_strBuf.append( pszValue );
	m_strBuf.append( MR_COL_SEP );
}

/**
 * @ingroup SipStack
 * @brief ÄÃ·³ ¹®ÀÚ¿­À» ÀúÀåÇÑ´Ù.
 * @param strValue ¹®ÀÚ¿­
 */
void CMonitorString::AddCol( const std::string & strValue )
{
	m_strBuf.append( strValue );
	m_strBuf.append( MR_COL_SEP );
}

/**
 * @ingroup SipStack
 * @brief ÄÃ·³ ¹®ÀÚ¿­¿¡ IP ÁÖ¼Ò¿Í Æ÷Æ® ¹øÈ£¸¦ ÀúÀåÇÑ´Ù.
 * @param strIp IP ÁÖ¼Ò
 * @param iPort Æ÷Æ® ¹øÈ£
 */
void CMonitorString::AddCol( const std::string & strIp, int iPort )
{
	m_strBuf.append( strIp );
	m_strBuf.append( ":" );
	AddCol( iPort );
}

/**
 * @ingroup SipStack
 * @brief ÄÃ·³ ¹®ÀÚ¿­¿¡ IP ÁÖ¼Ò¿Í Æ÷Æ® ¹øÈ£¸¦ ÀúÀåÇÑ´Ù.
 * @param iIp		IP ÁÖ¼Ò
 * @param iPort Æ÷Æ® ¹øÈ£
 */
void CMonitorString::AddCol( uint32_t iIp, uint16_t iPort )
{
	char	szTemp[25];

	snprintf( szTemp, sizeof(szTemp), "%d.%d.%d.%d:%d", (iIp)&0xFF, (iIp>>8)&0xFF, (iIp>>16)&0xFF, (iIp>>24)&0xFF, ntohs(iPort) );
	AddCol( szTemp );
}

/**
 * @ingroup SipStack
 * @brief ÄÃ·³ ¹®ÀÚ¿­¿¡ ¼ýÀÚ¸¦ ÀúÀåÇÑ´Ù.
 * @param iValue ¼ýÀÚ
 */
void CMonitorString::AddCol( int iValue )
{
	char	szValue[21];

	snprintf( szValue, sizeof(szValue), "%d", iValue );
	AddCol( szValue );
}

/**
 * @ingroup SipStack
 * @brief ÄÃ·³ ¹®ÀÚ¿­¿¡ ¼ýÀÚ¸¦ ÀúÀåÇÑ´Ù.
 * @param iValue ¼ýÀÚ
 */
void CMonitorString::AddCol( uint32_t iValue )
{
	char	szValue[21];

	snprintf( szValue, sizeof(szValue), "%u", iValue );
	AddCol( szValue );
}

/**
 * @ingroup SipStack
 * @brief ÄÃ·³ ¹®ÀÚ¿­¿¡ ½Ã°£ ¹®ÀÚ¿­À» ÀúÀåÇÑ´Ù.
 * @param iTime ½Ã°£
 */
void CMonitorString::AddCol( time_t iTime )
{
	char	szTime[21];

	GetDateTimeString( iTime, szTime, sizeof(szTime) );
	AddCol( szTime );
}

/**
 * @ingroup SipStack
 * @brief ÄÃ·³ ¹®ÀÚ¿­¿¡ true/false ¸¦ ÀúÀåÇÑ´Ù.
 * @param bValue true/false
 */
void CMonitorString::AddCol( bool bValue )
{
	AddCol( bValue ? "true" : "false" );
}

/**
 * @ingroup SipStack
 * @brief ÁÙÀÇ ¸¶Áö¸· ¹®ÀÚ¿­À» ÀúÀåÇÑ´Ù.
 * @param pszValue ¹®ÀÚ¿­
 */
void CMonitorString::AddRow( const char * pszValue )
{
	m_strBuf.append( pszValue );
	m_strBuf.append( MR_ROW_SEP );
}

/**
 * @ingroup SipStack
 * @brief ÁÙÀÇ ¸¶Áö¸· ¹®ÀÚ¿­À» ÀúÀåÇÑ´Ù.
 * @param strValue ¹®ÀÚ¿­
 */
void CMonitorString::AddRow( const std::string & strValue )
{
	m_strBuf.append( strValue );
	m_strBuf.append( MR_ROW_SEP );
}

/**
 * @ingroup SipStack
 * @brief ÁÙÀÇ ¸¶Áö¸· ¹®ÀÚ¿­¿¡ IP ÁÖ¼Ò¿Í Æ÷Æ® ¹øÈ£¸¦ ÀúÀåÇÑ´Ù.
 * @param strIp IP ÁÖ¼Ò
 * @param iPort Æ÷Æ® ¹øÈ£
 */
void CMonitorString::AddRow( const std::string & strIp, int iPort )
{
	m_strBuf.append( strIp );
	m_strBuf.append( ":" );
	AddRow( iPort );
}

/**
 * @ingroup SipStack
 * @brief ÁÙÀÇ ¸¶Áö¸· ¹®ÀÚ¿­¿¡ IP ÁÖ¼Ò¿Í Æ÷Æ® ¹øÈ£¸¦ ÀúÀåÇÑ´Ù.
 * @param iIp		IP ÁÖ¼Ò
 * @param iPort Æ÷Æ® ¹øÈ£
 */
void CMonitorString::AddRow( uint32_t iIp, uint16_t iPort )
{
	char	szTemp[25];

	snprintf( szTemp, sizeof(szTemp), "%d.%d.%d.%d:%d", (iIp)&0xFF, (iIp>>8)&0xFF, (iIp>>16)&0xFF, (iIp>>24)&0xFF, ntohs(iPort) );
	AddRow( szTemp );
}

/**
 * @ingroup SipStack
 * @brief ÁÙÀÇ ¸¶Áö¸· ¹®ÀÚ¿­¿¡ ¼ýÀÚ¸¦ ÀúÀåÇÑ´Ù.
 * @param iValue ¼ýÀÚ
 */
void CMonitorString::AddRow( int iValue )
{
	char	szValue[21];

	snprintf( szValue, sizeof(szValue), "%d", iValue );
	AddRow( szValue );
}

/**
 * @ingroup SipStack
 * @brief ÁÙÀÇ ¸¶Áö¸· ¹®ÀÚ¿­¿¡ ¼ýÀÚ¸¦ ÀúÀåÇÑ´Ù.
 * @param iValue ¼ýÀÚ
 */
void CMonitorString::AddRow( uint32_t iValue )
{
	char	szValue[21];

	snprintf( szValue, sizeof(szValue), "%u", iValue );
	AddRow( szValue );
}

/**
 * @ingroup SipStack
 * @brief ÁÙÀÇ ¸¶Áö¸· ¹®ÀÚ¿­¿¡ ½Ã°£ ¹®ÀÚ¿­À» ÀúÀåÇÑ´Ù.
 * @param iTime ½Ã°£
 */
void CMonitorString::AddRow( time_t iTime )
{
	char	szTime[21];

	GetDateTimeString( iTime, szTime, sizeof(szTime) );
	AddRow( szTime );
}

/**
 * @ingroup SipStack
 * @brief ÁÙÀÇ ¸¶Áö¸·  ¹®ÀÚ¿­¿¡ true/false ¸¦ ÀúÀåÇÑ´Ù.
 * @param bValue true/false
 */
void CMonitorString::AddRow( bool bValue )
{
	AddRow( bValue ? "true" : "false" );
}

/**
 * @ingroup SipStack
 * @brief ÀúÀåµÈ ¹®ÀÚ¿­À» ÃÊ±âÈ­½ÃÅ²´Ù.
 */
void CMonitorString::Clear( )
{
	m_strBuf.clear();
}

/**
 * @ingroup SipStack
 * @brief ÀúÀåµÈ ¹®ÀÚ¿­À» ¸®ÅÏÇÑ´Ù.
 * @returns ÀúÀåµÈ ¹®ÀÚ¿­À» ¸®ÅÏÇÑ´Ù.
 */
const char * CMonitorString::GetString()
{
	return m_strBuf.c_str();
}

/**
 * @ingroup SipStack
 * @brief ÀúÀåµÈ ¹®ÀÚ¿­ÀÇ ±æÀÌ¸¦ ¸®ÅÏÇÑ´Ù.
 * @returns ÀúÀåµÈ ¹®ÀÚ¿­ÀÇ ±æÀÌ¸¦ ¸®ÅÏÇÑ´Ù.
 */
int CMonitorString::GetLength()
{
	return (int)m_strBuf.length();
}
