/*----------------------------------------------------------
  PROJECT		: mldkgui

  FILE			: cStreamData.cpp
  VERSION		: 1.0

  CREATION		: 08.03.2004
  AUTHOR		: JLac
  LANGUAGE		: cpp
  AIM			: implementation of the cStreamData class.

  HISTORIC  	: -

-----------------------------------------------------------*/
#include <list> 
#include <string> 

#include "typedef.h"

using namespace std;

#include "LogMsg.h"
#include "cStreamData.h"


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

cStreamData::cStreamData()
{
	m_LastWordPosWrite = 0;
	m_LastWordPosRead = 0;
	m_LastWordDispo = 0;
    m_dataflush = 0;
}

cStreamData::~cStreamData()
{

}

/*DS****************************************************************
 *              
 *  FUNCTION:   ReadWord64
 *              
 *  RETURNS:    Word64
 *              
 *  PARAMETERS: 
 *		Input  :
 *          void
 *
 *		Output :
 *              
 *  COMMENTS:   -
 *              
 *******************************************************************/
UWord64 cStreamData::ReadWord64(void)
{
    UWord64 Word;
	UWord64 W1,W2,W3,W4,W5,W6,W7,W8;

	W1 = m_LastWord[m_LastWordPosRead++];	m_LastWordDispo --;
	W2 = m_LastWord[m_LastWordPosRead++];	m_LastWordDispo --;
	W3 = m_LastWord[m_LastWordPosRead++];	m_LastWordDispo --;
	W4 = m_LastWord[m_LastWordPosRead++];	m_LastWordDispo --;
	W5 = m_LastWord[m_LastWordPosRead++];	m_LastWordDispo --;
	W6 = m_LastWord[m_LastWordPosRead++];	m_LastWordDispo --;
	W7 = m_LastWord[m_LastWordPosRead++];	m_LastWordDispo --;
	W8 = m_LastWord[m_LastWordPosRead++];	m_LastWordDispo --;

	Word = (W8<<56) + (W7<<48) + (W6<<40) + (W5<<32) +
            (W4<<24) + (W3<<16) + (W2<<8) + (W1<<0);

	return(Word);
}


/*DS****************************************************************
 *              
 *  FUNCTION:   ReadWord32
 *              
 *  RETURNS:    Word32
 *              
 *  PARAMETERS: 
 *		Input  :
 *          void
 *
 *		Output :
 *              
 *  COMMENTS:   -
 *              
 *******************************************************************/
UWord32 cStreamData::ReadWord32(void)
{
	UWord32 Word;
	UWord32 W1,W2,W3,W4;

	W1 = m_LastWord[m_LastWordPosRead++];	m_LastWordDispo --;
	W2 = m_LastWord[m_LastWordPosRead++];	m_LastWordDispo --;
	W3 = m_LastWord[m_LastWordPosRead++];	m_LastWordDispo --;
	W4 = m_LastWord[m_LastWordPosRead++];	m_LastWordDispo --;

	Word = (W4<<24) + (W3<<16) + (W2<<8) + (W1<<0);

	return(Word);
}

/*DS****************************************************************
 *              
 *  FUNCTION:   ReadWord16
 *              
 *  RETURNS:    Word16
 *              
 *  PARAMETERS: 
 *		Input  :
 *          void
 *
 *		Output :
 *              
 *  COMMENTS:   -
 *              
 *******************************************************************/
UWord16 cStreamData::ReadWord16(void)
{
	UWord32 Word;
	UWord32 W1,W2;

	W1 = m_LastWord[m_LastWordPosRead++];	m_LastWordDispo --;
	W2 = m_LastWord[m_LastWordPosRead++];	m_LastWordDispo --;

	Word = (W2<<8) + (W1<<0);

	return(Word);
}

/*DS****************************************************************
 *              
 *  FUNCTION:   ReadWord8
 *              
 *  RETURNS:    Word8
 *              
 *  PARAMETERS: 
 *		Input  :
 *          void
 *
 *		Output :
 *              
 *  COMMENTS:   -
 *              
 *******************************************************************/
UWord8 cStreamData::ReadWord8(void)
{
	UWord8 Word;
	
	Word= m_LastWord[m_LastWordPosRead++];	m_LastWordDispo --;
	return(Word);
}

/*DS****************************************************************
 *              
 *  FUNCTION:   ReadString
 *              
 *  RETURNS:    void
 *              
 *  PARAMETERS: 
 *		Input  :
 *          string &str
 *
 *		Output :
 *              
 *  COMMENTS:   -
 *              
 *******************************************************************/
void cStreamData::ReadString(string &str)
{
	Word16 i,length;
	
	length = ReadWord16();

	for(i=0;i<length;i++)		
		str += ReadWord8();
}


/*DS****************************************************************
 *              
 *  FUNCTION:   Add
 *              
 *  RETURNS:    bool
 *              
 *  PARAMETERS: 
 *		Input  :
 *          Word8 *buf,Word32 size
 *
 *		Output :
 *              
 *  COMMENTS:   -
 *              
 *******************************************************************/
bool cStreamData::Add(Word8 *buf,Word32 size)
{
    Word32 i;
    Word32 retaddstatus = true;

    if( m_LastWordPosWrite+size >= SIZE_OF_BUFFER_STREAM)
    {
		LogMsg(LOGLEVEL_OPCODE,"MlDkGUI : cStreamData::Add(%d) Flush data : %d\n",size,m_LastWordDispo);

        m_dataflush = 1;

        for(i=0;i<m_LastWordDispo;i++)
        {
            m_LastWord[i] = m_LastWord[m_LastWordPosRead+i];
        }

        m_LastWordPosRead = 0;
        m_LastWordPosWrite = m_LastWordDispo;
    }

	LogMsg(LOGLEVEL_OPCODE,"MlDkGUI : cStreamData::Add(%d) Write In :%d (last read in : %d)\n",size,m_LastWordPosWrite,m_LastWordPosRead);

	for(i=0;i<size;i++)
	{
		m_LastWord[m_LastWordPosWrite++] = buf[i];
		m_LastWordDispo ++ ;
	}

    return(retaddstatus);
}

/*DS****************************************************************
 *              
 *  FUNCTION:   IsEnoughDataAvailable
 *              
 *  RETURNS:    bool
 *              
 *  PARAMETERS: 
 *		Input  :
 *          Word32 nb
 *
 *		Output :
 *              
 *  COMMENTS:   -
 *              
 *******************************************************************/
bool cStreamData::IsEnoughDataAvailable(Word32 nb)
{
    bool ret = false;

	if( m_LastWordDispo > nb )
    {
        ret = true;
    }

    return(ret);
}

/*DS****************************************************************
 *              
 *  FUNCTION:   SetPos
 *              
 *  RETURNS:    bool
 *              
 *  PARAMETERS: 
 *		Input  :
 *          Word32 pos
 *
 *		Output :
 *              
 *  COMMENTS:   -
 *              
 *******************************************************************/
bool cStreamData::SetPos(UWord32 pos)
{
    bool ret = true;

    if( m_dataflush == 0 )
    {
        m_LastWordPosRead = pos;
    }
    else
    {
        ret = false;
    }

    return(ret);
}

/*******************************************************************
 *              
 *  FUNCTION:   GetPos
 *              
 *  RETURNS:    UWord32
 *              
 *  PARAMETERS: 
 *		Input  :
 *          -
 *
 *		Output :
 *              
 *  COMMENTS:   -
 *              
 *******************************************************************/
UWord32 cStreamData::GetPos()
{
	m_LastWordPosRead_bkp = m_LastWordPosRead;
	m_LastWordDispo_bkp = m_LastWordDispo;

	m_dataflush = 0;
	return m_LastWordPosRead;
}

/*******************************************************************
 *              
 *  FUNCTION:   SeekPos
 *              
 *  RETURNS:    bool
 *              
 *  PARAMETERS: 
 *		Input  :
 *          UWord32 seek
 *
 *		Output :
 *              
 *  COMMENTS:   -
 *              
 *******************************************************************/
bool cStreamData::SeekPos(UWord32 seek)
{
	m_LastWordPosRead = m_LastWordPosRead_bkp + seek;
	m_LastWordDispo = m_LastWordDispo_bkp - seek;
	return(0);
}
