/* 
 * 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 "SdpConnection.h"
#include <stdlib.h>
#include "MemoryDebug.h"

/**
 * @ingroup SdpParser
 * @brief »ý¼ºÀÚ
 */
CSdpConnection::CSdpConnection() : m_iMulticastTtl(-1), m_iMulticastNum(-1)
{
}

/**
 * @ingroup SdpParser
 * @brief ¼Ò¸êÀÚ
 */
CSdpConnection::~CSdpConnection()
{
}

/**
 * @ingroup SdpParser
 * @brief SDP connection ÀÇ value ¹®ÀÚ¿­À» ÆÄ½ÌÇÑ´Ù.
 * @param pszText		SDP connection ÀÇ value ¹®ÀÚ¿­
 * @param iTextLen	SDP connection ÀÇ value ¹®ÀÚ¿­ ±æÀÌ
 * @returns ¼º°øÇÏ¸é ÆÄ½ÌÇÑ ±æÀÌ¸¦ ¸®ÅÏÇÏ°í ±×·¸Áö ¾ÊÀ¸¸é -1¸¦ ¸®ÅÏÇÑ´Ù.
 */
int CSdpConnection::Parse( const char * pszText, int iTextLen )
{
	Clear();
	if( pszText == NULL || iTextLen <= 0 ) return -1;

	int		iPos, iStartPos = 0, iType = 0;

	for( iPos = 0; iPos < iTextLen; ++iPos )
	{
		if( pszText[iPos] == ' ' || pszText[iPos] == '/' )
		{
			SetData( pszText + iStartPos, iPos - iStartPos, iType );
			++iType;
			iStartPos = iPos + 1;
		}
	}

	if( iStartPos < iTextLen )
	{
		SetData( pszText + iStartPos, iPos - iStartPos, iType );
	}

	return iTextLen;
}

/**
 * @ingroup SdpParser
 * @brief SDP connection ÀÇ value ¹®ÀÚ¿­À» ÀúÀåÇÑ´Ù.
 * @param pszText		SDP connection ÀÇ value ¹®ÀÚ¿­À» ÀúÀåÇÒ º¯¼ö
 * @param iTextSize pszText º¯¼öÀÇ Å©±â
 * @returns ¼º°øÇÏ¸é ÀúÀåµÈ ¹®ÀÚ¿­ ±æÀÌ¸¦ ¸®ÅÏÇÏ°í ±×·¸Áö ¾ÊÀ¸¸é -1 ¸¦ ¸®ÅÏÇÑ´Ù.
 */
int CSdpConnection::ToString( char * pszText, int iTextSize )
{
	if( pszText == NULL || iTextSize <= 0 ) return -1;
	if( Empty() ) return -1;

	int iLen;

	iLen = snprintf( pszText, iTextSize, "%s %s %s", m_strNetType.c_str(), m_strAddrType.c_str(), m_strAddr.c_str() );

	if( m_iMulticastTtl >= 0 )
	{
		iLen += snprintf( pszText + iLen, iTextSize - iLen, "/%d", m_iMulticastTtl );

		if( m_iMulticastNum >= 0 )
		{
			iLen += snprintf( pszText + iLen, iTextSize - iLen, "/%d", m_iMulticastNum );
		}
	}

	return iLen;
}

/**
 * @ingroup SdpParser
 * @brief ¸â¹ö º¯¼ö¸¦ ÃÊ±âÈ­½ÃÅ²´Ù.
 */
void CSdpConnection::Clear()
{
	m_strNetType.clear();
	m_strAddrType.clear();
	m_strAddr.clear();
	m_iMulticastTtl = -1;
	m_iMulticastNum = -1;
}

/**
 * @ingroup SdpParser
 * @brief µ¥ÀÌÅÍ°¡ Á¸ÀçÇÏ¸é false ¸¦ ¸®ÅÏÇÏ°í ±×·¸Áö ¾ÊÀ¸¸é true ¸¦ ¸®ÅÏÇÑ´Ù.
 * @return µ¥ÀÌÅÍ°¡ Á¸ÀçÇÏ¸é false ¸¦ ¸®ÅÏÇÏ°í ±×·¸Áö ¾ÊÀ¸¸é true ¸¦ ¸®ÅÏÇÑ´Ù.
 */
bool CSdpConnection::Empty()
{
	if( m_strNetType.empty() || m_strAddrType.empty() || m_strAddr.empty() ) return true;

	return false;
}

void CSdpConnection::SetData( const char * pszData, int iLen, int iType )
{
	switch( iType )
	{
	case 0:
		m_strNetType.append( pszData, iLen );
		break;
	case 1:
		m_strAddrType.append( pszData, iLen );
		break;
	case 2:
		m_strAddr.append( pszData, iLen );
		break;
	case 3:
		{
			std::string	strTemp;

			strTemp.append( pszData, iLen );
			m_iMulticastTtl = atoi( strTemp.c_str() );
		}
		break;
	case 4:
		{
			std::string	strTemp;

			strTemp.append( pszData, iLen );
			m_iMulticastNum = atoi( strTemp.c_str() );
		}
		break;
	}
}
